use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.
the class RelationshipProxyTest method shouldPrintCypherEsqueRelationshipToString.
@Test
public void shouldPrintCypherEsqueRelationshipToString() throws Exception {
// GIVEN
Node start, end;
RelationshipType type = RelationshipType.withName("NICE");
Relationship relationship;
try (Transaction tx = db.beginTx()) {
// GIVEN
start = db.createNode();
end = db.createNode();
relationship = start.createRelationshipTo(end, type);
tx.success();
// WHEN
String toString = relationship.toString();
// THEN
assertEquals("(" + start.getId() + ")-[" + type + "," + relationship.getId() + "]->(" + end.getId() + ")", toString);
}
}
use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.
the class RelationshipExpanderBuilder method describeRelationships.
@SuppressWarnings("unchecked")
public static PathExpander describeRelationships(Map<String, Object> description) {
PathExpanderBuilder expander = PathExpanderBuilder.allTypesAndDirections();
Object relationshipsDescription = description.get("relationships");
if (relationshipsDescription != null) {
Collection<Object> pairDescriptions;
if (relationshipsDescription instanceof Collection) {
pairDescriptions = (Collection<Object>) relationshipsDescription;
} else {
pairDescriptions = Arrays.asList(relationshipsDescription);
}
for (Object pairDescription : pairDescriptions) {
Map map = (Map) pairDescription;
String name = (String) map.get("type");
RelationshipType type = RelationshipType.withName(name);
String directionName = (String) map.get("direction");
expander = (directionName == null) ? expander.add(type) : expander.add(type, stringToEnum(directionName, RelationshipDirection.class, true).internal);
}
}
return expander.build();
}
use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.
the class TraversalDescriptionBuilder method describeExpander.
@SuppressWarnings("unchecked")
private TraversalDescription describeExpander(TraversalDescription result, Map<String, Object> description) {
Object relationshipsDescription = description.get("relationships");
if (relationshipsDescription != null) {
Collection<Object> pairDescriptions;
if (relationshipsDescription instanceof Collection) {
pairDescriptions = (Collection<Object>) relationshipsDescription;
} else {
pairDescriptions = Arrays.asList(relationshipsDescription);
}
PathExpanderBuilder builder = createExpander(description);
for (Object pairDescription : pairDescriptions) {
Map map = (Map) pairDescription;
String name = (String) map.get("type");
RelationshipType type = RelationshipType.withName(name);
String directionName = (String) map.get("direction");
builder = directionName == null ? builder.add(type) : builder.add(type, stringToEnum(directionName, RelationshipDirection.class, true).internal);
}
PathExpander<Object> expander = builder.build();
result = result.expand(expander);
}
return result;
}
use of org.neo4j.graphdb.RelationshipType in project neo4j-documentation by neo4j.
the class BatchInsertDocTest method insert.
@Test
public void insert() throws Exception {
// Make sure our scratch directory is clean
File tempStoreDir = clean("target/batchinserter-example").getAbsoluteFile();
// START SNIPPET: insert
BatchInserter inserter = null;
try {
inserter = BatchInserters.inserter(tempStoreDir);
Label personLabel = Label.label("Person");
inserter.createDeferredSchemaIndex(personLabel).on("name").create();
Map<String, Object> properties = new HashMap<>();
properties.put("name", "Mattias");
long mattiasNode = inserter.createNode(properties, personLabel);
properties.put("name", "Chris");
long chrisNode = inserter.createNode(properties, personLabel);
RelationshipType knows = RelationshipType.withName("KNOWS");
inserter.createRelationship(mattiasNode, chrisNode, knows, null);
} finally {
if (inserter != null) {
inserter.shutdown();
}
}
// END SNIPPET: insert
// try it out from a normal db
GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(new File("target/batchinserter-example")).newGraphDatabase();
try (Transaction tx = db.beginTx()) {
db.schema().awaitIndexesOnline(10, TimeUnit.SECONDS);
}
try (Transaction tx = db.beginTx()) {
Label personLabelForTesting = Label.label("Person");
Node mNode = db.findNode(personLabelForTesting, "name", "Mattias");
Node cNode = mNode.getSingleRelationship(RelationshipType.withName("KNOWS"), Direction.OUTGOING).getEndNode();
assertThat((String) cNode.getProperty("name"), is("Chris"));
assertThat(db.schema().getIndexes(personLabelForTesting).iterator().hasNext(), is(true));
} finally {
db.shutdown();
}
}
use of org.neo4j.graphdb.RelationshipType in project eol-globi-data by jhpoelen.
the class IndexInteractionsTest method indexInteractions.
@Test
public void indexInteractions() throws NodeFactoryException {
TaxonIndex taxonIndex = getOrCreateTaxonIndex();
// see https://github.com/jhpoelen/eol-globi-data/wiki/Nanopubs
StudyImpl study = new StudyImpl("some study", "some source", "http://doi.org/123.23/222", "some study citation");
NodeFactoryWithDatasetContext factory = new NodeFactoryWithDatasetContext(nodeFactory, new DatasetImpl("some/namespace", URI.create("https://some.uri")));
Study interaction = factory.getOrCreateStudy(study);
TaxonImpl donaldTaxon = new TaxonImpl("donald duck", "NCBI:1234");
Specimen donald = factory.createSpecimen(interaction, donaldTaxon);
donald.classifyAs(taxonIndex.getOrCreateTaxon(donaldTaxon));
TaxonImpl mickeyTaxon = new TaxonImpl("mickey mouse", "NCBI:4444");
Taxon mickeyTaxonNCBI = taxonIndex.getOrCreateTaxon(new TaxonImpl("mickey mouse", "EOL:567"));
NodeUtil.connectTaxa(mickeyTaxon, (TaxonNode) mickeyTaxonNCBI, getGraphDb(), RelTypes.SAME_AS);
Specimen mickey = factory.createSpecimen(interaction, mickeyTaxon);
mickey.classifyAs(taxonIndex.getOrCreateTaxon(mickeyTaxon));
donald.ate(mickey);
new IndexInteractions(getGraphDb()).link();
NodeFactoryNeo4j nodeFactoryNeo4j = new NodeFactoryNeo4j(getGraphDb());
StudyImpl study1 = new StudyImpl("some study", "some source", null, "come citation");
study1.setOriginatingDataset(new DatasetImpl("some/namespace", URI.create("some:uri")));
StudyNode someStudy = nodeFactoryNeo4j.getOrCreateStudy(study1);
assertThat(interaction.getOriginatingDataset().getNamespace(), is(someStudy.getOriginatingDataset().getNamespace()));
assertThat(interaction.getTitle(), is(someStudy.getTitle()));
Iterable<Relationship> specimens = NodeUtil.getSpecimens(someStudy);
RelationshipType hasParticipant = NodeUtil.asNeo4j(RelTypes.HAS_PARTICIPANT);
Set<Long> ids = new HashSet<>();
List<Long> idList = new ArrayList<>();
for (Relationship specimen : specimens) {
assertThat(specimen.getEndNode().hasRelationship(Direction.INCOMING, hasParticipant), Is.is(true));
Relationship singleRelationship = specimen.getEndNode().getSingleRelationship(hasParticipant, Direction.INCOMING);
long id = singleRelationship.getStartNode().getId();
ids.add(id);
idList.add(id);
}
assertThat(ids.size(), Is.is(1));
assertThat(idList.size(), Is.is(2));
Node interactionNode = getGraphDb().getNodeById(idList.get(0));
assertTrue(interactionNode.hasRelationship(Direction.OUTGOING, NodeUtil.asNeo4j(RelTypes.DERIVED_FROM)));
assertTrue(interactionNode.hasRelationship(Direction.OUTGOING, NodeUtil.asNeo4j(RelTypes.ACCESSED_AT)));
}
Aggregations