Search in sources :

Example 1 with Relationship

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

the class TableOutputFormatterTest method prettyPrintPath.

@Test
public void prettyPrintPath() {
    // given
    List<String> keys = asList("path");
    Node n1 = mock(Node.class);
    when(n1.id()).thenReturn(1L);
    List<String> labels = asList("L1");
    when(n1.labels()).thenReturn(labels);
    when(n1.asMap(anyObject())).thenReturn(Collections.emptyMap());
    Relationship r1 = mock(Relationship.class);
    when(r1.startNodeId()).thenReturn(2L);
    when(r1.type()).thenReturn("R1");
    when(r1.asMap(anyObject())).thenReturn(Collections.emptyMap());
    Node n2 = mock(Node.class);
    when(n2.id()).thenReturn(2L);
    when(n2.labels()).thenReturn(asList("L2"));
    when(n2.asMap(anyObject())).thenReturn(Collections.emptyMap());
    Relationship r2 = mock(Relationship.class);
    when(r2.startNodeId()).thenReturn(2L);
    when(r2.type()).thenReturn("R2");
    when(r2.asMap(anyObject())).thenReturn(Collections.emptyMap());
    Node n3 = mock(Node.class);
    when(n3.id()).thenReturn(3L);
    when(n3.labels()).thenReturn(asList("L3"));
    when(n3.asMap(anyObject())).thenReturn(Collections.emptyMap());
    Path.Segment s1 = mock(Path.Segment.class);
    when(s1.relationship()).thenReturn(r1);
    when(s1.start()).thenReturn(n1);
    when(s1.end()).thenReturn(n2);
    Path.Segment s2 = mock(Path.Segment.class);
    when(s2.relationship()).thenReturn(r2);
    when(s2.start()).thenReturn(n2);
    when(s2.end()).thenReturn(n3);
    List<Path.Segment> segments = asList(s1, s2);
    List<Node> nodes = asList(n1, n2);
    List<Relationship> relationships = asList(r1);
    InternalPath internalPath = new InternalPath(segments, nodes, relationships);
    Value value = new PathValue(internalPath);
    Record record = new InternalRecord(keys, new Value[] { value });
    // when
    String actual = verbosePrinter.format(new ListBoltResult(asList(record), mock(ResultSummary.class)));
    // then
    assertThat(actual, containsString("| (:L1)<-[:R1]-(:L2)-[:R2]->(:L3) |"));
}
Also used : Path(org.neo4j.driver.types.Path) InternalPath(org.neo4j.driver.internal.InternalPath) PathValue(org.neo4j.driver.internal.value.PathValue) Node(org.neo4j.driver.types.Node) InternalNode(org.neo4j.driver.internal.InternalNode) InternalPath(org.neo4j.driver.internal.InternalPath) StringContains.containsString(org.hamcrest.core.StringContains.containsString) InternalRecord(org.neo4j.driver.internal.InternalRecord) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Relationship(org.neo4j.driver.types.Relationship) InternalRelationship(org.neo4j.driver.internal.InternalRelationship) Value(org.neo4j.driver.Value) RelationshipValue(org.neo4j.driver.internal.value.RelationshipValue) NodeValue(org.neo4j.driver.internal.value.NodeValue) DurationValue(org.neo4j.driver.internal.value.DurationValue) PathValue(org.neo4j.driver.internal.value.PathValue) PointValue(org.neo4j.driver.internal.value.PointValue) InternalRecord(org.neo4j.driver.internal.InternalRecord) Record(org.neo4j.driver.Record) Test(org.junit.Test)

Example 2 with Relationship

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

the class PrettyPrinterTest method printRelationshipsAndNodesWithEscapingForSpecialCharacters.

@Test
public void printRelationshipsAndNodesWithEscapingForSpecialCharacters() {
    // given
    Record record = mock(Record.class);
    Value relVal = mock(Value.class);
    Value nodeVal = mock(Value.class);
    Relationship relationship = mock(Relationship.class);
    HashMap<String, Object> relProp = new HashMap<>();
    relProp.put("prop1", "\"prop1, value\"");
    relProp.put("prop2", "prop2_value");
    Node node = mock(Node.class);
    HashMap<String, Object> nodeProp = new HashMap<>();
    nodeProp.put("prop1", "\"prop1:value\"");
    nodeProp.put("1prop2", "\"\"");
    nodeProp.put("ä", "not-escaped");
    when(relVal.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.RELATIONSHIP());
    when(nodeVal.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.NODE());
    when(relVal.asRelationship()).thenReturn(relationship);
    when(relationship.type()).thenReturn("RELATIONSHIP,TYPE");
    when(relationship.asMap(anyObject())).thenReturn(unmodifiableMap(relProp));
    when(nodeVal.asNode()).thenReturn(node);
    when(node.labels()).thenReturn(asList("label `1", "label2"));
    when(node.asMap(anyObject())).thenReturn(unmodifiableMap(nodeProp));
    when(record.keys()).thenReturn(asList("rel", "node"));
    when(record.values()).thenReturn(asList(relVal, nodeVal));
    BoltResult result = new ListBoltResult(asList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("rel, node" + NEWLINE + "[:`RELATIONSHIP,TYPE` {prop2: prop2_value, prop1: \"prop1, value\"}], " + "(:`label ``1`:label2 {prop1: \"prop1:value\", `1prop2`: \"\", ä: not-escaped})" + NEWLINE));
}
Also used : ListBoltResult(org.neo4j.shell.state.ListBoltResult) HashMap(java.util.HashMap) Relationship(org.neo4j.driver.types.Relationship) Node(org.neo4j.driver.types.Node) Value(org.neo4j.driver.Value) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) Matchers.anyObject(org.mockito.Matchers.anyObject) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Example 3 with Relationship

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

