Java 类org.neo4j.graphdb.traversal.Uniqueness 实例源码

项目:SciGraph    文件:CategoryProcessor.java   
@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;
}
项目:dswarm-graph-neo4j    文件:GraphDBUtil.java   
/**
 * note: should be run in transaction scope
 *
 * @param graphDB
 * @param prefixedResourceURI
 * @return
 */
public static Iterable<Path> getResourcePaths(final GraphDatabaseService graphDB, final String prefixedResourceURI) {

    final Node resourceNode = getResourceNode(graphDB, prefixedResourceURI);

    // TODO: maybe replace with gethEntityPaths(GraphdataBaseService, Node)
    final Iterable<Path> paths = graphDB.traversalDescription().uniqueness(Uniqueness.RELATIONSHIP_GLOBAL)
            .order(BranchOrderingPolicies.POSTORDER_BREADTH_FIRST).expand(PathExpanderBuilder.allTypes(Direction.OUTGOING).build())
            .evaluator(path -> {

                final boolean hasLeafLabel = path.endNode() != null &&
                        path.endNode().hasLabel(org.dswarm.graph.GraphProcessingStatics.LEAF_LABEL);

                if (hasLeafLabel) {

                    return org.neo4j.graphdb.traversal.Evaluation.INCLUDE_AND_CONTINUE;
                }
                return org.neo4j.graphdb.traversal.Evaluation.EXCLUDE_AND_CONTINUE;
            }).traverse(resourceNode);

    return paths;
}
项目:dswarm-graph-neo4j    文件:GraphDBUtil.java   
/**
 * note: should be run in transaction scope
 *
 * @param graphDB
 * @param resourceNode
 * @return
 */
public static Iterable<Path> getResourcePaths(final GraphDatabaseService graphDB, final Node resourceNode) {

    // TODO: maybe replace with gethEntityPaths(GraphdataBaseService, Node)
    final Iterable<Path> paths = graphDB.traversalDescription().uniqueness(Uniqueness.RELATIONSHIP_GLOBAL)
            .order(BranchOrderingPolicies.POSTORDER_BREADTH_FIRST).expand(PathExpanderBuilder.allTypes(Direction.OUTGOING).build())
            .evaluator(path -> {

                final boolean reachedEndOfResourcePath =
                        path.length() >= 1 && (path.endNode().hasProperty(org.dswarm.graph.model.GraphStatics.URI_PROPERTY) || path.endNode()
                                .hasProperty(org.dswarm.graph.model.GraphStatics.VALUE_PROPERTY));

                if (reachedEndOfResourcePath) {

                    return org.neo4j.graphdb.traversal.Evaluation.INCLUDE_AND_CONTINUE;
                }

                return org.neo4j.graphdb.traversal.Evaluation.EXCLUDE_AND_CONTINUE;
            }).traverse(resourceNode);

    return paths;
}
项目:power_grid    文件:Energization.java   
@POST
public Response energization(String body, @Context GraphDatabaseService db) throws IOException {
    HashMap input = Validators.getValidEquipmentIds(body);

    Set<Node> startingEquipment = new HashSet<>();
    Set results = new HashSet<>();
    ArrayList<Long> skip = new ArrayList<>();

    try (Transaction tx = db.beginTx()) {
        ((Collection) input.get("ids")).forEach((id) -> startingEquipment.add(db.findNode(Labels.Equipment, "equipment_id", id)));

        if (startingEquipment.isEmpty()) {
            throw Exceptions.equipmentNotFound;
        }

        startingEquipment.forEach(bus -> {
            InitialBranchState.State<Double> ibs;
            ibs = new InitialBranchState.State<>((Double) bus.getProperty("voltage", 999.0), 0.0);
            TraversalDescription td = db.traversalDescription()
                    .depthFirst()
                    .expand(expander, ibs)
                    .uniqueness(Uniqueness.NODE_GLOBAL)
                    .evaluator(evaluator);

            for (org.neo4j.graphdb.Path position : td.traverse(bus)) {
                Node endNode = position.endNode();
                if (!skip.contains(endNode.getId())) {
                    results.add(position.endNode().getProperty("equipment_id"));
                    skip.add(endNode.getId());
                }

                endNode.setProperty("Energized", true);
            }
        });
        tx.success();
    }
    return  Response.ok().entity(objectMapper.writeValueAsString(results)).build();
}
项目:dnainator    文件:AnalyzeCommand.java   
private Iterable<Node> topologicalOrder(GraphDatabaseService service,
                    PrimitiveLongSet processed) {
    return service.traversalDescription()
            .depthFirst()
            .expand(new TopologicalPathExpander()
            , new State<>(processed, null))
            // We manage uniqueness for ourselves.
            .uniqueness(Uniqueness.NONE)
            .traverse(loop(roots))
            .nodes();
}
项目:unmanaged-extension    文件:UnmanagedExtension.java   
/**
     * demo usage of traversal API, see http://docs.neo4j.org/chunked/stable/tutorial-traversal.html
     */
    @GET
    @Produces("application/json")
    @Path("/traversalapi/{label}/{key}/{value}/{relType}/{depth}")
    public People findConnectedNodesUsingTraversalAPI(
            @PathParam("label") String label,
            @PathParam("key") String key,
            @PathParam("value") String value,
            @PathParam("relType") String relType,
            @PathParam("depth") int depth) {

        try (Transaction tx = graphDatabaseService.beginTx()) {
            Node startNode = IteratorUtil.single(graphDatabaseService.findNodesByLabelAndProperty(
                    DynamicLabel.label(label), key, value
            ));

            Iterable<org.neo4j.graphdb.Path> traverserResult =
                    graphDatabaseService
                            .traversalDescription()
                            .relationships(DynamicRelationshipType.withName(relType), Direction.OUTGOING)
                            .uniqueness(Uniqueness.NODE_LEVEL)
                            .evaluator(Evaluators.atDepth(depth))
//                            .evaluator(new TraversalPrinter(Evaluators.atDepth(depth)))
                            .traverse(startNode);
            return new People(new LastNodePropertyIterator(traverserResult, key));
        }
    }
