Skip to content

Commit 4ba3a81

Browse files
author
a-brandt
committed
fixed shortest path and renamed VisitedEntity to TraversalResultEntity
1 parent e8f481c commit 4ba3a81

12 files changed

+360
-78
lines changed

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import com.arangodb.entity.BatchResponseEntity;
3636
import com.arangodb.entity.BooleanResultEntity;
3737
import com.arangodb.entity.CollectionEntity;
38-
import com.arangodb.entity.CollectionKeyOption;
3938
import com.arangodb.entity.CollectionOptions;
4039
import com.arangodb.entity.CollectionsEntity;
4140
import com.arangodb.entity.CursorEntity;
@@ -64,6 +63,7 @@
6463
import com.arangodb.entity.ReplicationSyncEntity;
6564
import com.arangodb.entity.RestrictType;
6665
import com.arangodb.entity.ScalarExampleEntity;
66+
import com.arangodb.entity.ShortestPathEntity;
6767
import com.arangodb.entity.SimpleByResultEntity;
6868
import com.arangodb.entity.StatisticsDescriptionEntity;
6969
import com.arangodb.entity.StatisticsEntity;
@@ -5315,44 +5315,21 @@ public <T> CursorEntity<T> graphGetEdgesByExampleMap(
53155315
return result;
53165316
}
53175317

