Search in sources :

Example 61 with RelationshipType

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

the class Evaluators method lastRelationshipTypeIs.

/**
     * Returns an {@link Evaluator} which compares the type of the last relationship
     * in a {@link Path} to a given set of relationship types (one or more).If the type of
     * the last relationship in a path is of one of the given types then
     * {@code evaluationIfMatch} will be returned, otherwise
     * {@code evaluationIfNoMatch} will be returned.
     *
     * @param evaluationIfMatch   the {@link Evaluation} to return if the type of the
     *                            last relationship in the path matches any of the given types.
     * @param evaluationIfNoMatch the {@link Evaluation} to return if the type of the
     *                            last relationship in the path doesn't match any of the given types.
     * @param type                the (first) type (of possibly many) to match the last relationship
     *                            in paths with.
     * @param orAnyOfTheseTypes   additional types to match the last relationship in
     *                            paths with.
     * @param <STATE>             the type of the state object.
     * @return an {@link Evaluator} which compares the type of the last relationship
     *         in a {@link Path} to a given set of relationship types.
     */
public static <STATE> PathEvaluator<STATE> lastRelationshipTypeIs(final Evaluation evaluationIfMatch, final Evaluation evaluationIfNoMatch, final RelationshipType type, RelationshipType... orAnyOfTheseTypes) {
    if (orAnyOfTheseTypes.length == 0) {
        return new PathEvaluator.Adapter<STATE>() {

            @Override
            public Evaluation evaluate(Path path, BranchState state) {
                Relationship rel = path.lastRelationship();
                return rel != null && rel.isType(type) ? evaluationIfMatch : evaluationIfNoMatch;
            }
        };
    }
    final Set<String> expectedTypes = new HashSet<>();
    expectedTypes.add(type.name());
    for (RelationshipType otherType : orAnyOfTheseTypes) {
        expectedTypes.add(otherType.name());
    }
    return new PathEvaluator.Adapter<STATE>() {

        @Override
        public Evaluation evaluate(Path path, BranchState state) {
            Relationship lastRelationship = path.lastRelationship();
            if (lastRelationship == null) {
                return evaluationIfNoMatch;
            }
            return expectedTypes.contains(lastRelationship.getType().name()) ? evaluationIfMatch : evaluationIfNoMatch;
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType) HashSet(java.util.HashSet)

Example 62 with RelationshipType

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

the class StandardExpander method temporaryTypeMapFrom.

private static Map<Direction, Collection<RelationshipType>> temporaryTypeMapFrom(Map<Direction, RelationshipType[]> typeMap) {
    Map<Direction, Collection<RelationshipType>> map = new EnumMap<Direction, Collection<RelationshipType>>(Direction.class);
    for (Direction direction : Direction.values()) {
        ArrayList<RelationshipType> types = new ArrayList<RelationshipType>();
        map.put(direction, types);
        RelationshipType[] existing = typeMap.get(direction);
        if (existing != null) {
            types.addAll(asList(existing));
        }
    }
    return map;
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) ArrayList(java.util.ArrayList) Collection(java.util.Collection) EnumMap(java.util.EnumMap) Direction(org.neo4j.graphdb.Direction)

Example 63 with RelationshipType

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

the class StandardExpander method create.

public static StandardExpander create(RelationshipType type1, Direction dir1, RelationshipType type2, Direction dir2, Object... more) {
    Map<Direction, Collection<RelationshipType>> tempMap = temporaryTypeMap();
    tempMap.get(dir1).add(type1);
    tempMap.get(dir2).add(type2);
    for (int i = 0; i < more.length; i++) {
        RelationshipType type = (RelationshipType) more[i++];
        Direction direction = (Direction) more[i];
        tempMap.get(direction).add(type);
    }
    return new RegularExpander(toTypeMap(tempMap));
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) Collection(java.util.Collection) Direction(org.neo4j.graphdb.Direction)

Example 64 with RelationshipType

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

the class TestRecovery method testRecovery.

@Test
public void testRecovery() throws Exception {
    final GraphDatabaseService graphDb = newGraphDbService();
    final Index<Node> nodeIndex = graphDb.index().forNodes("node-index");
    final Index<Relationship> relIndex = graphDb.index().forRelationships("rel-index");
    final RelationshipType relType = DynamicRelationshipType.withName("recovery");
    graphDb.beginTx();
    Random random = new Random();
    Thread stopper = new Thread() {

        @Override
        public void run() {
            sleepNice(1000);
            graphDb.shutdown();
        }
    };
    final String[] keys = { "apoc", "zion", "neo" };
    try {
        stopper.start();
        for (int i = 0; i < 50; i++) {
            Node node = graphDb.createNode();
            Node otherNode = graphDb.createNode();
            Relationship rel = node.createRelationshipTo(otherNode, relType);
            for (int ii = 0; ii < 3; ii++) {
                nodeIndex.add(node, keys[random.nextInt(keys.length)], random.nextInt());
                relIndex.add(rel, keys[random.nextInt(keys.length)], random.nextInt());
            }
            sleepNice(10);
        }
    } catch (Exception e) {
    // Ok
    }
    // Wait until the stopper has run, i.e. the graph db is shut down
    while (stopper.getState() != State.TERMINATED) {
        sleepNice(100);
    }
    // Start up and let it recover
    final GraphDatabaseService newGraphDb = new EmbeddedGraphDatabase(getDbPath());
    newGraphDb.shutdown();
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) DynamicRelationshipType(org.neo4j.graphdb.DynamicRelationshipType) Random(java.util.Random) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 65 with RelationshipType

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

the class TestLuceneIndex method testNodeLocalRelationshipIndex.

@Test
public void testNodeLocalRelationshipIndex() {
    RelationshipIndex index = relationshipIndex("locality", LuceneIndexImplementation.EXACT_CONFIG);
    RelationshipType type = DynamicRelationshipType.withName("YO");
    Node startNode = graphDb.createNode();
    Node endNode1 = graphDb.createNode();
    Node endNode2 = graphDb.createNode();
    Relationship rel1 = startNode.createRelationshipTo(endNode1, type);
    Relationship rel2 = startNode.createRelationshipTo(endNode2, type);
    index.add(rel1, "name", "something");
    index.add(rel2, "name", "something");
    for (int i = 0; i < 2; i++) {
        assertThat(index.query("name:something"), contains(rel1, rel2));
        assertThat(index.query("name:something", null, endNode1), contains(rel1));
        assertThat(index.query("name:something", startNode, endNode2), contains(rel2));
        assertThat(index.query(null, startNode, endNode1), contains(rel1));
        assertThat(index.get("name", "something", null, endNode1), contains(rel1));
        assertThat(index.get("name", "something", startNode, endNode2), contains(rel2));
        assertThat(index.get(null, null, startNode, endNode1), contains(rel1));
        restartTx();
    }
    rel2.delete();
    rel1.delete();
    startNode.delete();
    endNode1.delete();
    endNode2.delete();
    index.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) DynamicRelationshipType(org.neo4j.graphdb.DynamicRelationshipType) RelationshipType(org.neo4j.graphdb.RelationshipType) RelationshipIndex(org.neo4j.graphdb.index.RelationshipIndex) Test(org.junit.Test)

Aggregations

RelationshipType (org.neo4j.graphdb.RelationshipType)97 Node (org.neo4j.graphdb.Node)53 Test (org.junit.Test)45 Relationship (org.neo4j.graphdb.Relationship)38 Transaction (org.neo4j.graphdb.Transaction)18 Direction (org.neo4j.graphdb.Direction)15 Traverser (org.neo4j.graphdb.Traverser)10 NotFoundException (org.neo4j.graphdb.NotFoundException)9 DynamicRelationshipType (org.neo4j.graphdb.DynamicRelationshipType)7 Label (org.neo4j.graphdb.Label)7 RelationshipRecord (org.neo4j.kernel.impl.nioneo.store.RelationshipRecord)7 Collection (java.util.Collection)6 HashSet (java.util.HashSet)6 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)6 Path (org.neo4j.graphdb.Path)6 StopEvaluator (org.neo4j.graphdb.StopEvaluator)6 TraversalPosition (org.neo4j.graphdb.TraversalPosition)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)4