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