Search in sources :

Example 41 with Path

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

the class ShortestPath method hitsToPaths.

private static Collection<Path> hitsToPaths(EvaluationContext context, Collection<Hit> depthHits, Node start, Node end, boolean stopAsap, int maxResultCount, MemoryTracker memoryTracker) {
    Set<Path> paths = HeapTrackingCollections.newSet(memoryTracker);
    for (Hit hit : depthHits) {
        for (PathImpl path : hitToPaths(context, hit, start, end, stopAsap)) {
            memoryTracker.allocateHeap(path.estimatedHeapUsage());
            paths.add(path);
            if (paths.size() >= maxResultCount) {
                break;
            }
        }
    }
    return paths;
}
Also used : Path(org.neo4j.graphdb.Path) PathImpl(org.neo4j.graphalgo.impl.util.PathImpl)

Example 42 with Path

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

the class PathProxyTest method shouldIterateThroughRelationshipsInReverse.

@Test
void shouldIterateThroughRelationshipsInReverse() {
    // given
    Path path = new PathProxy(transaction, new long[] { 1, 2, 3 }, new long[] { 100, 200 }, new int[] { 0, ~0 });
    Iterator<Relationship> iterator = path.reverseRelationships().iterator();
    Relationship relationship;
    // then
    assertTrue(iterator.hasNext());
    assertThat(relationship = iterator.next()).isInstanceOf(Relationship.class);
    assertEquals(200, relationship.getId());
    assertEquals(3, relationship.getStartNodeId());
    assertEquals(2, relationship.getEndNodeId());
    assertTrue(iterator.hasNext());
    assertThat(relationship = iterator.next()).isInstanceOf(Relationship.class);
    assertEquals(100, relationship.getId());
    assertEquals(1, relationship.getStartNodeId());
    assertEquals(2, relationship.getEndNodeId());
    assertFalse(iterator.hasNext());
}
Also used : Path(org.neo4j.graphdb.Path) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.jupiter.api.Test)

Example 43 with Path

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

the class ProcedureCompilation method toAnyValue.

/**
 * Takes an expression evaluating to one of the supported java values and turns
 * it into the corresponding AnyValue
 *
 * @param expression the expression to evaluate
 * @param userType the type of the expression to map
 * @return an expression properly mapped to AnyValue
 */
