Search in sources :

Example 11 with RelationshipType

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

Example 12 with RelationshipType

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();
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) Collection(java.util.Collection) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Map(java.util.Map)

Example 13 with RelationshipType

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;
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) Collection(java.util.Collection) PathExpanderBuilder(org.neo4j.graphdb.PathExpanderBuilder) Map(java.util.Map)

Example 14 with RelationshipType

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();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) Transaction(org.neo4j.graphdb.Transaction) GraphDatabaseFactory(org.neo4j.graphdb.factory.GraphDatabaseFactory) File(java.io.File) Test(org.junit.Test)

Example 15 with RelationshipType

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)));
}
Also used : Study(org.eol.globi.domain.Study) TaxonImpl(org.eol.globi.domain.TaxonImpl) Taxon(org.eol.globi.domain.Taxon) Node(org.neo4j.graphdb.Node) StudyNode(org.eol.globi.domain.StudyNode) TaxonNode(org.eol.globi.domain.TaxonNode) StudyImpl(org.eol.globi.domain.StudyImpl) RelationshipType(org.neo4j.graphdb.RelationshipType) ArrayList(java.util.ArrayList) DatasetImpl(org.eol.globi.service.DatasetImpl) NodeFactoryNeo4j(org.eol.globi.data.NodeFactoryNeo4j) NodeFactoryWithDatasetContext(org.eol.globi.data.NodeFactoryWithDatasetContext) StudyNode(org.eol.globi.domain.StudyNode) Specimen(org.eol.globi.domain.Specimen) Relationship(org.neo4j.graphdb.Relationship) TaxonIndex(org.eol.globi.data.TaxonIndex) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

RelationshipType (org.neo4j.graphdb.RelationshipType)147 Node (org.neo4j.graphdb.Node)84 Relationship (org.neo4j.graphdb.Relationship)59 Transaction (org.neo4j.graphdb.Transaction)52 Test (org.junit.Test)35 Test (org.junit.jupiter.api.Test)32 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)20 Direction (org.neo4j.graphdb.Direction)19 Label (org.neo4j.graphdb.Label)18 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)15 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)15 ArrayList (java.util.ArrayList)13 NotFoundException (org.neo4j.graphdb.NotFoundException)10 Traverser (org.neo4j.graphdb.Traverser)10 HashSet (java.util.HashSet)9 RelationshipRecord (org.neo4j.kernel.impl.nioneo.store.RelationshipRecord)7 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)7 Collection (java.util.Collection)6 HashMap (java.util.HashMap)6 LinkedList (java.util.LinkedList)6