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