Search in sources :

Example 16 with BigdataSailRepositoryConnection

use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.

the class BlazegraphMolecularModelManager method loadModelABox.

@Override
public OWLOntology loadModelABox(IRI modelId, OWLOntologyManager manager) throws OWLOntologyCreationException {
    LOG.info("Load model abox: " + modelId + " from database");
    try {
        BigdataSailRepositoryConnection connection = repo.getReadOnlyConnection();
        try {
            // TODO repeated code with loadModel
            RepositoryResult<Resource> graphs = connection.getContextIDs();
            if (!Iterations.asSet(graphs).contains(new URIImpl(modelId.toString()))) {
                throw new OWLOntologyCreationException("No such model in datastore: " + modelId);
            }
            graphs.close();
            RepositoryResult<Statement> statements = connection.getStatements(null, null, null, false, new URIImpl(modelId.toString()));
            // setting minimal to true will give an OWL abox with triples that won't be connected to the tbox, hence e.g. object properties might not be recognized.
            boolean minimal = true;
            OWLOntology abox;
            if (manager == null) {
                abox = loadOntologyDocumentSource(new RioMemoryTripleSource(statements), minimal);
            } else {
                abox = loadOntologyDocumentSource(new RioMemoryTripleSource(statements), minimal, manager);
            }
            statements.close();
            abox = postLoadFileFilter(abox);
            return abox;
        } finally {
            connection.close();
        }
    } catch (RepositoryException e) {
        throw new OWLOntologyCreationException(e);
    }
}
Also used : RioMemoryTripleSource(org.semanticweb.owlapi.rio.RioMemoryTripleSource) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) URIImpl(org.openrdf.model.impl.URIImpl) RepositoryException(org.openrdf.repository.RepositoryException)

Example 17 with BigdataSailRepositoryConnection

use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.

the class BlazegraphMolecularModelManager method getStoredModelIds.

/**
 * Retrieve a collection of all file/stored model ids found in the repo.<br>
 * Note: Models may not be loaded at this point.
 *
 * @return set of modelids.
 * @throws IOException
 */
public Set<IRI> getStoredModelIds() throws IOException {
    try {
        BigdataSailRepositoryConnection connection = repo.getReadOnlyConnection();
        try {
            RepositoryResult<Resource> graphs = connection.getContextIDs();
            Set<IRI> modelIds = new HashSet<>();
            while (graphs.hasNext()) {
                modelIds.add(IRI.create(graphs.next().stringValue()));
            }
            graphs.close();
            return Collections.unmodifiableSet(modelIds);
        } finally {
            connection.close();
        }
    } catch (RepositoryException e) {
        throw new IOException(e);
    }
}
Also used : BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) RepositoryException(org.openrdf.repository.RepositoryException)

Example 18 with BigdataSailRepositoryConnection

use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.

the class BlazegraphMolecularModelManager method addTaxonToDatabaseWithSparql.

// now try with sparql insert
public int addTaxonToDatabaseWithSparql(IRI model_iri, IRI taxon_iri) throws RepositoryException, UpdateExecutionException, MalformedQueryException, InterruptedException {
    int changes = 0;
    String update = "INSERT DATA\n" + "{ GRAPH <" + model_iri.toString() + "> { " + "  <" + model_iri.toString() + "> <" + BlazegraphOntologyManager.in_taxon_uri + "> <" + taxon_iri.toString() + ">" + "} }";
    synchronized (repo) {
        final BigdataSailRepositoryConnection conn = repo.getUnisolatedConnection();
        try {
            conn.begin();
            BlazegraphMutationCounter counter = new BlazegraphMutationCounter();
            conn.addChangeLog(counter);
            conn.prepareUpdate(QueryLanguage.SPARQL, update).execute();
            changes = counter.mutationCount();
            conn.removeChangeLog(counter);
            conn.commit();
        } finally {
            conn.close();
        }
    }
    return changes;
}
Also used : BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) BlazegraphMutationCounter(org.geneontology.minerva.util.BlazegraphMutationCounter)

Example 19 with BigdataSailRepositoryConnection

use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.

the class BlazegraphMolecularModelManager method writeModelToDatabase.

private void writeModelToDatabase(OWLOntology model, IRI modelId) throws RepositoryException, IOException {
    // Only one thread at a time can use the unisolated connection.
    synchronized (repo) {
        final BigdataSailRepositoryConnection connection = repo.getUnisolatedConnection();
        try {
            connection.begin();
            try {
                URI graph = new URIImpl(modelId.toString());
                connection.clear(graph);
                StatementCollector collector = new StatementCollector();
                RioRenderer renderer = new RioRenderer(model, collector, null);
                renderer.render();
                connection.add(collector.getStatements(), graph);
                connection.commit();
            } catch (Exception e) {
                connection.rollback();
                throw e;
            }
        } finally {
            connection.close();
        }
    }
}
Also used : RioRenderer(org.semanticweb.owlapi.rio.RioRenderer) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) StatementCollector(org.openrdf.rio.helpers.StatementCollector) URIImpl(org.openrdf.model.impl.URIImpl) RepositoryException(org.openrdf.repository.RepositoryException) UnknownIdentifierException(org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException)

