@Override public Stream<OWLAxiom> axiomsIgnoreAnnotations(OWLAxiom axiom) { return axioms(axiom.getAxiomType()).map(x -> (OWLAxiom) x) .filter(ax -> ax.equalsIgnoreAnnotations(axiom)); }
protected OWLReasonerBase(OWLOntology rootOntology, OWLReasonerConfiguration configuration, BufferingMode bufferingMode) { this.rootOntology = checkNotNull(rootOntology, "rootOntology cannot be null"); this.bufferingMode = checkNotNull(bufferingMode, "bufferingMode cannot be null"); this.configuration = checkNotNull(configuration, "configuration cannot be null"); timeOut = configuration.getTimeOut(); manager = rootOntology.getOWLOntologyManager(); manager.addOntologyChangeListener(this::handleRawOntologyChanges); reasonerAxioms = asUnorderedSet( rootOntology.importsClosure().flatMap(o -> Stream.concat(o.logicalAxioms(), o .axioms(AxiomType.DECLARATION))) .map(ax -> OWLAxiom.getAxiomWithoutAnnotations(ax))); }
private Collection<OWLClass> extractChildren(OWLClass parent) { childClassExtractor.setCurrentParentClass(parent); for (OWLOntology ont : ontologies) { for (OWLAxiom ax : ont.getReferencingAxioms(parent)) { if (ax.isLogicalAxiom()) { ax.accept(childClassExtractor); } } } return childClassExtractor.getResult(); }
@Override public boolean containsAxiomIgnoreAnnotations(OWLAxiom axiom) { if (containsAxiom(axiom)) { return true; } return axioms(axiom.getAxiomType()).anyMatch(ax -> ax.equalsIgnoreAnnotations(axiom)); }
/** * Removes all annotations (non-logical axioms) from the ontology causing the ontology to be changed in an unreversible way. For any entity that is only * referenced in an annotation but no logical axiom a declaration is added so that the referenced entities by the ontology remain same. Annotations have no * semantic importance and can be ignored for reasoning purposes including generating explanations and computing modules. Removing them from the ontology * completely reduces the memory requirements which is very high for large-scale annotation-heavy ontologies. * * @param ontology the ontology being changed */ public static void removeAllAnnotations(final OWLOntology ontology) { try { final Set<OWLEntity> referencedEntities = Stream.concat(// Stream.concat(ontology.classesInSignature(), ontology.objectPropertiesInSignature()), // Stream.concat(ontology.dataPropertiesInSignature(), ontology.individualsInSignature())// ).collect(Collectors.toSet()); // Remove every thing that is not logical. ontology.removeAxioms(ontology.axioms().filter(axiom -> !axiom.isLogicalAxiom())); final OWLDataFactory factory = ontology.getOWLOntologyManager().getOWLDataFactory(); // Add exactly once declaration per entity. ontology.addAxioms(referencedEntities.stream()// .filter(entity -> !ontology.containsEntityInSignature(entity))// .map(factory::getOWLDeclarationAxiom)// ); } catch (final OWLOntologyChangeException e) { throw new OWLRuntimeException(e); } }
private void insertChildren(@Nullable OWLEntity entity, ExplanationTree tree) { if (entity == null) { return; } Set<OWLAxiom> currentPath = new HashSet<>(tree.getUserObjectPathToRoot()); getAxioms(entity).filter(ax -> !passTypes.contains(ax.getAxiomType())).forEach(ax -> { Set<OWLAxiom> mapped = getIndexedSet(entity, mappedAxioms, true); if (!consumedAxioms.contains(ax) && !mapped.contains(ax) && !currentPath.contains(ax)) { mapped.add(ax); consumedAxioms.add(ax); ExplanationTree child = new ExplanationTree(ax); tree.addChild(child); getRHSEntitiesSorted(ax).forEach(ent -> insertChildren(ent, child)); } }); sortChildrenAxioms(tree); }
private void writeOWLAxioms2OWLOntology(@NonNull Set<@NonNull OWLAxiom> axioms) throws SWRLRuleEngineException { OWLInferredAxiomFilter inferredAxiomFilter = new OWLInferredAxiomFilter(axioms); List<OWLAxiom> filteredAxioms = axioms.stream().filter(a -> !a.accept(inferredAxiomFilter)) .collect(Collectors.toList()); List<? extends OWLOntologyChange> changes = filteredAxioms.stream().map(a -> new AddAxiom(getOWLOntology(), a)) .collect(Collectors.toList()); try { getOWLOntologyManager().applyChanges(changes); } catch (RuntimeException e) { throw new SWRLRuleEngineException("Error writing OWL axioms to ontology", e); } }
protected void expandAndAddAnnotations(String expandTo, AtomicBoolean expandedSomething, Set<OWLAnnotation> annotations) { visitor.getTool().parseManchesterExpressionFrames(expandTo).stream() .map(axp -> axp.getAxiom()).map( ax -> shouldTransferAnnotations() ? ax.getAnnotatedAxiom(annotations) : ax) .forEach(expandedAxiom -> { newAxioms.add(expandedAxiom); expandedSomething.set(true); }); } }
private Set<OWLClass> extractChildren(OWLClass parent) { ChildClassExtractor childClassExtractor = new ChildClassExtractor(); childClassExtractor.setCurrentParentClass(parent); for (OWLOntology ont : getOntologies()) { ont.getReferencingAxioms(parent).stream() .filter(OWLAxiom::isLogicalAxiom) .forEach(ax -> ax.accept(childClassExtractor)); } return childClassExtractor.getResult(); }
/** * Creates a new module extractor for a subset of a given ontology, its manager, and a specified * type of locality. * * @param man the manager for the associated ontology * @param axs the subset of the ontology as a set of axioms * @param moduleType the type of module this extractor will construct * @param excludeAssertions true if assertions should be excluded */ public SyntacticLocalityModuleExtractor(OWLOntologyManager man, Stream<OWLAxiom> axs, ModuleType moduleType, boolean excludeAssertions) { this.moduleType = checkNotNull(moduleType, "moduleType cannot be null"); manager = checkNotNull(man, "man cannot be null"); Predicate<OWLAxiom> filter = ax -> excludeAssertions ? !AxiomType.ABoxAxiomTypes.contains(ax.getAxiomType()) : true; List<OWLAxiom> collect = asList(axs.filter(filter)); ontologyAxiomSet = new OntologyAxiomSet(collect); try { ontology = checkNotNull(man.createOntology(collect)); } catch (OWLOntologyCreationException e) { throw new OWLRuntimeException(e); } }
private Set<OWLAxiom> build(OWLAxiom parentAxiom) { usedAxioms.add(parentAxiom); OWLAxiomPartExtractor extractor = new OWLAxiomPartExtractor(); parentAxiom.accept(extractor); return asUnorderedSet(extractor.getRHS().stream().flatMap(HasSignature::signature) .flatMap(this::getAxiomsByLHS).filter(usedAxioms::add)); }
@Override public int compare(@Nullable OWLAxiom o1, @Nullable OWLAxiom o2) { int index1 = verifyNotNull(o1).getAxiomType().getIndex(); int index2 = verifyNotNull(o2).getAxiomType().getIndex(); return index1 - index2; } }
private boolean shouldWrite(OWLAxiom ax) { if (ax.getAxiomType().equals(AxiomType.DIFFERENT_INDIVIDUALS)) { return false; } if (ax.getAxiomType().equals(AxiomType.DISJOINT_CLASSES) && ((OWLDisjointClassesAxiom) ax).classExpressions().count() > 2) { return false; } return true; }
protected OWLReasonerBase(OWLOntology rootOntology, OWLReasonerConfiguration configuration, BufferingMode bufferingMode) { this.rootOntology = rootOntology; this.bufferingMode = bufferingMode; this.configuration = configuration; this.timeOut = configuration.getTimeOut(); manager = rootOntology.getOWLOntologyManager(); manager.addOntologyChangeListener(ontologyChangeListener); reasonerAxioms = new HashSet<OWLAxiom>(); for (OWLOntology ont : rootOntology.getImportsClosure()) { for (OWLAxiom ax : ont.getLogicalAxioms()) { reasonerAxioms.add(ax.getAxiomWithoutAnnotations()); } for (OWLAxiom ax : ont.getAxioms(AxiomType.DECLARATION)) { reasonerAxioms.add(ax.getAxiomWithoutAnnotations()); } } }
/** * 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); } }
/** * Include all imports and load then into a single translated merge. * * @param owl * @return */ public Ontology translateAndClassify(OWLOntology owl) { Set<OWLOntology> owls = owl.getOWLOntologyManager().getImportsClosure(owl); log.debug("Load ontologies called. Translating {} ontologies.", owls.size()); OntologyBuilder builder = OntologyBuilderImpl.builder(rdfFactory); for (OWLOntology o : owls) { extractOntoloyVocabulary(o, builder); } for (OWLOntology o : owls) { OWLAxiomVisitorImpl visitor = new OWLAxiomVisitorImpl(o, builder); for (OWLAxiom axiom : o.getAxioms()) { axiom.accept(visitor); } } Ontology onto = builder.build(); log.debug("Ontology loaded: {}", onto); return onto; }
private void buildIndices() { reset(); AxiomMapBuilder builder = new AxiomMapBuilder(); currentExplanation.forEach(ax -> ax.accept(builder)); try { if (ont != null) { man.removeOntology(verifyNotNull(getOntology())); } ont = man .createOntology(IRI.create("http://www.semanticweb.org/", "ontology" + RANDOMSTART .incrementAndGet())); List<AddAxiom> changes = new ArrayList<>(); for (OWLAxiom ax : currentExplanation) { changes.add(new AddAxiom(getOntology(), ax)); ax.accept(builder); } man.applyChanges(changes); } catch (OWLOntologyCreationException e) { throw new OWLRuntimeException(e); } }