Search in sources :

Example 1 with RelationshipResult

use of apoc.result.RelationshipResult in project neo4j-apoc-procedures by neo4j-contrib.

the class SubgraphTest method testSubgraphAllShouldContainExpectedNodesAndRels.

@Test
public void testSubgraphAllShouldContainExpectedNodesAndRels() throws Throwable {
    String controlQuery = "MATCH path = (:Person {name: 'Keanu Reeves'})-[*0..3]-(subgraphNode) " + "with collect(distinct subgraphNode) as subgraph " + "call apoc.algo.cover([node in subgraph | id(node)]) yield rel " + "return subgraph, collect(rel) as relationships";
    final List<NodeResult> subgraph;
    final List<RelationshipResult> relationships;
    try (Transaction tx = db.beginTx()) {
        Result result = db.execute(controlQuery);
        Map<String, Object> row = result.next();
        subgraph = (List<NodeResult>) row.get("subgraph");
        relationships = (List<RelationshipResult>) row.get("relationships");
    }
    String query = "MATCH (k:Person {name: 'Keanu Reeves'}) " + "CALL apoc.path.subgraphAll(k,{maxLevel:3}) yield nodes, relationships " + "return nodes as subgraphNodes, relationships as subgraphRelationships";
    TestUtil.testCall(db, query, (row) -> {
        List<NodeResult> subgraphNodes = (List<NodeResult>) row.get("subgraphNodes");
        List<RelationshipResult> subgraphRelationships = (List<RelationshipResult>) row.get("subgraphRelationships");
        assertEquals(subgraph.size(), subgraphNodes.size());
        assertTrue(subgraph.containsAll(subgraphNodes));
        assertEquals(relationships.size(), subgraphRelationships.size());
        assertTrue(relationships.containsAll(subgraphRelationships));
    });
}
Also used : RelationshipResult(apoc.result.RelationshipResult) Transaction(org.neo4j.graphdb.Transaction) NodeResult(apoc.result.NodeResult) List(java.util.List) NodeResult(apoc.result.NodeResult) Result(org.neo4j.graphdb.Result) RelationshipResult(apoc.result.RelationshipResult) Test(org.junit.Test)

Example 2 with RelationshipResult

use of apoc.result.RelationshipResult in project neo4j-apoc-procedures by neo4j-contrib.

the class GraphRefactoring method mergeRelationships.

/**
 * Merges the relationships onto the first relationship and delete them.
 * All relationships must have the same starting node and ending node.
 */
@Procedure(mode = Mode.WRITE)
@Description("apoc.refactor.mergeRelationships([rel1,rel2]) merge relationships onto first in list")
public Stream<RelationshipResult> mergeRelationships(@Name("rels") List<Relationship> relationships, @Name(value = "config", defaultValue = "") Map<String, Object> config) {
    if (relationships == null || relationships.isEmpty())
        return Stream.empty();
    RefactorConfig conf = new RefactorConfig(config);
    Iterator<Relationship> it = relationships.iterator();
    Relationship first = it.next();
    while (it.hasNext()) {
        Relationship other = it.next();
        if (first.getStartNode().equals(other.getStartNode()) && first.getEndNode().equals(other.getEndNode()))
            mergeRels(other, first, true, conf);
        else
            throw new RuntimeException("All Relationships must have the same start and end nodes.");
    }
    return Stream.of(new RelationshipResult(first));
}
Also used : RelationshipResult(apoc.result.RelationshipResult) RefactorConfig(apoc.refactor.util.RefactorConfig)

Aggregations

RelationshipResult (apoc.result.RelationshipResult)2 RefactorConfig (apoc.refactor.util.RefactorConfig)1 NodeResult (apoc.result.NodeResult)1 List (java.util.List)1 Test (org.junit.Test)1 Result (org.neo4j.graphdb.Result)1 Transaction (org.neo4j.graphdb.Transaction)1