Skip to content

Commit

Permalink
change relationshipTypes from string to set for graph neighbors queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kshefchek committed Nov 14, 2019
1 parent e9cf1d6 commit 177fcbc
Showing 1 changed file with 27 additions and 24 deletions.
Expand Up @@ -77,7 +77,7 @@

@Path("/graph")
@Api(value = "/graph", description = "Graph services")
@SwaggerDefinition(tags = {@Tag(name="graph", description="Graph services")})
@SwaggerDefinition(tags = {@Tag(name = "graph", description = "Graph services")})
public class GraphService extends BaseResource {

private final Vocabulary vocabulary;
Expand All @@ -88,7 +88,7 @@ public class GraphService extends BaseResource {

@Inject
GraphService(Vocabulary vocabulary, GraphDatabaseService graphDb, GraphApi api,
CurieUtil curieUtil, CypherUtil cypherUtil) {
CurieUtil curieUtil, CypherUtil cypherUtil) {
this.vocabulary = vocabulary;
this.graphDb = graphDb;
this.api = api;
Expand All @@ -113,7 +113,7 @@ public Object getNeighborsFromMultipleRoots(
@ApiParam(value = "Traverse blank nodes",
required = false) @QueryParam("blankNodes") @DefaultValue("false") BooleanParam traverseBlankNodes,
@ApiParam(value = "Which relationship to traverse",
required = false) @QueryParam("relationshipType") Optional<String> relationshipType,
required = false) @QueryParam("relationshipType") Set<String> relationshipTypes,
@ApiParam(value = DocumentationStrings.DIRECTION_DOC, required = false,
allowableValues = DocumentationStrings.DIRECTION_ALLOWED) @QueryParam("direction") @DefaultValue("BOTH") String direction,
@ApiParam(value = "Should subproperties and equivalent properties be included",
Expand All @@ -132,33 +132,36 @@ public Object getNeighborsFromMultipleRoots(
roots.add(concept.get());
}
Set<DirectedRelationshipType> types = new HashSet<>();
if (relationshipType.isPresent()) {
String relationshipTypeString = relationshipType.get();
relationshipTypeString = curieUtil.getIri(relationshipTypeString).orElse(relationshipTypeString);
Set<String> relationships = new HashSet<>();
for (String relationshipTypeString : relationshipTypes) {
relationships.add(curieUtil.getIri(relationshipTypeString).orElse(relationshipTypeString));
if (!getRelationshipTypeNames().contains(relationshipTypeString)) {
throw new BadRequestException("Unknown relationship type: " + relationshipTypeString);
}
}

Direction dir = Direction.valueOf(direction);
try {
if (entail.get()) {
Set<RelationshipType> relationshipTypes =
cypherUtil.getEntailedRelationshipTypes(newHashSet(relationshipTypeString));
types = newHashSet(transform(relationshipTypes,
new Function<RelationshipType, DirectedRelationshipType>() {
@Override
public DirectedRelationshipType apply(RelationshipType type) {
return new DirectedRelationshipType(type, dir);
}
}));
} else {
Direction dir = Direction.valueOf(direction);
try {
if (entail.get()) {
Set<RelationshipType> entailedRelationships =
cypherUtil.getEntailedRelationshipTypes(relationships);
types = newHashSet(transform(entailedRelationships,
new Function<RelationshipType, DirectedRelationshipType>() {
@Override
public DirectedRelationshipType apply(RelationshipType type) {
return new DirectedRelationshipType(type, dir);
}
}));
} else {
for (String relationshipTypeString : relationships) {
RelationshipType type = RelationshipType.withName(relationshipTypeString);
types.add(new DirectedRelationshipType(type, dir));
}
} catch (Exception e) {
throw new BadRequestException("Unknown direction: " + direction);
}
} catch (Exception e) {
throw new BadRequestException("Unknown direction: " + direction);
}

Graph tg = new TinkerGraph();
try (Transaction tx = graphDb.beginTx()) {
Iterable<Node> nodes = transform(roots, new Function<Concept, Node>() {
Expand Down Expand Up @@ -204,7 +207,7 @@ public Object getNeighbors(
@ApiParam(value = "Traverse blank nodes",
required = false) @QueryParam("blankNodes") @DefaultValue("false") BooleanParam traverseBlankNodes,
@ApiParam(value = "Which relationship to traverse",
required = false) @QueryParam("relationshipType") Optional<String> relationshipType,
required = false) @QueryParam("relationshipType") Set<String> relationshipTypes,
@ApiParam(value = DocumentationStrings.DIRECTION_DOC, required = false,
allowableValues = DocumentationStrings.DIRECTION_ALLOWED) @QueryParam("direction") @DefaultValue("BOTH") String direction,
@ApiParam(value = "Should subproperties and equivalent properties be included",
Expand All @@ -214,7 +217,7 @@ public Object getNeighbors(
@ApiParam(value = DocumentationStrings.JSONP_DOC,
required = false) @QueryParam("callback") String callback) {
return getNeighborsFromMultipleRoots(newHashSet(id), depth, traverseBlankNodes,
relationshipType, direction, entail, projection, callback);
relationshipTypes, direction, entail, projection, callback);
}

@GET
Expand All @@ -233,7 +236,7 @@ public Object getNode(
required = false) @QueryParam("project") @DefaultValue("*") Set<String> projection,
@ApiParam(value = DocumentationStrings.JSONP_DOC,
required = false) @QueryParam("callback") String callback) {
return getNeighbors(id, new IntParam("0"), new BooleanParam("false"), Optional.<String>empty(),
return getNeighbors(id, new IntParam("0"), new BooleanParam("false"), new HashSet<String>(),
null, new BooleanParam("false"), projection, callback);
}

Expand Down

0 comments on commit 177fcbc

Please sign in to comment.