You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hugegraph.apache.org by ji...@apache.org on 2022/11/09 10:25:13 UTC

[incubator-hugegraph] 10/33: Support ring detect count (#17)

This is an automated email from the ASF dual-hosted git repository.

jin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git

commit 464d99080f9c9e5ca9f5e0cdf56ce4f51a403590
Author: zhoney <zh...@126.com>
AuthorDate: Tue May 26 20:52:17 2020 +0800

    Support ring detect count (#17)
    
    * support rings count for rings-detect ap algo
    * fix direction can't be null or Both for triangle and clusterCoeffcient
    * fix source_clabel error for rings-detect, fusiform and kcore
    * change limit meaning for fusiform similarity
    
    Change-Id: I5a4ccbf46b47c13ea2eafe7f3e335dc6aea4a83c
---
 .../hugegraph/job/algorithm/AbstractAlgorithm.java | 24 +++++++++++++--
 .../algorithm/comm/ClusterCoeffcientAlgorithm.java |  2 +-
 .../job/algorithm/comm/TriangleCountAlgorithm.java |  2 +-
 .../job/algorithm/path/RingsDetectAlgorithm.java   | 36 +++++++++++++++++++---
 .../similarity/FusiformSimilarityAlgorithm.java    | 14 +++++++--
 5 files changed, 66 insertions(+), 12 deletions(-)

diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AbstractAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AbstractAlgorithm.java
index 5bb3426ff..d3311772a 100644
--- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AbstractAlgorithm.java
+++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AbstractAlgorithm.java
@@ -19,7 +19,6 @@
 
 package com.baidu.hugegraph.job.algorithm;
 
-
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
@@ -131,6 +130,17 @@ public abstract class AbstractAlgorithm implements Algorithm {
         return parseDirection(direction);
     }
 
+    protected static Directions directionOutIn(Map<String, Object> parameters) {
+        E.checkArgument(parameters.containsKey(KEY_DIRECTION),
+                        "The direction must be set");
+        Object direction = parameter(parameters, KEY_DIRECTION);
+        Directions direct = parseDirection(direction);
+        E.checkArgument(direct == Directions.OUT || direct == Directions.IN,
+                        "The direction for triangle_count must be " +
+                        "either OUT or IN, but got: %s", direct);
+        return direct;
+    }
+
     protected static double alpha(Map<String, Object> parameters) {
         if (!parameters.containsKey(KEY_ALPHA)) {
             return DEFAULT_ALPHA;
@@ -330,8 +340,16 @@ public abstract class AbstractAlgorithm implements Algorithm {
 
         protected long traverse(String sourceLabel, String sourceCLabel,
                                 Consumer<Vertex> consumer, Runnable done) {
-            Iterator<Vertex> vertices = this.vertices(sourceLabel, sourceLabel,
-                                                      Query.NO_LIMIT);
+            return this.traverse(sourceLabel, sourceCLabel, consumer, done,
+                                 NO_LIMIT);
+        }
+
+        protected long traverse(String sourceLabel, String sourceCLabel,
+                                Consumer<Vertex> consumer, Runnable done,
+                                long limit) {
+            long actualLimit = limit == NO_LIMIT ? Query.NO_LIMIT : limit;
+            Iterator<Vertex> vertices = this.vertices(sourceLabel, sourceCLabel,
+                                                      actualLimit);
 
             Consumers<Vertex> consumers = new Consumers<>(this.executor,
                                                           consumer, done);
diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/ClusterCoeffcientAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/ClusterCoeffcientAlgorithm.java
index 52f0b07a7..0e5760e24 100644
--- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/ClusterCoeffcientAlgorithm.java
+++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/ClusterCoeffcientAlgorithm.java
@@ -34,7 +34,7 @@ public class ClusterCoeffcientAlgorithm extends AbstractCommAlgorithm {
 
     @Override
     public void checkParameters(Map<String, Object> parameters) {
-        direction(parameters);
+        directionOutIn(parameters);
         degree(parameters);
     }
 
diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/TriangleCountAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/TriangleCountAlgorithm.java
index 34a1a5658..6128c6b17 100644
--- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/TriangleCountAlgorithm.java
+++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/TriangleCountAlgorithm.java
@@ -42,7 +42,7 @@ public class TriangleCountAlgorithm extends AbstractCommAlgorithm {
 
     @Override
     public void checkParameters(Map<String, Object> parameters) {
-        direction(parameters);
+        directionOutIn(parameters);
         degree(parameters);
     }
 
diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/path/RingsDetectAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/path/RingsDetectAlgorithm.java
index 855b7c817..c7c0c677a 100644
--- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/path/RingsDetectAlgorithm.java
+++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/path/RingsDetectAlgorithm.java
@@ -20,6 +20,7 @@
 package com.baidu.hugegraph.job.algorithm.path;
 
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import com.baidu.hugegraph.backend.id.Id;
 import com.baidu.hugegraph.job.Job;
@@ -30,6 +31,8 @@ import com.baidu.hugegraph.util.JsonUtil;
 
 public class RingsDetectAlgorithm extends AbstractAlgorithm {
 
+    public static final String KEY_COUNT_ONLY = "count_only";
+
     @Override
     public String name() {
         return "rings_detect";
@@ -50,6 +53,7 @@ public class RingsDetectAlgorithm extends AbstractAlgorithm {
         sourceCLabel(parameters);
         direction(parameters);
         edgeLabel(parameters);
+        countOnly(parameters);
         workers(parameters);
     }
 
@@ -64,10 +68,18 @@ public class RingsDetectAlgorithm extends AbstractAlgorithm {
                                    depth(parameters),
                                    degree(parameters),
                                    capacity(parameters),
-                                   limit(parameters));
+                                   limit(parameters),
+                                   countOnly(parameters));
         }
     }
 
+    public boolean countOnly(Map<String, Object> parameters) {
+        if (!parameters.containsKey(KEY_COUNT_ONLY)) {
+            return false;
+        }
+        return parameterBoolean(parameters, KEY_COUNT_ONLY);
+    }
+
     public static class Traverser extends AlgoTraverser {
 
         public Traverser(Job<Object> job, int workers) {
@@ -76,13 +88,19 @@ public class RingsDetectAlgorithm extends AbstractAlgorithm {
 
         public Object rings(String sourceLabel, String sourceCLabel,
                             Directions dir, String label, int depth,
-                            long degree, long capacity, long limit) {
+                            long degree, long capacity, long limit,
+                            boolean countOnly) {
             JsonMap ringsJson = new JsonMap();
             ringsJson.startObject();
-            ringsJson.appendKey("rings");
-            ringsJson.startList();
+            if (countOnly) {
+                ringsJson.appendKey("rings_count");
+            } else {
+                ringsJson.appendKey("rings");
+                ringsJson.startList();
+            }
 
             SubGraphTraverser traverser = new SubGraphTraverser(this.graph());
+            AtomicInteger count = new AtomicInteger(0);
 
             this.traverse(sourceLabel, sourceCLabel, v -> {
                 Id source = (Id) v.id();
@@ -96,6 +114,10 @@ public class RingsDetectAlgorithm extends AbstractAlgorithm {
                         }
                     }
                     if (source.equals(min)) {
+                        if (countOnly) {
+                            count.incrementAndGet();
+                            continue;
+                        }
                         String ringJson = JsonUtil.toJson(ring.vertices());
                         synchronized (ringsJson) {
                             ringsJson.appendRaw(ringJson);
@@ -103,7 +125,11 @@ public class RingsDetectAlgorithm extends AbstractAlgorithm {
                     }
                 }
             });
-            ringsJson.endList();
+            if (countOnly) {
+                ringsJson.append(count.get());
+            } else {
+                ringsJson.endList();
+            }
             ringsJson.endObject();
 
             return ringsJson.asJson();
diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/similarity/FusiformSimilarityAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/similarity/FusiformSimilarityAlgorithm.java
index 463526c5d..673430623 100644
--- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/similarity/FusiformSimilarityAlgorithm.java
+++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/similarity/FusiformSimilarityAlgorithm.java
@@ -43,6 +43,7 @@ public class FusiformSimilarityAlgorithm extends AbstractAlgorithm {
     public static final int DEFAULT_MIN_NEIGHBORS = 10;
     public static final int DEFAULT_MIN_SIMILARS = 6;
     public static final int DEFAULT_MIN_GROUPS = 1;
+    public static final long DEFAULT_LIMIT = -1L;
 
     @Override
     public String name() {
@@ -126,6 +127,15 @@ public class FusiformSimilarityAlgorithm extends AbstractAlgorithm {
         return minGroups;
     }
 
+    protected static long limit(Map<String, Object> parameters) {
+        if (!parameters.containsKey(KEY_LIMIT)) {
+            return DEFAULT_LIMIT;
+        }
+        long limit = parameterLong(parameters, KEY_LIMIT);
+        HugeTraverser.checkLimit(limit);
+        return limit;
+    }
+
     protected static class Traverser extends AlgoTraverser {
 
         public Traverser(Job<Object> job, int workers) {
@@ -152,7 +162,7 @@ public class FusiformSimilarityAlgorithm extends AbstractAlgorithm {
                                        edgeLabel, minNeighbors, alpha,
                                        minSimilars, (int) topSimilars,
                                        groupProperty, minGroups, degree,
-                                       capacity, limit, true);
+                                       capacity, NO_LIMIT, true);
                 if (similars.isEmpty()) {
                     return;
                 }
@@ -161,7 +171,7 @@ public class FusiformSimilarityAlgorithm extends AbstractAlgorithm {
                 synchronized (similarsJson) {
                     similarsJson.appendRaw(result);
                 }
-            });
+            }, null, limit);
             similarsJson.endObject();
 
             return similarsJson.asJson();