Search in sources :

Example 1 with SubgraphMapper

use of org.neo4j.visualization.SubgraphMapper in project neo4j-documentation by neo4j.

the class TestGraphvizSubgraphOutput method testSimpleGraph.

@Test
public void testSimpleGraph() throws Exception {
    Path folder = Path.of("target/example-db" + System.nanoTime());
    DatabaseManagementService managementService = new DatabaseManagementServiceBuilder(folder).build();
    try {
        GraphDatabaseService neo = managementService.database(DEFAULT_DATABASE_NAME);
        try (Transaction tx = neo.beginTx()) {
            final Node emil = tx.createNode();
            emil.setProperty("name", "Emil Eifrém");
            emil.setProperty("country_of_residence", "USA");
            final Node tobias = tx.createNode();
            tobias.setProperty("name", "Tobias Ivarsson");
            tobias.setProperty("country_of_residence", "Sweden");
            final Node johan = tx.createNode();
            johan.setProperty("name", "Johan Svensson");
            johan.setProperty("country_of_residence", "Sweden");
            final Relationship emilKNOWStobias = emil.createRelationshipTo(tobias, type.KNOWS);
            final Relationship johanKNOWSemil = johan.createRelationshipTo(emil, type.KNOWS);
            final Relationship tobiasKNOWSjohan = tobias.createRelationshipTo(johan, type.KNOWS);
            final Relationship tobiasWORKS_FORemil = tobias.createRelationshipTo(emil, type.WORKS_FOR);
            OutputStream out = new ByteArrayOutputStream();
            SubgraphMapper subgraphMapper = node -> {
                if (node.hasProperty("country_of_residence")) {
                    return (String) node.getProperty("country_of_residence");
                }
                return null;
            };
            GraphvizWriter writer = new GraphvizWriter();
            SubgraphMapper.SubgraphMappingWalker walker = new SubgraphMapper.SubgraphMappingWalker(subgraphMapper) {

                @Override
                protected Iterable<Node> nodes() {
                    return asList(emil, tobias, johan);
                }

                @Override
                protected Iterable<Relationship> relationships() {
                    return asList(emilKNOWStobias, johanKNOWSemil, tobiasKNOWSjohan, tobiasWORKS_FORemil);
                }
            };
            writer.emit(out, walker);
            tx.commit();
        }
    } finally {
        managementService.shutdown();
        deleteDirectory(folder.toFile());
    }
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) Test(org.junit.Test) Node(org.neo4j.graphdb.Node) FileUtils.deleteDirectory(org.apache.commons.io.FileUtils.deleteDirectory) DEFAULT_DATABASE_NAME(org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME) Relationship(org.neo4j.graphdb.Relationship) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Arrays.asList(java.util.Arrays.asList) RelationshipType(org.neo4j.graphdb.RelationshipType) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Transaction(org.neo4j.graphdb.Transaction) Path(java.nio.file.Path) SubgraphMapper(org.neo4j.visualization.SubgraphMapper) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) SubgraphMapper(org.neo4j.visualization.SubgraphMapper) Node(org.neo4j.graphdb.Node) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 Arrays.asList (java.util.Arrays.asList)1 FileUtils.deleteDirectory (org.apache.commons.io.FileUtils.deleteDirectory)1 Test (org.junit.Test)1 DEFAULT_DATABASE_NAME (org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME)1 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)1 DatabaseManagementServiceBuilder (org.neo4j.dbms.api.DatabaseManagementServiceBuilder)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 Node (org.neo4j.graphdb.Node)1 Relationship (org.neo4j.graphdb.Relationship)1 RelationshipType (org.neo4j.graphdb.RelationshipType)1 Transaction (org.neo4j.graphdb.Transaction)1 SubgraphMapper (org.neo4j.visualization.SubgraphMapper)1