Java Code Examples for org.neo4j.driver.v1.StatementResult#hasNext()

The following examples show how to use org.neo4j.driver.v1.StatementResult#hasNext() . 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: JQA2JSON.java    From Getaviz with Apache License 2.0 6 votes vote down vote up
private String toMetaDataNamespace(Node namespace) {
	StatementResult parentHash = connector
			.executeRead("MATCH (parent:Package)-[:CONTAINS]->(namespace) WHERE ID(namespace) = " + namespace.id()
					+ " RETURN parent.hash");
	String belongsTo = "root";
	if (parentHash.hasNext()) {
		belongsTo = parentHash.single().get("parent.hash").asString();
	}
	return "\"id\":            \"" + namespace.get("hash").asString() + "\"," +
			"\n" +
			"\"qualifiedName\": \"" + namespace.get("fqn").asString() + "\"," +
			"\n" +
			"\"name\":          \"" + namespace.get("name").asString() + "\"," +
			"\n" +
			"\"type\":          \"FAMIX.Namespace\"," +
			"\n" +
			"\"belongsTo\":     \"" + belongsTo + "\"" +
			"\n";
}
 
Example 2
Source File: C2JSON.java    From Getaviz with Apache License 2.0 6 votes vote down vote up
private String getFunctionSignature(Node function){
	String signature;
	String returnType = "";
	StatementResult returnTypeNodes =  connector.executeRead(
			 "MATCH (function:Function)-[:RETURNS]->(node) WHERE ID(function) = " + function.id() + " RETURN node");
	if(returnTypeNodes.hasNext()) {
		returnType += returnTypeNodes.single().get("node").asNode().get("name").asString();
	}
	if(!returnType.endsWith("*")){
		returnType += " ";
	}
	String functionName = function.get("name").asString();
	List<Node> parameterList = new ArrayList<>();
	 StatementResult parameters =  connector.executeRead(
			 "MATCH (function:Function)-[:HAS]->(node) WHERE ID(function) = " + function.id() + " RETURN node ORDER BY node.index");
	 while (parameters.hasNext()) {
	 	parameterList.add(parameters.next().get("node").asNode());
	 }
	//var parameterList = function.getRelationships(Direction.OUTGOING, Rels.HAS).map[endNode]
	//sort parameters according to their index
	//parameterList = parameterList.sortBy[p|p.getProperty("index", 0) as Integer]
	String parameter = getFunctionParameters(parameterList);
	signature = returnType + functionName + "(" + parameter + ")";
	return signature;
}
 
Example 3
Source File: QueryRunner.java    From neoprofiler with Apache License 2.0 6 votes vote down vote up
public Map<String,List<Object>> runQueryComplexResult(NeoProfiler parent, String query, String...columns) {
	HashMap<String,List<Object>> all = new HashMap<String,List<Object>>();
	
	List<Object> retvals = new ArrayList<Object>();
	System.out.println(query);
	Session s = parent.getDriver().session();
	try ( Transaction tx = s.beginTransaction()) {
		// log.info(query);
		StatementResult result = tx.run(query);
		
		while(result.hasNext()) {
			Map<String,Object> row = result.next().asMap();
			
			for(String col : columns) { 
				if(!all.containsKey(col)) all.put(col, new ArrayList<Object>());
				all.get(col).add(row.get(col));
			}
		}	
		
		tx.close();
	} finally {
		s.close();
	}
	
	return all;
}
 
