Search in sources :

Example 1 with NodeFactory

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

the class IndexerDataset method index.

@Override
public void index() throws StudyImporterException {
    GraphDatabaseService graphService = graphServiceFactory.getGraphService();
    NodeFactory nodeFactory;
    try (Transaction tx = graphService.beginTx()) {
        nodeFactory = nodeFactoryFactory.create(graphService);
        tx.success();
    }
    try {
        indexDatasets(this.registry, nodeFactory);
    } finally {
        if (nodeFactory != null) {
            try {
                nodeFactory.close();
            } catch (Exception e) {
                throw new StudyImporterException("failed to gracefully end index process", e);
            }
        }
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) NodeFactory(org.eol.globi.data.NodeFactory) Transaction(org.neo4j.graphdb.Transaction) StudyImporterException(org.eol.globi.data.StudyImporterException) DatasetRegistryException(org.globalbioticinteractions.dataset.DatasetRegistryException) StudyImporterException(org.eol.globi.data.StudyImporterException)

Example 2 with NodeFactory

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

the class ExportUnmatchedTaxonNamesTest method exportOnePredatorTwoPrey.

@Test
public void exportOnePredatorTwoPrey() throws NodeFactoryException, IOException {
    taxonIndex = ExportTestUtil.taxonIndexWithEnricher(null, getGraphDb());
    String title = "my study\"";
    String citation = "citation my study";
    NodeFactory nodeFactory = nodeFactoryWithDataset();
    StudyNode study = (StudyNode) nodeFactory.getOrCreateStudy(new StudyImpl(title, null, citation));
    taxonIndex.getOrCreateTaxon(new TaxonImpl("Homo sapiens", null));
    Specimen predatorSpecimen = nodeFactory.createSpecimen(study, human());
    taxonIndex.getOrCreateTaxon(new TaxonImpl("Canis lupus", null));
    Specimen preySpecimen6 = nodeFactory.createSpecimen(study, dog());
    predatorSpecimen.interactsWith(preySpecimen6, InteractType.ATE);
    Specimen preySpecimen5 = nodeFactory.createSpecimen(study, dog());
    predatorSpecimen.interactsWith(preySpecimen5, InteractType.ATE);
    Specimen preySpecimen = nodeFactory.createSpecimen(study, new TaxonImpl("Caniz", null));
    predatorSpecimen.ate(preySpecimen);
    Specimen predatorSpecimen23 = nodeFactory.createSpecimen(study, new TaxonImpl("Homo sapiens2", null));
    Specimen preySpecimen4 = nodeFactory.createSpecimen(study, dog());
    predatorSpecimen23.interactsWith(preySpecimen4, InteractType.ATE);
    Specimen predatorSpecimen22 = nodeFactory.createSpecimen(study, new TaxonImpl("Homo sapiens2", null));
    Specimen preySpecimen3 = nodeFactory.createSpecimen(study, dog());
    predatorSpecimen22.interactsWith(preySpecimen3, InteractType.ATE);
    Study study2 = nodeFactory.getOrCreateStudy(new StudyImpl("my study2", null, "citation study2"));
    Specimen predatorSpecimen21 = nodeFactory.createSpecimen(study2, new TaxonImpl("Homo sapiens2", null));
    Specimen preySpecimen2 = nodeFactory.createSpecimen(study2, dog());
    predatorSpecimen21.interactsWith(preySpecimen2, InteractType.ATE);
    Specimen predatorSpecimen2 = nodeFactory.createSpecimen(study, new TaxonImpl("Homo sapiens3", PropertyAndValueDictionary.NO_MATCH));
    Specimen preySpecimen1 = nodeFactory.createSpecimen(study, dog());
    predatorSpecimen2.interactsWith(preySpecimen1, InteractType.ATE);
    resolveNames();
    StringWriter writer = new StringWriter();
    new ExportUnmatchedTaxonNames().exportStudy(study, ExportUtil.AppenderWriter.of(writer), true);
    String actual = writer.toString();
    assertThat(actual, startsWith("unmatched taxon name\tunmatched taxon id\tname status\tsimilar to taxon name\tsimilar to taxon path\tsimilar to taxon id\tstudy\tsource"));
    assertThat(actual, containsString("\nCaniz\t\t\t\t\t\tcitation my study\t<some:archive>"));
    assertThat(actual, containsString("\nHomo sapiens2\t\t\t\t\t\tcitation my study\t<some:archive>"));
    assertThat(actual, containsString("\nHomo sapiens3\tno:match\t\t\t\t\tcitation my study\t<some:archive>"));
}
Also used : Specimen(org.eol.globi.domain.Specimen) Study(org.eol.globi.domain.Study) NodeFactory(org.eol.globi.data.NodeFactory) StringWriter(java.io.StringWriter) TaxonImpl(org.eol.globi.domain.TaxonImpl) StudyImpl(org.eol.globi.domain.StudyImpl) Matchers.containsString(org.hamcrest.Matchers.containsString) StudyNode(org.eol.globi.domain.StudyNode) Test(org.junit.Test)

Example 3 with NodeFactory

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

the class NodeFactoryFactoryTransactingOnDatasetNeo4j3 method create.

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

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

            final AtomicBoolean closing = new AtomicBoolean(false);

            @Override
            public Dataset getOrCreateDataset(Dataset dataset) {
                if (closing.get()) {
                    throw new IllegalStateException("cannot create a dataset on closing node factory");
                } else {
                    Transaction transaction = tx.get();
                    if (transaction != null) {
                        transaction.success();
                        transaction.close();
                    }
                    tx.set(graphServiceFactory.getGraphService().beginTx());
                }
                return super.getOrCreateDataset(dataset);
            }

            @Override
            public void close() {
                closing.set(true);
                Transaction lastTx = tx.getAndSet(null);
                if (lastTx != null) {
                    lastTx.success();
                    lastTx.close();
                }
            }
        };
        tx.success();
        return nodeFactory;
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.neo4j.graphdb.Transaction) NodeFactory(org.eol.globi.data.NodeFactory) Dataset(org.globalbioticinteractions.dataset.Dataset) AtomicReference(java.util.concurrent.atomic.AtomicReference) NodeFactoryNeo4j3(org.eol.globi.data.NodeFactoryNeo4j3)

