use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.
the class BlazegraphMolecularModelManager method loadModel.
@Override
public void loadModel(IRI modelId, boolean isOverride) throws OWLOntologyCreationException {
if (modelMap.containsKey(modelId)) {
if (!isOverride) {
throw new OWLOntologyCreationException("Model already exists: " + modelId);
}
unlinkModel(modelId);
}
try {
BigdataSailRepositoryConnection connection = repo.getReadOnlyConnection();
try {
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 = false will load the abox with the tbox ontology manager, allowing for OWL understanding of tbox content
boolean minimal = false;
OWLOntology abox = loadOntologyDocumentSource(new RioMemoryTripleSource(statements), minimal);
statements.close();
abox = postLoadFileFilter(abox);
ModelContainer model = addModel(modelId, 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 executeSPARQLQueryWithoutPrefixManipulation.
public QueryResult executeSPARQLQueryWithoutPrefixManipulation(String queryText, int timeout) throws MalformedQueryException, QueryEvaluationException, RepositoryException {
BigdataSailRepositoryConnection connection = repo.getReadOnlyConnection();
try {
Query query = connection.prepareQuery(QueryLanguage.SPARQL, queryText.toString());
query.setMaxQueryTime(timeout);
if (query instanceof TupleQuery) {
TupleQuery tupleQuery = (TupleQuery) query;
return tupleQuery.evaluate();
} else if (query instanceof GraphQuery) {
GraphQuery graphQuery = (GraphQuery) query;
return graphQuery.evaluate();
} else if (query instanceof BooleanQuery) {
// FIXME
throw new UnsupportedOperationException("Unsupported query type.");
} else {
throw new UnsupportedOperationException("Unsupported query type.");
}
} finally {
connection.close();
}
}
use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.
the class BlazegraphMolecularModelManager method executeSPARQLQuery.
public QueryResult executeSPARQLQuery(String queryText, int timeout) throws MalformedQueryException, QueryEvaluationException, RepositoryException {
BigdataSailRepositoryConnection connection = repo.getReadOnlyConnection();
try {
List<QueryPrologLexer.Token> tokens = QueryPrologLexer.lex(queryText);
Set<String> declaredPrefixes = tokens.stream().filter(token -> token.getType().equals(QueryPrologLexer.TokenType.PREFIX)).map(token -> token.getStringValue()).collect(Collectors.toSet());
StringBuffer queryWithDefaultPrefixes = new StringBuffer();
for (Entry<String, String> entry : getCuriHandler().getMappings().entrySet()) {
if (!declaredPrefixes.contains(entry.getKey())) {
queryWithDefaultPrefixes.append("PREFIX " + entry.getKey() + ": <" + entry.getValue() + ">");
queryWithDefaultPrefixes.append("\n");
}
}
queryWithDefaultPrefixes.append(queryText);
Query query = connection.prepareQuery(QueryLanguage.SPARQL, queryWithDefaultPrefixes.toString());
query.setMaxQueryTime(timeout);
if (query instanceof TupleQuery) {
TupleQuery tupleQuery = (TupleQuery) query;
return tupleQuery.evaluate();
} else if (query instanceof GraphQuery) {
GraphQuery graphQuery = (GraphQuery) query;
return graphQuery.evaluate();
} else if (query instanceof BooleanQuery) {
// FIXME
throw new UnsupportedOperationException("Unsupported query type.");
} else {
throw new UnsupportedOperationException("Unsupported query type.");
}
} finally {
connection.close();
}
}
use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.
the class BlazegraphMolecularModelManager method getAllModelAnnotations.
public Map<IRI, Set<OWLAnnotation>> getAllModelAnnotations() throws IOException {
Map<IRI, Set<OWLAnnotation>> annotations = new HashMap<>();
// First get annotations from all the stored ontologies
try {
BigdataSailRepositoryConnection connection = repo.getReadOnlyConnection();
try {
String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "SELECT ?model ?p ?o " + "WHERE { " + "?model a owl:Ontology . " + "?model ?p ?o . " + "FILTER(?p NOT IN (owl:imports, rdf:type, <http://geneontology.org/lego/json-model>)) " + "} ";
TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
TupleQueryResult result = tupleQuery.evaluate();
OWLDataFactory factory = OWLManager.getOWLDataFactory();
while (result.hasNext()) {
BindingSet binding = result.next();
Value model = binding.getValue("model");
Value predicate = binding.getValue("p");
String value = binding.getValue("o").stringValue();
if ((model instanceof URI) && (predicate instanceof URI)) {
IRI modelId = IRI.create(((URI) model).toString());
OWLAnnotationProperty property = factory.getOWLAnnotationProperty(IRI.create(((URI) predicate).toString()));
OWLAnnotation annotation = factory.getOWLAnnotation(property, factory.getOWLLiteral(value));
Set<OWLAnnotation> modelAnnotations = annotations.getOrDefault(modelId, new HashSet<>());
modelAnnotations.add(annotation);
annotations.put(modelId, modelAnnotations);
}
}
} catch (MalformedQueryException e) {
throw new IOException(e);
} catch (QueryEvaluationException e) {
throw new IOException(e);
} finally {
connection.close();
}
} catch (RepositoryException e) {
throw new IOException(e);
}
// Next get annotations from ontologies that may not be stored, replacing any stored annotations
modelMap.values().stream().filter(mc -> mc.isModified()).forEach(mc -> {
annotations.put(mc.getModelId(), mc.getAboxOntology().getAnnotations());
});
return annotations;
}
use of com.bigdata.rdf.sail.BigdataSailRepositoryConnection in project minerva by geneontology.
the class BlazegraphOntologyManager method buildRegulationMap.
private Map<IRI, Set<IRI>> buildRegulationMap() throws IOException {
String regulationTargetsQuery = IOUtils.toString(BlazegraphOntologyManager.class.getResourceAsStream("regulation_targets.rq"), StandardCharsets.UTF_8);
try {
BigdataSailRepositoryConnection connection = go_lego_repo.getReadOnlyConnection();
try {
TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, regulationTargetsQuery);
TupleQueryResult result = tupleQuery.evaluate();
Map<IRI, Set<IRI>> regulators = new HashMap<>();
while (result.hasNext()) {
BindingSet binding = result.next();
IRI regulator = IRI.create(binding.getValue("subjectDown").stringValue());
IRI regulated = IRI.create(binding.getValue("fillerUp").stringValue());
if (!regulators.containsKey(regulator)) {
regulators.put(regulator, new HashSet<>());
}
regulators.get(regulator).add(regulated);
}
return regulators;
} catch (MalformedQueryException e) {
throw new IOException(e);
} catch (QueryEvaluationException e) {
throw new IOException(e);
} finally {
connection.close();
}
} catch (RepositoryException e) {
throw new IOException(e);
}
}
Aggregations