Example 4
Source File: JQA2JSON.java    From Getaviz with Apache License 2.0 6 votes vote down vote up
private String toMetaDataAnnotation(Node annotation) {
	String belongsTo = "";
	StatementResult parent = connector.executeRead("MATCH (parent:Package)-[:CONTAINS|DECLARES]->(annotation) WHERE ID(annotation) = " + annotation.id() 
		+ " RETURN parent.hash");
	if(parent.hasNext()) {
		belongsTo = parent.single().get("parent.hash").asString();
	}
	return "\"id\":            \"" + annotation.get("hash").asString() + "\"," +
			"\n" +
			"\"qualifiedName\": \"" + annotation.get("fqn").asString() + "\"," +
			"\n" +
			"\"name\":          \"" + annotation.get("name").asString() + "\"," +
			"\n" +
			"\"type\":          \"FAMIX.AnnotationType\"," +
			"\n" +
			"\"modifiers\":     \"" + getModifiers(annotation) + "\"," +
			"\n" +
			"\"subClassOf\":    \"\"," +
			"\n" +
			"\"superClassOf\":  \"\"," +
			"\n" +
			"\"belongsTo\":     \"" + belongsTo + "\"" +
			"\n";
}
 
Example 5
Source File: City2City.java    From Getaviz with Apache License 2.0 6 votes vote down vote up
private void calculateFloors(Node building) {
	Node position = connector.getPosition(building.id());
	double bHeight = building.get("height").asDouble();
	double bWidth = building.get("width").asDouble();
	double bLength = building.get("length").asDouble();
	double bPosX = position.get("x").asDouble();
	double bPosY = position.get("y").asDouble();
	double bPosZ = position.get("z").asDouble();
	StatementResult floors = connector.executeRead("MATCH (n)-[:CONTAINS]->(f:Floor) WHERE ID(n) = " + building.id() +
		" RETURN f");
	int floorNumberValue = connector.executeRead("MATCH (n)-[:CONTAINS]->(f:Floor) WHERE ID(n) = " + building.id() +
		" RETURN COUNT(f) as floorNumber").single().get("floorNumber").asInt();

	int floorCounter = 0;
	while (floors.hasNext()) {
		Record record = floors.next();
		long floor = record.get("f").asNode().id();
		floorCounter++;
		String statement = cypherSetBuildingSegmentAttributes(floor, bWidth * 1.1, bLength * 1.1,
			bHeight / (floorNumberValue + 2 ) * 0.80, floorColor);
		statement +=
			String.format("CREATE (n)-[:HAS]->(p:City:Position {x: %f, y: %f, z: %f})", bPosX,
				(bPosY - ( bHeight / 2) ) + bHeight / ( floorNumberValue + 2 ) * floorCounter, bPosZ);
		connector.executeWrite(statement);
	}
}
 
Example 6
Source File: C2JSON.java    From Getaviz with Apache License 2.0 6 votes vote down vote up
private String toMetaDataFunction(Node function) {
	String belongsTo = "";
	String dependsOn = "";
	StatementResult parent =  connector.executeRead(
			"MATCH (parent)-[:DECLARES]->(function:Function) WHERE ID(function) = " + function.id() + " RETURN parent.hash");
	if(parent.hasNext()) {
		belongsTo = parent.single().get("parent.hash").asString();
	}
	StatementResult dependentList = connector.executeRead(
			"MATCH (function:Function)-[:DEPENDS_ON]->(el) WHERE ID(function) = " + function.id() + " RETURN el");
	if(dependentList.hasNext()) {
		dependsOn = dependentList.single().get("el.hash").asString();
	}

	return formatLine("id", function.get("hash").asString()) +
			formatLine("qualifiedName", escapeHtml4(function.get("fqn").asString())) +
			formatLine("name", function.get("name").asString()) +
			formatLine("type", "FAMIX.Function") +
			formatLine("signature", getFunctionSignature(function)) +
			formatLine("calls", getCalls(function)) +
			formatLine("calledBy", getCalledBy(function)) +
			formatLine("accesses", getAccesses(function)) +
			formatLine("belongsTo", belongsTo) +
			formatLine("dependsOn", dependsOn) +
			formatEndline("filename", function.get("fileName").asString());
}
 
