Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt neighborrank and personalrank API #43

Merged
merged 7 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
197 changes: 197 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,197 @@
/*
* 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.graph.GraphAPI;
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 {
Linary marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

public static class RankRequest {
Linary marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

public static class Builder {

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

public Builder() {
this.request = new RankRequest();
this.stepBuilders = new ArrayList<>();
}

public Builder source(Object source) {
this.request.source = GraphAPI.formatVertexId(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 RankRequest 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;
}
}
}
}
}
133 changes: 133 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,133 @@
/*
* 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.List;
import java.util.Map;

import com.baidu.hugegraph.api.graph.GraphAPI;
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";
}

@SuppressWarnings("unchecked")
public Map<Object, Double> post(RankRequest request) {
RestResult result = this.client.post(this.path(), request);
return result.readObject(Map.class);
}

public static class RankRequest {
Linary marked this conversation as resolved.
Show resolved Hide resolved

@JsonProperty("source")
private String source;
@JsonProperty("label")
private String label;
@JsonProperty("alpha")
private double alpha;
@JsonProperty("degree")
public long degree = Traverser.DEFAULT_DEGREE;
@JsonProperty("max_depth")
private int maxDepth;
@JsonProperty("sorted")
private boolean sorted = true;

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

public static class Builder {

private RankRequest request;

public Builder() {
this.request = new RankRequest();
}

public Builder source(Object source) {
this.request.source = GraphAPI.formatVertexId(source);
return this;
}

public Builder label(String label) {
E.checkArgument(label != null, "The label of rank 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 maxDepth(int maxDepth) {
TraversersAPI.checkPositive(maxDepth,
"max depth of rank request " +
"for personal rank");
this.request.maxDepth = maxDepth;
return this;
}

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

public RankRequest 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.checkPositive(this.request.maxDepth,
"max depth of rank request " +
"for personal rank");
return this.request;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ public static void checkCapacity(long capacity) {
public static void checkLimit(long limit) {
checkLimit(limit, "Limit");
}

public static void checkAlpha(double alpha) {
E.checkArgument(alpha > 0 && alpha <= 1.0,
"The alpha of rank request must belong (0, 1], " +
"but got '%s'", alpha);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.structure;

import java.util.HashMap;

public class Ranks extends HashMap<Object, Double> {

Linary marked this conversation as resolved.
Show resolved Hide resolved
}
Loading