public double getNodeOutDegree(Node node) { Iterable<Relationship> rel = node.getRelationships(Direction.INCOMING, RelTypes.SIMILAR); return (double) (IteratorUtil.count(rel)); }
public double getNodeInDegree(Node node) { Iterable<Relationship> rel = node.getRelationships(Direction.OUTGOING, RelTypes.SIMILAR); return (double) (IteratorUtil.count(rel)); }
public static int getDocumentSize(GraphDatabaseService db) { int documentSize; String cacheKey = "GLOBAL_DOCUMENT_SIZE"; if(vectorSpaceModelCache.getIfPresent(cacheKey) == null) { documentSize = IteratorUtil.count(GlobalGraphOperations.at(db).getAllNodesWithLabel(DynamicLabel.label("Class"))); vectorSpaceModelCache.put(cacheKey, documentSize); } else { documentSize = (Integer)vectorSpaceModelCache.getIfPresent(cacheKey); } return documentSize; }
@Test public void create_node_using_create_unique_with_java_maps() throws Exception { Map<String, Object> props = new HashMap<String, Object>(); props.put( "name", "Andres" ); props.put( "position", "Developer" ); Map<String, Object> params = new HashMap<String, Object>(); params.put( "props", props ); String query = "START n=node(0) CREATE UNIQUE p = n-[:REL]->({props}) RETURN last(nodes(p)) AS X"; ExecutionResult result = engine.execute( query, params ); assertThat( count( result ), is( 1 ) ); }
@Test public void should_be_able_to_handle_two_params_without_named_nodes() throws Exception { Map<String, Object> props1 = new HashMap<String, Object>(); props1.put( "name", "Andres" ); props1.put( "position", "Developer" ); Map<String, Object> props2 = new HashMap<String, Object>(); props2.put( "name", "Lasse" ); props2.put( "awesome", "true" ); Map<String, Object> params = new HashMap<String, Object>(); params.put( "props1", props1 ); params.put( "props2", props2 ); String query = "START n=node(0) CREATE UNIQUE p = n-[:REL]->({props1})-[:LER]->({props2}) RETURN p"; ExecutionResult result = engine.execute( query, params ); assertThat( count( result ), is( 1 ) ); }
@Test public void create_node_from_map() throws Exception { // START SNIPPET: create_node_from_map Map<String, Object> props = new HashMap<String, Object>(); props.put( "name", "Andres" ); props.put( "position", "Developer" ); Map<String, Object> params = new HashMap<String, Object>(); params.put( "props", props ); String query = "CREATE ({props})"; engine.execute( query, params ); // END SNIPPET: create_node_from_map dumpToFile( "create_node_from_map", query, params ); ExecutionResult result = engine.execute( "match (n) where n.name = 'Andres' and n.position = 'Developer' return n" ); assertThat( count( result ), is( 1 ) ); }
@Test public void create_multiple_nodes_from_map() throws Exception { // START SNIPPET: create_multiple_nodes_from_map Map<String, Object> n1 = new HashMap<String, Object>(); n1.put( "name", "Andres" ); n1.put( "position", "Developer" ); n1.put( "awesome", true ); Map<String, Object> n2 = new HashMap<String, Object>(); n2.put( "name", "Michael" ); n2.put( "position", "Developer" ); n2.put( "children", 3 ); Map<String, Object> params = new HashMap<String, Object>(); List<Map<String, Object>> maps = Arrays.asList( n1, n2 ); params.put( "props", maps ); String query = "CREATE (n:Person {props}) RETURN n"; engine.execute( query, params ); // END SNIPPET: create_multiple_nodes_from_map dumpToFile( "create_multiple_nodes_from_map", query, params ); ExecutionResult result = engine.execute( "match (n:Person) where n.name in ['Andres', 'Michael'] and n.position = 'Developer' return n" ); assertThat( count( result ), is( 2 ) ); result = engine.execute( "match (n:Person) where n.children = 3 return n" ); assertThat( count( result ), is( 1 ) ); result = engine.execute( "match (n:Person) where n.awesome = true return n" ); assertThat( count( result ), is( 1 ) ); }
public static int getDocumentSizeForFeature(GraphDatabaseService db, Long id) { int documentSize; String cacheKey = "DOCUMENT_SIZE_FEATURE_" + id; if(vectorSpaceModelCache.getIfPresent(cacheKey) == null) { Node startNode = db.getNodeById(id); Iterator<Node> classes = db.traversalDescription() .depthFirst() .relationships(withName("HAS_CLASS"), Direction.OUTGOING) .evaluator(Evaluators.fromDepth(1)) .evaluator(Evaluators.toDepth(1)) .traverse(startNode) .nodes().iterator(); documentSize = IteratorUtil.count(classes); vectorSpaceModelCache.put(cacheKey, documentSize); } else { documentSize = (Integer)vectorSpaceModelCache.getIfPresent(cacheKey); } return documentSize; }
@Override public int getNodeCount() { int nodeCount = 0; try (final Transaction tx = beginUnforcedTransaction()) { try { nodeCount = IteratorUtil.count(GlobalGraphOperations.at(neo4jGraph).getAllNodes()); tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get node count", e); } } return nodeCount; }
@Override public double getGraphWeightSum() { int edgeCount = 0; try (final Transaction tx = beginUnforcedTransaction()) { try { edgeCount = IteratorUtil.count(GlobalGraphOperations.at(neo4jGraph).getAllRelationships()); tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get graph weight sum", e); } } return (double) edgeCount; }
@Test public void shouldWorkWithServer() throws Exception { // Given URI serverURI = neo4j.httpURI(); // When I access the server HTTP.Response response = HTTP.GET( serverURI.toString() ); // Then it should reply assertEquals(200, response.status()); // and we have access to underlying GraphDatabaseService try (Transaction tx = neo4j.getGraphDatabaseService().beginTx()) { assertEquals( 2, IteratorUtil.count( neo4j.getGraphDatabaseService().findNodes( DynamicLabel.label( "Admin" ) ) )); tx.success(); } } // END SNIPPET: useJUnitRule
@Test public void testMyExtensionWithFunctionFixture() throws Exception { // Given try ( ServerControls server = TestServerBuilders.newInProcessBuilder() .withExtension( "/myExtension", MyUnmanagedExtension.class ) .withFixture( new Function<GraphDatabaseService, Void>() { @Override public Void apply( GraphDatabaseService graphDatabaseService ) throws RuntimeException { try ( Transaction tx = graphDatabaseService.beginTx() ) { graphDatabaseService.createNode( DynamicLabel.label( "User" ) ); tx.success(); } return null; } } ) .newServer() ) { // When Result result = server.graph().execute( "MATCH (n:User) return n" ); // Then assertEquals( 1, IteratorUtil.count( result ) ); } } // END SNIPPET: testExtension