Skip to content
Permalink
Browse files
add default value() method for HugeGraphOutput (#144)
* add default value() method for HugeGraphOutput
* fix EXPECT_RINGS format
  • Loading branch information
javeme committed Nov 18, 2021
1 parent 74dff02 commit 7fb40dd73f56610b83a328ed51431c07f17fded5
Showing 11 changed files with 89 additions and 72 deletions.
@@ -0,0 +1,35 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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 com.baidu.hugegraph.computer.algorithm.centrality.degree;

import com.baidu.hugegraph.computer.core.output.hg.HugeGraphOutput;
import com.baidu.hugegraph.structure.constant.WriteType;

public class DegreeCentralityOutput extends HugeGraphOutput {

@Override
protected void prepareSchema() {
this.client().schema().propertyKey(this.name())
.asDouble()
.writeType(WriteType.OLAP_RANGE)
.ifNotExist()
.create();
}
}
@@ -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.LimitedLogOutput;

public class DegreeCentralityParams implements AlgorithmParams {

@@ -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,
LimitedLogOutput.class.getName());
DegreeCentralityOutput.class.getName());
}
}
@@ -19,24 +19,17 @@

package com.baidu.hugegraph.computer.algorithm.centrality.pagerank;

import com.baidu.hugegraph.computer.core.graph.value.DoubleValue;
import com.baidu.hugegraph.computer.core.graph.vertex.Vertex;
import com.baidu.hugegraph.computer.core.output.hg.HugeGraphOutput;
import com.baidu.hugegraph.structure.constant.WriteType;