Example 7
Source File: C2JSON.java    From Getaviz with Apache License 2.0 6 votes vote down vote up
private String toMetaDataUnion(Node union) {
	String belongsTo = "";
	String dependsOn = "";
	StatementResult parent =  connector.executeRead(
			"MATCH (parent)-[:DECLARES]->(union:Union) WHERE ID(union) = " + union.id() + " RETURN parent.hash");
	if(parent.hasNext()) {
		belongsTo = parent.single().get("parent.hash").asString();
	}
	StatementResult dependent =  connector.executeRead(
			"MATCH (union:Union)-[:DEPENDS_ON]->(type) WHERE ID(union) = " + union.id() + " RETURN type.hash");
	if(dependent.hasNext()){
		dependsOn = dependent.single().get("type.hash").asString();
	}

	return formatLine("id", union.get("hash").asString()) +
			formatLine("qualifiedName", union.get("fqn").asString()) +
			formatLine("name", union.get("name").asString()) +
			formatLine("type", "Union") +
			formatLine("belongsTo", belongsTo) +
			formatLine(dependsOn, dependsOn) +
			formatLine("filename", union.get("fileName").asString());
}
 
Example 8
Source File: C2JSON.java    From Getaviz with Apache License 2.0 6 votes vote down vote up
private String toMetaDataNegation(Node negation) {
	String negated = "";
	try {
		StatementResult negations =  connector.executeRead(
				"MATCH (negation:Negation)-[:NEGATES]->(condition) WHERE ID(negation) = " + negation.id() + " RETURN condition.hash");
		if(negations.hasNext()) {
			negated = negations.single().get("condition.hash").asString();
		}
	} catch (Exception e) {
		negated = "";
	}

	return formatLine("id", negation.get("hash").asString()) +
			formatLine("type", "Negation") +
			formatEndline("negated", negated);
}
 
Example 9
Source File: C2JSON.java    From Getaviz with Apache License 2.0 6 votes vote down vote up
private String toMetaDataStruct(Node struct) {
	String belongsTo = "";
	String dependsOn = "";
	StatementResult parent =  connector.executeRead(
			"MATCH (parent)-[:DECLARES]->(struct:Struct) WHERE ID(struct) = " + struct.id() + " RETURN parent.hash");
	if(parent.hasNext()) {
		belongsTo = parent.single().get("parent.hash").asString();
	}
	StatementResult dependent =  connector.executeRead(
			"MATCH (struct:Struct)-[:DEPENDS_ON]->(type) WHERE ID(struct) = " + struct.id() + " RETURN type.hash");
	if(dependent.hasNext()){
		dependsOn = dependent.single().get("type.hash").asString();
	}

	return formatLine("id", struct.get("hash").asString()) +
			formatLine("qualifiedName", struct.get("fqn").asString()) +
			formatLine("name", struct.get("name").asString()) +
			formatLine("type", "Struct") +
			formatLine("belongsTo", belongsTo) +
			formatLine(dependsOn, dependsOn) +
			formatLine("filename", struct.get("fileName").asString());
}
 
Example 10
Source File: CityUtils.java    From Getaviz with Apache License 2.0 5 votes vote down vote up
public static List<Node> getChildren(Long parent) {
	ArrayList<Node> children = new ArrayList<>();
	StatementResult childs = connector.executeRead("MATCH (n)-[:CONTAINS]->(child) WHERE ID(n) = " + parent + " RETURN child");
	while(childs.hasNext()) {
		children.add(childs.next().get("child").asNode());
	}
	return children;
}
 
