use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.
the class Dijkstra method getPathAsRelationships.
/**
* @return One of the shortest paths found or null.
*/
public List<Relationship> getPathAsRelationships() {
if (startNode == null || endNode == null) {
throw new RuntimeException("Start or end node undefined.");
}
calculate();
if (foundPathsMiddleNodes == null || foundPathsMiddleNodes.size() == 0) {
return null;
}
Node middleNode = foundPathsMiddleNodes.iterator().next();
List<Relationship> path = new LinkedList<Relationship>();
path.addAll(Util.constructSinglePathToNodeAsRelationships(middleNode, predecessors1, false));
path.addAll(Util.constructSinglePathToNodeAsRelationships(middleNode, predecessors2, true));
return path;
}
use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.
the class Dijkstra method getPathsAsRelationships.
/**
* @return All the found paths or null.
*/
public List<List<Relationship>> getPathsAsRelationships() {
if (startNode == null || endNode == null) {
throw new RuntimeException("Start or end node undefined.");
}
calculateMultiple();
if (foundPathsMiddleNodes == null || foundPathsMiddleNodes.size() == 0) {
return null;
}
// Currently we use a set to avoid duplicate paths
// TODO: can this be done smarter?
Set<List<Relationship>> paths = new HashSet<List<Relationship>>();
for (Node middleNode : foundPathsMiddleNodes) {
List<List<Relationship>> paths1 = Util.constructAllPathsToNodeAsRelationships(middleNode, predecessors1, false);
List<List<Relationship>> paths2 = Util.constructAllPathsToNodeAsRelationships(middleNode, predecessors2, true);
// For all combinations...
for (List<Relationship> part1 : paths1) {
for (List<Relationship> part2 : paths2) {
// Combine them
LinkedList<Relationship> path = new LinkedList<Relationship>();
path.addAll(part1);
path.addAll(part2);
// Add to collection
paths.add(path);
}
}
}
return new LinkedList<List<Relationship>>(paths);
}
use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.
the class FloydWarshall method calculate.
/**
* Internal calculate method that will do the calculation. This can however
* be called externally to manually trigger the calculation.
*/
@SuppressWarnings("unchecked")
public void calculate() {
// Don't do it more than once
if (doneCalculation) {
return;
}
doneCalculation = true;
// Build initial matrix
int n = nodeSet.size();
costMatrix = (CostType[][]) new Object[n][n];
predecessors = new Integer[n][n];
IndexedNodes = new Node[n];
nodeIndexes = new HashMap<Node, Integer>();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
costMatrix[i][j] = infinitelyBad;
}
costMatrix[i][i] = startCost;
}
int nodeIndex = 0;
for (Node node : nodeSet) {
nodeIndexes.put(node, nodeIndex);
IndexedNodes[nodeIndex] = node;
++nodeIndex;
}
// Put the relationships in there
for (Relationship relationship : relationshipSet) {
Integer i1 = nodeIndexes.get(relationship.getStartNode());
Integer i2 = nodeIndexes.get(relationship.getEndNode());
if (i1 == null || i2 == null) {
// exception?
continue;
}
if (relationDirection.equals(Direction.BOTH) || relationDirection.equals(Direction.OUTGOING)) {
costMatrix[i1][i2] = costEvaluator.getCost(relationship, Direction.OUTGOING);
predecessors[i1][i2] = i1;
}
if (relationDirection.equals(Direction.BOTH) || relationDirection.equals(Direction.INCOMING)) {
costMatrix[i2][i1] = costEvaluator.getCost(relationship, Direction.INCOMING);
predecessors[i2][i1] = i2;
}
}
// Do it!
for (int v = 0; v < n; ++v) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
CostType alternative = costAccumulator.addCosts(costMatrix[i][v], costMatrix[v][j]);
if (costComparator.compare(costMatrix[i][j], alternative) > 0) {
costMatrix[i][j] = alternative;
predecessors[i][j] = predecessors[v][j];
}
}
}
}
// TODO: detect negative cycles?
}
use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.
the class PathImplTest method pathsWithDifferentLengthAreNotEqual.
@Test
public void pathsWithDifferentLengthAreNotEqual() throws Exception {
Node node = createNode(1337L);
Relationship relationship = createRelationship(1337L, 7331L);
// Given
Path firstPath = new PathImpl.Builder(node).push(relationship).build();
Path secondPath = new PathImpl.Builder(node).push(relationship).push(createRelationship(1337L, 7331L)).build();
// When Then
assertThat(firstPath, not(equalTo(secondPath)));
assertThat(secondPath, not(equalTo(firstPath)));
}
use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.
the class DijkstraTest method testSmallGraphWithDefaults.
@Test
public void testSmallGraphWithDefaults() {
Relationship shortCTOXRelationship = createGraph(true);
PathFinder<WeightedPath> finder = factory.dijkstra(PathExpanders.forTypeAndDirection(MyRelTypes.R1, Direction.OUTGOING), CommonEvaluators.doubleCostEvaluator("cost", 1.0d));
// Assert that there are two matching paths
Node startNode = graph.getNode("start");
Node endNode = graph.getNode("x");
assertPaths(finder.findAllPaths(startNode, endNode), "start,a,b,c,x", "start,a,b,c,d,e,x");
// of the two from (c) --> (x)
for (WeightedPath path : finder.findAllPaths(startNode, endNode)) {
if (getPathDef(path).equals("start,a,b,c,x")) {
assertContainsRelationship(path, shortCTOXRelationship);
}
}
}
Aggregations