Skip to content

Commit

Permalink
Refactor computer: abstract computer-api from computer-core (#196)
Browse files Browse the repository at this point in the history
* address comment
* improve pom and options
  • Loading branch information
coderzc authored Nov 24, 2022
1 parent 7744295 commit 5320190
Show file tree
Hide file tree
Showing 53 changed files with 301 additions and 947 deletions.
5 changes: 2 additions & 3 deletions computer-algorithm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
<artifactId>computer-algorithm</artifactId>

<dependencies>
<!-- TODO improve structure to only import computer-api -->
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>computer-core</artifactId>
<version>${project.version}</version>
<artifactId>computer-api</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>org.mvel</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import com.baidu.hugegraph.computer.algorithm.AlgorithmParams;
import com.baidu.hugegraph.computer.algorithm.centrality.closeness.ClosenessCentrality;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.input.filter.ExtractAllPropertyInputFilter;
import com.baidu.hugegraph.computer.core.master.DefaultMasterComputation;
import com.baidu.hugegraph.computer.core.output.hg.HugeGraphDoubleOutput;

public class BetweennessCentralityParams implements AlgorithmParams {

Expand All @@ -41,9 +39,9 @@ public void setAlgorithmParameters(Map<String, String> params) {
this.setIfAbsent(params, ComputerOptions.ALGORITHM_MESSAGE_CLASS,
BetweennessMessage.class.getName());
this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS,
HugeGraphDoubleOutput.class.getName());
HUGEGRAPH_DOUBLE_OUTPUT_CLASS_NAME);
this.setIfAbsent(params, ComputerOptions.INPUT_FILTER_CLASS,
ExtractAllPropertyInputFilter.class.getName());
EXTRACTALLPROPERTYINPUTFILTER_CLASS_NAME);
this.setIfAbsent(params, ClosenessCentrality.OPTION_SAMPLE_RATE,
"0.5D");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

import com.baidu.hugegraph.computer.algorithm.AlgorithmParams;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.input.filter.ExtractAllPropertyInputFilter;
import com.baidu.hugegraph.computer.core.master.DefaultMasterComputation;
import com.baidu.hugegraph.computer.core.output.hg.HugeGraphDoubleOutput;

public class ClosenessCentralityParams implements AlgorithmParams {

Expand All @@ -40,9 +38,9 @@ public void setAlgorithmParameters(Map<String, String> params) {
this.setIfAbsent(params, ComputerOptions.ALGORITHM_MESSAGE_CLASS,
ClosenessMessage.class.getName());
this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS,
HugeGraphDoubleOutput.class.getName());
HUGEGRAPH_DOUBLE_OUTPUT_CLASS_NAME);
this.setIfAbsent(params, ComputerOptions.INPUT_FILTER_CLASS,
ExtractAllPropertyInputFilter.class.getName());
EXTRACTALLPROPERTYINPUTFILTER_CLASS_NAME);
this.setIfAbsent(params, ClosenessCentrality.OPTION_SAMPLE_RATE,
"0.5D");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