Example 11
Source File: JQA2JSON.java    From Getaviz with Apache License 2.0 5 votes vote down vote up
private String toMetaDataClass(Node c) {
	String belongsTo;
	StatementResult parent = connector
			.executeRead("MATCH (parent:Type)-[:DECLARES]->(class) WHERE ID(class) = " + c.id() + " RETURN parent");
	if (parent.hasNext()) {
		belongsTo = parent.single().get("parent").asNode().get("hash").asString("XXX");
	} else {
		parent = connector.executeRead(
				"MATCH (parent:Package)-[:CONTAINS]->(class) WHERE ID(class) = " + c.id() + " RETURN parent");
		belongsTo = parent.single().get("parent").asNode().get("hash").asString("YYY");
	}
	return "\"id\":            \"" + c.get("hash").asString() + "\"," +
			"\n" +
			"\"qualifiedName\": \"" + c.get("fqn").asString() + "\"," +
			"\n" +
			"\"name\":          \"" + c.get("name").asString() + "\"," +
			"\n" +
			"\"type\":          \"FAMIX.Class\"," +
			"\n" +
			"\"modifiers\":     \"" + getModifiers(c) + "\"," +
			"\n" +
			"\"subClassOf\":    \"" + getSuperClasses(c) + "\"," +
			"\n" +
			"\"superClassOf\":  \"" + getSubClasses(c) + "\"," +
			"\n" +
			"\"belongsTo\":     \"" + belongsTo + "\"" +
			"\n";
}
 
Example 12
Source File: QueryRunner.java    From neoprofiler with Apache License 2.0 5 votes vote down vote up
public List<Object> runQueryMultipleResult(NeoProfiler parent, String query, String columnReturn) {
	Session s = parent.getDriver().session();
	List<Object> retvals = new ArrayList<Object>();
	
	try ( Transaction tx = s.beginTransaction()) {
		// log.info(query);
		StatementResult result = tx.run(query);
				
		while(result.hasNext()) {
			retvals.add(result.next().get(columnReturn));
		}					
	}
	
	return retvals;
}
 
Example 13
Source File: C2JSON.java    From Getaviz with Apache License 2.0 5 votes vote down vote up
private String toMetaDataVariable(Node variable) {
	String belongsTo = "";
	String declaredType = "";
	String dependsOn = "";
	StatementResult parent =  connector.executeRead(
			"MATCH (parent)-[:DECLARES]->(variable:Variable) WHERE ID(variable) = " + variable.id() + " RETURN parent.hash");
	if(parent.hasNext()) {
		belongsTo = parent.single().get("parent.hash").asString();
	}
	StatementResult type =  connector.executeRead(
			"MATCH (variable:Variable)-[:OF_TYPE]->(type) WHERE ID(variable) = " + variable.id() + " RETURN type.name");
	if(type.hasNext()) {
		declaredType = type.single().get("type.name").asString();
	}
	StatementResult dependent =  connector.executeRead(
			"MATCH (variable:Variable)-[:DEPENDS_ON]->(type) WHERE ID(variable) = " + variable.id() + " RETURN type.hash");
	if(dependent.hasNext()){
		dependsOn = dependent.single().get("type.hash").asString();
	}

	return formatLine("id", variable.get("hash").asString()) +
			formatLine("qualifiedName", variable.get("fqn").asString()) +
			formatLine("name", variable.get("name").asString()) +
			formatLine("type", "FAMIX.Variable") +
			formatLine("declaredType", declaredType) +
			formatLine("accessedBy", getAccessedBy(variable)) +
			formatLine("belongsTo", belongsTo) +
			formatLine("dependsOn", dependsOn) +
			formatEndline("fileName", variable.get("fileName").asString());
}
 
Example 14
Source File: BrickLayout.java    From Getaviz with Apache License 2.0 5 votes vote down vote up
static void brickLayout(Long model) {
	StatementResult buildings = connector
			.executeRead("MATCH (n:City:Model)-[:CONTAINS*]->(b:Building) WHERE ID(n) = " + model + " RETURN b");
	while (buildings.hasNext()) {
		Node building = buildings.next().get("b").asNode();
		separateBuilding(building);
	}
}
 
Example 15
Source File: Neo4jNode.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
public static Neo4jNode get(long id, SnowGraphContext context){
    Neo4jNode node=null;
    Session session = context.getNeo4jBoltDriver().session();
    String stat = "match (n) where id(n)=" + id + " return id(n), labels(n)[0], n";
    StatementResult rs = session.run(stat);
    while (rs.hasNext()) {
        Record item=rs.next();
        node=new Neo4jNode(item.get("id(n)").asLong(),item.get("labels(n)[0]").asString());
        node.properties.putAll(item.get("n").asMap());
        node.properties.put("id",node.id);
        node.properties.put("label",node.label);
    }
    session.close();
    return node;
}
 
