org.neo4j.graphdb.Path Java Examples

The following examples show how to use org.neo4j.graphdb.Path. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CategoryProcessor.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Set<Long>> call() throws Exception {
  logger.info("Processsing " + category);
  Map<String, Set<Long>> map = new HashMap<String, Set<Long>>();
  Set<Long> nodeSet = new HashSet<Long>();
  Transaction tx = graphDb.beginTx();
  try {
    for (Path position : graphDb.traversalDescription().uniqueness(Uniqueness.NODE_GLOBAL).depthFirst()
        .relationships(OwlRelationships.RDFS_SUBCLASS_OF, Direction.INCOMING).relationships(OwlRelationships.RDF_TYPE, Direction.INCOMING)
        .relationships(OwlRelationships.OWL_EQUIVALENT_CLASS, Direction.BOTH).relationships(OwlRelationships.OWL_SAME_AS, Direction.BOTH)
        .traverse(root)) {
      Node end = position.endNode();
      nodeSet.add(end.getId());
    }
    logger.info("Discovered " + nodeSet.size() + " nodes for " + category);
    map.put(category, nodeSet);
  } catch (Exception e) {
    logger.warning("IRI not found for category: " + category);
  } finally {
    tx.success();
    tx.close();
  }
  return map;
}
 
Example #2
Source File: GraphApi.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
public Graph getEdges(RelationshipType type, boolean entail, long skip, long limit) {
  String query = "MATCH path = (start)-[r:" + type.name() + (entail ? "!" : "") + "]->(end) "
      + " RETURN path "
      // TODO: This slows down the query dramatically.
      // + " ORDER BY ID(r) "
      + " SKIP " + skip + " LIMIT " + limit;
  Graph graph = new TinkerGraph();
  TinkerGraphUtil tgu = new TinkerGraphUtil(graph, curieUtil);
  Result result;
  try {
    result = cypherUtil.execute(query);
    while (result.hasNext()) {
      Map<String, Object> map = result.next();
      Path path = (Path) map.get("path");
      tgu.addPath(path);
    }
  } catch (ArrayIndexOutOfBoundsException e) {
    // Return and empty graph if the limit is too high...
  }
  return graph;
}
 
Example #3
Source File: DecisionTreeEvaluator.java    From decision_trees_with_rules with MIT License 5 votes vote down vote up
@Override
public Evaluation evaluate(Path path, BranchState branchState) {
    // If we get to an Answer stop traversing, we found a valid path.
    if (path.endNode().hasLabel(Labels.Answer)) {
        return Evaluation.INCLUDE_AND_PRUNE;
    } else {
        // If not, continue down this path if there is anything else to find.
        return Evaluation.EXCLUDE_AND_CONTINUE;
    }
}
 
Example #4
Source File: GraphApi.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
/***
 * @param parent
 * @param relationship
 * @param traverseEquivalentEdges
 * @return the entailment
 */
public Collection<Node> getEntailment(Node parent, DirectedRelationshipType relationship,
    boolean traverseEquivalentEdges) {
  Set<Node> entailment = new HashSet<>();
  TraversalDescription description = graphDb.traversalDescription().depthFirst()
      .relationships(relationship.getType(), relationship.getDirection())
      .evaluator(Evaluators.fromDepth(0)).evaluator(Evaluators.all());
  if (traverseEquivalentEdges) {
    description = description.relationships(OwlRelationships.OWL_EQUIVALENT_CLASS);
  }
  for (Path path : description.traverse(parent)) {
    entailment.add(path.endNode());
  }
  return entailment;
}
 
Example #5
Source File: TinkerGraphUtil.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
public Graph resultToGraph(Result result) {
  graph = new TinkerGraph();
  while (result.hasNext()) {
    Map<String, Object> map = result.next();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
      Object value = entry.getValue();
      String key = entry.getKey();
      if (null == value) {
        continue;
      } else if (value instanceof PropertyContainer) {
        addElement((PropertyContainer) value);
      } else if (value instanceof Path) {
        for (PropertyContainer container : (Path) value) {
          addElement(container);
        }
      } else if (value instanceof ArrayList) {
        for (Object thing : (ArrayList<?>) value) {
          if (thing instanceof PropertyContainer) {
            addElement((PropertyContainer) thing);
          }
        }
      } else if (value instanceof Boolean) {
        // generates a lonely node which contains the result
        Vertex vertex = graph.addVertex(key);
        vertex.setProperty(key, value);
        vertex.setProperty(NodeProperties.LABEL, "Boolean result");
        vertex.setProperty(CommonProperties.IRI, key);
      } else {
        logger.warning("Not converting " + value.getClass() + " to tinker graph");
      }
    }
  }
  return graph;
}
 
Example #6
Source File: ReachabilityEvaluator.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Override
public Evaluation evaluate(Path path) {
  long currentId = path.endNode().getId();
  if (!nodePredicate.apply(path.endNode())) {
    inMemoryIndex.get(currentId);
    return Evaluation.EXCLUDE_AND_PRUNE;
  }

  long startId = path.startNode().getId(); // Vi
  InOutList listPair = inMemoryIndex.get(currentId);

  if (0 == path.length()) {
    // first node in the traverse - add itself to the in-out list
    listPair.getInList().add(currentId);
    listPair.getOutList().add(currentId);
    return Evaluation.INCLUDE_AND_CONTINUE;
  }
  else if (direction == Direction.INCOMING ) {
    // doing reverse BFS
    if (nodesAreConnected(currentId, startId)) {
      return Evaluation.EXCLUDE_AND_PRUNE;
    } else {
      listPair.getOutList().add(startId);
      return Evaluation.INCLUDE_AND_CONTINUE;
    }
  } else {
    //doing BFS
    if (nodesAreConnected(startId, currentId)) { // cur is w
      return Evaluation.EXCLUDE_AND_PRUNE;
    } else {
      listPair.getInList().add(startId);
      return Evaluation.INCLUDE_AND_CONTINUE;
    }
  }
}
 
Example #7
Source File: ReachabilityIndex.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  try (Transaction tx = startNode.getGraphDatabase().beginTx()) {
    for (Path p : traversalDescription.traverse(startNode)) {
      logger.finest(p.toString()); // Avoids unused variable warning
    }
    tx.success();
  }
}
 
Example #8
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@Override
public void shortestPath(Node n1, Integer i)
{
    PathFinder<Path> finder
        = GraphAlgoFactory.shortestPath(Traversal.expanderForTypes(Neo4jGraphDatabase.RelTypes.SIMILAR), 5);
    Node n2 = getVertex(i);
    Path path = finder.findSinglePath(n1, n2);

}
 
Example #9
Source File: DecisionTreeEvaluator.java    From decision_trees_with_rules with MIT License 4 votes vote down vote up
@Override
public Evaluation evaluate(Path path) {
    return null;
}
 
Example #10
Source File: PathResult.java    From decision_trees_with_rules with MIT License 4 votes vote down vote up
public PathResult(Path path) {
    this.path = path;
}
 
Example #11
Source File: PathOutput.java    From neo4j-versioner-core with Apache License 2.0 4 votes vote down vote up
public PathOutput(Path path) {
    this.path = path;
}
 
Example #12
Source File: DirectionalPathExpander.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Override
public Iterable<Relationship> expand(Path path, BranchState<Void> state) {
  return path.endNode().getRelationships(direction);
}