Search in sources :

Example 6 with BigdataSailRepositoryConnection

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

the class BlazegraphOntologyManager method exists.

public boolean exists(String entity) throws IOException {
    boolean exists = false;
    String query = "select * " + "WHERE {" + "{<" + entity + "> ?p ?o . } " + "UNION " + "{?s ?p <" + entity + "> . }" + "} limit 1";
    try {
        BigdataSailRepositoryConnection connection = go_lego_repo.getReadOnlyConnection();
        try {
            TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
            TupleQueryResult result = tupleQuery.evaluate();
            if (result.hasNext()) {
                exists = true;
                return exists;
            }
        } catch (MalformedQueryException e) {
            throw new IOException(e);
        } catch (QueryEvaluationException e) {
            throw new IOException(e);
        } finally {
            connection.close();
        }
    } catch (RepositoryException e) {
        throw new IOException(e);
    }
    return exists;
}
Also used : BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException)

Example 7 with BigdataSailRepositoryConnection

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

the class BlazegraphOntologyManager method loadRepositoryFromOntology.

public void loadRepositoryFromOntology(OWLOntology ontology, String iri, boolean reset) throws OWLOntologyCreationException, RepositoryException, IOException, RDFParseException, RDFHandlerException {
    synchronized (go_lego_repo) {
        final BigdataSailRepositoryConnection connection = go_lego_repo.getUnisolatedConnection();
        try {
            connection.begin();
            try {
                URI graph = new URIImpl(iri);
                if (reset) {
                    connection.clear(graph);
                }
                StatementCollector collector = new StatementCollector();
                RioRenderer renderer = new RioRenderer(ontology, 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) URI(org.openrdf.model.URI) RepositoryException(org.openrdf.repository.RepositoryException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) IOException(java.io.IOException) RDFParseException(org.openrdf.rio.RDFParseException)

Example 8 with BigdataSailRepositoryConnection

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

the class BlazegraphOntologyManager method getClassDepth.

public int getClassDepth(String term, String root_term) throws IOException {
    int depth = -1;
    try {
        BigdataSailRepositoryConnection connection = go_lego_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#> " + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + "SELECT ?class (count(?mid) as ?depth) " + "WHERE { " + "values ?class {<" + term + ">} . " + "?class rdfs:subClassOf* ?mid . " + "  ?mid rdfs:subClassOf* <" + root_term + "> ." + "filter ( ?class != ?mid )}" + "group by ?class " + " order by ?depth";
            TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
            TupleQueryResult result = tupleQuery.evaluate();
            while (result.hasNext()) {
                BindingSet binding = result.next();
                Value v = binding.getValue("depth");
                depth = Integer.parseInt(v.stringValue());
            }
        } catch (MalformedQueryException e) {
            throw new IOException(e);
        } catch (QueryEvaluationException e) {
            throw new IOException(e);
        } finally {
            connection.close();
        }
    } catch (RepositoryException e) {
        throw new IOException(e);
    }
    return depth;
}
Also used : BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) Value(org.openrdf.model.Value) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException)

Example 9 with BigdataSailRepositoryConnection

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

the class BlazegraphOntologyManager method getAllSuperClasses.

public Set<String> getAllSuperClasses(String uri) throws IOException {
    Set<String> supers = new HashSet<String>();
    try {
        BigdataSailRepositoryConnection connection = go_lego_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#> " + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + "SELECT ?super " + "WHERE { " + "<" + uri + "> rdfs:subClassOf* ?super . " + "} ";
            TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
            TupleQueryResult result = tupleQuery.evaluate();
            while (result.hasNext()) {
                BindingSet binding = result.next();
                Value v = binding.getValue("super");
                // ignore anonymous super classes
                if (v instanceof URI) {
                    String superclass = binding.getValue("super").stringValue();
                    supers.add(superclass);
                }
            }
        } catch (MalformedQueryException e) {
            throw new IOException(e);
        } catch (QueryEvaluationException e) {
            throw new IOException(e);
        } finally {
            connection.close();
        }
    } catch (RepositoryException e) {
        throw new IOException(e);
    }
    return supers;
}
Also used : RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) URI(org.openrdf.model.URI) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) Value(org.openrdf.model.Value)

Example 10 with BigdataSailRepositoryConnection

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

the class BlazegraphOntologyManager method getSuperClassMap.

public Map<String, Set<String>> getSuperClassMap(Set<String> uris) throws IOException {
    Map<String, Set<String>> sub_supers = new HashMap<String, Set<String>>();
    try {
        BigdataSailRepositoryConnection connection = go_lego_repo.getReadOnlyConnection();
        try {
            String q = "VALUES ?sub {";
            for (String uri : uris) {
                q += "<" + uri + "> ";
            }
            q += "} . ";
            String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + "SELECT ?sub ?super " + "WHERE { " + q + "?sub rdfs:subClassOf* ?super . " + "} ";
            TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, query);
            TupleQueryResult result = tupleQuery.evaluate();
            while (result.hasNext()) {
                BindingSet binding = result.next();
                Value parent = binding.getValue("super");
                Value child = binding.getValue("sub");
                // ignore anonymous super classes
                if (parent instanceof URI && child instanceof URI) {
                    String superclass = binding.getValue("super").stringValue();
                    String subclass = binding.getValue("sub").stringValue();
                    Set<String> supers = sub_supers.get(subclass);
                    if (supers == null) {
                        supers = new HashSet<String>();
                    }
                    supers.add(superclass);
                    sub_supers.put(subclass, supers);
                }
            }
        } catch (MalformedQueryException e) {
            throw new IOException(e);
        } catch (QueryEvaluationException e) {
            throw new IOException(e);
        } finally {
            connection.close();
        }
    } catch (RepositoryException e) {
        throw new IOException(e);
    }
    return sub_supers;
}
Also used : RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) URI(org.openrdf.model.URI) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) Value(org.openrdf.model.Value)

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