private static Expression toAnyValue(Expression expression, Class<?> userType, Expression context) {
    if (AnyValue.class.isAssignableFrom(userType)) {
        return nullCheck(expression, cast(userType, expression));
    }
    String type = userType.getCanonicalName();
    if (type.equals(LONG)) {
        return invoke(methodReference(Values.class, LongValue.class, "longValue", long.class), expression);
    } else if (type.equals(BOXED_LONG)) {
        return nullCheck(expression, invoke(methodReference(Values.class, LongValue.class, "longValue", long.class), unbox(expression)));
    } else if (type.equals(DOUBLE)) {
        return invoke(methodReference(Values.class, DoubleValue.class, "doubleValue", double.class), expression);
    } else if (type.equals(BOXED_DOUBLE)) {
        return nullCheck(expression, invoke(methodReference(Values.class, DoubleValue.class, "doubleValue", double.class), unbox(expression)));
    } else if (type.equals(NUMBER)) {
        return nullCheck(expression, invoke(methodReference(Values.class, NumberValue.class, "numberValue", Number.class), expression));
    } else if (type.equals(BOOLEAN)) {
        return invoke(methodReference(Values.class, BooleanValue.class, "booleanValue", boolean.class), expression);
    } else if (type.equals(BOXED_BOOLEAN)) {
        return nullCheck(expression, invoke(methodReference(Values.class, BooleanValue.class, "booleanValue", boolean.class), unbox(expression)));
    } else if (type.equals(STRING)) {
        return invoke(methodReference(Values.class, Value.class, "stringOrNoValue", String.class), expression);
    } else if (type.equals(BYTE_ARRAY)) {
        return nullCheck(expression, invoke(methodReference(Values.class, ByteArray.class, "byteArray", byte[].class), expression));
    } else if (type.equals(LIST)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, ListValue.class, "asListValue", Iterable.class), expression));
    } else if (type.equals(MAP)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, MapValue.class, "asMapValue", Map.class), expression));
    } else if (type.equals(ZONED_DATE_TIME)) {
        return nullCheck(expression, invoke(methodReference(DateTimeValue.class, DateTimeValue.class, "datetime", ZonedDateTime.class), expression));
    } else if (type.equals(OFFSET_TIME)) {
        return nullCheck(expression, invoke(methodReference(TimeValue.class, TimeValue.class, "time", OffsetTime.class), expression));
    } else if (type.equals(LOCAL_DATE)) {
        return nullCheck(expression, invoke(methodReference(DateValue.class, DateValue.class, "date", LocalDate.class), expression));
    } else if (type.equals(LOCAL_TIME)) {
        return nullCheck(expression, invoke(methodReference(LocalTimeValue.class, LocalTimeValue.class, "localTime", LocalTime.class), expression));
    } else if (type.equals(LOCAL_DATE_TIME)) {
        return nullCheck(expression, invoke(methodReference(LocalDateTimeValue.class, LocalDateTimeValue.class, "localDateTime", LocalDateTime.class), expression));
    } else if (type.equals(TEMPORAL_AMOUNT)) {
        return nullCheck(expression, invoke(methodReference(Values.class, DurationValue.class, "durationValue", TemporalAmount.class), expression));
    } else if (type.equals(NODE)) {
        Expression internalTransaction = invoke(context, methodReference(Context.class, InternalTransaction.class, "internalTransactionOrNull"));
        Expression getNode = invoke(internalTransaction, methodReference(InternalTransaction.class, Entity.class, "validateSameDB", Entity.class), expression);
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, NodeValue.class, "fromNodeEntity", Node.class), getNode));
    } else if (type.equals(RELATIONSHIP)) {
        Expression internalTransaction = invoke(context, methodReference(Context.class, InternalTransaction.class, "internalTransactionOrNull"));
        Expression getRelationship = invoke(internalTransaction, methodReference(InternalTransaction.class, Entity.class, "validateSameDB", Entity.class), expression);
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, RelationshipValue.class, "fromRelationshipEntity", Relationship.class), getRelationship));
    } else if (type.equals(PATH)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, PathValue.class, "fromPath", Path.class), expression));
    } else if (type.equals(POINT)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, PointValue.class, "asPointValue", Point.class), expression));
    } else {
        return invoke(methodReference(ValueUtils.class, AnyValue.class, "of", Object.class), expression);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Context(org.neo4j.kernel.api.procedure.Context) Path(org.neo4j.graphdb.Path) NodeValue(org.neo4j.values.virtual.NodeValue) Entity(org.neo4j.graphdb.Entity) LocalDateTimeValue(org.neo4j.values.storable.LocalDateTimeValue) DateTimeValue(org.neo4j.values.storable.DateTimeValue) PathValue(org.neo4j.values.virtual.PathValue) ListValue(org.neo4j.values.virtual.ListValue) Node(org.neo4j.graphdb.Node) Values(org.neo4j.values.storable.Values) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) LocalDate(java.time.LocalDate) LocalDateTimeValue(org.neo4j.values.storable.LocalDateTimeValue) ValueUtils(org.neo4j.kernel.impl.util.ValueUtils) DoubleValue(org.neo4j.values.storable.DoubleValue) ZonedDateTime(java.time.ZonedDateTime) Expression(org.neo4j.codegen.Expression) DateValue(org.neo4j.values.storable.DateValue) LongValue(org.neo4j.values.storable.LongValue) AnyValue(org.neo4j.values.AnyValue)

Example 44 with Path

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

the class ExecutionResultSerializerTest method shouldProduceResultStreamWithLegacyRestFormat.

