public void put(Explanation<OWLAxiom> explanation) { Set<Explanation<OWLAxiom>> expls = cache.get(explanation.getEntailment()); if(expls == null) { expls = new HashSet<>(); cache.put(explanation.getEntailment(), expls); } expls.add(explanation); }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof Explanation)) { return false; } Explanation<?> other = (Explanation<?>) obj; return other.getEntailment().equals(entailment) && other.getAxioms().equals(justification); }
public void foundExplanation(ExplanationGenerator<OWLAxiom> explanationGenerator, Explanation<OWLAxiom> explanation, Set<Explanation<OWLAxiom>> explanations) { progressDialog.getProgressMonitor().foundExplanation(explanationGenerator, explanation, explanations); found.add(explanation); logger.info(MARKER, "Explanation {} found", found.size(), explanation.getEntailment()); }
public Explanation<OWLAxiom> getLaconicJustification(Explanation<OWLAxiom> explanation) { Set<Explanation<OWLAxiom>> explanations = getLaconicExplanations(explanation, 1); if(explanations.isEmpty()) { return Explanation.getEmptyExplanation(explanation.getEntailment()); } else { return explanations.iterator().next(); } }
public int getIndentation(Explanation<?> explanation, OWLAxiom axiom) { if(!explanation.getAxioms().contains(axiom) && !explanation.getEntailment().equals(axiom)) { throw new IllegalArgumentException("The explanation does not contain the specified axiom: " + axiom + " " + explanation); } initIfNecessary(explanation); Integer i = indentMap.get(explanation).get(axiom); if(i != null) { return i; } else { return 0; } }
@Override public Explanation<E> generateExplanation(E entailment) { for (Explanation<E> expl : cache) { if (expl.getEntailment().equals(entailment) && module.containsAll(expl.getAxioms())) { cacheHitCounter++; return expl; } } Explanation<E> expl = computeExplanation(entailment); if (!expl.isEmpty()) { cache.add(expl); } return expl; }
private void init(Explanation<?> explanation) { ExplanationOrderer orderer = new ProtegeExplanationOrderer(OWLManager.createOWLOntologyManager()); ExplanationTree tree = orderer.getOrderedExplanation((OWLAxiom) explanation.getEntailment(), explanation.getAxioms()); List<OWLAxiom> ordering = new ArrayList<>(); Map<OWLAxiom, Integer> im = new HashMap<>(); fill(tree, ordering, im); indentMap.put(explanation, im); orderingMap.put(explanation, ordering); }
@Override public String getLabel() { Explanation<OWLAxiom> explanation = getRootObject(); if(explanation == null) { return ""; } OWLModelManager modelManager = getOWLEditorKit().getOWLModelManager(); String rendering = modelManager.getRendering(explanation.getEntailment()).replaceAll("\\s+", " "); return LABEL + rendering; }
/** * Checks to see if a justification for a given entailment is laconic. * @param justification The justification to be checked * @return <code>true</code> if the justification is laconic, otherwise <code>false</code> * @throws ExplanationException If there was a problem. The details of this are implementation specific. */ public boolean isLaconic(Explanation<E> justification) throws ExplanationException { // OBSERVATION: If a justification is laconic, then given its O+, there should be // one justification that is equal to itself. // Could optimise more here - we know that a laconic justification won't contain // equivalent classes axioms, inverse properties axioms etc. If an checker doesn't // appear in O+ then it's not laconic! Set<OWLAxiom> justificationSigmaClosure = computeOPlus(justification.getAxioms()); ExplanationGenerator<E> gen2 = explanationGeneratorFactory.createExplanationGenerator(justificationSigmaClosure); Set<Explanation<E>> exps = gen2.getExplanations(justification.getEntailment(), 2); return Collections.singleton(justification).equals(exps); }
private AttributedString getPopularityString(boolean sel, JustificationFrameSectionRow row) { int popularity = workbenchManager.getPopularity(row.getAxiom()); OWLAxiom entailment = row.getRoot().getEntailment(); int count = workbenchManager.getJustificationCount(entailment); StringBuilder sb = new StringBuilder("In "); int start = sb.length(); final Color highlightColor; if(popularity <= 1) { sb.append("NO"); highlightColor = SINGLE_POPULARITY_COLOR; } else if (popularity != count) { sb.append(popularity - 1); highlightColor = MULTI_POPULARITY_COLOR; } else { sb.append("ALL"); highlightColor = ALL_POPULARITY_COLOR; } int end = sb.length(); sb.append(" other justifications"); AttributedString as = new AttributedString(sb.toString()); as.addAttribute(TextAttribute.FOREGROUND, Color.LIGHT_GRAY, 0, sb.length()); if (!sel) { as.addAttribute(TextAttribute.FOREGROUND, highlightColor, start, end); } return as; } }
@Override public boolean isMaskingPresent(Explanation<OWLAxiom> explanation) { OWLDataFactory dataFactory = manager.getOWLDataFactory(); StructuralTransformation st = new StructuralTransformation(dataFactory); Set<OWLAxiom> axioms = st.getTransformedAxioms(explanation.getAxioms()); ExplanationGenerator<OWLAxiom> expGen = expGenFac.createExplanationGenerator(axioms); Set<Explanation<OWLAxiom>> expls = expGen.getExplanations(explanation.getEntailment()); return expls.size() > 1; } }
if (reconstitutedAxiomSourcesWithMultipleSources.isEmpty()) { return Collections.singleton(new Explanation<>(expl.getEntailment(), pool)); return expGen.getExplanations(expl.getEntailment());
/** * Stores the specified explanation to the specified output stream * @param explanation The explanation to be stored * @param os The output stream to store the explanation to * @throws IOException if there was a problem writing out the explanation */ public static void store(Explanation<OWLAxiom> explanation, OutputStream os, Supplier<OWLOntologyManager> m) throws IOException { try { OWLOntology ontology = m.get().createOntology(explanation.getAxioms()); OWLDataFactory df = ontology.getOWLOntologyManager().getOWLDataFactory(); OWLAnnotationProperty entailmentMarkerAnnotationProperty = df.getOWLAnnotationProperty(ENTAILMENT_MARKER_IRI); OWLAnnotation entailmentAnnotation = df.getOWLAnnotation(entailmentMarkerAnnotationProperty, df.getOWLLiteral(true)); OWLAxiom annotatedEntailment = explanation.getEntailment().getAnnotatedAxiom(Collections.singleton(entailmentAnnotation)); ontology.add(annotatedEntailment); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(os); OWLXMLDocumentFormat justificationOntologyFormat = new OWLXMLDocumentFormat(); ontology.saveOntology(justificationOntologyFormat, bufferedOutputStream); bufferedOutputStream.flush(); } catch (OWLOntologyStorageException | OWLOntologyCreationException e) { throw new RuntimeException(e); } }
@Override protected Color getItemBackgroundColor(MListItem item) { if(item instanceof JustificationFrameSectionRow) { JustificationFrameSectionRow row = (JustificationFrameSectionRow) item; OWLAxiom axiom = row.getAxiom(); int rowIndex = row.getFrameSection().getRowIndex(row) + 1; if(!isSelectedIndex(rowIndex)) { if(axiomSelectionModel.getSelectedAxioms().contains(axiom)) { return Color.YELLOW; } else { boolean inAll = true; for(Explanation<?> expl : workbenchManager.getJustifications(getRootObject().getEntailment())) { if(!expl.contains(axiom)) { inAll = false; break; } } if(inAll) { return new Color(245, 255 , 235); } } } } return super.getItemBackgroundColor(item); }
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; int size = getModel().getSize(); for(int i = 0; i < size; i++) { Object element = getModel().getElementAt(i); if(element instanceof JustificationFrameSectionRow) { JustificationFrameSectionRow row = (JustificationFrameSectionRow) element; Rectangle rect = getCellBounds(i, i); if (rect.intersects(g.getClip().getBounds())) { OWLAxiom entailment = getRootObject().getEntailment(); if(workbenchManager.getJustificationCount(entailment) > 1) { AttributedString popularityString = getPopularityString(isSelectedIndex(i), row); TextLayout textLayout = new TextLayout(popularityString.getIterator(), g2.getFontRenderContext()); float advance = textLayout.getAdvance(); float x = rect.x + rect.width - advance - buttonRunWidth; float h = textLayout.getAscent() + textLayout.getDescent(); float y = ((rect.height - h) / 2) + rect.y + textLayout.getLeading() + textLayout.getAscent(); textLayout.draw(g2, x, y); g2.setColor(Color.LIGHT_GRAY); TextLayout numberLayout = new TextLayout(i + ")", g2.getFont(), g2.getFontRenderContext()); float numberX = 20 - numberLayout.getAdvance(); numberLayout.draw(g2, numberX, y); } } } } }
/** * Render an Explanation object as Markdown text, linking text labels to term IRIs and indenting * axioms. * * @param explanation explanation to render * @param manager OWLOntologyManager containing source ontologies for explanation axioms * @return Markdown-formatted explanation text */ public static String renderExplanationAsMarkdown( Explanation<OWLAxiom> explanation, OWLOntologyManager manager) { ExplanationOrderer orderer = new ProtegeExplanationOrderer(manager); ExplanationTree tree = orderer.getOrderedExplanation(explanation.getEntailment(), explanation.getAxioms()); ShortFormProvider labelProvider = new AnnotationValueShortFormProvider( Collections.singletonList(OWLManager.getOWLDataFactory().getRDFSLabel()), Collections.emptyMap(), manager); ShortFormProvider linkProvider = new MarkdownLinkShortFormProvider(labelProvider); ManchesterOWLSyntaxOWLObjectRendererImpl axiomRenderer = new ManchesterOWLSyntaxOWLObjectRendererImpl(); axiomRenderer.setShortFormProvider(linkProvider); return renderTree(tree, axiomRenderer); }
public void buildHittingSetTree(HittingSetTree<E> hittingSetTree, int limit, ExplanationGeneratorMediator<E> handler, HittingSetTreeNode<E> currentNode) { for (OWLAxiom ax : currentNode.getExplanation().getAxioms()) { handler.removeAxiom(ax); Set<OWLAxiom> pathContents = new HashSet<>(currentNode.getPathToRoot()); pathContents.add(ax); if (hittingSetTree.addExploredPath(pathContents)) { // Look to reuse a justification Explanation<E> expl = getNonIntersectingExplanation(hittingSetTree, pathContents); boolean reuse = true; if (expl == null) { reuse = false; expl = handler.generateExplanation(currentNode.getExplanation().getEntailment()); hittingSetTree.addExplanation(expl); if(hittingSetTree.getExplanations().size() == limit) { return; } } if (!expl.isEmpty()) { HittingSetTreeNode<E> hittingSetTreeNode = new HittingSetTreeNode<>(ax, currentNode, expl, reuse); currentNode.addChild(ax, hittingSetTreeNode); buildHittingSetTree(hittingSetTree, limit, handler, hittingSetTreeNode); } else { hittingSetTree.addClosedPath(new HashSet<>(pathContents)); } } handler.addAxiom(ax); } }
private Set<Explanation<OWLAxiom>> computeLaconicExplanations(Explanation<OWLAxiom> explanation, int limit) throws ExplanationException { try { if(modelManager.getReasoner().isConsistent()) { OWLReasonerFactory rf = getReasonerFactory(); ExplanationGenerator<OWLAxiom> g = org.semanticweb.owl.explanation.api.ExplanationManager.createLaconicExplanationGeneratorFactory(rf).createExplanationGenerator(explanation.getAxioms()); return g.getExplanations(explanation.getEntailment(), limit); } else { OWLReasonerFactory rf = getReasonerFactory(); InconsistentOntologyExplanationGeneratorFactory fac = new InconsistentOntologyExplanationGeneratorFactory(rf, Long.MAX_VALUE); LaconicExplanationGeneratorFactory<OWLAxiom> lacFac = new LaconicExplanationGeneratorFactory<>(fac); ExplanationGenerator<OWLAxiom> g = lacFac.createExplanationGenerator(explanation.getAxioms()); return g.getExplanations(explanation.getEntailment(), limit); } } catch (ExplanationException e) { throw new ExplanationException(e); } }
reuse = false; hittingSetTree.incrementNumberOfNodesWithCallsToFindOne(); expl = handler.generateExplanation(currentNode.getExplanation().getEntailment()); hittingSetTree.addExplanation(expl); if (hittingSetTree.getExplanations().size() == limit) {
public static void saveExplanationAsOntology(OWLEditorKit editorKit, Explanation<?> explanation) throws OWLOntologyCreationException, OWLOntologyChangeException, OWLOntologyStorageException { File f = UIUtil.saveFile( SwingUtilities.getAncestorOfClass(JFrame.class, editorKit.getWorkspace()), "Save ontology as", "Save justification as ontology", CollectionFactory.createSet("owl", "txt", "rdf"), "justification.owl"); if(f == null) { return; } OWLOntologyManager man = OWLManager.createOWLOntologyManager(); OWLOntology ont = man.createOntology(explanation.getAxioms()); OWLDataFactory df = man.getOWLDataFactory(); IRI annotationIRI = IRI.create("http://owl.cs.manchester.ac.uk/explanation/annotations/inferredAxiom"); String entailmentRendering = editorKit.getOWLModelManager().getRendering((OWLAxiom) explanation.getEntailment()); OWLAnnotationProperty prop = df.getOWLAnnotationProperty(annotationIRI); OWLAnnotation anno = df.getOWLAnnotation(prop, df.getOWLLiteral(entailmentRendering)); man.applyChange(new AddOntologyAnnotation(ont, anno)); man.saveOntology(ont, IRI.create(f)); } }