use of org.openkilda.neo.OkNode in project open-kilda by telstra.
the class PathComputerTest method dijkstraAlgorithm.
/* ==========> TESTING DIJKSTRA
* THE FOLLOWING CAN BE USED DIRECTLY IN THE NEO4J BROWSER.
MERGE (A:switch {name:'00:01', state:'active'})
MERGE (B:switch {name:'00:02', state:'active'})
MERGE (C:switch {name:'00:03', state:'active'})
MERGE (D:switch {name:'00:04', state:'active'})
MERGE (A)-[ab:isl {available_bandwidth:1000, cost:10, status:'inactive'}]->(B)
MERGE (A)-[ac:isl {available_bandwidth:1000, cost:20, status:'active'}]->(C)
MERGE (B)-[bd:isl {available_bandwidth:1000, cost:10, status:'inactive'}]->(D)
MERGE (C)-[cd:isl {available_bandwidth:1000, cost:20, status:'active'}]->(D)
MERGE (B)-[ba:isl {available_bandwidth:1000, cost:10, status:'inactive'}]->(A)
MERGE (C)-[ca:isl {available_bandwidth:1000, cost:20, status:'active'}]->(A)
MERGE (D)-[db:isl {available_bandwidth:1000, cost:10, status:'inactive'}]->(B)
MERGE (D)-[dc:isl {available_bandwidth:1000, cost:20, status:'active'}]->(C)
return A,B,C,D
TESTING with no_cost:
MERGE (A:switch {name:'00:01', state:'active'})
MERGE (B:switch {name:'00:02', state:'active'})
MERGE (C:switch {name:'00:03', state:'active'})
MERGE (D:switch {name:'00:04', state:'active'})
MERGE (A)-[ab:isl {available_bandwidth:1000, status:'inactive'}]->(B)
MERGE (A)-[ac:isl {available_bandwidth:1000, status:'active'}]->(C)
MERGE (B)-[bd:isl {available_bandwidth:1000, status:'inactive'}]->(D)
MERGE (C)-[cd:isl {available_bandwidth:1000, status:'active'}]->(D)
MERGE (B)-[ba:isl {available_bandwidth:1000, status:'inactive'}]->(A)
MERGE (C)-[ca:isl {available_bandwidth:1000, status:'active'}]->(A)
MERGE (D)-[db:isl {available_bandwidth:1000, status:'inactive'}]->(B)
MERGE (D)-[dc:isl {available_bandwidth:1000, status:'active'}]->(C)
return A,B,C,D
==> WORKS
MATCH (from:switch{name:"00:01"}), (to:switch{name:"00:04"})
CALL apoc.algo.dijkstraWithDefaultWeight(from, to, 'isl', 'cost', 700) YIELD path AS p, weight AS weight
RETURN p, weight
MATCH (from:switch{name:"00:01"}), (to:switch{name:"00:04"})
CALL apoc.algo.dijkstraWithDefaultWeight(from, to, 'isl', 'cost', 700) YIELD path AS p, weight AS weight
WHERE ALL(y in rels(p) WHERE y.status = 'active')
RETURN p, weight
MATCH (from:switch), (to:switch)
CALL apoc.algo.dijkstraWithDefaultWeight(from, to, 'isl', 'cost', 700) YIELD path AS p, weight AS weight
WHERE from.name = "00:01" AND to.name = "00:04" AND ALL(r IN rels(path) WHERE r.state = 'active')
RETURN p, weight
MATCH (from:switch{name:"00:01"}), (to:switch{name:"00:04"}), paths = allShortestPaths((from)-[r:isl*]->(to))
WITH REDUCE(cost = 0, rel in rels(paths) | cost + rel.cost) AS cost, paths
WHERE ALL (x in r WHERE x.status = 'active')
RETURN paths, cost
ORDER BY cost
LIMIT 1
==> WORKS
MATCH (from:switch{name:"00:01"}), (to:switch{name:"00:04"}), paths = allShortestPaths((from)-[r:isl*..100]->(to))
WITH REDUCE(cost = 0, rel in rels(paths) | cost + rel.cost) AS cost, paths
WHERE ALL (x in r WHERE x.status = 'active')
RETURN paths ORDER BY cost LIMIT 1
==> WORKING WITH NO COST
MATCH (from:switch{name:"00:01"}), (to:switch{name:"00:04"}), paths = allShortestPaths((from)-[r:isl*..100]->(to))
WITH REDUCE(cost = 0, rel in rels(paths) | cost + rel.cost) AS cost, paths
WHERE ALL (x in r WHERE x.status = 'active')
RETURN paths ORDER BY cost LIMIT 1
MATCH (a:switch{name:"00:01"}),(b:switch{name:"00:04"}), p = shortestPath((a)-[r:isl*..100]->(b))
where ALL(x in nodes(p) WHERE x.state = 'active')
AND ALL(y in r WHERE y.status = 'active' AND y.available_bandwidth >= 100)
RETURN p
==> WORKS
MATCH (a:switch{name:"00:01"}),(b:switch{name:"00:04"}), p = shortestPath((a)-[r:isl*..100]->(b))
WHERE ALL(y in r WHERE y.status = 'active' AND y.available_bandwidth >= 100)
return p
StringJoiner where = new StringJoiner("\n AND ", "where ", "");
where.add("ALL(x in nodes(p) WHERE x.state = 'active')");
if (flow.isIgnoreBandwidth()) {
where.add("ALL(y in r WHERE y.status = 'active')");
StringJoiner where = new StringJoiner("\n AND ", "where ", "");
where.add("ALL(x in nodes(p) WHERE x.state = 'active')");
if (flow.isIgnoreBandwidth()) {
where.add("ALL(y in r WHERE y.status = 'active')");
} else {
where.add("ALL(y in r WHERE y.status = 'active' AND y.available_bandwidth >= {bandwidth})");
parameters.put("bandwidth", Values.value(flow.getBandwidth()));
}
String result = "RETURN p";
*/
/**
* Current status of this test is .. in alpha:
* - it works with standard graph node
* - but we want to evolve it to work with an oknode, so that we can filter relationships
*/
@Ignore
@Test
public void dijkstraAlgorithm() {
NeoUtils nuts = new NeoUtils(graphDb);
OkNode nodeA, nodeB, nodeC;
try (Transaction tx = graphDb.beginTx()) {
nodeA = nuts.node("A");
nodeB = nuts.node("B");
nodeC = nuts.node("C");
nodeA.edge(OkRels.isl, nodeB).property("length", 2d);
nodeB.edge(OkRels.isl, nodeC).property("length", 3d);
nodeA.edge(OkRels.isl, nodeC).property("length", 10d);
tx.success();
}
try (Transaction tx = graphDb.beginTx()) {
Dijkstra<Double> dike = nuts.getDijkstra(0d, OkRels.isl, "length", nodeA, nodeC);
// assertEquals(5.0d, dike.getCost().doubleValue(), 0.01);
assertEquals(3, dike.getPathAsNodes().size());
tx.success();
}
}