import com.baidu.hugegraph.computer.core.common.ComputerContext;
import com.baidu.hugegraph.computer.core.graph.GraphFactory;
import com.baidu.hugegraph.computer.core.graph.id.BytesId;
import com.baidu.hugegraph.computer.core.graph.id.Id;
import com.baidu.hugegraph.computer.core.graph.id.IdFactory;
import com.baidu.hugegraph.computer.core.graph.value.DoubleValue;
import com.baidu.hugegraph.computer.core.graph.value.Value.CustomizeValue;
import com.baidu.hugegraph.computer.core.graph.value.ValueType;
Expand All @@ -40,7 +40,7 @@ public class ClosenessMessage implements CustomizeValue<ClosenessMessage> {
private DoubleValue distance;

public ClosenessMessage() {
this(new BytesId(), new BytesId(), new DoubleValue(0.0D));
this(IdFactory.createId(), IdFactory.createId(), new DoubleValue(0.0D));
}

public ClosenessMessage(Id senderId, Id startId, DoubleValue distance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.graph.value.DoubleValue;
import com.baidu.hugegraph.computer.core.master.DefaultMasterComputation;
import com.baidu.hugegraph.computer.core.output.hg.HugeGraphDoubleOutput;

public class DegreeCentralityParams implements AlgorithmParams {

Expand All @@ -43,6 +42,6 @@ public void setAlgorithmParameters(Map<String, String> params) {
this.setIfAbsent(params, ComputerOptions.WORKER_COMBINER_CLASS,
DoubleValueSumCombiner.class.getName());
this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS,
HugeGraphDoubleOutput.class.getName());
HUGEGRAPH_DOUBLE_OUTPUT_CLASS_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.baidu.hugegraph.computer.core.combiner.DoubleValueSumCombiner;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.graph.value.DoubleValue;
import com.baidu.hugegraph.computer.core.output.hg.HugeGraphDoubleOutput;

public class PageRankParams implements AlgorithmParams {

Expand All @@ -42,6 +41,6 @@ public void setAlgorithmParameters(Map<String, String> params) {
this.setIfAbsent(params, ComputerOptions.WORKER_COMBINER_CLASS,
DoubleValueSumCombiner.class.getName());
this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS,
HugeGraphDoubleOutput.class.getName());
HUGEGRAPH_DOUBLE_OUTPUT_CLASS_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import com.baidu.hugegraph.computer.algorithm.AlgorithmParams;
import com.baidu.hugegraph.computer.core.combiner.ValueMinCombiner;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.graph.id.BytesId;
import com.baidu.hugegraph.computer.core.output.hg.HugeGraphIdOutput;

public class KcoreParams implements AlgorithmParams {

Expand All @@ -36,10 +34,10 @@ public void setAlgorithmParameters(Map<String, String> params) {
this.setIfAbsent(params, ComputerOptions.ALGORITHM_RESULT_CLASS,
KcoreValue.class.getName());
this.setIfAbsent(params, ComputerOptions.ALGORITHM_MESSAGE_CLASS,
BytesId.class.getName());
BYTESID_CLASS_NAME);
this.setIfAbsent(params, ComputerOptions.WORKER_COMBINER_CLASS,
ValueMinCombiner.class.getName());
this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS,
HugeGraphIdOutput.class.getName());
HUGEGRAPH_ID_OUTPUT_CLASS_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import java.io.IOException;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.hugegraph.util.E;

import com.baidu.hugegraph.computer.core.graph.id.BytesId;
import com.baidu.hugegraph.computer.core.graph.id.Id;
import com.baidu.hugegraph.computer.core.graph.id.IdFactory;
import com.baidu.hugegraph.computer.core.graph.value.Value.CustomizeValue;
import com.baidu.hugegraph.computer.core.io.RandomAccessInput;
import com.baidu.hugegraph.computer.core.io.RandomAccessOutput;
import org.apache.hugegraph.util.E;

public class KcoreValue implements CustomizeValue<Object> {

Expand All @@ -37,7 +37,7 @@ public class KcoreValue implements CustomizeValue<Object> {

public KcoreValue() {
this.degree = 0;
this.core = new BytesId();
this.core = IdFactory.createId();
}

public void degree(int degree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

import com.baidu.hugegraph.computer.algorithm.AlgorithmParams;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.graph.id.BytesId;
import com.baidu.hugegraph.computer.core.output.hg.HugeGraphIdOutput;

public class LpaParams implements AlgorithmParams {

Expand All @@ -33,10 +31,10 @@ public void setAlgorithmParameters(Map<String, String> params) {
this.setIfAbsent(params, ComputerOptions.WORKER_COMPUTATION_CLASS,
Lpa.class.getName());
this.setIfAbsent(params, ComputerOptions.ALGORITHM_RESULT_CLASS,
BytesId.class.getName());
BYTESID_CLASS_NAME);
this.setIfAbsent(params, ComputerOptions.ALGORITHM_MESSAGE_CLASS,
BytesId.class.getName());
BYTESID_CLASS_NAME);
this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS,
HugeGraphIdOutput.class.getName());
HUGEGRAPH_ID_OUTPUT_CLASS_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.config.EdgeFrequency;
import com.baidu.hugegraph.computer.core.graph.value.IdList;
import com.baidu.hugegraph.computer.core.output.hg.HugeGraphIntOutput;

public class TriangleCountParams implements AlgorithmParams {

Expand All @@ -38,7 +37,7 @@ public void setAlgorithmParameters(Map<String, String> params) {
this.setIfAbsent(params, ComputerOptions.ALGORITHM_RESULT_CLASS,
TriangleCountValue.class.getName());
this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS,
HugeGraphIntOutput.class.getName());
HUGEGRAPH_INT_OUTPUT_CLASS_NAME);
this.setIfAbsent(params, ComputerOptions.INPUT_EDGE_FREQ.name(),
EdgeFrequency.SINGLE.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import com.baidu.hugegraph.computer.algorithm.AlgorithmParams;
import com.baidu.hugegraph.computer.core.combiner.ValueMinCombiner;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.graph.id.BytesId;
import com.baidu.hugegraph.computer.core.output.hg.HugeGraphIdOutput;

public class WccParams implements AlgorithmParams {

Expand All @@ -34,12 +32,12 @@ public void setAlgorithmParameters(Map<String, String> params) {
this.setIfAbsent(params, ComputerOptions.WORKER_COMPUTATION_CLASS,
Wcc.class.getName());
this.setIfAbsent(params, ComputerOptions.ALGORITHM_RESULT_CLASS,
BytesId.class.getName());
BYTESID_CLASS_NAME);
this.setIfAbsent(params, ComputerOptions.ALGORITHM_MESSAGE_CLASS,
BytesId.class.getName());
BYTESID_CLASS_NAME);
this.setIfAbsent(params, ComputerOptions.WORKER_COMBINER_CLASS,
ValueMinCombiner.class.getName());
this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS,
HugeGraphIdOutput.class.getName());
HUGEGRAPH_ID_OUTPUT_CLASS_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.baidu.hugegraph.computer.algorithm.path.rings.RingsDetectionOutput;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.graph.value.IdListList;
import com.baidu.hugegraph.computer.core.input.filter.ExtractAllPropertyInputFilter;

public class RingsDetectionWithFilterParams implements AlgorithmParams {

Expand All @@ -40,6 +39,6 @@ public void setAlgorithmParameters(Map<String, String> params) {
this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS,
RingsDetectionOutput.class.getName());
this.setIfAbsent(params, ComputerOptions.INPUT_FILTER_CLASS,
ExtractAllPropertyInputFilter.class.getName());
EXTRACTALLPROPERTYINPUTFILTER_CLASS_NAME);
}
}
5 changes: 5 additions & 0 deletions computer-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<artifactId>computer-api</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3-version}</version>
</dependency>
<dependency>
<groupId>org.apache.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,41 @@

package com.baidu.hugegraph.computer.algorithm;

import java.util.Map;
import static com.baidu.hugegraph.computer.core.config.ComputerOptions.COMPUTER_PROHIBIT_USER_OPTIONS;

import org.slf4j.Logger;
import java.util.Map;

import org.apache.hugegraph.config.ConfigOption;
import org.apache.hugegraph.util.Log;
import org.slf4j.Logger;

public interface AlgorithmParams {

Logger LOG = Log.logger(AlgorithmParams.class);

String BYTESID_CLASS_NAME = "com.baidu.hugegraph.computer.core.graph.id.BytesId";
String HUGEGRAPH_ID_OUTPUT_CLASS_NAME = "com.baidu.hugegraph.computer.core" +
".output.hg.HugeGraphIdOutput";
String LOG_OUTPUT_CLASS_NAME = "com.baidu.hugegraph.computer.core.output.LogOutput";
String HUGEGRAPH_DOUBLE_OUTPUT_CLASS_NAME = "com.baidu.hugegraph.computer.core" +
".output.hg.HugeGraphDoubleOutput";
String HUGEGRAPH_FLOAT_OUTPUT_CLASS_NAME = "com.baidu.hugegraph.computer.core" +
".output.hg.HugeGraphFloatOutput";
String HUGEGRAPH_INT_OUTPUT_CLASS_NAME = "com.baidu.hugegraph.computer.core" +
".output.hg.HugeGraphIntOutput";
String HUGEGRAPH_LONG_OUTPUT_CLASS_NAME = "com.baidu.hugegraph.computer.core" +
".output.hg.HugeGraphLongOutput";
String HUGEGRAPH_STRING_OUTPUT_CLASS_NAME = "com.baidu.hugegraph.computer.core" +
".output.hg.HugeGraphStringOutput";
String HUGEGRAPH_LIST_OUTPUT_CLASS_NAME = "com.baidu.hugegraph.computer.core" +
".output.hg.HugeGraphListOutput";
String DEFAULTINPUTFILTER_CLASS_NAME = "com.baidu.hugegraph.computer" +
".core.input.filter" +
".DefaultInputFilter";
String EXTRACTALLPROPERTYINPUTFILTER_CLASS_NAME = "com.baidu.hugegraph.computer" +
".core.input.filter" +
".ExtractAllPropertyInputFilter";

/**
* set algorithm's specific configuration
* @param params
Expand All @@ -38,7 +62,7 @@ public interface AlgorithmParams {

default void setIfAbsent(Map<String, String> params, String key,
String value) {
if (!params.keySet().contains(key)) {
if (!params.containsKey(key) && !COMPUTER_PROHIBIT_USER_OPTIONS.contains(key)) {
LOG.debug("Put parameters key={}, value={}", key, value);
params.put(key, value);
}
Expand Down
Loading

0 comments on commit 5320190

Please sign in to comment.