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;
}
};
}
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;
}
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));
}
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();
}
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();
}
Aggregations