private Object toRelationship(Object value, boolean virtual, Map<Long, Object> nodesCache) { Value internalValue = ((InternalEntity) value).asValue(); Relationship relationship = internalValue.asRelationship(); if (virtual) { VirtualNode start = (VirtualNode) nodesCache.getOrDefault(relationship.startNodeId(), new VirtualNode(relationship.startNodeId(), db)); VirtualNode end = (VirtualNode) nodesCache.getOrDefault(relationship.endNodeId(), new VirtualNode(relationship.endNodeId(), db)); VirtualRelationship virtualRelationship = new VirtualRelationship(relationship.id(), start, end, RelationshipType.withName(relationship.type()), relationship.asMap()); return virtualRelationship; } else return Util.map("entityType", internalValue.type().name(), "type", relationship.type(), "id", relationship.id(), "start", relationship.startNodeId(), "end", relationship.endNodeId(), "properties", relationship.asMap()); }
private Relationship get( Value value, Relationship defaultValue ) { if( value.equals( Values.NULL ) ) { return defaultValue; } else { return value.asRelationship(); } }
private Relationship asRelationship(Record record, String alias) { Value value = record.get( alias ); if ( value.isNull() ) { return null; } return value.asRelationship(); }
private Relationship relationship(StatementResult result) { if ( result.hasNext() ) { return result.next().get( 0 ).asRelationship(); } return null; }
public static RelationInfo fromRecordValue(Value val) { if (val instanceof ListValue) return RelationInfo.fromRecordValue(((ListValue)val).get(0)); RelationInfo ret = null; if (val != null) { String typName = val.type().name(); // must be: RELATIONSHIP if ("RELATIONSHIP".equals(typName)) { ret = new RelationInfo(); ret.startNodeId = val.asRelationship().startNodeId(); ret.endNodeId = val.asRelationship().endNodeId(); } } return ret; }
public static RelationInfo fromRecordValue(Value val) { if (val instanceof ListValue) return RelationInfo.fromRecordValue(((ListValue)val).get(0)); RelationInfo ret = null; if (val != null) { String typName = val.type().name(); // must be: RELATIONSHIP if ("RELATIONSHIP".equals(typName)) { ret = new RelationInfo(); ret.startNodeId = val.asRelationship().startNodeId(); ret.endNodeId = val.asRelationship().endNodeId(); } } return ret; }
public static ElementInfo fromRecordValue(Value val) { if (val instanceof ListValue) return ElementInfo.fromRecordValue(((ListValue)val).get(0)); ElementInfo ret = null; if (val != null) { String typName = val.type().name(); // NODE, RELATIONSHIP, NULL if ("NODE".equals(typName)) { ret = new ElementInfo(); ret.isNull = false; ret.id = val.asNode().id(); ret.type = ElemType.NODE; } else if ("RELATIONSHIP".equals(typName)) { ret = new ElementInfo(); ret.isNull = false; ret.id = val.asRelationship().id(); ret.type = ElemType.RELATION; } else if ("NULL".equals(typName)) ret = ElementInfo.nullElement(); } return ret; }
@Test public void prettyPrintRelationships() throws Exception { // given BoltResult result = mock(BoltResult.class); Record record = mock(Record.class); Value value = mock(Value.class); Relationship relationship = mock(Relationship.class); HashMap<String, Object> propertiesAsMap = new HashMap<>(); propertiesAsMap.put("prop1", "prop1_value"); propertiesAsMap.put("prop2", "prop2_value"); when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.RELATIONSHIP()); when(value.asRelationship()).thenReturn(relationship); when(relationship.type()).thenReturn("RELATIONSHIP_TYPE"); when(relationship.asMap(anyObject())).thenReturn(unmodifiableMap(propertiesAsMap)); when(record.keys()).thenReturn(asList("rel")); when(record.values()).thenReturn(asList(value)); when(result.getRecords()).thenReturn(asList(record)); when(result.getSummary()).thenReturn(mock(ResultSummary.class)); // when String actual = plainPrinter.format(result); // then assertThat(actual, is("rel\n[:RELATIONSHIP_TYPE {prop2: prop2_value, prop1: prop1_value}]")); }
public static ElementInfo fromRecordValue(Value val) { if (val instanceof ListValue) return ElementInfo.fromRecordValue(((ListValue)val).get(0)); ElementInfo ret = null; if (val != null) { String typName = val.type().name(); // NODE, RELATIONSHIP, NULL if ("NODE".equals(typName)) { ret = new ElementInfo(); ret.isNull = false; ret.id = val.asNode().id(); ret.type = ElemType.NODE; } else if ("RELATIONSHIP".equals(typName)) { ret = new ElementInfo(); ret.isNull = false; ret.id = val.asRelationship().id(); ret.type = ElemType.RELATION; } else if ("NULL".equals(typName)) ret = ElementInfo.nullElement(); } return ret; }
private Entity getPropertiesObject(long id, int rowIndex, ElemType typ) { Record rec = this.records.get(rowIndex); List<Pair<String, Value>> flds = rec.fields(); for (Pair<String, Value> pair : flds) { if (typ == ElemType.NODE && pair.value() instanceof NodeValue) { Node nd = pair.value().asNode(); if (nd.id() == id) return nd; } else if (typ == ElemType.RELATION && pair.value() instanceof RelationshipValue) { Relationship rel = pair.value().asRelationship(); if (rel.id() == id) return rel; } } // element with id may not have been loaded return this.reloaded.getEntity(id, typ); }
private Entity getPropertiesObject(long id, int rowIndex, ElemType typ) { Record rec = this.records.get(rowIndex); List<Pair<String, Value>> flds = rec.fields(); for (Pair<String, Value> pair : flds) { if (typ == ElemType.NODE && pair.value() instanceof NodeValue) { Node nd = pair.value().asNode(); if (nd.id() == id) return nd; } else if (typ == ElemType.RELATION && pair.value() instanceof RelationshipValue) { Relationship rel = pair.value().asRelationship(); if (rel.id() == id) return rel; } } // element with id may not have been loaded return this.reloaded.getEntity(id, typ); }
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));
Relationship versionRel = field.value().asRelationship(); if (versionRelationshipId != versionRel.id()) { versionRelationshipId = versionRel.id(); List<Relationship> embeds = field.value().asList(v -> v.asRelationship()); StringBuilder sb = new StringBuilder("$"); for (Relationship embedRel : embeds) {
@Test public void returnNodeAndRelationship() { Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort()); try (Session session = driver.session()) { StatementResult result = session.run("CREATE (n1:Person {name: 'Marko'})-[r:knows {since:1999}]->(n2:Person)" + "RETURN n1,r,n2", parameters("message", "Hello")); Record record = result.single(); Node n1 = record.get("n1").asNode(); Relationship r = record.get("r").asRelationship(); Node n2 = record.get("n2").asNode(); assertThat(n1.hasLabel("Person")).isTrue(); assertThat(n1.get("name").asString()).isEqualTo("Marko"); assertThat(r.hasType("knows")).isTrue(); assertThat(r.startNodeId()).isEqualTo(n1.id()); assertThat(r.endNodeId()).isEqualTo(n2.id()); assertThat(r.get("since").asLong()).isEqualTo(1999L); assertThat(n2.hasLabel("Person")).isTrue(); } }
return nodeAsString(value.asNode()); case RELATIONSHIP: return relationshipAsString(value.asRelationship()); case PATH: return pathAsString(value.asPath());
@Test public void returnPath() { Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort()); try (Session session = driver.session()) { StatementResult setup = session.run("CREATE (n1:Person {name: 'Anders'})-[r:knows]->(n2:Person)" + "RETURN n1,r,n2"); Record createdNodes = setup.single(); Node n1 = createdNodes.get("n1").asNode(); Node n2 = createdNodes.get("n2").asNode(); Relationship r = createdNodes.get("r").asRelationship(); StatementResult result = session.run("MATCH p =(b1 { name: 'Anders' })-->()" + "RETURN p"); Path path = result.single().get("p").asPath(); assertThat(path.contains(n1)).isTrue(); assertThat(path.contains(n2)).isTrue(); assertThat(path.contains(r)).isTrue(); assertThat(path.relationships()).hasSize(1); assertThat(path.nodes()).hasSize(2); } }
.map(r -> r.get("r").asRelationship());
@Test public void shouldCreateTheRelationshipAssociatedToANewStateHavingRequestedDate() { try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given Node entityA = initEntity(session); Node entityB = initEntity(session); String testType = "testType"; Long date = 593920000000L; // When String query = "MATCH (a:Entity), (b:Entity) WHERE id(a) = %d AND id(b) = %d WITH a, b CALL graph.versioner.relationship.create(a, b, '%s', {}, localdatetime('1988-10-27T02:46:40')) YIELD relationship RETURN relationship"; session.run(String.format(query, entityA.id(), entityB.id(), testType)); // Then String querySourceCurrent = "MATCH (e:Entity)-[r:CURRENT]->(:State)-[:%s]->(:R) WHERE id(e) = %d RETURN r"; Relationship currentRelationship = session.run(String.format(querySourceCurrent, testType, entityA.id())).single().get("r").asRelationship(); assertThat(currentRelationship) .matches(rel -> rel.containsKey("date") && rel.get("date").asLocalDateTime().equals(convertEpochToLocalDateTime(date))); } }
@Test public void shouldCreateTheRelationshipAndTheNewCurrentStateBetweenEntities() throws Throwable { try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given Node entityA = initEntity(session); Node entityB = initEntity(session); String testType = "testType"; // When String query = "MATCH (a:Entity), (b:Entity) WHERE id(a) = %d AND id(b) = %d WITH a, b CALL graph.versioner.relationship.create(a, b, '%s') YIELD relationship RETURN relationship"; Relationship relationship = session.run(String.format(query, entityA.id(), entityB.id(), testType)).single().get("relationship").asRelationship(); // Then String querySourceCurrent = "MATCH (e:Entity)-[:CURRENT]-(s:State) WHERE id(e) = %d RETURN s"; String queryDestinationR = "MATCH (e:Entity)<-[:FOR]-(r:R) WHERE id(e) = %d RETURN r"; Node sourceCurrent = session.run(String.format(querySourceCurrent, entityA.id())).single().get("s").asNode(); Node destinationR = session.run(String.format(queryDestinationR, entityB.id())).single().get("r").asNode(); Relationship expected = new InternalRelationship(0L, sourceCurrent.id(), destinationR.id(), testType); assertThat(relationship).isEqualToIgnoringGivenFields(expected, "id"); } }
@Test public void shouldCreateTheRelationshipInANewCurrentStatePreservingTheOldOne() { try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given Node entityA = initEntity(session); Node entityB = initEntity(session); String testType = "testType"; Long entityACurrentId = session.run(String.format("MATCH (e:Entity)-[:CURRENT]->(s:State) WHERE id(e) = %d RETURN s", entityA.id())).single().get("s").asNode().id(); // When String query = "MATCH (a:Entity), (b:Entity) WHERE id(a) = %d AND id(b) = %d WITH a, b CALL graph.versioner.relationship.create(a, b, '%s') YIELD relationship RETURN relationship"; Relationship relationship = session.run(String.format(query, entityA.id(), entityB.id(), testType)).single().get("relationship").asRelationship(); // Then String querySourceStates = "MATCH (:R)<-[r:%s]-(s1:State)-[:PREVIOUS]->(s2:State) WHERE id(r) = %d RETURN s1, s2"; StatementResult result = session.run(String.format(querySourceStates, testType, relationship.id())); assertThat(result) .hasSize(1) .allMatch(r -> r.get("s1").asNode().id() != r.get("s2").asNode().id() && r.get("s2").asNode().id() == entityACurrentId); } }