I have a graph in Neo4j and every relationship in this graph has a property called InverseRandomNumber. I want to traverse all the nodes and for each one of them get those properties from all the relationships attached to the node, sum those property values and save the sum as property in the node. The InverseRandomNumber is an integer or a double and i use Neo4j 2.3.3 embedded in Java. Below you can find my code.
Label myLabel = DynamicLabel.label("Data");
final RelationshipType type2 = DynamicRelationshipType.withName("Rel");
ResourceIterator<Node> users = graphDb.findNodes( myLabel, "Group", "Random" );
Node firstUserNode;
while ( users.hasNext() )
{
firstUserNode = users.next();
ArrayList<Object> RelList = new ArrayList<Object>();
if(firstUserNode.hasRelationship(Direction.BOTH)){
for (Relationship relationship : firstUserNode.getRelationships(Direction.BOTH)){
RelList.add(relationship.getProperty("InverseRandomNumber"));
}
}
Double sum = 0.0;
Double dd = 0.0;
for(int j=0; j < RelList.size(); j++){
dd = toDouble(RelList.get(j));
sum+=dd;
}
firstUserNode.setProperty("SumInverse",sum);
}
users.close();
In the above code i use the toDouble
method as to turn the value i get into double. Is that the correct way to handle numerical properties or is there another method more efficient and accurate?
Any ideas?