Example 16
Source File: Neo4jCypherInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private Set<String> getTypes(boolean refresh) {
  if (types == null || refresh) {
    types = new HashSet<>();
    StatementResult result = this.neo4jConnectionManager.execute("CALL db.relationshipTypes()");
    while (result.hasNext()) {
      Record record = result.next();
      types.add(record.get("relationshipType").asString());
    }
  }
  return types;
}
 
Example 17
Source File: Neo4jRelation.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
public static Neo4jRelation get(long rId, SnowGraphContext context){
    Neo4jRelation r=null;
    Session session = context.getNeo4jBoltDriver().session();
    String stat = "match p=(n)-[r]-(x) where id(r)=" + rId + " return id(r), id(startNode(r)), id(endNode(r)), type(r)";
    StatementResult rs = session.run(stat);
    while (rs.hasNext()) {
        Record item=rs.next();
        r=new Neo4jRelation(item.get("id(startNode(r))").asLong(),item.get("id(endNode(r))").asLong(),item.get("id(r)").asLong(),item.get("type(r)").asString());
    }
    session.close();
    return r;
}
 
Example 18
Source File: Importer.java    From Getaviz with Apache License 2.0 4 votes vote down vote up
private boolean isC() {
    DatabaseConnector connector = DatabaseConnector.getInstance();
    StatementResult result = connector.executeRead("MATCH (n:C) RETURN n LIMIT 2");
    return result.hasNext();
}
 
Example 19
Source File: Importer.java    From Getaviz with Apache License 2.0 4 votes vote down vote up
private boolean isJava() {
    DatabaseConnector connector = DatabaseConnector.getInstance();
    StatementResult result = connector.executeRead("MATCH (n:Java) RETURN n LIMIT 2");
    return result.hasNext();
}
 
Example 20
Source File: ApiLocator.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
public SubGraph queryExpand(String queryString) {
    SubGraph r = new SubGraph();
    SubGraph searchResult1 = query(queryString);
    r.nodes.addAll(searchResult1.nodes);
    r.cost = searchResult1.cost;

    Set<Long> selected = new HashSet<>();
    selected.add(r.nodes.iterator().next());
    int rootCount = searchResult1.nodes.size() - 1;

    while(rootCount-- > 0){
        int minStep = Integer.MAX_VALUE;
        long candidate = 0;
        SubGraph path = null;
        for (long seed1 : selected){
            for (long seed2: searchResult1.nodes){
                if (selected.contains(seed2))
                    continue;
                SubGraph currentPath = new SubGraph();
                Session session = context.connection.session();
                String stat = "match p=shortestPath((n1)-[:" + codeRels + "*..10]-(n2)) where id(n1)=" + seed1 + " and id(n2)=" + seed2
                        + " unwind relationships(p) as r return id(startNode(r)), id(endNode(r)), id(r)";
                StatementResult rs = session.run(stat);
                while (rs.hasNext()) {
                    Record item=rs.next();
                    long node1 = item.get("id(startNode(r))").asLong();
                    long node2 = item.get("id(endNode(r))").asLong();
                    long rel = item.get("id(r)").asLong();
                    currentPath.nodes.add(node1);
                    currentPath.nodes.add(node2);
                    currentPath.edges.add(rel);
                }
                session.close(); // may have no path
                if (currentPath.edges.size() > 0 && currentPath.edges.size() < minStep){
                    minStep = currentPath.edges.size();
                    path = currentPath;
                    candidate = seed2;
                }
            }

        }
        if (path == null) // cannot expand to other nodes
            break;
        selected.add(candidate);
        r.nodes.addAll(path.nodes);
        r.edges.addAll(path.edges);
    }
    if(debug)
        System.out.println("expanded graph size: " + r.nodes.size()+"\n");
    return r;
}