Search in sources :

Example 1 with NodeResult

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

the class GraphRefactoring method mergeNodes.

/**
 * Merges the nodes onto the first node.
 * The other nodes are deleted and their relationships moved onto that first node.
 */
@Procedure(mode = Mode.WRITE)
@Description("apoc.refactor.mergeNodes([node1,node2],[{properties:'override' or 'discard' or 'combine'}]) merge nodes onto first in list")
public Stream<NodeResult> mergeNodes(@Name("nodes") List<Node> nodes, @Name(value = "config", defaultValue = "") Map<String, Object> config) {
    if (nodes == null || nodes.isEmpty())
        return Stream.empty();
    // grab write locks upfront consistently ordered
    try (Transaction tx = db.beginTx()) {
        nodes.stream().distinct().sorted(Comparator.comparingLong(Node::getId)).forEach(tx::acquireWriteLock);
        tx.success();
    }
    RefactorConfig conf = new RefactorConfig(config);
    final Node first = nodes.get(0);
    nodes.stream().skip(1).distinct().forEach(node -> mergeNodes(node, first, true, conf));
    return Stream.of(new NodeResult(first));
}
Also used : NodeResult(apoc.result.NodeResult) RefactorConfig(apoc.refactor.util.RefactorConfig)

Example 2 with NodeResult

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

the class SubgraphTest method testOptionalSubgraphWithResultsShouldYieldExpectedResults.

@Test
public void testOptionalSubgraphWithResultsShouldYieldExpectedResults() throws Throwable {
    String controlQuery = "MATCH (m:Movie {title: 'The Matrix'})-[*0..3]-(subgraphNode) return collect(distinct subgraphNode) as subgraph";
    List<NodeResult> subgraph;
    try (Transaction tx = db.beginTx()) {
        Result result = db.execute(controlQuery);
        subgraph = (List<NodeResult>) result.next().get("subgraph");
    }
    String query = "MATCH (m:Movie {title: 'The Matrix'}) CALL apoc.path.subgraphNodes(m,{maxLevel:3, optional:true}) yield node return COLLECT(node) as subgraphNodes";
    TestUtil.testCall(db, query, (row) -> {
        List<NodeResult> subgraphNodes = (List<NodeResult>) row.get("subgraphNodes");
        assertEquals(subgraph.size(), subgraphNodes.size());
        assertTrue(subgraph.containsAll(subgraphNodes));
    });
}
Also used : 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 3 with NodeResult

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

the class SubgraphTest method testSubgraphWithRelationshipFilterShouldContainExpectedNodes.

@Test
public void testSubgraphWithRelationshipFilterShouldContainExpectedNodes() throws Throwable {
    String controlQuery = "MATCH path = (:Person {name: 'Keanu Reeves'})-[:ACTED_IN*0..3]-(subgraphNode) return collect(distinct subgraphNode) as subgraph";
    List<NodeResult> subgraph;
    try (Transaction tx = db.beginTx()) {
        Result result = db.execute(controlQuery);
        subgraph = (List<NodeResult>) result.next().get("subgraph");
    }
    String query = "MATCH (k:Person {name: 'Keanu Reeves'}) CALL apoc.path.subgraphNodes(k,{maxLevel:3, relationshipFilter:'ACTED_IN'}) yield node return COLLECT(node) as subgraphNodes";
    TestUtil.testCall(db, query, (row) -> {
        List<NodeResult> subgraphNodes = (List<NodeResult>) row.get("subgraphNodes");
        assertEquals(subgraph.size(), subgraphNodes.size());
        assertTrue(subgraph.containsAll(subgraphNodes));
    });
}
Also used : 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 4 with NodeResult

use of apoc.result.NodeResult 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 5 with NodeResult

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

the class SubgraphTest method testSpanningTreeShouldHaveOnlyOnePathToEachNode.

@Test
public void testSpanningTreeShouldHaveOnlyOnePathToEachNode() throws Throwable {
    String controlQuery = "MATCH (m:Movie {title: 'The Matrix'})-[*0..4]-(subgraphNode) return collect(distinct subgraphNode) as subgraph";
    List<NodeResult> subgraph;
    try (Transaction tx = db.beginTx()) {
        Result result = db.execute(controlQuery);
        subgraph = (List<NodeResult>) result.next().get("subgraph");
    }
    String query = "MATCH (m:Movie {title: 'The Matrix'}) " + "CALL apoc.path.spanningTree(m,{maxLevel:4}) yield path " + "with collect(path) as paths " + "with paths, size(paths) as pathCount " + "unwind paths as path " + "with pathCount, collect(distinct last(nodes(path))) as subgraphNodes " + "return pathCount, subgraphNodes";
    TestUtil.testCall(db, query, (row) -> {
        List<NodeResult> subgraphNodes = (List<NodeResult>) row.get("subgraphNodes");
        long pathCount = (Long) row.get("pathCount");
        assertEquals(subgraph.size(), subgraphNodes.size());
        assertTrue(subgraph.containsAll(subgraphNodes));
        // assert every node has a single path to that node - no cycles
        assertEquals(pathCount, subgraphNodes.size());
    });
}
Also used : 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)

Aggregations

NodeResult (apoc.result.NodeResult)9 RelationshipResult (apoc.result.RelationshipResult)6 List (java.util.List)6 Test (org.junit.Test)6 Result (org.neo4j.graphdb.Result)6 Transaction (org.neo4j.graphdb.Transaction)6 RefactorConfig (apoc.refactor.util.RefactorConfig)1 QName (javax.xml.namespace.QName)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)1 SortedIndexReader (org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader)1 Description (org.neo4j.procedure.Description)1 Procedure (org.neo4j.procedure.Procedure)1