From af1a436635391207dcc904a67caa6c1bc790c054 Mon Sep 17 00:00:00 2001 From: diaohancai Date: Mon, 23 Oct 2023 18:31:42 +0800 Subject: [PATCH 01/12] feat(algorithm): random walk --- .../algorithm/sampling/RandomWalk.java | 177 ++++++++++++++++++ .../algorithm/sampling/RandomWalkMessage.java | 67 +++++++ .../algorithm/sampling/RandomWalkOutput.java | 36 ++++ .../algorithm/sampling/RandomWalkParams.java | 32 ++++ .../algorithm/sampling/RandomWalkTest.java | 18 ++ 5 files changed, 330 insertions(+) create mode 100644 computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java create mode 100644 computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java create mode 100644 computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java create mode 100644 computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java create mode 100644 computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java new file mode 100644 index 000000000..9d009bb51 --- /dev/null +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java @@ -0,0 +1,177 @@ +package org.apache.hugegraph.computer.algorithm.sampling; + +import org.apache.hugegraph.computer.core.common.exception.ComputerException; +import org.apache.hugegraph.computer.core.config.Config; +import org.apache.hugegraph.computer.core.graph.edge.Edge; +import org.apache.hugegraph.computer.core.graph.edge.Edges; +import org.apache.hugegraph.computer.core.graph.id.Id; +import org.apache.hugegraph.computer.core.graph.value.IdList; +import org.apache.hugegraph.computer.core.graph.value.IdListList; +import org.apache.hugegraph.computer.core.graph.vertex.Vertex; +import org.apache.hugegraph.computer.core.worker.Computation; +import org.apache.hugegraph.computer.core.worker.ComputationContext; +import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + +import java.util.Iterator; +import java.util.Random; + +/** + * @author: diaohancai + * @date: 2023-10-19 + */ +public class RandomWalk implements Computation { + + private static final Logger LOG = Log.logger(RandomWalk.class); + + public static final String OPTION_WALK_PER_NODE = "randomwalk.walk_per_node"; + public static final String OPTION_WALK_LENGTH = "randomwalk.walk_length"; + + /** + * number of walk to the same vertex(source vertex) + */ + private Integer walkPerNode; + + /** + * walk length + */ + private Integer walkLength; + + /** + * random + */ + private Random random; + + @Override + public String category() { + return "sampling"; + } + + @Override + public String name() { + return "randomWalk"; + } + + @Override + public void init(Config config) { + this.walkPerNode = config.getInt(OPTION_WALK_PER_NODE, 3); + if (this.walkPerNode < 1) { + throw new ComputerException("The param %s must be greater than 1, " + + "actual got '%s'", + OPTION_WALK_PER_NODE, this.walkPerNode); + } + + this.walkLength = config.getInt(OPTION_WALK_LENGTH, 3); + if (this.walkLength < 1) { + throw new ComputerException("The param %s must be greater than 1, " + + "actual got '%s'", + OPTION_WALK_LENGTH, this.walkLength); + } + + this.random = new Random(); + } + + @Override + public void compute0(ComputationContext context, Vertex vertex) { + vertex.value(new IdListList()); + + RandomWalkMessage message = new RandomWalkMessage(); + message.addPath(vertex); + + if (vertex.numEdges() <= 0) { + // isolated vertex + vertex.value(message.path()); + vertex.inactivate(); + return; + } + + for (int i = 0; i < walkPerNode; ++i) { + // random select one edge and walk + Edge selectedEdge = this.randomSelectEdge(vertex.edges()); + context.sendMessage(selectedEdge.targetId(), message); + } + } + + @Override + public void compute(ComputationContext context, Vertex vertex, Iterator messages) { + while (messages.hasNext()) { + RandomWalkMessage message = messages.next(); + + if (message.getIsFinish()) { + // save result + this.savePath(vertex, message.path()); + + vertex.inactivate(); + continue; + } + + message.addPath(vertex); + + if (vertex.numEdges() <= 0) { + // there is nowhere to walk,finish eariler + message.setIsFinish(true); + context.sendMessage(this.getSourceId(message.path()), message); + + vertex.inactivate(); + continue; + } + + if (message.path().size() >= this.walkLength + 1) { + message.setIsFinish(true); + Id sourceId = this.getSourceId(message.path()); + + if (vertex.id().equals(sourceId)) { + // current vertex is the source vertex,no need to send message once more + // save result + this.savePath(vertex, message.path()); + } else { + context.sendMessage(sourceId, message); + } + + vertex.inactivate(); + continue; + } + + // random select one edge and walk + Edge selectedEdge = this.randomSelectEdge(vertex.edges()); + context.sendMessage(selectedEdge.targetId(), message); + } + } + + /** + * random select one edge + */ + private Edge randomSelectEdge(Edges edges) { + Edge selectedEdge = null; + int randomNum = random.nextInt(edges.size()); + + int i = 0; + Iterator iterator = edges.iterator(); + while (iterator.hasNext()) { + selectedEdge = iterator.next(); + if (i == randomNum) { + break; + } + i++; + } + + return selectedEdge; + } + + /** + * get source id of path + */ + private Id getSourceId(IdList path) { + // the first id of path is the source id + return path.get(0); + } + + /** + * save path + */ + private void savePath(Vertex sourceVertex, IdList path) { + IdListList curValue = sourceVertex.value(); + curValue.add(path.copy()); + } + +} diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java new file mode 100644 index 000000000..34f421b85 --- /dev/null +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java @@ -0,0 +1,67 @@ +package org.apache.hugegraph.computer.algorithm.sampling; + +import org.apache.hugegraph.computer.core.graph.value.BooleanValue; +import org.apache.hugegraph.computer.core.graph.value.IdList; +import org.apache.hugegraph.computer.core.graph.value.Value; +import org.apache.hugegraph.computer.core.graph.vertex.Vertex; +import org.apache.hugegraph.computer.core.io.RandomAccessInput; +import org.apache.hugegraph.computer.core.io.RandomAccessOutput; + +import java.io.IOException; +import java.util.List; + +/** + * @author: diaohancai + * @date: 2023-10-20 + */ +public class RandomWalkMessage implements Value.CustomizeValue> { + + /** + * random walk path + */ + private final IdList path; + + /** + * finish flag + */ + private BooleanValue isFinish; + + public RandomWalkMessage() { + this.path = new IdList(); + this.isFinish = new BooleanValue(false); + } + + @Override + public void read(RandomAccessInput in) throws IOException { + this.path.read(in); + this.isFinish.read(in); + } + + @Override + public void write(RandomAccessOutput out) throws IOException { + this.path.write(out); + this.isFinish.write(out); + } + + @Override + public List value() { + return this.path.value(); + } + + public IdList path() { + return this.path; + } + + public void addPath(Vertex vertex) { + this.path.add(vertex.id()); + } + + public Boolean getIsFinish() { + return isFinish.value(); + } + + public void setIsFinish(Boolean isFinish) { + this.isFinish = new BooleanValue(isFinish); + } + +} diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java new file mode 100644 index 000000000..67f8fa936 --- /dev/null +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java @@ -0,0 +1,36 @@ +package org.apache.hugegraph.computer.algorithm.sampling; + +import org.apache.hugegraph.computer.core.graph.value.IdListList; +import org.apache.hugegraph.computer.core.graph.vertex.Vertex; +import org.apache.hugegraph.computer.core.output.hg.HugeGraphOutput; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author: diaohancai + * @date: 2023-10-20 + */ +public class RandomWalkOutput extends HugeGraphOutput> { + + @Override + protected void prepareSchema() { + this.client().schema().propertyKey(this.name()) + .asText() + .writeType(this.writeType()) + .valueList() + .ifNotExist() + .create(); + } + + @Override + protected List value(Vertex vertex) { + IdListList value = vertex.value(); + List propValues = new ArrayList<>(); + for (int i = 0; i < value.size(); i++) { + propValues.add(value.get(i).toString()); + } + return propValues; + } + +} diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java new file mode 100644 index 000000000..33437d0fe --- /dev/null +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java @@ -0,0 +1,32 @@ +package org.apache.hugegraph.computer.algorithm.sampling; + +import org.apache.hugegraph.computer.algorithm.AlgorithmParams; +import org.apache.hugegraph.computer.core.config.ComputerOptions; +import org.apache.hugegraph.computer.core.graph.value.IdListList; + +import java.util.Map; + +/** + * @author: diaohancai + * @date: 2023-10-20 + */ +public class RandomWalkParams implements AlgorithmParams { + + @Override + public void setAlgorithmParameters(Map params) { + this.setIfAbsent(params, ComputerOptions.WORKER_COMPUTATION_CLASS, + RandomWalk.class.getName()); + this.setIfAbsent(params, ComputerOptions.ALGORITHM_MESSAGE_CLASS, + RandomWalkMessage.class.getName()); + this.setIfAbsent(params, ComputerOptions.ALGORITHM_RESULT_CLASS, + IdListList.class.getName()); + this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS, + RandomWalkOutput.class.getName()); + + this.setIfAbsent(params, RandomWalk.OPTION_WALK_PER_NODE, + "3"); + this.setIfAbsent(params, RandomWalk.OPTION_WALK_LENGTH, + "3"); + } + +} diff --git a/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java b/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java new file mode 100644 index 000000000..2a4df6dfe --- /dev/null +++ b/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java @@ -0,0 +1,18 @@ +package org.apache.hugegraph.computer.algorithm.sampling; + +import org.apache.hugegraph.computer.algorithm.AlgorithmTestBase; +import org.apache.hugegraph.computer.algorithm.sampling.RandomWalkParams; +import org.junit.Test; + +/** + * @author: diaohancai + * @date: 2023-10-20 + */ +public class RandomWalkTest extends AlgorithmTestBase { + + @Test + public void testRunAlgorithm() throws InterruptedException { + runAlgorithm(RandomWalkParams.class.getName()); + } + +} From d59676efbbffa8505bcef698965666286351ed71 Mon Sep 17 00:00:00 2001 From: diaohancai Date: Mon, 23 Oct 2023 20:15:29 +0800 Subject: [PATCH 02/12] chore: add licensed description --- .../algorithm/sampling/RandomWalk.java | 21 ++++++++++++++---- .../algorithm/sampling/RandomWalkMessage.java | 21 ++++++++++++++---- .../algorithm/sampling/RandomWalkOutput.java | 21 ++++++++++++++---- .../algorithm/sampling/RandomWalkParams.java | 21 ++++++++++++++---- .../algorithm/sampling/RandomWalkTest.java | 22 ++++++++++++++----- 5 files changed, 85 insertions(+), 21 deletions(-) diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java index 9d009bb51..6dc8be87e 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + package org.apache.hugegraph.computer.algorithm.sampling; import org.apache.hugegraph.computer.core.common.exception.ComputerException; @@ -16,10 +33,6 @@ import java.util.Iterator; import java.util.Random; -/** - * @author: diaohancai - * @date: 2023-10-19 - */ public class RandomWalk implements Computation { private static final Logger LOG = Log.logger(RandomWalk.class); diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java index 34f421b85..aaadd9809 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + package org.apache.hugegraph.computer.algorithm.sampling; import org.apache.hugegraph.computer.core.graph.value.BooleanValue; @@ -10,10 +27,6 @@ import java.io.IOException; import java.util.List; -/** - * @author: diaohancai - * @date: 2023-10-20 - */ public class RandomWalkMessage implements Value.CustomizeValue> { /** diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java index 67f8fa936..63ddf8c84 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + package org.apache.hugegraph.computer.algorithm.sampling; import org.apache.hugegraph.computer.core.graph.value.IdListList; @@ -7,10 +24,6 @@ import java.util.ArrayList; import java.util.List; -/** - * @author: diaohancai - * @date: 2023-10-20 - */ public class RandomWalkOutput extends HugeGraphOutput> { @Override diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java index 33437d0fe..eba05d9c9 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + package org.apache.hugegraph.computer.algorithm.sampling; import org.apache.hugegraph.computer.algorithm.AlgorithmParams; @@ -6,10 +23,6 @@ import java.util.Map; -/** - * @author: diaohancai - * @date: 2023-10-20 - */ public class RandomWalkParams implements AlgorithmParams { @Override diff --git a/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java b/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java index 2a4df6dfe..919d6ea6d 100644 --- a/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java +++ b/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java @@ -1,13 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + package org.apache.hugegraph.computer.algorithm.sampling; import org.apache.hugegraph.computer.algorithm.AlgorithmTestBase; -import org.apache.hugegraph.computer.algorithm.sampling.RandomWalkParams; import org.junit.Test; -/** - * @author: diaohancai - * @date: 2023-10-20 - */ public class RandomWalkTest extends AlgorithmTestBase { @Test From d6a23d2971e6884284569d1080e1e097d8df6f1f Mon Sep 17 00:00:00 2001 From: diaohancai Date: Mon, 23 Oct 2023 20:24:33 +0800 Subject: [PATCH 03/12] fix: isolated vertex result type --- .../computer/algorithm/sampling/RandomWalk.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java index 6dc8be87e..5c9a19057 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java @@ -73,6 +73,7 @@ public void init(Config config) { "actual got '%s'", OPTION_WALK_PER_NODE, this.walkPerNode); } + LOG.info("[RandomWalk] algorithm param, {}: {}", OPTION_WALK_PER_NODE, walkPerNode); this.walkLength = config.getInt(OPTION_WALK_LENGTH, 3); if (this.walkLength < 1) { @@ -80,6 +81,7 @@ public void init(Config config) { "actual got '%s'", OPTION_WALK_LENGTH, this.walkLength); } + LOG.info("[RandomWalk] algorithm param, {}: {}", OPTION_WALK_LENGTH, walkLength); this.random = new Random(); } @@ -93,7 +95,7 @@ public void compute0(ComputationContext context, Vertex vertex) { if (vertex.numEdges() <= 0) { // isolated vertex - vertex.value(message.path()); + this.savePath(vertex, message.path()); // save result vertex.inactivate(); return; } @@ -111,8 +113,7 @@ public void compute(ComputationContext context, Vertex vertex, Iterator Date: Mon, 23 Oct 2023 21:44:23 +0800 Subject: [PATCH 04/12] fix: complete random walk unit test --- .../algorithm/sampling/RandomWalkTest.java | 122 +++++++++++++++++- 1 file changed, 121 insertions(+), 1 deletion(-) diff --git a/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java b/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java index 919d6ea6d..7d6e7f92e 100644 --- a/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java +++ b/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java @@ -17,14 +17,134 @@ package org.apache.hugegraph.computer.algorithm.sampling; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import org.apache.hugegraph.computer.algorithm.AlgorithmTestBase; +import org.apache.hugegraph.computer.core.config.ComputerOptions; +import org.apache.hugegraph.computer.core.graph.id.Id; +import org.apache.hugegraph.driver.GraphManager; +import org.apache.hugegraph.driver.HugeClient; +import org.apache.hugegraph.driver.SchemaManager; +import org.apache.hugegraph.structure.constant.T; +import org.apache.hugegraph.structure.graph.Vertex; +import org.apache.hugegraph.testutil.Assert; +import org.apache.hugegraph.util.Log; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; public class RandomWalkTest extends AlgorithmTestBase { + private static final Map> EXPECT_WALK_PATH = + ImmutableMap.of( + "F", ImmutableList.of( + "[F, G]", + "[F, G]", + "[F, G]"), + "G", ImmutableList.of("[G]"), + "I", ImmutableList.of("[I]") + ); + + @BeforeClass + public static void setup() { + clearAll(); + + HugeClient client = client(); + SchemaManager schema = client.schema(); + + schema.vertexLabel("user") + .useCustomizeStringId() + .ifNotExist() + .create(); + schema.edgeLabel("know") + .sourceLabel("user") + .targetLabel("user") + .ifNotExist() + .create(); + + GraphManager graph = client.graph(); + Vertex vA = graph.addVertex(T.LABEL, "user", T.ID, "A"); + Vertex vB = graph.addVertex(T.LABEL, "user", T.ID, "B"); + Vertex vC = graph.addVertex(T.LABEL, "user", T.ID, "C"); + Vertex vD = graph.addVertex(T.LABEL, "user", T.ID, "D"); + Vertex vE = graph.addVertex(T.LABEL, "user", T.ID, "E"); + + Vertex vI = graph.addVertex(T.LABEL, "user", T.ID, "I"); + + Vertex vF = graph.addVertex(T.LABEL, "user", T.ID, "F"); + Vertex vG = graph.addVertex(T.LABEL, "user", T.ID, "G"); + + vA.addEdge("know", vB); + vA.addEdge("know", vC); + vA.addEdge("know", vD); + vB.addEdge("know", vC); + vC.addEdge("know", vA); + vC.addEdge("know", vE); + vD.addEdge("know", vA); + vD.addEdge("know", vC); + vE.addEdge("know", vD); + + vF.addEdge("know", vG); + } + + @AfterClass + public static void clear() { + clearAll(); + } + @Test public void testRunAlgorithm() throws InterruptedException { - runAlgorithm(RandomWalkParams.class.getName()); + runAlgorithm(RandomWalkTestParams.class.getName()); + } + + public static class RandomWalkTestParams extends RandomWalkParams { + + private static Integer WALK_PER_NODE = 3; + private static Integer WALK_LENGTH = 3; + + @Override + public void setAlgorithmParameters(Map params) { + this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS, + RandomWalkTest.RandomWalkTestOutput.class.getName()); + this.setIfAbsent(params, RandomWalk.OPTION_WALK_PER_NODE, + WALK_PER_NODE.toString()); + this.setIfAbsent(params, RandomWalk.OPTION_WALK_LENGTH, + WALK_LENGTH.toString()); + + super.setAlgorithmParameters(params); + } + } + + public static class RandomWalkTestOutput extends RandomWalkOutput { + + private static final Logger LOG = Log.logger(RandomWalkTestOutput.class); + + @Override + public List value( + org.apache.hugegraph.computer.core.graph.vertex.Vertex vertex) { + List pathList = super.value(vertex); + LOG.info("vertex: {}, walk path: {}", vertex.id(), pathList); + + this.assertResult(vertex.id(), pathList); + return pathList; + } + + private void assertResult(Id id, List path) { + Set keys = RandomWalkTest.EXPECT_WALK_PATH.keySet(); + if (keys.contains(id.string())) { + List expect = RandomWalkTest.EXPECT_WALK_PATH + .getOrDefault(id.toString(), new ArrayList<>()); + Assert.assertEquals(expect, path); + } else { + Assert.assertEquals(RandomWalkTestParams.WALK_PER_NODE.intValue(), path.size()); + } + } } } From 435ac2b4425f4a76afb75d7787ceb46183752da7 Mon Sep 17 00:00:00 2001 From: diaohancai Date: Tue, 24 Oct 2023 10:17:30 +0800 Subject: [PATCH 05/12] chore: RandomWalkTest add to test suite --- .../hugegraph/computer/algorithm/AlgorithmTestSuite.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/AlgorithmTestSuite.java b/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/AlgorithmTestSuite.java index ad0fc9465..41e9174ce 100644 --- a/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/AlgorithmTestSuite.java +++ b/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/AlgorithmTestSuite.java @@ -28,6 +28,7 @@ import org.apache.hugegraph.computer.algorithm.community.wcc.WccTest; import org.apache.hugegraph.computer.algorithm.path.rings.RingsDetectionTest; import org.apache.hugegraph.computer.algorithm.path.rings.RingsDetectionWithFilterTest; +import org.apache.hugegraph.computer.algorithm.sampling.RandomWalkTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; @@ -43,7 +44,8 @@ RingsDetectionWithFilterTest.class, ClusteringCoefficientTest.class, ClosenessCentralityTest.class, - BetweennessCentralityTest.class + BetweennessCentralityTest.class, + RandomWalkTest.class }) public class AlgorithmTestSuite { } From 5019b9dd2cbfcc8d0b588395f7e8e9eac31463ed Mon Sep 17 00:00:00 2001 From: diaohancai Date: Tue, 24 Oct 2023 10:25:14 +0800 Subject: [PATCH 06/12] chore: modify comment --- .../hugegraph/computer/algorithm/sampling/RandomWalk.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java index 5c9a19057..580163375 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java @@ -41,7 +41,7 @@ public class RandomWalk implements Computation { public static final String OPTION_WALK_LENGTH = "randomwalk.walk_length"; /** - * number of walk to the same vertex(source vertex) + * number of times per vertex(source vertex) walks */ private Integer walkPerNode; From 6e9c66018b004849fce3f93368ab39efe4444ccf Mon Sep 17 00:00:00 2001 From: diaohancai Date: Tue, 24 Oct 2023 10:35:08 +0800 Subject: [PATCH 07/12] =?UTF-8?q?fix:=20walkPerNode=E3=80=81walkLength=20p?= =?UTF-8?q?aram=20can=20be=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hugegraph/computer/algorithm/sampling/RandomWalk.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java index 580163375..4bc6cb6da 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java @@ -68,16 +68,16 @@ public String name() { @Override public void init(Config config) { this.walkPerNode = config.getInt(OPTION_WALK_PER_NODE, 3); - if (this.walkPerNode < 1) { - throw new ComputerException("The param %s must be greater than 1, " + + if (this.walkPerNode <= 0) { + throw new ComputerException("The param %s must be greater than 0, " + "actual got '%s'", OPTION_WALK_PER_NODE, this.walkPerNode); } LOG.info("[RandomWalk] algorithm param, {}: {}", OPTION_WALK_PER_NODE, walkPerNode); this.walkLength = config.getInt(OPTION_WALK_LENGTH, 3); - if (this.walkLength < 1) { - throw new ComputerException("The param %s must be greater than 1, " + + if (this.walkLength <= 0) { + throw new ComputerException("The param %s must be greater than 0, " + "actual got '%s'", OPTION_WALK_LENGTH, this.walkLength); } From 6abb5781c05ed2d6faa46fda232d98ef7c5e2b1e Mon Sep 17 00:00:00 2001 From: diaohancai Date: Wed, 25 Oct 2023 09:41:56 +0800 Subject: [PATCH 08/12] refact: method rename --- .../computer/algorithm/sampling/RandomWalk.java | 13 ++++++------- .../algorithm/sampling/RandomWalkMessage.java | 7 +++---- .../algorithm/sampling/RandomWalkOutput.java | 1 - .../algorithm/sampling/RandomWalkParams.java | 1 - 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java index 4bc6cb6da..0995a0151 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java @@ -62,7 +62,7 @@ public String category() { @Override public String name() { - return "randomWalk"; + return "random_walk"; } @Override @@ -91,7 +91,7 @@ public void compute0(ComputationContext context, Vertex vertex) { vertex.value(new IdListList()); RandomWalkMessage message = new RandomWalkMessage(); - message.addPath(vertex); + message.addToPath(vertex); if (vertex.numEdges() <= 0) { // isolated vertex @@ -112,18 +112,18 @@ public void compute(ComputationContext context, Vertex vertex, Iterator= this.walkLength + 1) { - message.setIsFinish(true); + message.setFinish(true); Id sourceId = this.getSourceId(message.path()); if (vertex.id().equals(sourceId)) { @@ -186,5 +186,4 @@ private void savePath(Vertex sourceVertex, IdList path) { IdListList curValue = sourceVertex.value(); curValue.add(path.copy()); } - } diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java index aaadd9809..1f71b2c09 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java @@ -65,16 +65,15 @@ public IdList path() { return this.path; } - public void addPath(Vertex vertex) { + public void addToPath(Vertex vertex) { this.path.add(vertex.id()); } - public Boolean getIsFinish() { + public Boolean isFinish() { return isFinish.value(); } - public void setIsFinish(Boolean isFinish) { + public void setFinish(boolean isFinish) { this.isFinish = new BooleanValue(isFinish); } - } diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java index 63ddf8c84..ad43d5bd7 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java @@ -45,5 +45,4 @@ protected List value(Vertex vertex) { } return propValues; } - } diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java index eba05d9c9..a7486b9f0 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java @@ -41,5 +41,4 @@ public void setAlgorithmParameters(Map params) { this.setIfAbsent(params, RandomWalk.OPTION_WALK_LENGTH, "3"); } - } From f33073447c3b8f94b95122f52b9e89b3736b5f5f Mon Sep 17 00:00:00 2001 From: diaohancai Date: Wed, 25 Oct 2023 09:46:20 +0800 Subject: [PATCH 09/12] refact: method rename --- .../hugegraph/computer/algorithm/sampling/RandomWalk.java | 4 ++-- .../computer/algorithm/sampling/RandomWalkMessage.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java index 0995a0151..54425a595 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java @@ -123,7 +123,7 @@ public void compute(ComputationContext context, Vertex vertex, Iterator= this.walkLength + 1) { - message.setFinish(true); + message.finish(); Id sourceId = this.getSourceId(message.path()); if (vertex.id().equals(sourceId)) { diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java index 1f71b2c09..e0b3fd1b1 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java @@ -73,7 +73,7 @@ public Boolean isFinish() { return isFinish.value(); } - public void setFinish(boolean isFinish) { - this.isFinish = new BooleanValue(isFinish); + public void finish() { + this.isFinish = new BooleanValue(true); } } From 4c83451cdb4f0591c1c474519973f5c3c89735e8 Mon Sep 17 00:00:00 2001 From: diaohancai Date: Wed, 25 Oct 2023 09:52:41 +0800 Subject: [PATCH 10/12] chore: blank line --- .../hugegraph/computer/algorithm/sampling/RandomWalkTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java b/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java index 7d6e7f92e..5c09af0a7 100644 --- a/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java +++ b/computer-test/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkTest.java @@ -146,5 +146,4 @@ private void assertResult(Id id, List path) { } } } - } From f52acbd95419edcbb0df103ef08ec8d02a0e667c Mon Sep 17 00:00:00 2001 From: diaohancai Date: Thu, 26 Oct 2023 09:21:10 +0800 Subject: [PATCH 11/12] chore: change Boolean to boolean --- .../computer/algorithm/sampling/RandomWalkMessage.java | 4 ++-- .../computer/algorithm/sampling/RandomWalkParams.java | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java index e0b3fd1b1..bf32ee75c 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java @@ -69,8 +69,8 @@ public void addToPath(Vertex vertex) { this.path.add(vertex.id()); } - public Boolean isFinish() { - return isFinish.value(); + public boolean isFinish() { + return this.isFinish.boolValue(); } public void finish() { diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java index a7486b9f0..273d7fd67 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java @@ -36,9 +36,7 @@ public void setAlgorithmParameters(Map params) { this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS, RandomWalkOutput.class.getName()); - this.setIfAbsent(params, RandomWalk.OPTION_WALK_PER_NODE, - "3"); - this.setIfAbsent(params, RandomWalk.OPTION_WALK_LENGTH, - "3"); + this.setIfAbsent(params, RandomWalk.OPTION_WALK_PER_NODE, "3"); + this.setIfAbsent(params, RandomWalk.OPTION_WALK_LENGTH, "3"); } } From 565d6e7f2ee44efe053140e4bc3e30e70fb9f24f Mon Sep 17 00:00:00 2001 From: diaohancai Date: Thu, 26 Oct 2023 09:25:00 +0800 Subject: [PATCH 12/12] chore: wrap --- .../hugegraph/computer/algorithm/sampling/RandomWalk.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java index 54425a595..33d738440 100644 --- a/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java +++ b/computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java @@ -108,7 +108,8 @@ public void compute0(ComputationContext context, Vertex vertex) { } @Override - public void compute(ComputationContext context, Vertex vertex, Iterator messages) { + public void compute(ComputationContext context, Vertex vertex, + Iterator messages) { while (messages.hasNext()) { RandomWalkMessage message = messages.next();