Search in sources :

Example 1 with BlazegraphOntologyManager

use of org.geneontology.minerva.BlazegraphOntologyManager in project minerva by geneontology.

the class ExportExplanation method exportExplanation.

public static String exportExplanation(WorkingMemory wm, BlazegraphOntologyManager go_lego, Map<IRI, String> labelMap) {
    Set<Triple> triples = new HashSet<>();
    Model model = ModelFactory.createDefaultModel();
    model.add(toJava(wm.facts()).stream().map(t -> model.asStatement(Bridge.jenaFromTriple(t))).collect(Collectors.toList()));
    QueryExecution qe = QueryExecutionFactory.create(mainQuery, model);
    ResultSet results = qe.execSelect();
    while (results.hasNext()) {
        QuerySolution qs = results.next();
        triples.add(new Triple(new URI(qs.getResource("s").getURI()), new URI(qs.getResource("p").getURI()), new URI(qs.getResource("o").getURI())));
    }
    qe.close();
    // Make sure all the asserted triples are included, in case they got filtered out as indirect by the first query
    Model assertedModel = ModelFactory.createDefaultModel();
    assertedModel.add(toJava(wm.asserted()).stream().map(t -> assertedModel.asStatement(Bridge.jenaFromTriple(t))).collect(Collectors.toList()));
    QueryExecution aqe = QueryExecutionFactory.create(mainQuery, assertedModel);
    ResultSet assertedResults = aqe.execSelect();
    while (assertedResults.hasNext()) {
        QuerySolution qs = assertedResults.next();
        triples.add(new Triple(new URI(qs.getResource("s").getURI()), new URI(qs.getResource("p").getURI()), new URI(qs.getResource("o").getURI())));
    }
    aqe.close();
    Set<Triple> asserted = triples.stream().filter(t -> wm.asserted().contains(t)).collect(Collectors.toSet());
    Set<Triple> inferred = triples.stream().filter(t -> !wm.asserted().contains(t)).collect(Collectors.toSet());
    Map<Triple, Set<Explanation>> allExplanations = inferred.stream().collect(Collectors.toMap(Function.identity(), s -> toJava(wm.explain(s))));
    Set<Rule> allRules = allExplanations.values().stream().flatMap(es -> es.stream().flatMap(e -> toJava(e.rules()).stream())).collect(Collectors.toSet());
    Stream<URI> subjects = triples.stream().map(t -> (URI) (t.s()));
    Stream<URI> predicates = triples.stream().map(t -> (URI) (t.p()));
    Stream<URI> objects = triples.stream().map(t -> (URI) (t.o()));
    Set<URI> allTerms = new HashSet<>();
    for (Rule rule : allRules) {
        for (TriplePattern tp : toJavaList(rule.body())) {
            if (tp.s() instanceof URI)
                allTerms.add((URI) tp.s());
            if (tp.p() instanceof URI)
                allTerms.add((URI) tp.p());
            if (tp.o() instanceof URI)
                allTerms.add((URI) tp.o());
        }
        for (TriplePattern tp : toJavaList(rule.head())) {
            if (tp.s() instanceof URI)
                allTerms.add((URI) tp.s());
            if (tp.p() instanceof URI)
                allTerms.add((URI) tp.p());
            if (tp.o() instanceof URI)
                allTerms.add((URI) tp.o());
        }
    }
    allTerms.addAll(subjects.collect(Collectors.toSet()));
    allTerms.addAll(predicates.collect(Collectors.toSet()));
    allTerms.addAll(objects.collect(Collectors.toSet()));
    Map<URI, String> labels = findLabels(allTerms, asserted, go_lego, labelMap);
    int currentBlankNode = 0;
    Map<Triple, ExplanationTriple> assertedForJSON = new HashMap<>();
    for (Triple t : asserted) {
        ExplanationTriple et = new ExplanationTriple();
        et.id = "_:" + currentBlankNode++;
        et.subject = ((URI) (t.s())).uri();
        et.predicate = ((URI) (t.p())).uri();
        et.object = ((URI) (t.o())).uri();
        assertedForJSON.put(t, et);
    }
    Map<Rule, ExplanationRule> rulesForJSON = new HashMap<>();
    for (Rule r : allRules) {
        ExplanationRule er = new ExplanationRule();
        er.id = "_:" + currentBlankNode++;
        List<ExplanationTriple> body = new ArrayList<>();
        List<ExplanationTriple> head = new ArrayList<>();
        for (TriplePattern t : toJavaList(r.body())) {
            ExplanationTriple et = new ExplanationTriple();
            et.subject = patternNodeToString(t.s());
            et.predicate = patternNodeToString(t.p());
            et.object = patternNodeToString(t.o());
            body.add(et);
        }
        for (TriplePattern t : toJavaList(r.head())) {
            ExplanationTriple et = new ExplanationTriple();
            et.subject = patternNodeToString(t.s());
            et.predicate = patternNodeToString(t.p());
            et.object = patternNodeToString(t.o());
            head.add(et);
        }
        er.body = body.toArray(new ExplanationTriple[] {});
        er.head = head.toArray(new ExplanationTriple[] {});
        rulesForJSON.put(r, er);
    }
    Map<Triple, ExplanationTriple> inferredForJSON = new HashMap<>();
    for (Triple t : inferred) {
        ExplanationTriple et = new ExplanationTriple();
        et.subject = ((URI) (t.s())).uri();
        et.predicate = ((URI) (t.p())).uri();
        et.object = ((URI) (t.o())).uri();
        Explanation explanation = allExplanations.get(t).iterator().next();
        org.geneontology.minerva.explanation.Explanation ex = new org.geneontology.minerva.explanation.Explanation();
        ex.triples = toJava(explanation.facts()).stream().map(f -> assertedForJSON.get(f).id).toArray(String[]::new);
        ex.rules = toJava(explanation.rules()).stream().map(r -> rulesForJSON.get(r).id).toArray(String[]::new);
        et.explanation = ex;
        inferredForJSON.put(t, et);
    }
    ModelExplanation me = new ModelExplanation();
    me.terms = labels.keySet().stream().map(uri -> {
        ExplanationTerm et = new ExplanationTerm();
        et.id = uri.uri();
        et.label = labels.get(uri);
        return et;
    }).toArray(ExplanationTerm[]::new);
    me.assertions = assertedForJSON.values().toArray(new ExplanationTriple[] {});
    me.rules = rulesForJSON.values().toArray(new ExplanationRule[] {});
    me.inferences = inferredForJSON.values().toArray(new ExplanationTriple[] {});
    GsonBuilder builder = new GsonBuilder();
    builder = builder.setPrettyPrinting();
    Gson gson = builder.create();
    String json = gson.toJson(me);
    return json;
}
Also used : java.util(java.util) org.geneontology.rules.engine(org.geneontology.rules.engine) Function(java.util.function.Function) GsonBuilder(com.google.gson.GsonBuilder) QueryExecutionFactory(org.apache.jena.query.QueryExecutionFactory) Model(org.apache.jena.rdf.model.Model) Logger(org.apache.log4j.Logger) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolution(org.apache.jena.query.QuerySolution) IRI(org.semanticweb.owlapi.model.IRI) Gson(com.google.gson.Gson) ExplanationTerm(org.geneontology.minerva.explanation.ExplanationTerm) IOException(java.io.IOException) BlazegraphOntologyManager(org.geneontology.minerva.BlazegraphOntologyManager) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) Stream(java.util.stream.Stream) ExplanationTriple(org.geneontology.minerva.explanation.ExplanationTriple) ModelExplanation(org.geneontology.minerva.explanation.ModelExplanation) JavaConverters(scala.collection.JavaConverters) ExplanationRule(org.geneontology.minerva.explanation.ExplanationRule) Bridge(org.geneontology.rules.util.Bridge) ModelFactory(org.apache.jena.rdf.model.ModelFactory) ResultSet(org.apache.jena.query.ResultSet) ResultSet(org.apache.jena.query.ResultSet) ModelExplanation(org.geneontology.minerva.explanation.ModelExplanation) Gson(com.google.gson.Gson) ExplanationRule(org.geneontology.minerva.explanation.ExplanationRule) QueryExecution(org.apache.jena.query.QueryExecution) ResultSet(org.apache.jena.query.ResultSet) ModelExplanation(org.geneontology.minerva.explanation.ModelExplanation) ExplanationTerm(org.geneontology.minerva.explanation.ExplanationTerm) ExplanationTriple(org.geneontology.minerva.explanation.ExplanationTriple) GsonBuilder(com.google.gson.GsonBuilder) ExplanationTriple(org.geneontology.minerva.explanation.ExplanationTriple) QuerySolution(org.apache.jena.query.QuerySolution) Model(org.apache.jena.rdf.model.Model) ExplanationRule(org.geneontology.minerva.explanation.ExplanationRule)

