Search in sources :

Example 16 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project neo4j-mobile-android by neo4j-contrib.

the class Dijkstra method getPath.

/**
 * @return One of the shortest paths found or null.
 */
public List<PropertyContainer> getPath() {
    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();
    LinkedList<PropertyContainer> path = new LinkedList<PropertyContainer>();
    path.addAll(Util.constructSinglePathToNode(middleNode, predecessors1, true, false));
    path.addAll(Util.constructSinglePathToNode(middleNode, predecessors2, false, true));
    return path;
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Node(org.neo4j.graphdb.Node) LinkedList(java.util.LinkedList)

Example 17 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project neo4j-mobile-android by neo4j-contrib.

the class Dijkstra method getPaths.

/**
 * @return All the found paths or null.
 */
public List<List<PropertyContainer>> getPaths() {
    if (startNode == null || endNode == null) {
        throw new RuntimeException("Start or end node undefined.");
    }
    calculateMultiple();
    if (foundPathsMiddleNodes == null || foundPathsMiddleNodes.size() == 0) {
        return Collections.emptyList();
    }
    // Currently we use a set to avoid duplicate paths
    // TODO: can this be done smarter?
    Set<List<PropertyContainer>> paths = new HashSet<List<PropertyContainer>>();
    for (Node middleNode : foundPathsMiddleNodes) {
        List<List<PropertyContainer>> paths1 = Util.constructAllPathsToNode(middleNode, predecessors1, true, false);
        List<List<PropertyContainer>> paths2 = Util.constructAllPathsToNode(middleNode, predecessors2, false, true);
        // For all combinations...
        for (List<PropertyContainer> part1 : paths1) {
            for (List<PropertyContainer> part2 : paths2) {
                // Combine them
                LinkedList<PropertyContainer> path = new LinkedList<PropertyContainer>();
                path.addAll(part1);
                path.addAll(part2);
                // Add to collection
                paths.add(path);
            }
        }
    }
    return new LinkedList<List<PropertyContainer>>(paths);
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Node(org.neo4j.graphdb.Node) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 18 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project blueprints by tinkerpop.

the class Neo4j2BatchGraph method populateKeyIndices.

private static <T extends PropertyContainer> void populateKeyIndices(final GraphDatabaseService rawGraphDB, final AutoIndexer<T> rawAutoIndexer, final Iterable<T> rawElements, final Class elementClass) {
    if (!rawAutoIndexer.isEnabled())
        return;
    final Set<String> properties = rawAutoIndexer.getAutoIndexedProperties();
    Transaction tx = rawGraphDB.beginTx();
    final PropertyContainer kernel = ((GraphDatabaseAPI) rawGraphDB).getDependencyResolver().resolveDependency(NodeManager.class).newGraphProperties();
    kernel.setProperty(elementClass.getSimpleName() + INDEXED_KEYS_POSTFIX, properties.toArray(new String[properties.size()]));
    int count = 0;
    for (final PropertyContainer pc : rawElements) {
        for (final String property : properties) {
            if (!pc.hasProperty(property))
                continue;
            pc.setProperty(property, pc.getProperty(property));
            count++;
            if (count >= 10000) {
                count = 0;
                tx.success();
                tx.finish();
                tx = rawGraphDB.beginTx();
            }
        }
    }
    tx.success();
    tx.finish();
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) NodeManager(org.neo4j.kernel.impl.core.NodeManager) Transaction(org.neo4j.graphdb.Transaction) GraphDatabaseAPI(org.neo4j.kernel.GraphDatabaseAPI)

Example 19 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project graphdb by neo4j-attic.

the class DijkstraMultiplePathsTest method test7.

@Test
public void test7() {
    Relationship edgeAB = graph.makeEdge("a", "b");
    Relationship edgeBC = graph.makeEdge("b", "c");
    Relationship edgeCD = graph.makeEdge("c", "d");
    Relationship edgeDE = graph.makeEdge("d", "e");
    Relationship edgeAB2 = graph.makeEdge("a", "b2");
    Relationship edgeB2C = graph.makeEdge("b2", "c");
    Relationship edgeCD2 = graph.makeEdge("c", "d2");
    Relationship edgeD2E = graph.makeEdge("d2", "e");
    Dijkstra<Double> dijkstra = new Dijkstra<Double>(0.0, graph.getNode("a"), graph.getNode("e"), new CostEvaluator<Double>() {

        public Double getCost(Relationship relationship, Direction direction) {
            return 1.0;
        }
    }, new DoubleAdder(), new DoubleComparator(), Direction.OUTGOING, MyRelTypes.R1);
    // path discovery flags
    boolean pathBD = false;
    boolean pathB2D = false;
    boolean pathBD2 = false;
    boolean pathB2D2 = false;
    List<List<PropertyContainer>> paths = dijkstra.getPaths();
    assertTrue(paths.size() == 4);
    for (List<PropertyContainer> path : paths) {
        assertTrue(path.size() == 9);
        assertTrue(path.get(0).equals(graph.getNode("a")));
        assertTrue(path.get(4).equals(graph.getNode("c")));
        assertTrue(path.get(8).equals(graph.getNode("e")));
        // first choice
        if (path.get(2).equals(graph.getNode("b"))) {
            assertTrue(path.get(1).equals(edgeAB));
            assertTrue(path.get(3).equals(edgeBC));
        } else {
            assertTrue(path.get(1).equals(edgeAB2));
            assertTrue(path.get(2).equals(graph.getNode("b2")));
            assertTrue(path.get(3).equals(edgeB2C));
        }
        // second choice
        if (path.get(6).equals(graph.getNode("d"))) {
            assertTrue(path.get(5).equals(edgeCD));
            assertTrue(path.get(7).equals(edgeDE));
        } else {
            assertTrue(path.get(5).equals(edgeCD2));
            assertTrue(path.get(6).equals(graph.getNode("d2")));
            assertTrue(path.get(7).equals(edgeD2E));
        }
        // combinations
        if (path.get(2).equals(graph.getNode("b"))) {
            if (path.get(6).equals(graph.getNode("d"))) {
                pathBD = true;
            } else if (path.get(6).equals(graph.getNode("d2"))) {
                pathBD2 = true;
            }
        } else {
            if (path.get(6).equals(graph.getNode("d"))) {
                pathB2D = true;
            } else if (path.get(6).equals(graph.getNode("d2"))) {
                pathB2D2 = true;
            }
        }
    }
    assertTrue(pathBD);
    assertTrue(pathB2D);
    assertTrue(pathBD2);
    assertTrue(pathB2D2);
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) DoubleComparator(org.neo4j.graphalgo.impl.util.DoubleComparator) Direction(org.neo4j.graphdb.Direction) Dijkstra(org.neo4j.graphalgo.impl.shortestpath.Dijkstra) DoubleAdder(org.neo4j.graphalgo.impl.util.DoubleAdder) Relationship(org.neo4j.graphdb.Relationship) List(java.util.List) Test(org.junit.Test)

Example 20 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project neo4j by neo4j.

the class TestTimeline method createTimestampedEntity.

private Pair<PropertyContainer, Long> createTimestampedEntity(EntityCreator<PropertyContainer> creator, TimelineIndex<PropertyContainer> timeline, long timestamp) {
    PropertyContainer entity = creator.create();
    timeline.add(entity, timestamp);
    return Pair.of(entity, timestamp);
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer)

Aggregations

PropertyContainer (org.neo4j.graphdb.PropertyContainer)36 Test (org.junit.Test)11 Node (org.neo4j.graphdb.Node)11 LinkedList (java.util.LinkedList)8 Relationship (org.neo4j.graphdb.Relationship)7 Transaction (org.neo4j.graphdb.Transaction)6 HashSet (java.util.HashSet)4 IOException (java.io.IOException)3 List (java.util.List)3 Map (java.util.Map)3 GraphDatabaseAPI (org.neo4j.kernel.GraphDatabaseAPI)3 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)3 HashMap (java.util.HashMap)2 SystemException (javax.transaction.SystemException)2 NotFoundException (org.neo4j.graphdb.NotFoundException)2 Path (org.neo4j.graphdb.Path)2 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)2 ArrayBackedList (apoc.util.ArrayBackedList)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1