项目:SplitCharater    文件:DbManager.java   
public DbManager( GraphDatabaseService db )
{
    this.db = db;
    // START SNIPPET: basetraverser
    friendsTraversal = db.traversalDescription()
            .depthFirst()
            .relationships( Rels.KNOWS )
            .uniqueness( Uniqueness.RELATIONSHIP_GLOBAL );
    // END SNIPPET: basetraverser
}
项目:obelix    文件:NeoGraphDatabase.java   
public final List<UserItemRelationship> getRelationships(final String userID,
                                                         final String depth,
                                                         final String sinceTimestamp,
                                                         final String untilTimestamp,
                                                         final boolean removeDuplicates)

        throws ObelixNodeNotFoundException {
    try (Transaction tx = neoDb.beginTx()) {
        List<UserItemRelationship> userItemRelationships = new LinkedList<>();
        Node user = getUserNode(neoDb, userID);

        for (Path path : neoDb.traversalDescription()
                .breadthFirst()
                .expand(new TimeStampExpander(sinceTimestamp, untilTimestamp, depth))
                .evaluator(Evaluators.toDepth(Integer.parseInt(depth)))
                .uniqueness(Uniqueness.RELATIONSHIP_GLOBAL)
                .traverse(user)) {

            if (path.lastRelationship() != null) {

                String itemid = path.lastRelationship()
                        .getEndNode().getProperty("node_id").toString();

                userItemRelationships.add(new UserItemRelationship(itemid, path.length()));
            }
        }
        tx.success();
        return userItemRelationships;
    }
}
项目:steps    文件:StepsBuilderTest.java   
@Before
public void openDb() throws IOException {
    String tempDir = temporaryFolder.newFolder().getAbsolutePath();
    db = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(tempDir).newGraphDatabase();
    stepsBuilder = new StepsBuilder();
    baseTraversalDescription = db.traversalDescription().uniqueness(Uniqueness.NONE).breadthFirst();
}
项目:steps    文件:StepTraversalTest.java   
@Before
public void openDb() throws IOException {
    String tempDir = temporaryFolder.newFolder().getAbsolutePath();
    db = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(tempDir).newGraphDatabase();
    engine = new ExecutionEngine(db);
    stepsBuilder = new StepsBuilder();
    baseTraversalDescription = db.traversalDescription().uniqueness(Uniqueness.NONE).breadthFirst();
}
项目:SciGraph    文件:Clique.java   
@Override
public void run() {
  logger.info("Starting clique merge");

  Transaction tx = graphDb.beginTx();
  ResourceIterable<Node> allNodes = graphDb.getAllNodes();
  int size = Iterators.size(allNodes.iterator());
  tx.success();
  tx.close();

  logger.info(size + " nodes left to process");

  tx = graphDb.beginTx();
  TraversalDescription traversalDescription =
      graphDb.traversalDescription().breadthFirst().uniqueness(Uniqueness.NODE_GLOBAL);
  for (RelationshipType rel : relationships) {
    traversalDescription = traversalDescription.relationships(rel, Direction.BOTH);
  }

  Set<Long> processedNodes = new HashSet<Long>();

  for (Node baseNode : allNodes) {

    size -= 1;

    if (size % 100000 == 0) {
      logger.info(size + " nodes left to process");
    }

    if (size % batchCommitSize == 0) {
      logger.fine("Node batch commit");
      tx.success();
      tx.close();
      tx = graphDb.beginTx();
    }

    logger.fine("Processing Node - " + baseNode.getProperty(NodeProperties.IRI));

    if (!processedNodes.contains(baseNode.getId())) {
      // Keep a list of equivalentNodes
      List<Node> clique = new ArrayList<Node>();
      for (Node node : traversalDescription.traverse(baseNode).nodes()) {
        logger.fine("-- " + node.getProperty(NodeProperties.IRI));
        clique.add(node);
        processedNodes.add(node.getId());
      }

      logger.fine("clique size: " + clique.size());
      if (clique.size() == 1) {
        Node defactoLeader = clique.get(0);
        markAsCliqueLeader(defactoLeader);
      } else {
        Node leader = electCliqueLeader(clique, prefixLeaderPriority);
        markAsCliqueLeader(leader);
        clique.remove(leader); // keep only the peasants
        moveEdgesToLeader(leader, clique, tx);
        ensureLabel(leader, clique);
      }

    }

  }

  tx.success();
  tx.close();
}
项目:SciGraph    文件:ReachabilityIndex.java   
/**
 * Create a reachability index on a graph.
 * 
 * @throws InterruptedException
 */