public class PageRankOutput extends HugeGraphOutput {

@Override
public void prepareSchema() {
protected void prepareSchema() {
this.client().schema().propertyKey(this.name())
.asDouble()
.writeType(WriteType.OLAP_RANGE)
.ifNotExist()
.create();
}

@Override
public Object value(Vertex vertex) {
return ((DoubleValue) vertex.value()).value();
}
}
@@ -19,23 +19,17 @@

package com.baidu.hugegraph.computer.algorithm.community.trianglecount;

import com.baidu.hugegraph.computer.core.graph.vertex.Vertex;
import com.baidu.hugegraph.computer.core.output.hg.HugeGraphOutput;
import com.baidu.hugegraph.structure.constant.WriteType;

public class TriangleCountOutput extends HugeGraphOutput {

@Override
public void prepareSchema() {
protected void prepareSchema() {
this.client().schema().propertyKey(this.name())
.asLong()
.writeType(WriteType.OLAP_RANGE)
.ifNotExist()
.create();
}

@Override
public Object value(Vertex vertex) {
return ((TriangleCountValue) vertex.value()).count();
}
}
@@ -45,10 +45,10 @@ public IdList idList() {
}

public long count() {
return this.count.value();
return this.count.longValue();
}

public void count(Long count) {
public void count(long count) {
this.count.value(count);
}

@@ -97,11 +97,11 @@ public String toString() {

@Override
public String string() {
return String.valueOf(this.count);
return String.valueOf(this.value());
}

@Override
public Object value() {
throw new UnsupportedOperationException();
return this.count.value();
}
}
@@ -30,7 +30,7 @@
public class RingsDetectionOutput extends HugeGraphOutput {

@Override
public void prepareSchema() {
protected void prepareSchema() {
this.client().schema().propertyKey(this.name())
.asText()
.writeType(WriteType.OLAP_COMMON)
@@ -40,7 +40,7 @@ public void prepareSchema() {
}

@Override
public Object value(Vertex vertex) {
protected Object value(Vertex vertex) {
IdListList value = vertex.value();
List<String> propValues = new ArrayList<>();
for (int i = 0; i < value.size(); i++) {
@@ -89,7 +89,9 @@ protected com.baidu.hugegraph.structure.graph.Vertex constructHugeVertex(
return hugeVertex;
}

protected abstract void prepareSchema();
protected Object value(Vertex vertex) {
return vertex.value().value();
}

protected abstract Object value(Vertex vertex);
protected abstract void prepareSchema();
}
@@ -32,13 +32,10 @@
import com.baidu.hugegraph.computer.core.graph.edge.Edge;
import com.baidu.hugegraph.computer.core.graph.value.DoubleValue;
import com.baidu.hugegraph.computer.core.graph.vertex.Vertex;
import com.baidu.hugegraph.computer.core.output.hdfs.HdfsOutput;
import com.google.common.collect.Streams;

public class DegreeCentralityTest extends AlgorithmTestBase {

public static boolean isRun;

@Test
public void testRunAlgorithm() throws InterruptedException {
runAlgorithm(DegreeCentralityTestParams.class.getName(),
@@ -60,9 +57,11 @@ public void setAlgorithmParameters(Map<String, String> params) {
}
}

public static class DegreeCentralityTestOutput extends HdfsOutput {
public static class DegreeCentralityTestOutput
extends DegreeCentralityOutput {

private String weight;
private static boolean isRun;

public DegreeCentralityTestOutput() {
}
@@ -71,18 +70,16 @@ public DegreeCentralityTestOutput() {
public void init(Config config, int partition) {
super.init(config, partition);
this.weight = config.getString(
DegreeCentrality.OPTION_WEIGHT_PROPERTY, "");
DegreeCentrality.OPTION_WEIGHT_PROPERTY, "");
isRun = false;
}

@Override
public void write(Vertex vertex) {
super.write(vertex);
public Double value(Vertex vertex) {
Double value = (Double) super.value(vertex);
isRun = true;
DoubleValue value = vertex.value();
if (StringUtils.isEmpty(this.weight)) {
Assert.assertEquals(vertex.numEdges(),
value.value(), 0.000001);
Assert.assertEquals(vertex.numEdges(), value, 0.000001);
} else {
Iterator<Edge> edges = vertex.edges().iterator();
double totalValue = Streams.stream(edges).map(
@@ -95,13 +92,9 @@ public void write(Vertex vertex) {
return weightValue.value();
}
}).reduce(Double::sum).orElse(0.0);
Assert.assertEquals(totalValue, value.value(), 0.000001);
Assert.assertEquals(totalValue, value, 0.000001);
}
}

@Override
public void close() {
super.close();
return value;
}

public static void assertResult() {
@@ -120,15 +120,15 @@ public void testTriangleCountValue() {
public static class TriangleCountOutputTest extends TriangleCountOutput {

@Override
public Vertex constructHugeVertex(
com.baidu.hugegraph.computer.core.graph.vertex.Vertex vertex) {
Vertex result = super.constructHugeVertex(vertex);
Long expected = EXPECTED_RESULTS.get(result.id());
public Long value(
com.baidu.hugegraph.computer.core.graph.vertex.Vertex vertex) {
Long value = (Long) super.value(vertex);
Long expected = EXPECTED_RESULTS.get(vertex.id());
if (expected != null) {
Assert.assertEquals(expected, result.property(super.name()));
Assert.assertEquals(expected, value);
}

return result;
return value;
}
}
}
@@ -20,6 +20,7 @@
package com.baidu.hugegraph.computer.algorithm.path.rings;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@@ -29,8 +30,7 @@

import com.baidu.hugegraph.computer.algorithm.AlgorithmTestBase;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.graph.value.IdList;
import com.baidu.hugegraph.computer.core.graph.value.IdListList;
import com.baidu.hugegraph.computer.core.graph.id.Id;
import com.baidu.hugegraph.driver.GraphManager;
import com.baidu.hugegraph.driver.HugeClient;
import com.baidu.hugegraph.driver.SchemaManager;
@@ -44,9 +44,13 @@ public class RingsDetectionTest extends AlgorithmTestBase {

private static final Map<String, Set<String>> EXPECT_RINGS =
ImmutableMap.of(
"A", ImmutableSet.of("ABCA", "ACA", "ABCEDA", "ADA",
"ADCA", "ACEDA"),
"C", ImmutableSet.of("CEDC")
"A", ImmutableSet.of("[A, B, C, A]",
"[A, C, A]",
"[A, B, C, E, D, A]",
"[A, D, A]",
"[A, D, C, A]",
"[A, C, E, D, A]"),
"C", ImmutableSet.of("[C, E, D, C]")
);

@BeforeClass
@@ -108,30 +112,25 @@ public void setAlgorithmParameters(Map<String, String> params) {

public static class RingsDetectionTestOutput extends RingsDetectionOutput {

public static Map<String, Set<String>> EXPECT_RINGS;
protected static Map<String, Set<String>> EXPECT_RINGS;

@Override
public void write(
public List<String> value(
com.baidu.hugegraph.computer.core.graph.vertex.Vertex vertex) {
super.write(vertex);
this.assertResult(vertex);
@SuppressWarnings("unchecked")
List<String> rings = (List<String>) super.value(vertex);
this.assertResult(vertex.id(), rings);
return rings;
}

private void assertResult(
com.baidu.hugegraph.computer.core.graph.vertex.Vertex vertex) {
IdListList rings = vertex.value();
Set<String> expect =
EXPECT_RINGS.getOrDefault(vertex.id().toString(),
new HashSet<>());
private void assertResult(Id id, List<String> rings) {
Set<String> expect = EXPECT_RINGS.getOrDefault(id.toString(),
new HashSet<>());

Assert.assertEquals(expect.size(), rings.size());
for (int i = 0; i < rings.size(); i++) {
IdList ring = rings.get(i);
StringBuilder ringValue = new StringBuilder();
for (int j = 0; j < ring.size(); j++) {
ringValue.append(ring.get(j).toString());
}
Assert.assertTrue(expect.contains(ringValue.toString()));
for (String ring : rings) {
String error = "Expect: '" + ring + "' in " + expect;
Assert.assertTrue(error, expect.contains(ring));
}
}
}
@@ -43,9 +43,11 @@ public class RingsDetectionWithFilterTest extends AlgorithmTestBase {

private static final Map<String, Set<String>> EXPECT_RINGS =
ImmutableMap.of(
"A", ImmutableSet.of("ACA", "ADCA", "ABCA"),
"B", ImmutableSet.of("BCB"),
"C", ImmutableSet.of("CDC")
"A", ImmutableSet.of("[A, C, A]",
"[A, D, C, A]",
"[A, B, C, A]"),
"B", ImmutableSet.of("[B, C, B]"),
"C", ImmutableSet.of("[C, D, C]")
);

@BeforeClass

0 comments on commit 7fb40dd

Please sign in to comment.