the class PrettyPrinterTest method prettyPrintThreeSegmentPath.

@Test
public void prettyPrintThreeSegmentPath() {
    // 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 second = mock(Node.class);
    when(second.labels()).thenReturn(asList("second"));
    when(second.id()).thenReturn(2L);
    Node third = mock(Node.class);
    when(third.labels()).thenReturn(asList("third"));
    when(third.id()).thenReturn(3L);
    Node end = mock(Node.class);
    when(end.labels()).thenReturn(asList("end"));
    when(end.id()).thenReturn(4L);
    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).thenReturn(3L);
    Path.Segment segment1 = mock(Path.Segment.class);
    when(segment1.start()).thenReturn(start);
    when(segment1.end()).thenReturn(second);
    when(segment1.relationship()).thenReturn(relationship);
    Path.Segment segment2 = mock(Path.Segment.class);
    when(segment2.start()).thenReturn(second);
    when(segment2.end()).thenReturn(third);
    when(segment2.relationship()).thenReturn(relationship);
    Path.Segment segment3 = mock(Path.Segment.class);
    when(segment3.start()).thenReturn(third);
    when(segment3.end()).thenReturn(end);
    when(segment3.relationship()).thenReturn(relationship);
    when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.PATH());
    when(value.asPath()).thenReturn(path);
    when(path.iterator()).thenReturn(asList(segment1, segment2, segment3).iterator());
    when(record.keys()).thenReturn(asList("path"));
    when(record.values()).thenReturn(asList(value));
    BoltResult result = new ListBoltResult(singletonList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("path" + NEWLINE + "(:start)-[:RELATIONSHIP_TYPE]->" + "(:second)<-[:RELATIONSHIP_TYPE]-(:third)-[: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 4 with Relationship

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

the class Neo4jCypherInterpreter method runQuery.

private InterpreterResult runQuery(String cypherQuery, InterpreterContext interpreterContext) {
    if (StringUtils.isBlank(cypherQuery)) {
        return new InterpreterResult(Code.SUCCESS);
    }
    try {
        Iterator<Record> result = this.neo4jConnectionManager.execute(cypherQuery, interpreterContext).iterator();
        Set<Node> nodes = new HashSet<>();
        Set<Relationship> relationships = new HashSet<>();
        List<String> columns = new ArrayList<>();
        List<List<String>> lines = new ArrayList<List<String>>();
        while (result.hasNext()) {
            Record record = result.next();
            List<Pair<String, Value>> fields = record.fields();
            List<String> line = new ArrayList<>();
            for (Pair<String, Value> field : fields) {
                if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.NODE())) {
                    nodes.add(field.value().asNode());
                } else if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.RELATIONSHIP())) {
                    relationships.add(field.value().asRelationship());
                } else if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.PATH())) {
                    nodes.addAll(Iterables.asList(field.value().asPath().nodes()));
                    relationships.addAll(Iterables.asList(field.value().asPath().relationships()));
                } else {
                    setTabularResult(field.key(), field.value(), columns, line, InternalTypeSystem.TYPE_SYSTEM);
                }
            }
            if (!line.isEmpty()) {
                lines.add(line);
            }
        }
        if (!nodes.isEmpty()) {
            return renderGraph(nodes, relationships);
        } else {
            return renderTable(columns, lines);
        }
    } catch (Exception e) {
        LOGGER.error("Exception while interpreting cypher query", e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    }
}
Also used : Node(org.neo4j.driver.types.Node) ArrayList(java.util.ArrayList) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Relationship(org.neo4j.driver.types.Relationship) Value(org.neo4j.driver.Value) Record(org.neo4j.driver.Record) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Pair(org.neo4j.driver.util.Pair)

Example 5 with Relationship

use of org.neo4j.driver.types.Relationship 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)

Aggregations

Relationship (org.neo4j.driver.types.Relationship)9 Node (org.neo4j.driver.types.Node)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