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;
}
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);
}
Aggregations