public void createIndex(Predicate<Node> nodePredicate) throws InterruptedException {
  if (indexExists()) {
    throw new IllegalStateException(
        "Reachability index already exists. Drop it first and then recreate it.");
  }

  long startTime = System.currentTimeMillis();
  Set<Entry<Long, Integer>> hopCoverages = getHopCoverages(nodePredicate);
  logger.info(format("Calculated hop coverage in %d second(s)",
      TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime)));

  InMemoryReachabilityIndex inMemoryIndex = new InMemoryReachabilityIndex();

  TraversalDescription incomingTraversal = graphDb.traversalDescription().breadthFirst()
      .uniqueness(Uniqueness.NODE_GLOBAL).expand(new DirectionalPathExpander(Direction.INCOMING))
      .evaluator(new ReachabilityEvaluator(inMemoryIndex, Direction.INCOMING, nodePredicate));

  TraversalDescription outgoingTraversal = graphDb.traversalDescription().breadthFirst()
      .uniqueness(Uniqueness.NODE_GLOBAL).expand(new DirectionalPathExpander(Direction.OUTGOING))
      .evaluator(new ReachabilityEvaluator(inMemoryIndex, Direction.OUTGOING, nodePredicate));

  startTime = System.currentTimeMillis();

  try (Transaction tx = graphDb.beginTx()) {
    for (Entry<Long, Integer> coverage : hopCoverages) {

      Node workingNode = graphDb.getNodeById(coverage.getKey());

      if (coverage.getValue() < 0) {
        inMemoryIndex.put(coverage.getKey(), new InOutList());
      } else {
        InOutListTraverser incomingListTaverser = new InOutListTraverser(incomingTraversal,
            workingNode);
        incomingListTaverser.start();

        InOutListTraverser outgoingListTaverser = new InOutListTraverser(outgoingTraversal,
            workingNode);
        outgoingListTaverser.start();

        incomingListTaverser.join();
        outgoingListTaverser.join();
      }

    }
    tx.success();
  }

  logger.info("Built an InMemoryReachability index in " + ((System.currentTimeMillis() - startTime) / 1000)
      + " sec(s).");
  commitIndexToGraph(inMemoryIndex);
  logger.info("Reachability index created.");
}
项目:dswarm-graph-neo4j    文件:GraphDBUtil.java   
/**
 * note: should be run in transaction scope
 *
 * @param graphDB
 * @param entityNode
 * @return
 */
