Search in sources :

Example 26 with Relationship

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;
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList)

Example 27 with Relationship

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);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 28 with Relationship

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?
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 29 with Relationship

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)));
}
Also used : Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 30 with Relationship

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);
        }
    }
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Aggregations

Relationship (org.neo4j.graphdb.Relationship)484 Node (org.neo4j.graphdb.Node)367 Test (org.junit.Test)250 Transaction (org.neo4j.graphdb.Transaction)138 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)48 LinkedList (java.util.LinkedList)45 RelationshipType (org.neo4j.graphdb.RelationshipType)37 NotFoundException (org.neo4j.graphdb.NotFoundException)35 Path (org.neo4j.graphdb.Path)29 HashMap (java.util.HashMap)27 Direction (org.neo4j.graphdb.Direction)25 HashSet (java.util.HashSet)24 ArrayList (java.util.ArrayList)18 RelationshipIndex (org.neo4j.graphdb.index.RelationshipIndex)18 WeightedPath (org.neo4j.graphalgo.WeightedPath)14 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)14 List (java.util.List)13 Map (java.util.Map)13 PropertyContainer (org.neo4j.graphdb.PropertyContainer)12 Graph (org.neo4j.test.GraphDescription.Graph)11