@Test
void shouldProduceResultStreamWithLegacyRestFormat() throws Exception {
    // given
    Node[] node = { node(0, properties(property("name", "node0"))), node(1, properties(property("name", "node1"))), node(2, properties(property("name", "node2"))) };
    Relationship[] rel = { relationship(0, node[0], "KNOWS", node[1], property("name", "rel0")), relationship(1, node[2], "LOVES", node[1], property("name", "rel1")) };
    Path path = GraphMock.path(node[0], link(rel[0], node[1]), link(rel[1], node[2]));
    serializer = getSerializerWith(transactionHandle, output, "http://base.uri/");
    var resultRow = Map.of("node", node[0], "rel", rel[0], "path", path, "map", Map.of("n1", node[1], "r1", rel[1]));
    // when
    writeStatementStart(serializer, Collections.singletonList(ResultDataContent.rest), "node", "rel", "path", "map");
    writeRecord(serializer, resultRow, "node", "rel", "path", "map");
    writeStatementEnd(serializer);
    writeTransactionInfo(serializer);
    // then
    String result = output.toString(UTF_8);
    JsonNode json = jsonNode(result);
    Map<String, Integer> columns = new HashMap<>();
    int col = 0;
    JsonNode results = json.get("results").get(0);
    for (JsonNode column : results.get("columns")) {
        columns.put(column.asText(), col++);
    }
    JsonNode row = results.get("data").get(0).get("rest");
    JsonNode jsonNode = row.get(columns.get("node"));
    JsonNode jsonRel = row.get(columns.get("rel"));
    JsonNode jsonPath = row.get(columns.get("path"));
    JsonNode jsonMap = row.get(columns.get("map"));
    assertEquals("http://base.uri/node/0", jsonNode.get("self").asText());
    assertEquals("http://base.uri/relationship/0", jsonRel.get("self").asText());
    assertEquals(2, jsonPath.get("length").asInt());
    assertEquals("http://base.uri/node/0", jsonPath.get("start").asText());
    assertEquals("http://base.uri/node/2", jsonPath.get("end").asText());
    assertEquals("http://base.uri/node/1", jsonMap.get("n1").get("self").asText());
    assertEquals("http://base.uri/relationship/1", jsonMap.get("r1").get("self").asText());
}
Also used : Path(org.neo4j.graphdb.Path) HashMap(java.util.HashMap) JsonHelper.jsonNode(org.neo4j.server.rest.domain.JsonHelper.jsonNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Example 45 with Path

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

the class StandardBranchCollisionDetector method evaluate.

@Override
@SuppressWarnings("unchecked")
public Collection<Path> evaluate(TraversalBranch branch, Direction direction) {
    // [0] for paths from start, [1] for paths from end
    Collection<TraversalBranch>[] pathsHere = paths.get(branch.endNode());
    int index = direction.ordinal();
    if (pathsHere == null) {
        pathsHere = new Collection[] { new ArrayList<>(), new ArrayList<>() };
        paths.put(branch.endNode(), pathsHere);
    }
    pathsHere[index].add(branch);
    // If there are paths from the other side then include all the
    // combined paths
    Collection<TraversalBranch> otherCollections = pathsHere[index == 0 ? 1 : 0];
    if (!otherCollections.isEmpty()) {
        Collection<Path> foundPaths = new ArrayList<>();
        for (TraversalBranch otherBranch : otherCollections) {
            TraversalBranch startPath = index == 0 ? branch : otherBranch;
            TraversalBranch endPath = index == 0 ? otherBranch : branch;
            BidirectionalTraversalBranchPath path = new BidirectionalTraversalBranchPath(startPath, endPath);
            if (isAcceptablePath(path)) {
                if (returnedPaths.add(path) && includePath(path, startPath, endPath)) {
                    foundPaths.add(path);
                }
            }
        }
        if (!foundPaths.isEmpty()) {
            return foundPaths;
        }
    }
    return null;
}
Also used : Path(org.neo4j.graphdb.Path) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Aggregations

Path (org.neo4j.graphdb.Path)170 Node (org.neo4j.graphdb.Node)73 Transaction (org.neo4j.graphdb.Transaction)51 Test (org.junit.jupiter.api.Test)49 Relationship (org.neo4j.graphdb.Relationship)47 Test (org.junit.Test)37 WeightedPath (org.neo4j.graphalgo.WeightedPath)25 Traverser (org.neo4j.graphdb.traversal.Traverser)24 BasicEvaluationContext (org.neo4j.graphalgo.BasicEvaluationContext)21 ArrayList (java.util.ArrayList)19 Map (java.util.Map)18 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)18 HashSet (java.util.HashSet)12 RelationshipType (org.neo4j.graphdb.RelationshipType)10 Entity (org.neo4j.graphdb.Entity)9 Evaluator (org.neo4j.graphdb.traversal.Evaluator)9 HashMap (java.util.HashMap)7 LinkedList (java.util.LinkedList)6 PathFinder (org.neo4j.graphalgo.PathFinder)6 PathExpander (org.neo4j.graphdb.PathExpander)6