Search in sources :

Example 1 with WeightedPathImpl

use of org.neo4j.graphalgo.impl.util.WeightedPathImpl in project graphdb by neo4j-attic.

the class AStar method findSinglePath.

public WeightedPath findSinglePath(Node start, Node end) {
    Doer doer = new Doer(start, end);
    while (doer.hasNext()) {
        Node node = doer.next();
        GraphDatabaseService graphDb = node.getGraphDatabase();
        if (node.equals(end)) {
            // Hit, return path
            double weight = doer.score.get(node.getId()).wayLength;
            LinkedList<Relationship> rels = new LinkedList<Relationship>();
            Relationship rel = graphDb.getRelationshipById(doer.cameFrom.get(node.getId()));
            while (rel != null) {
                rels.addFirst(rel);
                node = rel.getOtherNode(node);
                Long nextRelId = doer.cameFrom.get(node.getId());
                rel = nextRelId == null ? null : graphDb.getRelationshipById(nextRelId);
            }
            Path path = toPath(start, rels);
            return new WeightedPathImpl(weight, path);
        }
    }
    return null;
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPathImpl(org.neo4j.graphalgo.impl.util.WeightedPathImpl) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList)

Example 2 with WeightedPathImpl

use of org.neo4j.graphalgo.impl.util.WeightedPathImpl in project neo4j-mobile-android by neo4j-contrib.

the class AStar method findSinglePath.

public WeightedPath findSinglePath(Node start, Node end) {
    Doer doer = new Doer(start, end);
    while (doer.hasNext()) {
        Node node = doer.next();
        GraphDatabaseService graphDb = node.getGraphDatabase();
        if (node.equals(end)) {
            // Hit, return path
            double weight = doer.score.get(node.getId()).wayLength;
            LinkedList<Relationship> rels = new LinkedList<Relationship>();
            Relationship rel = graphDb.getRelationshipById(doer.cameFrom.get(node.getId()));
            while (rel != null) {
                rels.addFirst(rel);
                node = rel.getOtherNode(node);
                Long nextRelId = doer.cameFrom.get(node.getId());
                rel = nextRelId == null ? null : graphDb.getRelationshipById(nextRelId);
            }
            Path path = toPath(start, rels);
            return new WeightedPathImpl(weight, path);
        }
    }
    return null;
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPathImpl(org.neo4j.graphalgo.impl.util.WeightedPathImpl) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList)

Example 3 with WeightedPathImpl

use of org.neo4j.graphalgo.impl.util.WeightedPathImpl in project neo4j by neo4j.

the class AStar method findSinglePath.

@Override
public WeightedPath findSinglePath(Node start, Node end) {
    lastMetadata = new Metadata();
    AStarIterator iterator = new AStarIterator(start, end);
    while (iterator.hasNext()) {
        Node node = iterator.next();
        GraphDatabaseService graphDb = node.getGraphDatabase();
        if (node.equals(end)) {
            // Hit, return path
            double weight = iterator.visitData.get(node.getId()).wayLength;
            final Path path;
            if (start.getId() == end.getId()) {
                // Nothing to iterate over
                path = PathImpl.singular(start);
            } else {
                LinkedList<Relationship> rels = new LinkedList<Relationship>();
                Relationship rel = graphDb.getRelationshipById(iterator.visitData.get(node.getId()).cameFromRelationship);
                while (rel != null) {
                    rels.addFirst(rel);
                    node = rel.getOtherNode(node);
                    long nextRelId = iterator.visitData.get(node.getId()).cameFromRelationship;
                    rel = nextRelId == -1 ? null : graphDb.getRelationshipById(nextRelId);
                }
                path = toPath(start, rels);
            }
            lastMetadata.paths++;
            return new WeightedPathImpl(weight, path);
        }
    }
    return null;
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPathImpl(org.neo4j.graphalgo.impl.util.WeightedPathImpl) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) TraversalMetadata(org.neo4j.graphdb.traversal.TraversalMetadata) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)3 WeightedPath (org.neo4j.graphalgo.WeightedPath)3 WeightedPathImpl (org.neo4j.graphalgo.impl.util.WeightedPathImpl)3 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)3 Node (org.neo4j.graphdb.Node)3 Path (org.neo4j.graphdb.Path)3 Relationship (org.neo4j.graphdb.Relationship)3 TraversalMetadata (org.neo4j.graphdb.traversal.TraversalMetadata)1