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());
}
}
Aggregations