Search in sources :

Example 1 with StandardExpander

use of org.neo4j.graphdb.impl.StandardExpander in project neo4j by neo4j.

the class TestShortestPath method withFilters.

@Test
void withFilters() {
    // 
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c,d");
        graph.makeEdgeChain(transaction, "a,g,h,d");
        final Node a = graph.getNode(transaction, "a");
        final Node d = graph.getNode(transaction, "d");
        final Node b = graph.getNode(transaction, "b");
        b.setProperty("skip", true);
        var context = new BasicEvaluationContext(transaction, graphDb);
        final Predicate<Node> filter = item -> {
            final boolean skip = (Boolean) item.getProperty("skip", false);
            return !skip;
        };
        testShortestPathFinder(context, finder -> assertPaths(finder.findAllPaths(a, d), "a,g,h,d"), ((StandardExpander) allTypesAndDirections()).addNodeFilter(filter), 10);
        transaction.commit();
    }
}
Also used : ResourceIterator(org.neo4j.graphdb.ResourceIterator) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Label(org.neo4j.graphdb.Label) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PathFinder(org.neo4j.graphalgo.PathFinder) OUTGOING(org.neo4j.graphdb.Direction.OUTGOING) GraphAlgoFactory.shortestPath(org.neo4j.graphalgo.GraphAlgoFactory.shortestPath) EmptyMemoryTracker(org.neo4j.memory.EmptyMemoryTracker) PathExpanders(org.neo4j.graphdb.PathExpanders) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BOTH(org.neo4j.graphdb.Direction.BOTH) EvaluationContext(org.neo4j.graphalgo.EvaluationContext) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PathExpander(org.neo4j.graphdb.PathExpander) Iterables(org.neo4j.internal.helpers.collection.Iterables) Arrays.asList(java.util.Arrays.asList) R1(common.Neo4jAlgoTestCase.MyRelTypes.R1) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Transaction(org.neo4j.graphdb.Transaction) StandardExpander(org.neo4j.graphdb.impl.StandardExpander) Iterator(java.util.Iterator) Iterables.count(org.neo4j.internal.helpers.collection.Iterables.count) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Neo4jAlgoTestCase(common.Neo4jAlgoTestCase) INCOMING(org.neo4j.graphdb.Direction.INCOMING) BranchState(org.neo4j.graphdb.traversal.BranchState) Test(org.junit.jupiter.api.Test) Path(org.neo4j.graphdb.Path) PathExpanders.forTypeAndDirection(org.neo4j.graphdb.PathExpanders.forTypeAndDirection) List(java.util.List) Relationship(org.neo4j.graphdb.Relationship) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) PathExpanders.allTypesAndDirections(org.neo4j.graphdb.PathExpanders.allTypesAndDirections) RelationshipType(org.neo4j.graphdb.RelationshipType) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.jupiter.api.Test)

Example 2 with StandardExpander

use of org.neo4j.graphdb.impl.StandardExpander in project neo4j by neo4j.

the class TestShortestPath method filtersTouchesAllIntermediateNodes.

@Test
void filtersTouchesAllIntermediateNodes() {
    // 
    try (Transaction transaction = graphDb.beginTx()) {
        graph.makeEdgeChain(transaction, "a,b,c,d");
        final Node a = graph.getNode(transaction, "a");
        final Node d = graph.getNode(transaction, "d");
        Collection<Node> touchedByFilter = new HashSet<>();
        final Predicate<Node> filter = item -> {
            touchedByFilter.add(item);
            return true;
        };
        final PathExpander expander = PathExpanderBuilder.empty().add(R1, OUTGOING).addNodeFilter(filter).build();
        // final PathExpander expander = ((StandardExpander) PathExpanders.forTypeAndDirection(R1, OUTGOING)).addNodeFilter( filter );
        var context = new BasicEvaluationContext(transaction, graphDb);
        Path path = Iterables.single(shortestPath(context, expander, 10).findAllPaths(a, d));
        assertEquals(3, path.length());
        List<Node> nodes = Iterables.asList(path.nodes());
        List<Node> intermediateNodes = nodes.subList(1, nodes.size() - 1);
        assertTrue(touchedByFilter.containsAll(intermediateNodes), "touchedByFilter: " + touchedByFilter);
        assertFalse(touchedByFilter.contains(a));
        assertFalse(touchedByFilter.contains(d));
        transaction.commit();
    }
}
Also used : ResourceIterator(org.neo4j.graphdb.ResourceIterator) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Label(org.neo4j.graphdb.Label) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PathFinder(org.neo4j.graphalgo.PathFinder) OUTGOING(org.neo4j.graphdb.Direction.OUTGOING) GraphAlgoFactory.shortestPath(org.neo4j.graphalgo.GraphAlgoFactory.shortestPath) EmptyMemoryTracker(org.neo4j.memory.EmptyMemoryTracker) PathExpanders(org.neo4j.graphdb.PathExpanders) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BOTH(org.neo4j.graphdb.Direction.BOTH) EvaluationContext(org.neo4j.graphalgo.EvaluationContext) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PathExpander(org.neo4j.graphdb.PathExpander) Iterables(org.neo4j.internal.helpers.collection.Iterables) Arrays.asList(java.util.Arrays.asList) R1(common.Neo4jAlgoTestCase.MyRelTypes.R1) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Transaction(org.neo4j.graphdb.Transaction) StandardExpander(org.neo4j.graphdb.impl.StandardExpander) Iterator(java.util.Iterator) Iterables.count(org.neo4j.internal.helpers.collection.Iterables.count) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Neo4jAlgoTestCase(common.Neo4jAlgoTestCase) INCOMING(org.neo4j.graphdb.Direction.INCOMING) BranchState(org.neo4j.graphdb.traversal.BranchState) Test(org.junit.jupiter.api.Test) Path(org.neo4j.graphdb.Path) PathExpanders.forTypeAndDirection(org.neo4j.graphdb.PathExpanders.forTypeAndDirection) List(java.util.List) Relationship(org.neo4j.graphdb.Relationship) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) PathExpanders.allTypesAndDirections(org.neo4j.graphdb.PathExpanders.allTypesAndDirections) RelationshipType(org.neo4j.graphdb.RelationshipType) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) GraphAlgoFactory.shortestPath(org.neo4j.graphalgo.GraphAlgoFactory.shortestPath) Path(org.neo4j.graphdb.Path) BasicEvaluationContext(org.neo4j.graphalgo.BasicEvaluationContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) PathExpander(org.neo4j.graphdb.PathExpander) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

Neo4jAlgoTestCase (common.Neo4jAlgoTestCase)2 R1 (common.Neo4jAlgoTestCase.MyRelTypes.R1)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Set (java.util.Set)2 Predicate (java.util.function.Predicate)2 MutableInt (org.apache.commons.lang3.mutable.MutableInt)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)2 Test (org.junit.jupiter.api.Test)2 BasicEvaluationContext (org.neo4j.graphalgo.BasicEvaluationContext)2 EvaluationContext (org.neo4j.graphalgo.EvaluationContext)2 GraphAlgoFactory.shortestPath (org.neo4j.graphalgo.GraphAlgoFactory.shortestPath)2 PathFinder (org.neo4j.graphalgo.PathFinder)2