-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt neighborrank and personalrank API (#43)
Change-Id: I5f881af79bd514d117e903b9ee823ca87daad82a
- Loading branch information
Showing
16 changed files
with
1,784 additions
and
543 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
src/main/java/com/baidu/hugegraph/api/traverser/NeighborRankAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
164
src/main/java/com/baidu/hugegraph/api/traverser/PersonalRankAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.