use of org.neo4j.graphdb.Direction in project graphdb by neo4j-attic.
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);
// eigenvectorCentrality.setMaxIterations( 100 );
assertApproximateCentrality(eigenvectorCentrality, "a", 0.0851, 0.01);
assertApproximateCentrality(eigenvectorCentrality, "b", 0.244, 0.01);
assertApproximateCentrality(eigenvectorCentrality, "c", 0.456, 0.01);
assertApproximateCentrality(eigenvectorCentrality, "d", 0.852, 0.01);
}
use of org.neo4j.graphdb.Direction in project graphdb by neo4j-attic.
the class TestTraversal method testSanityChecks2.
@Test
public void testSanityChecks2() throws Exception {
// ------------- with traverser direction -------------
// Valid data
Node root = getGraphDb().createNode();
RelationshipType[] traversableRels = new RelationshipType[] { MyRelTypes.TEST };
Direction[] traversableDirs = new Direction[] { Direction.OUTGOING };
// Null traversable relationships
this.sanityCheckTraverser("Sanity check failed: null traversable " + "rels should throw an " + "IllegalArgumentException", BREADTH_FIRST, root, null, traversableDirs[0], StopEvaluator.END_OF_GRAPH, ReturnableEvaluator.ALL);
// Null traversable directions
this.sanityCheckTraverser("Sanity check failed: null traversable " + "rels should throw an " + "IllegalArgumentException", BREADTH_FIRST, root, traversableRels[0], null, StopEvaluator.END_OF_GRAPH, ReturnableEvaluator.ALL);
// Null stop evaluator
this.sanityCheckTraverser("Sanity check failed: null stop eval " + "should throw an IllegalArgumentException", BREADTH_FIRST, root, traversableRels[0], traversableDirs[0], null, ReturnableEvaluator.ALL);
// Null returnable evaluator
this.sanityCheckTraverser("Sanity check failed: null returnable " + "evaluator should throw an " + "IllegalArgumentException", BREADTH_FIRST, root, traversableRels[0], traversableDirs[0], StopEvaluator.END_OF_GRAPH, null);
// traversable relationships length not equal to traversable directions
// length
this.sanityCheckTraverser("Sanity check failed: null returnable " + "evaluator should throw an " + "IllegalArgumentException", BREADTH_FIRST, root, traversableRels[0], null, StopEvaluator.END_OF_GRAPH, null);
this.sanityCheckTraverser("Sanity check failed: null returnable " + "evaluator should throw an " + "IllegalArgumentException", BREADTH_FIRST, root, null, traversableDirs[0], StopEvaluator.END_OF_GRAPH, null);
root.delete();
}
use of org.neo4j.graphdb.Direction in project neo4j by neo4j.
the class TestLoopRelationships method verifyRelationships.
private void verifyRelationships(String message, Node node, boolean[] loop, Relationship... relationships) {
try (Transaction transaction = getGraphDb().beginTx()) {
var root = transaction.getNodeById(node.getId());
for (Direction dir : Direction.values()) {
Set<Relationship> expected = new HashSet<>();
for (int i = 0; i < relationships.length; i++) {
if (relationships[i] != null && (dir != Direction.INCOMING || loop[i])) {
expected.add(relationships[i]);
}
}
for (Relationship rel : root.getRelationships(dir)) {
assertTrue(expected.remove(rel), message + ": unexpected relationship: " + rel);
}
assertTrue(expected.isEmpty(), message + ": expected relationships not seen " + expected);
}
transaction.commit();
}
}
use of org.neo4j.graphdb.Direction in project neo4j by neo4j.
the class TestRelationshipCount method ensureRightDegree.
private void ensureRightDegree(int initialSize, Collection<RelationshipCreationSpec> cspecs, Collection<RelationshipDeletionSpec> dspecs) {
Map<RelType, int[]> expectedCounts = new EnumMap<>(RelType.class);
for (RelType type : RelType.values()) {
expectedCounts.put(type, new int[3]);
}
Node me = tx.createNode();
for (int i = 0; i < initialSize; i++) {
me.createRelationshipTo(tx.createNode(), RelType.INITIAL);
}
newTransaction();
me = tx.getNodeById(me.getId());
expectedCounts.get(RelType.INITIAL)[0] = initialSize;
assertCounts(me, expectedCounts);
int counter = 0;
for (RelationshipCreationSpec spec : cspecs) {
for (int i = 0; i < spec.count; i++) {
Node otherNode = null;
if (spec.dir == Direction.OUTGOING) {
otherNode = tx.createNode();
me.createRelationshipTo(otherNode, spec.type);
} else if (spec.dir == Direction.INCOMING) {
otherNode = tx.createNode();
otherNode.createRelationshipTo(me, spec.type);
} else {
me.createRelationshipTo(me, spec.type);
}
expectedCounts.get(spec.type)[spec.dir.ordinal()]++;
if (otherNode != null) {
assertEquals(1, otherNode.getDegree());
}
assertCounts(me, expectedCounts);
if (counter % 3 == 0 && counter > 0) {
newTransaction();
assertCounts(me, expectedCounts);
}
}
}
assertCounts(me, expectedCounts);
newTransaction();
me = tx.getNodeById(me.getId());
assertCounts(me, expectedCounts);
// Delete one of each type/direction combination
counter = 0;
if (dspecs == null) {
for (RelType type : RelType.values()) {
if (!type.measure) {
continue;
}
for (Direction direction : Direction.values()) {
int[] counts = expectedCounts.get(type);
if (counts[direction.ordinal()] > 0) {
deleteOneRelationship(me, type, direction, 0);
counts[direction.ordinal()]--;
assertCounts(me, expectedCounts);
if (counter % 3 == 0 && counter > 0) {
newTransaction();
assertCounts(me, expectedCounts);
}
}
}
}
} else {
for (RelationshipDeletionSpec spec : dspecs) {
deleteOneRelationship(me, spec.type, spec.dir, spec.which);
expectedCounts.get(spec.type)[spec.dir.ordinal()]--;
assertCounts(me, expectedCounts);
if (counter % 3 == 0 && counter > 0) {
newTransaction();
assertCounts(me, expectedCounts);
}
}
}
assertCounts(me, expectedCounts);
newTransaction();
me = tx.getNodeById(me.getId());
assertCounts(me, expectedCounts);
// Clean up
for (Relationship rel : me.getRelationships()) {
Node otherNode = rel.getOtherNode(me);
if (!otherNode.equals(me)) {
otherNode.delete();
}
rel.delete();
}
me.delete();
}
use of org.neo4j.graphdb.Direction in project neo4j by neo4j.
the class DijkstraDirectionTest method testDijkstraDirection2.
@Test
void testDijkstraDirection2() {
try (Transaction transaction = graphDb.beginTx()) {
graph.makeEdge(transaction, "a", "b");
graph.makeEdge(transaction, "b", "c");
graph.makeEdge(transaction, "c", "d");
graph.makeEdge(transaction, "d", "a");
graph.makeEdge(transaction, "s", "a");
graph.makeEdge(transaction, "b", "s");
graph.makeEdge(transaction, "e", "c");
graph.makeEdge(transaction, "d", "e");
Dijkstra<Double> dijkstra = new Dijkstra<>((double) 0, graph.getNode(transaction, "s"), graph.getNode(transaction, "e"), (relationship, direction) -> {
assertEquals(Direction.OUTGOING, direction);
return 1.0;
}, new DoubleAdder(), Double::compareTo, Direction.OUTGOING, MyRelTypes.R1);
dijkstra.getCost();
dijkstra = new Dijkstra<>((double) 0, graph.getNode(transaction, "s"), graph.getNode(transaction, "e"), (relationship, direction) -> {
assertEquals(Direction.INCOMING, direction);
return 1.0;
}, new DoubleAdder(), Double::compareTo, Direction.INCOMING, MyRelTypes.R1);
dijkstra.getCost();
transaction.commit();
}
}
Aggregations