Search in sources :

Example 1 with NodeFactoryNeo4j2

use of org.eol.globi.data.NodeFactoryNeo4j2 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/globalbioticinteractions/globalbioticinteractions/wiki/Nanopubs
    StudyImpl study = new StudyImpl("some study", new DOI("123.23", "222"), "some study citation");
    NodeFactoryWithDatasetContext factory = new NodeFactoryWithDatasetContext(nodeFactory, new DatasetImpl("some/namespace", URI.create("https://some.uri"), inStream -> inStream));
    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(new GraphServiceFactoryProxy(getGraphDb())).index();
    NodeFactoryNeo4j nodeFactoryNeo4j = new NodeFactoryNeo4j2(getGraphDb());
    StudyImpl study1 = new StudyImpl("some study", new DOI("123.23", "222"), "come citation");
    study1.setOriginatingDataset(new DatasetImpl("some/namespace", URI.create("some:uri"), inStream -> inStream));
    StudyNode someStudy = nodeFactoryNeo4j.getOrCreateStudy(study1);
    assertThat(interaction.getOriginatingDataset().getNamespace(), is(someStudy.getOriginatingDataset().getNamespace()));
    assertThat(interaction.getTitle(), is(someStudy.getTitle()));
    RelationshipType hasParticipant = NodeUtil.asNeo4j(RelTypes.HAS_PARTICIPANT);
    Set<Long> ids = new HashSet<>();
    List<Long> idList = new ArrayList<>();
    NodeUtil.handleCollectedRelationships(new NodeTypeDirection(someStudy.getUnderlyingNode()), new RelationshipListener() {

        @Override
        public void on(Relationship specimen) {
            assertThat(specimen.getEndNode().hasRelationship(Direction.INCOMING, hasParticipant), Is.is(true));
            Iterable<Relationship> relationships = specimen.getEndNode().getRelationships(hasParticipant, Direction.INCOMING);
            for (Relationship relationship : relationships) {
                long id = relationship.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 : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TaxonIndex(org.eol.globi.data.TaxonIndex) NodeUtil(org.eol.globi.util.NodeUtil) DatasetImpl(org.globalbioticinteractions.dataset.DatasetImpl) Specimen(org.eol.globi.domain.Specimen) Direction(org.neo4j.graphdb.Direction) RelTypes(org.eol.globi.domain.RelTypes) StudyImpl(org.eol.globi.domain.StudyImpl) Node(org.neo4j.graphdb.Node) Is(org.hamcrest.core.Is) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TaxonImpl(org.eol.globi.domain.TaxonImpl) RelationshipListener(org.eol.globi.util.RelationshipListener) DOI(org.globalbioticinteractions.doi.DOI) URI(java.net.URI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) GraphDBTestCase(org.eol.globi.data.GraphDBTestCase) Taxon(org.eol.globi.domain.Taxon) NodeFactoryNeo4j2(org.eol.globi.data.NodeFactoryNeo4j2) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StudyNode(org.eol.globi.domain.StudyNode) NodeTypeDirection(org.eol.globi.util.NodeTypeDirection) GraphServiceFactoryProxy(org.eol.globi.db.GraphServiceFactoryProxy) NodeFactoryNeo4j(org.eol.globi.data.NodeFactoryNeo4j) List(java.util.List) Relationship(org.neo4j.graphdb.Relationship) NodeFactoryException(org.eol.globi.data.NodeFactoryException) RelationshipType(org.neo4j.graphdb.RelationshipType) Study(org.eol.globi.domain.Study) NodeFactoryWithDatasetContext(org.eol.globi.data.NodeFactoryWithDatasetContext) TaxonNode(org.eol.globi.domain.TaxonNode) 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) NodeFactoryNeo4j(org.eol.globi.data.NodeFactoryNeo4j) RelationshipListener(org.eol.globi.util.RelationshipListener) Specimen(org.eol.globi.domain.Specimen) TaxonIndex(org.eol.globi.data.TaxonIndex) DOI(org.globalbioticinteractions.doi.DOI) HashSet(java.util.HashSet) Study(org.eol.globi.domain.Study) NodeTypeDirection(org.eol.globi.util.NodeTypeDirection) TaxonImpl(org.eol.globi.domain.TaxonImpl) Taxon(org.eol.globi.domain.Taxon) GraphServiceFactoryProxy(org.eol.globi.db.GraphServiceFactoryProxy) DatasetImpl(org.globalbioticinteractions.dataset.DatasetImpl) NodeFactoryWithDatasetContext(org.eol.globi.data.NodeFactoryWithDatasetContext) StudyNode(org.eol.globi.domain.StudyNode) Relationship(org.neo4j.graphdb.Relationship) NodeFactoryNeo4j2(org.eol.globi.data.NodeFactoryNeo4j2) Test(org.junit.Test)

Example 2 with NodeFactoryNeo4j2

use of org.eol.globi.data.NodeFactoryNeo4j2 in project eol-globi-data by jhpoelen.

the class NodeFactoryFactoryTransactingOnDatasetNeo4j2 method create.

@Override
public NodeFactory create(GraphDatabaseService service) {
    GraphDatabaseService graphService = graphServiceFactory.getGraphService();
    try (Transaction tx = graphService.beginTx()) {
        NodeFactory nodeFactory = new NodeFactoryNeo4j2(graphService) {

            final AtomicReference<Transaction> tx = new AtomicReference<>();

            final AtomicBoolean closing = new AtomicBoolean(false);

            final AtomicLong counter = new AtomicLong(0);

            @Override
            public Dataset getOrCreateDataset(Dataset dataset) {
                if (closing.get()) {
                    throw new IllegalStateException("cannot create a dataset on closing node factory");
                } else {
                    startBatchTransactionIfNeeded();
                }
                return super.getOrCreateDataset(dataset);
            }

            void startBatchTransactionIfNeeded() {
                tx.getAndUpdate(transaction -> {
                    if (counter.getAndIncrement() % TRANSACTION_BATCH_SIZE_DEFAULT == 0) {
                        if (transaction != null) {
                            transaction.success();
                            transaction.close();
                            transaction = null;
                        }
                    }
                    return transaction == null ? beginTx() : transaction;
                });
            }

            private Transaction beginTx() {
                return graphServiceFactory.getGraphService().beginTx();
            }

            @Override
            public SpecimenNode createSpecimen(Study study, Taxon taxon, RelTypes... types) throws NodeFactoryException {
                startBatchTransactionIfNeeded();
                return super.createSpecimen(study, taxon, types);
            }

            @Override
            public void close() {
                tx.getAndUpdate(tx -> {
                    closing.set(true);
                    if (tx != null) {
                        tx.success();
                        tx.close();
                    }
                    return null;
                });
            }
        };
        tx.success();
        return nodeFactory;
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Study(org.eol.globi.domain.Study) Dataset(org.globalbioticinteractions.dataset.Dataset) Taxon(org.eol.globi.domain.Taxon) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.neo4j.graphdb.Transaction) NodeFactory(org.eol.globi.data.NodeFactory) RelTypes(org.eol.globi.domain.RelTypes) NodeFactoryNeo4j2(org.eol.globi.data.NodeFactoryNeo4j2)

Example 3 with NodeFactoryNeo4j2

use of org.eol.globi.data.NodeFactoryNeo4j2 in project eol-globi-data by jhpoelen.

the class CypherTestUtil method validate.

public static void validate(CypherQuery cypherQuery, GraphDatabaseService graphDatabaseService) {
    try (Transaction tx = graphDatabaseService.beginTx()) {
        new NodeFactoryNeo4j2(graphDatabaseService);
        new NonResolvingTaxonIndex(graphDatabaseService);
        new LinkerTaxonIndex(new GraphServiceFactoryProxy(graphDatabaseService)).index();
        CacheService cacheService = new CacheService();
        File cacheDir = new File("target/reportGeneration" + UUID.randomUUID());
        cacheService.setCacheDir(cacheDir);
        CmdGenerateReport reportGenerator = new CmdGenerateReport(graphDatabaseService, cacheService);
        reportGenerator.run(NOPLogger.NOP_LOGGER);
        Map<String, Object> params = cypherQuery.getParams() == null ? Collections.emptyMap() : new HashMap<>(cypherQuery.getParams());
        try {
            graphDatabaseService.execute(cypherQuery.getVersionedQuery(), params);
        } catch (NullPointerException ex) {
            // encountered nullpointer exceptions were caused by initialization of graph database
            throw ex;
        } catch (RuntimeException ex) {
            // work fine in cypher query, but cause parse exception when running programatically
            if (!ex.getMessage().contains("Encountered \" \":\" \": \"\"")) {
                throw ex;
            }
        } finally {
            FileUtils.deleteQuietly(cacheDir);
        }
    }
}
Also used : NonResolvingTaxonIndex(org.eol.globi.taxon.NonResolvingTaxonIndex) GraphServiceFactoryProxy(org.eol.globi.db.GraphServiceFactoryProxy) LinkerTaxonIndex(org.eol.globi.tool.LinkerTaxonIndex) Transaction(org.neo4j.graphdb.Transaction) CmdGenerateReport(org.eol.globi.tool.CmdGenerateReport) File(java.io.File) NodeFactoryNeo4j2(org.eol.globi.data.NodeFactoryNeo4j2) CacheService(org.eol.globi.service.CacheService)

Example 4 with NodeFactoryNeo4j2

use of org.eol.globi.data.NodeFactoryNeo4j2 in project eol-globi-data by jhpoelen.

the class NodeFactoryTest method createNeo4j2.

private NodeFactory createNeo4j2(GraphDatabaseService graphDb) {
    NodeFactory factory;
    try (Transaction tx = getGraphDb().beginTx()) {
        factory = new NodeFactoryNeo4j2(graphDb);
        tx.success();
    }
    return factory;
}
Also used : NodeFactory(org.eol.globi.data.NodeFactory) Transaction(org.neo4j.graphdb.Transaction) NodeFactoryNeo4j2(org.eol.globi.data.NodeFactoryNeo4j2)

Aggregations

NodeFactoryNeo4j2 (org.eol.globi.data.NodeFactoryNeo4j2)4 Transaction (org.neo4j.graphdb.Transaction)3 NodeFactory (org.eol.globi.data.NodeFactory)2 GraphServiceFactoryProxy (org.eol.globi.db.GraphServiceFactoryProxy)2 RelTypes (org.eol.globi.domain.RelTypes)2 Study (org.eol.globi.domain.Study)2 Taxon (org.eol.globi.domain.Taxon)2 File (java.io.File)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 GraphDBTestCase (org.eol.globi.data.GraphDBTestCase)1 NodeFactoryException (org.eol.globi.data.NodeFactoryException)1 NodeFactoryNeo4j (org.eol.globi.data.NodeFactoryNeo4j)1 NodeFactoryWithDatasetContext (org.eol.globi.data.NodeFactoryWithDatasetContext)1