private static Iterable<Path> getEntityPaths(final GraphDatabaseService graphDB, final Node entityNode) {

    return graphDB.traversalDescription().uniqueness(Uniqueness.RELATIONSHIP_GLOBAL).order(BranchOrderingPolicies.POSTORDER_BREADTH_FIRST)
            .expand(PathExpanderBuilder.allTypes(Direction.OUTGOING).build()).evaluator(path -> {

                final boolean hasLeafLabel = path.endNode().hasLabel(org.dswarm.graph.GraphProcessingStatics.LEAF_LABEL);

                if (hasLeafLabel) {

                    return org.neo4j.graphdb.traversal.Evaluation.INCLUDE_AND_PRUNE;
                }

                return org.neo4j.graphdb.traversal.Evaluation.EXCLUDE_AND_CONTINUE;
            }).traverse(entityNode);
}
项目:dswarm-graph-neo4j    文件:GraphDBUtil.java   
/**
 * note: should be run in transaction scope
 *
 * @param graphDB
 * @param entityId
 * @param leafNodeId
 * @return
 */
public static Iterable<Path> getEntityPaths(final GraphDatabaseService graphDB, final long entityId, final long leafNodeId) {

    final Node entityNode = graphDB.getNodeById(entityId);

    return graphDB.traversalDescription().uniqueness(Uniqueness.RELATIONSHIP_GLOBAL).order(BranchOrderingPolicies.POSTORDER_BREADTH_FIRST)
            .expand(PathExpanderBuilder.allTypes(Direction.OUTGOING).build()).evaluator(path -> {

                final boolean hasLeafLabel = path.endNode().hasLabel(org.dswarm.graph.GraphProcessingStatics.LEAF_LABEL);

                if (hasLeafLabel && path.endNode().getId() == leafNodeId) {

                    return org.neo4j.graphdb.traversal.Evaluation.INCLUDE_AND_PRUNE;
                }

                return org.neo4j.graphdb.traversal.Evaluation.EXCLUDE_AND_CONTINUE;
            }).traverse(entityNode);
}
项目:dswarm-graph-neo4j    文件:GraphDBUtil.java   
/**
 * note: should be executed in transaction scope
 *
 * @param nodeId
 * @param graphDB
 * @return
 */
public static Iterable<Path> getNonMatchedSubGraphPaths(final long nodeId, final GraphDatabaseService graphDB) {

    final Node entityNode = graphDB.getNodeById(nodeId);

    // final int entityNodeHierarchyLevel = (int) entityNode.getProperty("__HIERARCHY_LEVEL__");

    final Iterable<Path> paths = graphDB.traversalDescription().uniqueness(Uniqueness.RELATIONSHIP_GLOBAL)
            .order(BranchOrderingPolicies.POSTORDER_BREADTH_FIRST).expand(PathExpanderBuilder.allTypes(Direction.OUTGOING).build())
            .evaluator(path -> {

                // if (entityNodeHierarchyLevel > (int) path.endNode().getProperty("__HIERARCHY_LEVEL__")) {
                //
                // return Evaluation.EXCLUDE_AND_PRUNE;
                // }

                if (path.lastRelationship() == null && path.endNode().hasLabel(org.dswarm.graph.GraphProcessingStatics.LEAF_LABEL)) {

                    return org.neo4j.graphdb.traversal.Evaluation.EXCLUDE_AND_PRUNE;
                }

                if (path.lastRelationship() == null) {

                    return org.neo4j.graphdb.traversal.Evaluation.EXCLUDE_AND_CONTINUE;
                }

                if (path.lastRelationship().hasProperty(org.dswarm.graph.delta.DeltaStatics.MATCHED_PROPERTY)) {

                    // include only non-matched relationships (paths)
                    return org.neo4j.graphdb.traversal.Evaluation.EXCLUDE_AND_PRUNE;
                }

                final boolean hasLeafLabel = path.endNode().hasLabel(org.dswarm.graph.GraphProcessingStatics.LEAF_LABEL);

                if (hasLeafLabel) {

                    return org.neo4j.graphdb.traversal.Evaluation.INCLUDE_AND_PRUNE;
                }

                return org.neo4j.graphdb.traversal.Evaluation.EXCLUDE_AND_CONTINUE;
            }).traverse(entityNode);

    return paths;
}