Search in sources :

Example 1 with BigdataSailRepositoryConnection

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);
    }
}
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 2 with BigdataSailRepositoryConnection

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();
    }
}
Also used : BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection)

Example 3 with BigdataSailRepositoryConnection

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();
    }
}
Also used : Iterations(info.aduna.iteration.Iterations) URIImpl(org.openrdf.model.impl.URIImpl) java.util(java.util) RioRenderer(org.semanticweb.owlapi.rio.RioRenderer) org.semanticweb.owlapi.model(org.semanticweb.owlapi.model) BlazegraphMutationCounter(org.geneontology.minerva.util.BlazegraphMutationCounter) RDFHandlerBase(org.openrdf.rio.helpers.RDFHandlerBase) StringUtils(org.apache.commons.lang3.StringUtils) OWLManager(org.semanticweb.owlapi.apibinding.OWLManager) Logger(org.apache.log4j.Logger) RepositoryException(org.openrdf.repository.RepositoryException) UnknownIdentifierException(org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException) RepositoryResult(org.openrdf.repository.RepositoryResult) RDF(org.openrdf.model.vocabulary.RDF) org.openrdf.query(org.openrdf.query) ReverseChangeGenerator(org.geneontology.minerva.util.ReverseChangeGenerator) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) Options(com.bigdata.journal.Options) FileUtils(org.apache.commons.io.FileUtils) OWL(org.openrdf.model.vocabulary.OWL) AnnotationShorthand(org.geneontology.minerva.util.AnnotationShorthand) Collectors(java.util.stream.Collectors) QueryPrologLexer(org.openrdf.query.parser.QueryPrologLexer) BigdataSailRepository(com.bigdata.rdf.sail.BigdataSailRepository) BigdataSail(com.bigdata.rdf.sail.BigdataSail) java.io(java.io) CurieHandler(org.geneontology.minerva.curie.CurieHandler) org.openrdf.model(org.openrdf.model) Entry(java.util.Map.Entry) org.semanticweb.owlapi.formats(org.semanticweb.owlapi.formats) StatementCollector(org.openrdf.rio.helpers.StatementCollector) RioMemoryTripleSource(org.semanticweb.owlapi.rio.RioMemoryTripleSource) org.openrdf.rio(org.openrdf.rio) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection)

Example 4 with BigdataSailRepositoryConnection

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;
}
Also used : Iterations(info.aduna.iteration.Iterations) URIImpl(org.openrdf.model.impl.URIImpl) java.util(java.util) RioRenderer(org.semanticweb.owlapi.rio.RioRenderer) org.semanticweb.owlapi.model(org.semanticweb.owlapi.model) BlazegraphMutationCounter(org.geneontology.minerva.util.BlazegraphMutationCounter) RDFHandlerBase(org.openrdf.rio.helpers.RDFHandlerBase) StringUtils(org.apache.commons.lang3.StringUtils) OWLManager(org.semanticweb.owlapi.apibinding.OWLManager) Logger(org.apache.log4j.Logger) RepositoryException(org.openrdf.repository.RepositoryException) UnknownIdentifierException(org.geneontology.minerva.MolecularModelManager.UnknownIdentifierException) RepositoryResult(org.openrdf.repository.RepositoryResult) RDF(org.openrdf.model.vocabulary.RDF) org.openrdf.query(org.openrdf.query) ReverseChangeGenerator(org.geneontology.minerva.util.ReverseChangeGenerator) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) Options(com.bigdata.journal.Options) FileUtils(org.apache.commons.io.FileUtils) OWL(org.openrdf.model.vocabulary.OWL) AnnotationShorthand(org.geneontology.minerva.util.AnnotationShorthand) Collectors(java.util.stream.Collectors) QueryPrologLexer(org.openrdf.query.parser.QueryPrologLexer) BigdataSailRepository(com.bigdata.rdf.sail.BigdataSailRepository) BigdataSail(com.bigdata.rdf.sail.BigdataSail) java.io(java.io) CurieHandler(org.geneontology.minerva.curie.CurieHandler) org.openrdf.model(org.openrdf.model) Entry(java.util.Map.Entry) org.semanticweb.owlapi.formats(org.semanticweb.owlapi.formats) StatementCollector(org.openrdf.rio.helpers.StatementCollector) RioMemoryTripleSource(org.semanticweb.owlapi.rio.RioMemoryTripleSource) org.openrdf.rio(org.openrdf.rio) RepositoryException(org.openrdf.repository.RepositoryException) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection)

Example 5 with BigdataSailRepositoryConnection

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);
    }
}
Also used : RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) 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