Java Code Examples for org.neo4j.graphdb.ResourceIterator#next()

The following examples show how to use org.neo4j.graphdb.ResourceIterator#next() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Neo4JDb.java    From knowledge-extraction with Apache License 2.0 6 votes vote down vote up
private Node getNode(Label label, String key, Object value) {
	Node node = null;
	try (Transaction tx = graphDb.beginTx()) {
		ResourceIterator<Node> nodes = null;
		if (label != null){
			nodes = graphDb.findNodesByLabelAndProperty(
				label, key, value).iterator();
		}
		else {
			String validValue = StringEscapeUtils.escapeJavaScript((String) value);
			ExecutionEngine engine = new ExecutionEngine(graphDb);
			nodes = engine.execute(
					"START n=node(*)"
					+ " WHERE n." + key + "=\"" + validValue + "\""
					+ " RETURN n").columnAs("n");
			
		}
		if (nodes.hasNext()) {
			node = nodes.next();
		}
		nodes.close();
	}
	return node;
}
 
Example 2
Source File: GraphDbBootstrapTest.java    From extended-objects with Apache License 2.0 6 votes vote down vote up
@Test
public void bootstrap() throws URISyntaxException {
    GraphDatabaseService graphDatabaseService = new TestGraphDatabaseFactory().newImpermanentDatabase();
    Properties properties = new Properties();
    properties.put(GraphDatabaseService.class.getName(), graphDatabaseService);
    XOUnit xoUnit = XOUnit.builder().uri(new URI("graphDb:///")).provider(EmbeddedNeo4jXOProvider.class).types(singletonList(A.class))
            .properties(properties).build();
    XOManagerFactory xoManagerFactory = XO.createXOManagerFactory(xoUnit);
    XOManager xoManager = xoManagerFactory.createXOManager();
    xoManager.currentTransaction().begin();
    A a = xoManager.create(A.class);
    a.setName("Test");
    xoManager.currentTransaction().commit();
    xoManager.close();
    xoManagerFactory.close();
    try (Transaction transaction = graphDatabaseService.beginTx()) {
        ResourceIterator<Node> iterator = graphDatabaseService.findNodes(label("A"), "name", "Test");
        assertThat(iterator.hasNext(), equalTo(true));
        Node node = iterator.next();
        assertThat(node.hasLabel(label("A")), equalTo(true));
        assertThat(node.getProperty("name"), equalTo((Object) "Test"));
        transaction.success();
    }
}
 
Example 3
Source File: Neo4jSingleInsertion.java    From graphdb-benchmarks with Apache License 2.0 6 votes vote down vote up
public Node getOrCreate(String nodeId)
{
    Node result = null;

    try(final Transaction tx = ((GraphDatabaseAPI) neo4jGraph).tx().unforced().begin())
    {
        try
        {
            String queryString = "MERGE (n:Node {nodeId: {nodeId}}) RETURN n";
            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put("nodeId", nodeId);
            ResourceIterator<Node> resultIterator = engine.execute(queryString, parameters).columnAs("n");
            result = resultIterator.next();
            tx.success();
        }
        catch (Exception e)
        {
            tx.failure();
            throw new BenchmarkingException("unable to get or create node " + nodeId, e);
        }
    }

    return result;
}
 
Example 4
Source File: CodeIndexes.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
public CodeIndexes(GraphDatabaseService db) {
    try (Transaction tx = db.beginTx()) {
        ResourceIterator<Node> nodes = db.getAllNodes().iterator();
        Set<Node> codeNodes = new HashSet<>();
        while (nodes.hasNext()) {
            Node node = nodes.next();
            if (node.hasLabel(Label.label(JavaCodeExtractor.CLASS)) || node.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)) || node.hasLabel(Label.label(JavaCodeExtractor.METHOD))) {
                codeNodes.add(node);
            }
        }

        for (Node codeNode : codeNodes) {
            String name = "";
            boolean type = true;
            if (codeNode.hasLabel(Label.label(JavaCodeExtractor.CLASS)))
                name = (String) codeNode.getProperty(JavaCodeExtractor.CLASS_FULLNAME);
            if (codeNode.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)))
                name = (String) codeNode.getProperty(JavaCodeExtractor.INTERFACE_FULLNAME);
            if (codeNode.hasLabel(Label.label(JavaCodeExtractor.METHOD))) {
                name = codeNode.getProperty(JavaCodeExtractor.METHOD_BELONGTO) + "." + codeNode.getProperty(JavaCodeExtractor.METHOD_NAME);
                type = false;
            }
            if (name.contains("$"))
                continue;
            if (type) {
                typeMap.put(name, codeNode.getId());
                idToTypeNameMap.put(codeNode.getId(), name);
                String shortName = name;
                int p = shortName.lastIndexOf('.');
                if (p > 0)
                    shortName = shortName.substring(p + 1, shortName.length());
                if (!typeShortNameMap.containsKey(shortName))
                    typeShortNameMap.put(shortName, new HashSet<>());
                typeShortNameMap.get(shortName).add(codeNode.getId());
            } else {
                if (!methodMap.containsKey(name))
                    methodMap.put(name, new HashSet<>());
                methodMap.get(name).add(codeNode.getId());
                idToMethodNameMap.put(codeNode.getId(), name);
                int p1 = name.lastIndexOf('.');
                int p2 = name.lastIndexOf('.', p1 - 1);
                String midName, shortName;
                if (p2 > 0) {
                    midName = name.substring(p2 + 1);
                    shortName = name.substring(p1 + 1);
                } else {
                    midName = name;
                    shortName = name.substring(p1 + 1);
                }
                if (!methodMidNameMap.containsKey(midName))
                    methodMidNameMap.put(midName, new HashSet<>());
                methodMidNameMap.get(midName).add(codeNode.getId());
                if (!methodShortNameMap.containsKey(shortName))
                    methodShortNameMap.put(shortName, new HashSet<>());
                methodShortNameMap.get(shortName).add(codeNode.getId());
            }
        }

        tx.success();
    }
}