Search in sources :

Example 1 with PathExpander

use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.

the class RelationshipExpanderBuilderTest method shouldInterpretNoSpecifiedRelationshipsAsAll.

@Test
public void shouldInterpretNoSpecifiedRelationshipsAsAll() throws Exception {
    // GIVEN
    Node node = createSomeData();
    PathExpander expander = RelationshipExpanderBuilder.describeRelationships(map());
    // WHEN
    Set<Relationship> expanded;
    try (Transaction tx = db.beginTx()) {
        expanded = asSet(expander.expand(singleNodePath(node), NO_STATE));
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        // THEN
        assertEquals(asSet(node.getRelationships()), expanded);
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) PathExpander(org.neo4j.graphdb.PathExpander) Test(org.junit.Test)

Example 2 with PathExpander

use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.

the class DijkstraBidirectional method traverser.

private Traverser traverser(Node start, final Node end, PathInterest interest) {
    final MutableDouble shortestSoFar = new MutableDouble(Double.MAX_VALUE);
    final MutableDouble startSideShortest = new MutableDouble(0);
    final MutableDouble endSideShortest = new MutableDouble(0);
    PathExpander dijkstraExpander = new DijkstraBidirectionalPathExpander(expander, shortestSoFar, true, startSideShortest, endSideShortest, epsilon);
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription side = db.traversalDescription().expand(dijkstraExpander, stateFactory).order(new DijkstraSelectorFactory(interest, costEvaluator)).evaluator(new DijkstraBidirectionalEvaluator(costEvaluator)).uniqueness(Uniqueness.NODE_PATH);
    TraversalDescription startSide = side;
    TraversalDescription endSide = side.reverse();
    BidirectionalTraversalDescription traversal = db.bidirectionalTraversalDescription().startSide(startSide).endSide(endSide).collisionEvaluator(Evaluators.all()).collisionPolicy(new BranchCollisionPolicy() {

        @Override
        public BranchCollisionDetector create(Evaluator evaluator, Predicate<Path> pathPredicate) {
            return new DijkstraBranchCollisionDetector(evaluator, costEvaluator, shortestSoFar, epsilon, pathPredicate);
        }
    });
    lastTraverser = traversal.traverse(start, end);
    return lastTraverser;
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) BranchCollisionPolicy(org.neo4j.graphdb.traversal.BranchCollisionPolicy) MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) PathExpander(org.neo4j.graphdb.PathExpander) DijkstraSelectorFactory(org.neo4j.graphalgo.impl.util.DijkstraSelectorFactory) PathEvaluator(org.neo4j.graphdb.traversal.PathEvaluator) Evaluator(org.neo4j.graphdb.traversal.Evaluator) CostEvaluator(org.neo4j.graphalgo.CostEvaluator) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) BranchCollisionDetector(org.neo4j.graphdb.traversal.BranchCollisionDetector) DijkstraBranchCollisionDetector(org.neo4j.graphalgo.impl.util.DijkstraBranchCollisionDetector) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) DijkstraBranchCollisionDetector(org.neo4j.graphalgo.impl.util.DijkstraBranchCollisionDetector)

Example 3 with PathExpander

use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.

the class TestAStar method canUseBranchState.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void canUseBranchState() throws Exception {
    // This test doesn't use the predefined finder, which only means an unnecessary instantiation
    // if such an object. And this test will be run twice (once for each finder type in data()).
    /**
         * <pre>
         *   012345    A - B:  2
         *  +------>y  A - B:  2
         * 0|A         B - C:  3
         * 1|          A - C:  10
         * 2| B
         * 3|
         * 4|
         * 5|
         * 6|
         * 7|C
         *  V
         *  x
         *
         * </pre>
         */
    Node nodeA = graph.makeNode("A", "x", 0d, "y", 0d);
    Node nodeB = graph.makeNode("B", "x", 2d, "y", 1d);
    Node nodeC = graph.makeNode("C", "x", 7d, "y", 0d);
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("A", "B", "length", 2d);
    graph.makeEdge("B", "C", "length", 3d);
    graph.makeEdge("A", "C", "length", 10d);
    final Map<Node, Double> seenBranchStates = new HashMap<Node, Double>();
    PathExpander<Double> expander = new PathExpander<Double>() {

        @Override
        public Iterable<Relationship> expand(Path path, BranchState<Double> state) {
            double newState = state.getState();
            if (path.length() > 0) {
                newState += (Double) path.lastRelationship().getProperty("length");
                state.setState(newState);
            }
            seenBranchStates.put(path.endNode(), newState);
            return path.endNode().getRelationships(OUTGOING);
        }

        @Override
        public PathExpander<Double> reverse() {
            throw new UnsupportedOperationException();
        }
    };
    double initialStateValue = 0D;
    PathFinder<WeightedPath> traversalFinder = new TraversalAStar(expander, new InitialBranchState.State(initialStateValue, initialStateValue), doubleCostEvaluator("length"), ESTIMATE_EVALUATOR);
    WeightedPath path = traversalFinder.findSinglePath(nodeA, nodeC);
    assertEquals((Double) 5.0D, (Double) path.weight());
    assertPathDef(path, "A", "B", "C");
    assertEquals(MapUtil.<Node, Double>genericMap(nodeA, 0D, nodeB, 2D), seenBranchStates);
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) TraversalAStar(org.neo4j.graphalgo.impl.path.TraversalAStar) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) BranchState(org.neo4j.graphdb.traversal.BranchState) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) WeightedPath(org.neo4j.graphalgo.WeightedPath) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 4 with PathExpander

