Search in sources :

Example 1 with BlazegraphMutationCounter

use of org.geneontology.minerva.util.BlazegraphMutationCounter in project minerva by geneontology.

the class CommandLineInterface method sparqlUpdate.

/**
 * Updates the journal with the provided update sparql statement.
 * cli parameter --sparql-update
 *
 * @param journalFilePath
 * @param updateFile
 * @throws OWLOntologyCreationException
 * @throws IOException
 * @throws RepositoryException
 * @throws MalformedQueryException
 * @throws UpdateExecutionException
 */
public static void sparqlUpdate(String journalFilePath, String updateFile) throws OWLOntologyCreationException, IOException, RepositoryException, MalformedQueryException, UpdateExecutionException {
    // minimal inputs
    if (journalFilePath == null) {
        System.err.println("No journal file was configured.");
        System.exit(-1);
        return;
    }
    if (updateFile == null) {
        System.err.println("No update file was configured.");
        System.exit(-1);
        return;
    }
    String update = FileUtils.readFileToString(new File(updateFile), StandardCharsets.UTF_8);
    Properties properties = new Properties();
    properties.load(CommandLineInterface.class.getResourceAsStream("/org/geneontology/minerva/blazegraph.properties"));
    properties.setProperty(com.bigdata.journal.Options.FILE, journalFilePath);
    BigdataSail sail = new BigdataSail(properties);
    BigdataSailRepository repository = new BigdataSailRepository(sail);
    repository.initialize();
    BigdataSailRepositoryConnection conn = repository.getUnisolatedConnection();
    BlazegraphMutationCounter counter = new BlazegraphMutationCounter();
    conn.addChangeLog(counter);
    conn.prepareUpdate(QueryLanguage.SPARQL, update).execute();
    int changes = counter.mutationCount();
    conn.removeChangeLog(counter);
    System.out.println("\nApplied " + changes + " changes");
    conn.close();
}
Also used : BigdataSailRepository(com.bigdata.rdf.sail.BigdataSailRepository) BigdataSailRepositoryConnection(com.bigdata.rdf.sail.BigdataSailRepositoryConnection) BigdataSail(com.bigdata.rdf.sail.BigdataSail) BlazegraphMutationCounter(org.geneontology.minerva.util.BlazegraphMutationCounter)

Example 2 with BlazegraphMutationCounter

use of org.geneontology.minerva.util.BlazegraphMutationCounter in project minerva by geneontology.

the class ReplaceObsoleteReferencesCommand method run.

public static void run(String ontologyIRI, String catalogPath, String journalFilePath) throws FatalReplaceObsoleteReferencesError {
    if (journalFilePath == null) {
        throw new FatalReplaceObsoleteReferencesError("No journal file was configured.");
    }
    if (ontologyIRI == null) {
        throw new FatalReplaceObsoleteReferencesError("No ontology IRI was configured.");
    }
    final OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    if (catalogPath != null) {
        try {
            manager.getIRIMappers().set(new CatalogXmlIRIMapper(catalogPath));
        } catch (IOException e) {
            throw new FatalReplaceObsoleteReferencesError("Could not load catalog file from " + catalogPath, e);
        }
    }
    final OWLOntology tbox;
    try {
        tbox = manager.loadOntology(IRI.create(ontologyIRI));
    } catch (OWLOntologyCreationException e) {
        throw new FatalReplaceObsoleteReferencesError("Could not load tbox ontology from " + ontologyIRI, e);
    }
    Properties properties = new Properties();
    try {
        properties.load(CommandLineInterface.class.getResourceAsStream("/org/geneontology/minerva/blazegraph.properties"));
    } catch (IOException e) {
        throw new FatalReplaceObsoleteReferencesError("Could not read blazegraph properties resource from jar file.");
    }
    properties.setProperty(com.bigdata.journal.Options.FILE, journalFilePath);
    BigdataSail sail = new BigdataSail(properties);
    BigdataSailRepository repository = new BigdataSailRepository(sail);
    try {
        repository.initialize();
    } catch (RepositoryException e) {
        throw new FatalReplaceObsoleteReferencesError("Could not initialize SAIL repository for database.", e);
    }
    BlazegraphMutationCounter counter = new BlazegraphMutationCounter();
    String replacements = createReplacementsValuesList(tbox);
    String sparqlUpdate = updateTemplate.replace("%%%values%%%", replacements);
    String complementsSparqlUpdate = complementsUpdateTemplate.replace("%%%values%%%", replacements);
    LOGGER.debug("Will apply SPARQL update:\n" + sparqlUpdate);
    try {
        applySPARQLUpdate(repository, sparqlUpdate, Optional.of(counter));
        applySPARQLUpdate(repository, complementsSparqlUpdate, Optional.of(counter));
        int changes = counter.mutationCount();
        LOGGER.info("Successfully applied database updates to replace obsolete terms: " + changes + " changes");
    } catch (RepositoryException | UpdateExecutionException | MalformedQueryException e) {
        throw new FatalReplaceObsoleteReferencesError("Failed to apply SPARQL update.", e);
    }
}
Also used : UpdateExecutionException(org.openrdf.query.UpdateExecutionException) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) Properties(java.util.Properties) CatalogXmlIRIMapper(org.obolibrary.robot.CatalogXmlIRIMapper) BigdataSailRepository(com.bigdata.rdf.sail.BigdataSailRepository) MalformedQueryException(org.openrdf.query.MalformedQueryException) BigdataSail(com.bigdata.rdf.sail.BigdataSail) BlazegraphMutationCounter(org.geneontology.minerva.util.BlazegraphMutationCounter)

Example 3 with BlazegraphMutationCounter

use of org.geneontology.minerva.util.BlazegraphMutationCounter 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)

Aggregations

BlazegraphMutationCounter (org.geneontology.minerva.util.BlazegraphMutationCounter)3 BigdataSail (com.bigdata.rdf.sail.BigdataSail)2 BigdataSailRepository (com.bigdata.rdf.sail.BigdataSailRepository)2 BigdataSailRepositoryConnection (com.bigdata.rdf.sail.BigdataSailRepositoryConnection)2 IOException (java.io.IOException)1 Properties (java.util.Properties)1 CatalogXmlIRIMapper (org.obolibrary.robot.CatalogXmlIRIMapper)1 MalformedQueryException (org.openrdf.query.MalformedQueryException)1 UpdateExecutionException (org.openrdf.query.UpdateExecutionException)1 RepositoryException (org.openrdf.repository.RepositoryException)1