Search in sources :

Example 6 with Node

use of org.neo4j.driver.types.Node in project zeppelin by apache.

the class Neo4jCypherInterpreter method renderGraph.

private InterpreterResult renderGraph(Set<Node> nodes, Set<Relationship> relationships) {
    LOGGER.info("Executing renderGraph method");
    List<org.apache.zeppelin.tabledata.Node> nodesList = new ArrayList<>();
    List<org.apache.zeppelin.tabledata.Relationship> relsList = new ArrayList<>();
    for (Relationship rel : relationships) {
        relsList.add(Neo4jConversionUtils.toZeppelinRelationship(rel));
    }
    Map<String, String> labels = getLabels(true);
    for (Node node : nodes) {
        nodesList.add(Neo4jConversionUtils.toZeppelinNode(node, labels));
    }
    return new GraphResult(Code.SUCCESS, new GraphResult.Graph(nodesList, relsList, labels, getTypes(true), true));
}
Also used : GraphResult(org.apache.zeppelin.interpreter.graph.GraphResult) Node(org.neo4j.driver.types.Node) Relationship(org.neo4j.driver.types.Relationship) ArrayList(java.util.ArrayList)

Example 7 with Node

use of org.neo4j.driver.types.Node in project neo4j by neo4j.

the class OutputFormatter method pathAsString.

@Nonnull
default String pathAsString(@Nonnull Path path) {
    List<String> list = new ArrayList<>(path.length());
    Node lastTraversed = path.start();
    if (lastTraversed != null) {
        list.add(nodeAsString(lastTraversed));
        for (Path.Segment segment : path) {
            Relationship relationship = segment.relationship();
            if (relationship.startNodeId() == lastTraversed.id()) {
                list.add("-" + relationshipAsString(relationship) + "->");
            } else {
                list.add("<-" + relationshipAsString(relationship) + "-");
            }
            list.add(nodeAsString(segment.end()));
            lastTraversed = segment.end();
        }
    }
    return String.join("", list);
}
Also used : Path(org.neo4j.driver.types.Path) Node(org.neo4j.driver.types.Node) Relationship(org.neo4j.driver.types.Relationship) ArrayList(java.util.ArrayList) Nonnull(javax.annotation.Nonnull)

Example 8 with Node

use of org.neo4j.driver.types.Node in project neo4j by neo4j.

the class PrettyPrinterTest method prettyPrintSingleNodePath.

@Test
public void prettyPrintSingleNodePath() {
    // given
    Record record = mock(Record.class);
    Value value = mock(Value.class);
    Node start = mock(Node.class);
    when(start.labels()).thenReturn(asList("start"));
    when(start.id()).thenReturn(1L);
    Node end = mock(Node.class);
    when(end.labels()).thenReturn(asList("end"));
    when(end.id()).thenReturn(2L);
    Path path = mock(Path.class);
    when(path.start()).thenReturn(start);
    Relationship relationship = mock(Relationship.class);
    when(relationship.type()).thenReturn("RELATIONSHIP_TYPE");
    when(relationship.startNodeId()).thenReturn(1L);
    Path.Segment segment1 = mock(Path.Segment.class);
    when(segment1.start()).thenReturn(start);
    when(segment1.end()).thenReturn(end);
    when(segment1.relationship()).thenReturn(relationship);
    when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.PATH());
    when(value.asPath()).thenReturn(path);
    when(path.iterator()).thenReturn(asList(segment1).iterator());
    when(record.keys()).thenReturn(asList("path"));
    when(record.values()).thenReturn(asList(value));
    BoltResult result = new ListBoltResult(asList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("path" + NEWLINE + "(:start)-[:RELATIONSHIP_TYPE]->(:end)" + NEWLINE));
}
Also used : Path(org.neo4j.driver.types.Path) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Node(org.neo4j.driver.types.Node) Relationship(org.neo4j.driver.types.Relationship) Value(org.neo4j.driver.Value) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Example 9 with Node

use of org.neo4j.driver.types.Node in project neo4j by neo4j.

the class PrettyPrinterTest method prettyPrintPaths.

@Test
public void prettyPrintPaths() {
    // given
    Record record = mock(Record.class);
    Value value = mock(Value.class);
    Node start = mock(Node.class);
    HashMap<String, Object> startProperties = new HashMap<>();
    startProperties.put("prop1", "prop1_value");
    when(start.labels()).thenReturn(asList("start"));
    when(start.id()).thenReturn(1L);
    Node middle = mock(Node.class);
    when(middle.labels()).thenReturn(asList("middle"));
    when(middle.id()).thenReturn(2L);
    Node end = mock(Node.class);
    HashMap<String, Object> endProperties = new HashMap<>();
    endProperties.put("prop2", "prop2_value");
    when(end.labels()).thenReturn(asList("end"));
    when(end.id()).thenReturn(3L);
    Path path = mock(Path.class);
    when(path.start()).thenReturn(start);
    Relationship relationship = mock(Relationship.class);
    when(relationship.type()).thenReturn("RELATIONSHIP_TYPE");
    when(relationship.startNodeId()).thenReturn(1L).thenReturn(3L);
    Path.Segment segment1 = mock(Path.Segment.class);
    when(segment1.start()).thenReturn(start);
    when(segment1.end()).thenReturn(middle);
    when(segment1.relationship()).thenReturn(relationship);
    Path.Segment segment2 = mock(Path.Segment.class);
    when(segment2.start()).thenReturn(middle);
    when(segment2.end()).thenReturn(end);
    when(segment2.relationship()).thenReturn(relationship);
    when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.PATH());
    when(value.asPath()).thenReturn(path);
    when(path.iterator()).thenReturn(asList(segment1, segment2).iterator());
    when(start.asMap(anyObject())).thenReturn(unmodifiableMap(startProperties));
    when(end.asMap(anyObject())).thenReturn(unmodifiableMap(endProperties));
    when(record.keys()).thenReturn(asList("path"));
    when(record.values()).thenReturn(asList(value));
    BoltResult result = new ListBoltResult(asList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("path" + NEWLINE + "(:start {prop1: prop1_value})-[:RELATIONSHIP_TYPE]->" + "(:middle)<-[:RELATIONSHIP_TYPE]-(:end {prop2: prop2_value})" + NEWLINE));
}
Also used : Path(org.neo4j.driver.types.Path) HashMap(java.util.HashMap) Node(org.neo4j.driver.types.Node) ResultSummary(org.neo4j.driver.summary.ResultSummary) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Relationship(org.neo4j.driver.types.Relationship) Value(org.neo4j.driver.Value) Record(org.neo4j.driver.Record) Matchers.anyObject(org.mockito.Matchers.anyObject) Test(org.junit.Test)

Aggregations

Node (org.neo4j.driver.types.Node)9 Relationship (org.neo4j.driver.types.Relationship)8 Record (org.neo4j.driver.Record)7 Value (org.neo4j.driver.Value)7 Test (org.junit.Test)6 ListBoltResult (org.neo4j.shell.state.ListBoltResult)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 ResultSummary (org.neo4j.driver.summary.ResultSummary)5 Path (org.neo4j.driver.types.Path)5 BoltResult (org.neo4j.shell.state.BoltResult)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Matchers.anyObject (org.mockito.Matchers.anyObject)3 HashSet (java.util.HashSet)1 List (java.util.List)1 Nonnull (javax.annotation.Nonnull)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1 GraphResult (org.apache.zeppelin.interpreter.graph.GraphResult)1 StringContains.containsString (org.hamcrest.core.StringContains.containsString)1 InternalNode (org.neo4j.driver.internal.InternalNode)1