Example 2 with BlazegraphOntologyManager

use of org.geneontology.minerva.BlazegraphOntologyManager in project minerva by geneontology.

the class ShexValidatorTest method setUpBeforeClass.

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    go_lego = new BlazegraphOntologyManager(go_lego_journal_file, true);
    CurieHandler curieHandler = DefaultCurieHandler.getDefaultHandler();
    shex = new ShexValidator(schemaFile, mainShapemapFile, go_lego, curieHandler);
    shexMeta = new ShexValidator(metadataSchemaFile, metadataShapemapFile, go_lego, curieHandler);
}
Also used : BlazegraphOntologyManager(org.geneontology.minerva.BlazegraphOntologyManager) CurieHandler(org.geneontology.minerva.curie.CurieHandler) DefaultCurieHandler(org.geneontology.minerva.curie.DefaultCurieHandler) BeforeClass(org.junit.BeforeClass)

Aggregations

BlazegraphOntologyManager (org.geneontology.minerva.BlazegraphOntologyManager)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 IOUtils (org.apache.commons.io.IOUtils)1 QueryExecution (org.apache.jena.query.QueryExecution)1 QueryExecutionFactory (org.apache.jena.query.QueryExecutionFactory)1 QuerySolution (org.apache.jena.query.QuerySolution)1 ResultSet (org.apache.jena.query.ResultSet)1 Model (org.apache.jena.rdf.model.Model)1 ModelFactory (org.apache.jena.rdf.model.ModelFactory)1 Logger (org.apache.log4j.Logger)1 CurieHandler (org.geneontology.minerva.curie.CurieHandler)1 DefaultCurieHandler (org.geneontology.minerva.curie.DefaultCurieHandler)1 ExplanationRule (org.geneontology.minerva.explanation.ExplanationRule)1