Example 4 with NodeFactory

use of org.eol.globi.data.NodeFactory 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 5 with NodeFactory

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

the class IndexerDataset method indexDatasets.

private static void indexDatasets(DatasetRegistry registry, NodeFactory nodeFactory) {
    try {
        final Collection<String> namespaces = registry.findNamespaces();
        String namespacelist = StringUtils.join(namespaces, CharsetConstant.SEPARATOR);
        LOG.info("found dataset namespaces: {" + namespacelist + "}");
        DatasetImporterForRegistry importer = new DatasetImporterForRegistry(new ParserFactoryLocal(), nodeFactory, registry);
        importer.setDatasetFilter(x -> !DatasetUtil.isDeprecated(x));
        importer.setDataset(new DatasetLocal(inStream -> inStream));
        importer.setLogger(new NullImportLogger());
        importer.importStudy();
    } catch (DatasetRegistryException | StudyImporterException e) {
        LOG.error("problem encountered while importing [" + DatasetImporterForRegistry.class.getName() + "]", e);
    }
}
Also used : DatasetRegistryException(org.globalbioticinteractions.dataset.DatasetRegistryException) Logger(org.slf4j.Logger) Collection(java.util.Collection) NodeFactory(org.eol.globi.data.NodeFactory) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) DatasetLocal(org.eol.globi.service.DatasetLocal) StudyImporterException(org.eol.globi.data.StudyImporterException) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) ParserFactoryLocal(org.eol.globi.data.ParserFactoryLocal) GraphServiceFactory(org.eol.globi.db.GraphServiceFactory) DatasetImporterForRegistry(org.eol.globi.data.DatasetImporterForRegistry) DatasetUtil(org.globalbioticinteractions.dataset.DatasetUtil) Transaction(org.neo4j.graphdb.Transaction) CharsetConstant(org.eol.globi.data.CharsetConstant) DatasetRegistry(org.globalbioticinteractions.dataset.DatasetRegistry) DatasetImporterForRegistry(org.eol.globi.data.DatasetImporterForRegistry) DatasetRegistryException(org.globalbioticinteractions.dataset.DatasetRegistryException) StudyImporterException(org.eol.globi.data.StudyImporterException) ParserFactoryLocal(org.eol.globi.data.ParserFactoryLocal) DatasetLocal(org.eol.globi.service.DatasetLocal)

Aggregations

NodeFactory (org.eol.globi.data.NodeFactory)9 Transaction (org.neo4j.graphdb.Transaction)6 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)5 Test (org.junit.Test)3 StringWriter (java.io.StringWriter)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 NodeFactoryNeo4j2 (org.eol.globi.data.NodeFactoryNeo4j2)2 StudyImporterException (org.eol.globi.data.StudyImporterException)2 Specimen (org.eol.globi.domain.Specimen)2 Study (org.eol.globi.domain.Study)2 StudyImpl (org.eol.globi.domain.StudyImpl)2 StudyNode (org.eol.globi.domain.StudyNode)2 Taxon (org.eol.globi.domain.Taxon)2 TaxonImpl (org.eol.globi.domain.TaxonImpl)2 Dataset (org.globalbioticinteractions.dataset.Dataset)2 DatasetRegistryException (org.globalbioticinteractions.dataset.DatasetRegistryException)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Collection (java.util.Collection)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1