Example 20 with BigdataSailRepositoryConnection

use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.

the class BlazegraphMolecularModelManager method importModelToDatabase.

/**
 * Imports ontology RDF directly to database. Will remove any import statements in the ontology (because GO-CAMs should not have any as of now)
 *
 * @param file
 * @throws OWLOntologyCreationException
 * @throws IOException
 * @throws RepositoryException
 */
public String importModelToDatabase(File file, boolean skipMarkedDelete) throws OWLOntologyCreationException, RepositoryException, IOException, RDFParseException, RDFHandlerException {
    final boolean delete;
    if (skipMarkedDelete) {
        delete = scanForIsDelete(file);
    } else {
        delete = false;
    }
    String modeliri = null;
    if (!delete) {
        java.util.Optional<URI> ontIRIOpt = scanForOntologyIRI(file).map(id -> new URIImpl(id));
        if (ontIRIOpt.isPresent()) {
            java.util.Optional<URI> importOpt = scanForImport(file).map(id -> new URIImpl(id));
            if (importOpt.isPresent()) {
                modeliri = ontIRIOpt.get().stringValue();
                // need to remove the imports before loading.
                // if the imports are large, this gets slow
                // consider 1) loading the model as below 2) running a SPARQL update to get rid of the imports
                OWLOntologyManager ontman = OWLManager.createOWLOntologyManager();
                OWLOntology cam = ontman.loadOntologyFromOntologyDocument(file);
                Set<OWLImportsDeclaration> imports = cam.getImportsDeclarations();
                for (OWLImportsDeclaration impdec : imports) {
                    RemoveImport rm = new RemoveImport(cam, impdec);
                    ontman.applyChange(rm);
                }
                // write it
                this.writeModelToDatabase(cam, IRI.create(ontIRIOpt.get().stringValue()));
            } else {
                // otherwise just load it all up as rdf (faster because avoids owl api)
                synchronized (repo) {
                    final BigdataSailRepositoryConnection connection = repo.getUnisolatedConnection();
                    try {
                        connection.begin();
                        try {
                            URI graph = ontIRIOpt.get();
                            connection.clear(graph);
                            // FIXME Turtle format is hard-coded here
                            if (file.getName().endsWith(".ttl")) {
                                connection.add(file, "", RDFFormat.TURTLE, graph);
                            } else if (file.getName().endsWith(".owl")) {
                                connection.add(file, "", RDFFormat.RDFXML, graph);
                            }
                            connection.commit();
                            modeliri = graph.toString();
                        } catch (Exception e) {
                            connection.rollback();
                            throw e;
                        }
                    } finally {
                        connection.close();
                    }
                }
            }
        } else {
            throw new OWLOntologyCreationException("Detected anonymous ontology; must have IRI");
        }
    } else {
        System.err.println("skipping " + file.getName());
    }
    return modeliri;
}
Also used : URIImpl(org.openrdf.model.impl.URIImpl) RepositoryException(org.openrdf.repository.RepositoryException) UnknownIdentifierException(org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException) java.util(java.util) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection)

Aggregations

BigdataSailRepositoryConnection (com.bigdata.rdf.sail.BigdataSailRepositoryConnection)28 RepositoryException (org.openrdf.repository.RepositoryException)25 IOException (java.io.IOException)15 Value (org.openrdf.model.Value)12 URI (org.openrdf.model.URI)9 URIImpl (org.openrdf.model.impl.URIImpl)9 UnknownIdentifierException (org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException)4 BlazegraphMutationCounter (org.geneontology.minerva.util.BlazegraphMutationCounter)4 StatementCollector (org.openrdf.rio.helpers.StatementCollector)4 RioMemoryTripleSource (org.semanticweb.owlapi.rio.RioMemoryTripleSource)4 RioRenderer (org.semanticweb.owlapi.rio.RioRenderer)4 BigdataSail (com.bigdata.rdf.sail.BigdataSail)3 BigdataSailRepository (com.bigdata.rdf.sail.BigdataSailRepository)3 java.util (java.util)3 Options (com.bigdata.journal.Options)2 Iterations (info.aduna.iteration.Iterations)2 java.io (java.io)2 Entry (java.util.Map.Entry)2 Collectors (java.util.stream.Collectors)2 FileUtils (org.apache.commons.io.FileUtils)2