use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.

the class PathShellApp method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws Exception {
    String fromString = parser.options().get("from");
    String toString = parser.argument(0, "Must supply a 'to' node as first argument");
    String algo = parser.options().get("a");
    String maxDepthString = parser.options().get("m");
    boolean quietPrint = parser.options().containsKey("q");
    boolean caseInsensitiveFilters = parser.options().containsKey("i");
    boolean looseFilters = parser.options().containsKey("l");
    int maxDepth = maxDepthString != null ? Integer.parseInt(maxDepthString) : Integer.MAX_VALUE;
    fromString = fromString != null ? fromString : String.valueOf(this.getCurrent(session).getId());
    Map<String, Object> filter = parseFilter(parser.options().get("f"), out);
    PathExpander expander = toExpander(getServer().getDb(), Direction.BOTH, filter, caseInsensitiveFilters, looseFilters);
    PathFinder<Path> finder = expander != null ? getPathFinder(algo, expander, maxDepth, out) : null;
    if (finder != null) {
        Node fromNode = getNodeById(Long.parseLong(fromString));
        Node toNode = getNodeById(Long.parseLong(toString));
        boolean single = parser.options().containsKey("s");
        Iterable<Path> paths = single ? Arrays.asList(finder.findSinglePath(fromNode, toNode)) : finder.findAllPaths(fromNode, toNode);
        for (Path path : paths) {
            printPath(path, quietPrint, session, out);
        }
    }
    return Continuation.INPUT_COMPLETE;
}
Also used : Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander)

Example 5 with PathExpander

use of org.neo4j.graphdb.PathExpander in project neo4j by neo4j.

the class AncestorTestCase method test.

@Test
@Graph({ "root contains child1", "child1 contains child11", "child1 contains child12", "root contains child2", "child12 contains child121", "child1 contains child13" })
public void test() {
    PathExpander expander = PathExpanders.forTypeAndDirection(Rels.contains, Direction.INCOMING);
    List<Node> nodeSet = new ArrayList<Node>();
    Map<String, Node> graph = data.get();
    nodeSet.add(graph.get("child1"));
    nodeSet.add(graph.get("root"));
    try (Transaction transaction = gdb.beginTx()) {
        Node ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
        assertEquals(graph.get("root"), ancestor);
        nodeSet.clear();
        nodeSet.add(graph.get("child12"));
        nodeSet.add(graph.get("child11"));
        ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
        assertEquals(graph.get("child1"), ancestor);
        nodeSet.clear();
        nodeSet.add(graph.get("child121"));
        nodeSet.add(graph.get("child12"));
        ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
        assertEquals(graph.get("child12"), ancestor);
        nodeSet.clear();
        nodeSet.add(graph.get("child11"));
        nodeSet.add(graph.get("child13"));
        ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
        assertEquals(graph.get("child1"), ancestor);
        nodeSet.clear();
        nodeSet.add(graph.get("child2"));
        nodeSet.add(graph.get("child121"));
        ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
        assertEquals(graph.get("root"), ancestor);
        nodeSet.clear();
        nodeSet.add(graph.get("child11"));
        nodeSet.add(graph.get("child12"));
        nodeSet.add(graph.get("child13"));
        ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
        assertEquals(graph.get("child1"), ancestor);
        nodeSet.clear();
        nodeSet.add(graph.get("child11"));
        nodeSet.add(graph.get("child12"));
        nodeSet.add(graph.get("child13"));
        nodeSet.add(graph.get("child121"));
        ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
        assertEquals(graph.get("child1"), ancestor);
        nodeSet.clear();
        nodeSet.add(graph.get("child11"));
        nodeSet.add(graph.get("child12"));
        nodeSet.add(graph.get("child13"));
        nodeSet.add(graph.get("child121"));
        nodeSet.add(graph.get("child2"));
        ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
        assertEquals(graph.get("root"), ancestor);
        nodeSet.clear();
        nodeSet.add(graph.get("child11"));
        nodeSet.add(graph.get("child12"));
        nodeSet.add(graph.get("child13"));
        nodeSet.add(graph.get("child121"));
        nodeSet.add(graph.get("child12"));
        nodeSet.add(graph.get("root"));
        ancestor = AncestorsUtil.lowestCommonAncestor(nodeSet, expander);
        assertEquals(graph.get("root"), ancestor);
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) PathExpander(org.neo4j.graphdb.PathExpander) Graph(org.neo4j.test.GraphDescription.Graph) Test(org.junit.Test)

Aggregations

PathExpander (org.neo4j.graphdb.PathExpander)13 Node (org.neo4j.graphdb.Node)12 Transaction (org.neo4j.graphdb.Transaction)8 Path (org.neo4j.graphdb.Path)6 Test (org.junit.jupiter.api.Test)5 Relationship (org.neo4j.graphdb.Relationship)5 Test (org.junit.Test)4 BranchState (org.neo4j.graphdb.traversal.BranchState)4 BasicEvaluationContext (org.neo4j.graphalgo.BasicEvaluationContext)3 WeightedPath (org.neo4j.graphalgo.WeightedPath)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TraversalAStar (org.neo4j.graphalgo.impl.path.TraversalAStar)2 PathExpanderBuilder (org.neo4j.graphdb.PathExpanderBuilder)2 OrderedByTypeExpander (org.neo4j.graphdb.impl.OrderedByTypeExpander)2 Neo4jAlgoTestCase (common.Neo4jAlgoTestCase)1 R1 (common.Neo4jAlgoTestCase.MyRelTypes.R1)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1