Search in sources :

Example 1 with BranchState

use of org.neo4j.graphdb.traversal.BranchState 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 2 with BranchState

use of org.neo4j.graphdb.traversal.BranchState in project neo4j by neo4j.

the class TestBranchState method evaluateState.

@Test
public void evaluateState() throws Exception {
    /*
         * (a)-1->(b)-2->(c)-3->(d)
         *   \           ^
         *    4         6
         *    (e)-5->(f)
         */
    createGraph("a TO b", "b TO c", "c TO d", "a TO e", "e TO f", "f TO c");
    try (Transaction tx = beginTx()) {
        PathEvaluator<Integer> evaluator = new PathEvaluator.Adapter<Integer>() {

            @Override
            public Evaluation evaluate(Path path, BranchState<Integer> state) {
                return ofIncludes(path.endNode().getProperty("name").equals("c") && state.getState() == 3);
            }
        };
        expectPaths(getGraphDb().traversalDescription().uniqueness(Uniqueness.NODE_PATH).expand(new RelationshipWeightExpander(), new InitialBranchState.State<>(1, 1)).evaluator(evaluator).traverse(getNodeWithName("a")), "a,b,c");
        tx.success();
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) BranchState(org.neo4j.graphdb.traversal.BranchState) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) BranchState(org.neo4j.graphdb.traversal.BranchState) InitialBranchState(org.neo4j.graphdb.traversal.InitialBranchState) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 Path (org.neo4j.graphdb.Path)2 BranchState (org.neo4j.graphdb.traversal.BranchState)2 InitialBranchState (org.neo4j.graphdb.traversal.InitialBranchState)2 HashMap (java.util.HashMap)1 WeightedPath (org.neo4j.graphalgo.WeightedPath)1 TraversalAStar (org.neo4j.graphalgo.impl.path.TraversalAStar)1 Node (org.neo4j.graphdb.Node)1 PathExpander (org.neo4j.graphdb.PathExpander)1 Relationship (org.neo4j.graphdb.Relationship)1 Transaction (org.neo4j.graphdb.Transaction)1