Skip to content

Commit

Permalink
Adapt neighborrank and personalrank API (#43)
Browse files Browse the repository at this point in the history
Change-Id: I5f881af79bd514d117e903b9ee823ca87daad82a
  • Loading branch information
Linary authored and javeme committed Apr 19, 2019
1 parent 5d149b0 commit ba22ba8
Show file tree
Hide file tree
Showing 16 changed files with 1,784 additions and 543 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
<version>1.6.12</version>
<version>1.7.0</version>
<packaging>jar</packaging>

<name>hugegraph-client</name>
Expand Down Expand Up @@ -53,7 +53,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.source>1.8</compiler.source>
<compiler.target>1.8</compiler.target>
<hugegraph.common.version>1.5.9</hugegraph.common.version>
<hugegraph.common.version>1.6.0</hugegraph.common.version>
<jersey.version>2.22</jersey.version>
<mockito.version>2.8.47</mockito.version>
</properties>
Expand Down Expand Up @@ -113,7 +113,7 @@
<manifestEntries>
<!-- Must be on one line, otherwise the automatic
upgrade script cannot replace the version number -->
<Implementation-Version>1.6.12.0</Implementation-Version>
<Implementation-Version>1.7.0.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
202 changes: 202 additions & 0 deletions src/main/java/com/baidu/hugegraph/api/traverser/NeighborRankAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* 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.api.traverser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.baidu.hugegraph.api.traverser.structure.Ranks;
import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.rest.RestResult;
import com.baidu.hugegraph.structure.constant.Direction;
import com.baidu.hugegraph.structure.constant.Traverser;
import com.baidu.hugegraph.util.E;
import com.fasterxml.jackson.annotation.JsonProperty;

public class NeighborRankAPI extends TraversersAPI {

public NeighborRankAPI(RestClient client, String graph) {
super(client, graph);
}

@Override
protected String type() {
return "neighborrank";
}

public List<Ranks> post(Request request) {
RestResult result = this.client.post(this.path(), request);
return result.readList("ranks", Ranks.class);
}

public static class Request {

@JsonProperty("source")
private Object source;
@JsonProperty("steps")
private List<Step> steps;
@JsonProperty("alpha")
private double alpha;
@JsonProperty("capacity")
private long capacity;

private Request() {
this.source = null;
this.steps = new ArrayList<>();
this.alpha = Traverser.DEFAULT_ALPHA;
this.capacity = Traverser.DEFAULT_CAPACITY;
}

public static Builder builder() {
return new Builder();
}

@Override
public String toString() {
return String.format("Request{source=%s,steps=%s,alpha=%s," +
"capacity=%s}", this.source, this.steps,
this.alpha, this.capacity);
}

public static class Builder {

private Request request;
private List<Step.Builder> stepBuilders;

private Builder() {
this.request = new Request();
this.stepBuilders = new ArrayList<>();
}

public Builder source(Object source) {
E.checkArgument(source != null, "The label of request " +
"for neighbor rank can't be null");
this.request.source = source;
return this;
}

public Step.Builder steps() {
Step.Builder builder = new Step.Builder();
this.stepBuilders.add(builder);
return builder;
}

public Builder alpha(double alpha) {
TraversersAPI.checkAlpha(alpha);
this.request.alpha = alpha;
return this;
}

public Builder capacity(long capacity) {
TraversersAPI.checkCapacity(capacity);
this.request.capacity = capacity;
return this;
}

public Request build() {
for (Step.Builder builder : this.stepBuilders) {
this.request.steps.add(builder.build());
}
E.checkArgument(this.request.source != null,
"Source vertex can't be null");
E.checkArgument(this.request.steps != null &&
!this.request.steps.isEmpty(),
"Steps can't be null or empty");
TraversersAPI.checkCapacity(this.request.capacity);
TraversersAPI.checkAlpha(this.request.alpha);
return this.request;
}
}

public static class Step {

@JsonProperty("direction")
private String direction;
@JsonProperty("labels")
private List<String> labels;
@JsonProperty("degree")
private long degree;
@JsonProperty("top")
private int top;

private Step() {
this.direction = null;
this.labels = new ArrayList<>();
this.degree = Traverser.DEFAULT_DEGREE;
this.top = (int) Traverser.DEFAULT_PATHS_LIMIT;
}

@Override
public String toString() {
return String.format("Step{direction=%s,labels=%s,degree=%s," +
"top=%s}", this.direction, this.labels,
this.degree, this.top);
}

public static class Builder {

private Step step;

private Builder() {
this.step = new Step();
}

public Step.Builder direction(Direction direction) {
this.step.direction = direction.toString();
return this;
}

public Step.Builder labels(List<String> labels) {
this.step.labels.addAll(labels);
return this;
}

public Step.Builder labels(String... labels) {
this.step.labels.addAll(Arrays.asList(labels));
return this;
}

public Step.Builder degree(long degree) {
TraversersAPI.checkDegree(degree);
this.step.degree = degree;
return this;
}

public Step.Builder top(int top) {
E.checkArgument(top > 0 && top <= Traverser.DEFAULT_MAX_TOP,
"The top of each layer cannot exceed %s",
Traverser.DEFAULT_MAX_TOP);
this.step.top = top;
return this;
}

private Step build() {
TraversersAPI.checkDegree(this.step.degree);
E.checkArgument(this.step.top > 0 &&
this.step.top <= Traverser.DEFAULT_MAX_TOP,
"The top of each layer cannot exceed %s",
Traverser.DEFAULT_MAX_TOP);
return this.step;
}
}
}
}
}
164 changes: 164 additions & 0 deletions src/main/java/com/baidu/hugegraph/api/traverser/PersonalRankAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* 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.api.traverser;

import com.baidu.hugegraph.api.traverser.structure.Ranks;
import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.rest.RestResult;
import com.baidu.hugegraph.structure.constant.Traverser;
import com.baidu.hugegraph.util.E;
import com.fasterxml.jackson.annotation.JsonProperty;

public class PersonalRankAPI extends TraversersAPI {

public PersonalRankAPI(RestClient client, String graph) {
super(client, graph);
}

@Override
protected String type() {
return "personalrank";
}

public Ranks post(Request request) {
RestResult result = this.client.post(this.path(), request);
return result.readObject(Ranks.class);
}

public static class Request {

@JsonProperty("source")
private Object source;
@JsonProperty("label")
private String label;
@JsonProperty("alpha")
private double alpha = Traverser.DEFAULT_ALPHA;
@JsonProperty("degree")
public long degree = Traverser.DEFAULT_DEGREE;
@JsonProperty("limit")
private long limit = Traverser.DEFAULT_LIMIT;
@JsonProperty("max_depth")
private int maxDepth = 5;
@JsonProperty("with_label")
private WithLabel withLabel = WithLabel.BOTH_LABEL;
@JsonProperty("sorted")
private boolean sorted = true;

public static Builder builder() {
return new Builder();
}

@Override
public String toString() {
return String.format("Request{source=%s,label=%s,alpha=%s," +
"degree=%s,limit=%s,maxDepth=%s," +
"withLabel=%s,sorted=%s}",
this.source, this.label, this.alpha,
this.degree, this.limit, this.maxDepth,
this.withLabel, this.sorted);
}

public enum WithLabel {
SAME_LABEL,
OTHER_LABEL,
BOTH_LABEL
}

public static class Builder {

private Request request;

private Builder() {
this.request = new Request();
}

public Builder source(Object source) {
E.checkArgument(source != null, "The source of request " +
"for personal rank can't be null");
this.request.source = source;
return this;
}

public Builder label(String label) {
E.checkArgument(label != null, "The label of request " +
"for personal rank can't be null");
this.request.label = label;
return this;
}

public Builder alpha(double alpha) {
TraversersAPI.checkAlpha(alpha);
this.request.alpha = alpha;
return this;
}

public Builder degree(long degree) {
TraversersAPI.checkDegree(degree);
this.request.degree = degree;
return this;
}

public Builder limit(long limit) {
TraversersAPI.checkLimit(limit);
this.request.limit = limit;
return this;
}

public Builder maxDepth(int maxDepth) {
E.checkArgument(maxDepth > 0 &&
maxDepth <= Traverser.DEFAULT_MAX_DEPTH,
"The max depth must be in range (0, %s], " +
"but got: %s",
Traverser.DEFAULT_MAX_DEPTH, maxDepth);
this.request.maxDepth = maxDepth;
return this;
}

public Builder withLabel(WithLabel withLabel) {
this.request.withLabel = withLabel;
return this;
}

public Builder sorted(boolean sorted) {
this.request.sorted = sorted;
return this;
}

public Request build() {
E.checkArgument(this.request.source != null,
"Source vertex can't be null");
E.checkArgument(this.request.label != null,
"The label of rank request " +
"for personal rank can't be null");
TraversersAPI.checkAlpha(this.request.alpha);
TraversersAPI.checkDegree(this.request.degree);
TraversersAPI.checkLimit(this.request.limit);
E.checkArgument(this.request.maxDepth > 0 &&
this.request.maxDepth <=
Traverser.DEFAULT_MAX_DEPTH,
"The max depth must be in range (0, %s], " +
"but got: %s",
Traverser.DEFAULT_MAX_DEPTH,
this.request.maxDepth);
return this.request;
}
}
}
}
Loading

0 comments on commit ba22ba8

Please sign in to comment.