5318-
/**
5319-
* Returns all Edges of vertices matching the map.
5320-
*
5321-
* @param graphName
5322-
* The name of the graph.
5323-
* @param clazz
5324-
* Class of returned edge documents.
5325-
* @param startVertexExample
5326-
* Map with example of vertex, where edges start
5327-
* @param endVertexExample
5328-
* Map with example of vertex, where edges end
5329-
* @param shortestPathOptions
5330-
* Options for the shortest path
5331-
* @param aqlQueryOptions
5332-
* AQL query options
5333-
* @return EdgeCursor<T>
5334-
* @throws ArangoException
5335-
*/
5336-
public <T> EdgeCursor<T> graphGetShortesPath(
5318+
public <V, E> ShortestPathEntity<V, E> graphGetShortesPath(
53375319
String graphName,
5338-
Class<T> clazz,
53395320
Object startVertexExample,
53405321
Object endVertexExample,
53415322
ShortestPathOptions shortestPathOptions,
5342-
AqlQueryOptions aqlQueryOptions) throws ArangoException {
5343-
5344-
validateCollectionName(graphName);
5345-
5346-
String query = "for i in graph_shortest_path(@graphName, @startVertexExample, @endVertexExample, @options) return i";
5347-
5348-
Map<String, Object> options = shortestPathOptions == null ? new MapBuilder().get() : shortestPathOptions
5349-
.toMap();
5323+
AqlQueryOptions aqlQueryOptions,
5324+
Class<V> vertexClass,
5325+
Class<E> edgeClass) throws ArangoException {
53505326

5351-
Map<String, Object> bindVars = new MapBuilder().put("graphName", graphName)
5352-
.put("startVertexExample", startVertexExample).put("endVertexExample", endVertexExample)
5353-
.put("options", options).get();
5327+
if (aqlQueryOptions == null) {
5328+
aqlQueryOptions = getDefaultAqlQueryOptions();
5329+
}
53545330

5355-
return executeEdgeQuery(query, bindVars, aqlQueryOptions, clazz);
5331+
return cursorDriver.getShortesPath(getDefaultDatabase(), graphName, startVertexExample, endVertexExample,
5332+
shortestPathOptions, aqlQueryOptions, vertexClass, edgeClass);
53565333
}
53575334

53585335
// public <T, S> CursorEntity<EdgeEntity<T>> graphGetEdgesByExampleObject1(

src/main/java/com/arangodb/InternalCursorDriver.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import com.arangodb.entity.CursorEntity;
66
import com.arangodb.entity.DefaultEntity;
77
import com.arangodb.entity.DocumentEntity;
8+
import com.arangodb.entity.ShortestPathEntity;
89
import com.arangodb.impl.BaseDriverInterface;
910
import com.arangodb.util.AqlQueryOptions;
11+
import com.arangodb.util.ShortestPathOptions;
1012

1113
/**
1214
* Created by fbartels on 10/27/14.
@@ -45,6 +47,30 @@ <T, S extends DocumentEntity<T>> DocumentCursorResult<T, S> executeBaseCursorQue
4547
Class<S> classDocumentEntity,
4648
Class<T> clazz) throws ArangoException;
4749

50+
/**
51+
* Get the shortest path from a vertex to another vertex
52+
*
53+
* @param database
54+
* @param graphName
55+
* @param startVertexExample
56+
* @param endVertexExample
57+
* @param shortestPathOptions
58+
* @param aqlQueryOptions
59+
* @param vertexClass
60+
* @param edgeClass
61+
* @return
62+
* @throws ArangoException
63+
*/
64+
public <V, E> ShortestPathEntity<V, E> getShortesPath(
65+
String database,
66+
String graphName,
67+
Object startVertexExample,
68+
Object endVertexExample,
69+
ShortestPathOptions shortestPathOptions,
70+
AqlQueryOptions aqlQueryOptions,
71+
Class<V> vertexClass,
72+
Class<E> edgeClass) throws ArangoException;
73+
4874
@Deprecated
4975
<T> CursorEntity<T> executeQuery(
5076
String database,

src/main/java/com/arangodb/entity/EntityDeserializers.java

Lines changed: 124 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,7 @@ public EdgeEntity<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializa
19081908
}
19091909

19101910
public static class TraversalEntityDeserializer implements JsonDeserializer<TraversalEntity<?, ?>> {
1911+
@SuppressWarnings("unchecked")
19111912
@Override
19121913
public TraversalEntity<?, ?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
19131914
throws JsonParseException {
@@ -1923,31 +1924,35 @@ public static class TraversalEntityDeserializer implements JsonDeserializer<Trav
19231924

19241925
if (obj.has("result")) {
19251926
JsonObject result = obj.getAsJsonObject("result");
1927+
TraversalResultEntity<Object, Object> v = null;
1928+
19261929
if (result.has("visited")) {
19271930
JsonObject visited = result.getAsJsonObject("visited");
1928-
@SuppressWarnings("unchecked")
1929-
VisitedEntity<Object, Object> v = (VisitedEntity<Object, Object>) context.deserialize(visited,
1930-
VisitedEntity.class);
1931-
entity.setEntity(v);
1931+
v = (TraversalResultEntity<Object, Object>) context.deserialize(visited,
1932+
TraversalResultEntity.class);
1933+
} else {
1934+
v = new TraversalResultEntity<Object, Object>();
19321935
}
1936+
entity.setEntity(v);
19331937
}
19341938

19351939
return entity;
19361940
}
1937-
19381941
}
19391942

1940-
public static class VisitedEntityDeserializer implements JsonDeserializer<VisitedEntity<?, ?>> {
1943+
public static class TraversalResultEntityDeserializer implements JsonDeserializer<TraversalResultEntity<?, ?>> {
19411944
@Override
1942-
public VisitedEntity<?, ?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
1943-
throws JsonParseException {
1945+
public TraversalResultEntity<?, ?> deserialize(
1946+
JsonElement json,
1947+
Type typeOfT,
1948+
JsonDeserializationContext context) throws JsonParseException {
19441949

19451950
if (json.isJsonNull()) {
19461951
return null;
19471952
}
19481953

19491954
JsonObject visited = json.getAsJsonObject();
1950-
VisitedEntity<Object, Object> entity = new VisitedEntity<Object, Object>();
1955+
TraversalResultEntity<Object, Object> entity = new TraversalResultEntity<Object, Object>();
19511956

19521957
Class<?> vertexClazz = getParameterized();
19531958
Class<?> edgeClazz = null;
@@ -1959,28 +1964,107 @@ public static class VisitedEntityDeserializer implements JsonDeserializer<Visite
19591964
entity.setVertices(getVertices(vertexClazz, context, visited.getAsJsonArray("vertices")));
19601965
}
19611966
if (visited.has("paths")) {
1962-
List<PathEntity<Object, Object>> pathEntities = new ArrayList<PathEntity<Object, Object>>();
1963-
JsonArray paths = visited.getAsJsonArray("paths");
1964-
if (!paths.equals(null)) {
1965-
for (int i = 0, imax = paths.size(); i < imax; i++) {
1966-
JsonObject path = paths.get(i).getAsJsonObject();
1967-
PathEntity<Object, Object> pathEntity = new PathEntity<Object, Object>();
1968-
1969-
if (path.has("edges")) {
1970-
pathEntity.setEdges(getEdges(edgeClazz, context, path.getAsJsonArray("edges")));
1971-
}
1972-
if (path.has("vertices")) {
1973-
pathEntity.setVertices(getVertices(vertexClazz, context, path.getAsJsonArray("vertices")));
1974-
}
1967+
entity.setPaths(getPaths(context, visited, vertexClazz, edgeClazz));
1968+
}
19751969

1976-
pathEntities.add(pathEntity);
1977-
}
1970+
return entity;
1971+
}
1972+
}
1973+
1974+
public static class ShortestPathEntityDeserializer implements JsonDeserializer<ShortestPathEntity<?, ?>> {
1975+
@SuppressWarnings("unchecked")
1976+
@Override
1977+
public ShortestPathEntity<?, ?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
1978+
throws JsonParseException {
1979+
1980+
if (json.isJsonNull()) {
1981+
return null;
1982+
}
1983+
1984+
JsonObject obj = json.getAsJsonObject();
1985+
ShortestPathEntity<Object, Object> entity = deserializeBaseParameter(obj,
1986+
new ShortestPathEntity<Object, Object>());
1987+
deserializeDocumentParameter(obj, entity);
1988+
1989+
if (obj.has("result")) {
1990+
JsonArray result = obj.getAsJsonArray("result");
1991+
ShortestPathResultEntity<Object, Object> v = null;
1992+
1993+
if (result.size() > 0) {
1994+
v = (ShortestPathResultEntity<Object, Object>) context.deserialize(result.get(0),
1995+
ShortestPathResultEntity.class);
1996+
} else {
1997+
v = new ShortestPathResultEntity<Object, Object>();
19781998
}
1979-
entity.setPaths(pathEntities);
1999+
entity.setEntity(v);
2000+
}
2001+
2002+
return entity;
2003+
}
2004+
}
2005+
2006+
public static class ShortestPathResultEntityDeserializer implements
2007+
JsonDeserializer<ShortestPathResultEntity<?, ?>> {
2008+
@Override
2009+
public ShortestPathResultEntity<?, ?> deserialize(
2010+
JsonElement json,
2011+
Type typeOfT,
2012+
JsonDeserializationContext context) throws JsonParseException {
2013+
2014+
if (json.isJsonNull()) {
2015+
return null;
2016+
}
2017+
2018+
JsonObject visited = json.getAsJsonObject();
2019+
ShortestPathResultEntity<Object, Object> entity = new ShortestPathResultEntity<Object, Object>();
2020+
2021+
Class<?> vertexClazz = getParameterized();
2022+
Class<?> edgeClazz = null;
2023+
if (hasNextParameterized()) {
2024+
edgeClazz = nextParameterized();
2025+
}
2026+
2027+
if (visited.has("vertex")) {
2028+
entity.setVertex(getVertex(context, visited.getAsJsonObject("vertex"), vertexClazz));
2029+
}
2030+
if (visited.has("distance")) {
2031+
entity.setDistance(visited.get("distance").getAsLong());
2032+
}
2033+
if (visited.has("startVertex")) {
2034+
entity.setStartVertex(visited.get("startVertex").getAsString());
2035+
}
2036+
if (visited.has("paths")) {
2037+
entity.setPaths(getPaths(context, visited, vertexClazz, edgeClazz));
19802038
}
19812039

19822040
return entity;
19832041
}
2042+
2043+
}
2044+
2045+
private static List<PathEntity<Object, Object>> getPaths(
2046+
JsonDeserializationContext context,
2047+
JsonObject visited,
2048+
Class<?> vertexClazz,
2049+
Class<?> edgeClazz) {
2050+
List<PathEntity<Object, Object>> pathEntities = new ArrayList<PathEntity<Object, Object>>();
2051+
JsonArray paths = visited.getAsJsonArray("paths");
2052+
if (!paths.equals(null)) {
2053+
for (int i = 0, imax = paths.size(); i < imax; i++) {
2054+
JsonObject path = paths.get(i).getAsJsonObject();
2055+
PathEntity<Object, Object> pathEntity = new PathEntity<Object, Object>();
2056+
2057+
if (path.has("edges")) {
2058+
pathEntity.setEdges(getEdges(edgeClazz, context, path.getAsJsonArray("edges")));
2059+
}
2060+
if (path.has("vertices")) {
2061+
pathEntity.setVertices(getVertices(vertexClazz, context, path.getAsJsonArray("vertices")));
2062+
}
2063+
2064+
pathEntities.add(pathEntity);
2065+
}
2066+
}
2067+
return pathEntities;
19842068
}
19852069

19862070
private static List<VertexEntity<Object>> getVertices(
@@ -1991,19 +2075,27 @@ private static List<VertexEntity<Object>> getVertices(
19912075
if (!vertices.equals(null)) {
19922076
for (int i = 0, imax = vertices.size(); i < imax; i++) {
19932077
JsonObject vertex = vertices.get(i).getAsJsonObject();
1994-
VertexEntity<Object> ve = deserializeBaseParameter(vertex, new VertexEntity<Object>());
1995-
deserializeDocumentParameter(vertex, ve);
1996-
if (vertexClazz != null) {
1997-
ve.setEntity(context.deserialize(vertex, vertexClazz));
1998-
} else {
1999-
ve.setEntity(context.deserialize(vertex, Object.class));
2000-
}
2078+
VertexEntity<Object> ve = getVertex(context, vertex, vertexClazz);
20012079
list.add(ve);
20022080
}
20032081
}
20042082
return list;
20052083
}
20062084

2085+
private static VertexEntity<Object> getVertex(
2086+
JsonDeserializationContext context,
2087+
JsonObject vertex,
2088+
Class<?> vertexClazz) {
2089+
VertexEntity<Object> ve = deserializeBaseParameter(vertex, new VertexEntity<Object>());
2090+
deserializeDocumentParameter(vertex, ve);
2091+
if (vertexClazz != null) {
2092+
ve.setEntity(context.deserialize(vertex, vertexClazz));
2093+
} else {
2094+
ve.setEntity(context.deserialize(vertex, Object.class));
2095+
}
2096+
return ve;
2097+
}
2098+
20072099
private static List<EdgeEntity<Object>> getEdges(
20082100
Class<?> edgeClazz,
20092101
JsonDeserializationContext context,

src/main/java/com/arangodb/entity/EntityFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ public static GsonBuilder getGsonBuilder() {
114114
.registerTypeAdapter(VertexEntity.class, new EntityDeserializers.VertexEntityDeserializer())
115115
.registerTypeAdapter(EdgeEntity.class, new EntityDeserializers.EdgeEntityDeserializer())
116116
.registerTypeAdapter(TraversalEntity.class, new EntityDeserializers.TraversalEntityDeserializer())
117-
.registerTypeAdapter(VisitedEntity.class, new EntityDeserializers.VisitedEntityDeserializer());
117+
.registerTypeAdapter(TraversalResultEntity.class,
118+
new EntityDeserializers.TraversalResultEntityDeserializer())
119+
.registerTypeAdapter(ShortestPathEntity.class, new EntityDeserializers.ShortestPathEntityDeserializer())
120+
.registerTypeAdapter(ShortestPathResultEntity.class,
121+
new EntityDeserializers.ShortestPathResultEntityDeserializer());
118122
}
119123

120124
static {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2004-2015 triAGENS GmbH, Cologne, Germany
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
* Copyright holder is triAGENS GmbH, Cologne, Germany
16+
*
17+
* @author a-brandt
18+
* @author Copyright 2015, triAGENS GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.entity;
22+
23+
/**
24+
* @author a-brandt
25+
*/
26+
public class ShortestPathEntity<V, T> extends DocumentEntity<ShortestPathResultEntity<V, T>> {
27+
}

0 commit comments

Comments
 (0)