use of org.neo4j.graphdb.Direction in project neo4j by neo4j.
the class EigenvectorCentralityTest method shouldHandleTargetNodeBeingOrphan.
@Test
public void shouldHandleTargetNodeBeingOrphan() {
graph.makeNode("o");
EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.BOTH, new CostEvaluator<Double>() {
@Override
public Double getCost(Relationship relationship, Direction direction) {
return 1d;
}
}, graph.getAllNodes(), graph.getAllEdges(), 0.01);
assertApproximateCentrality(eigenvectorCentrality, "o", 0d, 0.02);
}
use of org.neo4j.graphdb.Direction in project neo4j by neo4j.
the class EigenvectorCentralityTest method testWeightAndDirection.
/**
* Same network as above, but with direction BOTH and weights in different
* directions are given by a map.
*/
@Test
public void testWeightAndDirection() {
graph.makeEdgeChain("a,b");
graph.makeEdgeChain("b,c");
graph.makeEdgeChain("c,d");
graph.makeEdgeChain("c,a");
final Map<String, Double> costs = new HashMap<String, Double>();
costs.put("a,b", 1.0);
costs.put("b,c", 1.0);
costs.put("c,d", 1.0);
costs.put("c,b", 0.1);
costs.put("c,a", 0.1);
EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.BOTH, new CostEvaluator<Double>() {
public Double getCost(Relationship relationship, Direction direction) {
String start = graph.getNodeId(relationship.getStartNode());
String end = graph.getNodeId(relationship.getEndNode());
if (direction == Direction.INCOMING) {
// swap
String tmp = end;
end = start;
start = tmp;
}
Double value = costs.get(start + "," + end);
if (value == null) {
return 0.0;
}
return value;
}
}, graph.getAllNodes(), graph.getAllEdges(), 0.01);
assertApproximateCentrality(eigenvectorCentrality, "a", 0.0851, 0.02);
assertApproximateCentrality(eigenvectorCentrality, "b", 0.244, 0.02);
assertApproximateCentrality(eigenvectorCentrality, "c", 0.456, 0.02);
assertApproximateCentrality(eigenvectorCentrality, "d", 0.852, 0.02);
}
use of org.neo4j.graphdb.Direction in project neo4j by neo4j.
the class EigenvectorCentralityTest method testDirection.
/**
* Same as above, but inverted direction.
*/
@Test
public void testDirection() {
graph.makeEdgeChain("d,c,b,a");
graph.makeEdges("a,b,a,c");
EigenvectorCentrality eigenvectorCentrality = getEigenvectorCentrality(Direction.INCOMING, new CostEvaluator<Double>() {
public Double getCost(Relationship relationship, Direction direction) {
return 1.0;
}
}, graph.getAllNodes(), graph.getAllEdges(), 0.01);
assertApproximateCentrality(eigenvectorCentrality, "a", 0.693, 0.02);
assertApproximateCentrality(eigenvectorCentrality, "b", 0.523, 0.02);
assertApproximateCentrality(eigenvectorCentrality, "c", 0.395, 0.02);
assertApproximateCentrality(eigenvectorCentrality, "d", 0.298, 0.02);
}
use of org.neo4j.graphdb.Direction in project neo4j by neo4j.
the class NodeRelationshipCacheTest method shouldPutRandomStuff.
@Test
public void shouldPutRandomStuff() throws Exception {
// GIVEN
int nodes = 10_000;
PrimitiveLongObjectMap<long[]> key = Primitive.longObjectMap(nodes);
cache = new NodeRelationshipCache(NumberArrayFactory.HEAP, 1, 1000, base);
// mark random nodes as dense (dense node threshold is 1 so enough with one increment
cache.setHighNodeId(nodes);
for (long nodeId = 0; nodeId < nodes; nodeId++) {
if (random.nextBoolean()) {
cache.incrementCount(nodeId);
}
}
// WHEN
for (int i = 0; i < 100_000; i++) {
long nodeId = random.nextLong(nodes);
boolean dense = cache.isDense(nodeId);
Direction direction = random.among(Direction.values());
long relationshipId = random.nextLong(1_000_000);
long previousHead = cache.getAndPutRelationship(nodeId, direction, relationshipId, false);
long[] keyIds = key.get(nodeId);
int keyIndex = dense ? direction.ordinal() : 0;
if (keyIds == null) {
key.put(nodeId, keyIds = minusOneLongs(Direction.values().length));
}
assertEquals(keyIds[keyIndex], previousHead);
keyIds[keyIndex] = relationshipId;
}
}
use of org.neo4j.graphdb.Direction in project neo4j by neo4j.
the class NodeRelationshipCacheTest method shouldObserveFirstRelationshipAsEmptyInEachDirection.
@Test
public void shouldObserveFirstRelationshipAsEmptyInEachDirection() throws Exception {
// GIVEN
cache = new NodeRelationshipCache(NumberArrayFactory.AUTO, 1, 100, base);
int nodes = 100;
Direction[] directions = Direction.values();
GroupVisitor groupVisitor = mock(GroupVisitor.class);
cache.setForwardScan(true);
cache.setHighNodeId(nodes + 1);
for (int i = 0; i < nodes; i++) {
assertEquals(-1L, cache.getFirstRel(nodes, groupVisitor));
cache.incrementCount(i);
long previous = cache.getAndPutRelationship(i, directions[i % directions.length], random.nextInt(1_000_000), true);
assertEquals(-1L, previous);
}
// WHEN
cache.setForwardScan(false);
for (int i = 0; i < nodes; i++) {
long previous = cache.getAndPutRelationship(i, directions[i % directions.length], random.nextInt(1_000_000), false);
assertEquals(-1L, previous);
}
// THEN
cache.setForwardScan(true);
for (int i = 0; i < nodes; i++) {
assertEquals(-1L, cache.getFirstRel(nodes, groupVisitor));
}
}
Aggregations