public DLOntology_withMaps preprocessAndClausify(OWLOntology rootOntology) { OWLDataFactory factory = rootOntology.getOWLOntologyManager().getOWLDataFactory(); String ontologyIRI = rootOntology.getOntologyID().getDefaultDocumentIRI() == null ? "urn:hermit:kb" : rootOntology.getOntologyID().getDefaultDocumentIRI().toString(); Collection<OWLOntology> importClosure = rootOntology.getImportsClosure(); OWLAxioms_withMaps axioms = new OWLAxioms_withMaps(); OWLNormalization_withMaps normalization = new OWLNormalization_withMaps(factory, axioms, 0, m_datatypeManager); for (OWLOntology ontology : importClosure) normalization.processOntology(ontology); BuiltInPropertyManager_withMaps builtInPropertyManager = new BuiltInPropertyManager_withMaps(factory); builtInPropertyManager.axiomatizeBuiltInPropertiesAsNeeded(axioms); ObjectPropertyInclusionManager_withMaps objectPropertyInclusionManager = new ObjectPropertyInclusionManager_withMaps(axioms); // now object property inclusion manager added all non-simple properties to // axioms.m_complexObjectPropertyExpressions // now that we know which roles are non-simple, we can decide which negative object property // assertions have to be // expressed as concept assertions so that transitivity rewriting applies properly. objectPropertyInclusionManager.rewriteNegativeObjectPropertyAssertions( factory, axioms, normalization.getDefinitions().size()); objectPropertyInclusionManager.rewriteAxioms(factory, axioms, 0); OWLAxiomsExpressivity_withMaps axiomsExpressivity = new OWLAxiomsExpressivity_withMaps(axioms); DLOntology_withMaps dlOntology = clausify_withMaps(factory, ontologyIRI, axioms, axiomsExpressivity); return dlOntology; }
public DLQueryParser(OWLOntology rootOntology, ShortFormProvider shortFormProvider) { this.rootOntology = rootOntology; OWLOntologyManager manager = rootOntology.getOWLOntologyManager(); Set<OWLOntology> importsClosure = rootOntology.getImportsClosure(); // Create a bidirectional short form provider to do the actual mapping. // It will generate names using the input // short form provider. bidiShortFormProvider = new BidirectionalShortFormProviderAdapter(manager, importsClosure, shortFormProvider); }
public OWLProfileReport checkOntology(OWLOntology ontology) { this.ont = ontology; OWL2DLProfile profile = new OWL2DLProfile(); OWLProfileReport report = profile.checkOntology(ontology); Set<OWLProfileViolation> violations = new HashSet<OWLProfileViolation>(); violations.addAll(report.getViolations()); OWLOntologyWalker ontologyWalker = new OWLOntologyWalker(ontology.getImportsClosure()); OWL2ELProfileObjectVisitor visitor = new OWL2ELProfileObjectVisitor(ontologyWalker, ontology.getOWLOntologyManager()); ontologyWalker.walkStructure(visitor); violations.addAll(visitor.getProfileViolations()); return new OWLProfileReport(this, violations); }
/** Clears the reasoner and reloads all the axioms in the imports closure. */ public void refresh() { visitor.clear(); kb.clear(); importsClosure = ontology.getImportsClosure(); visitor.setAddAxiom(true); for (OWLOntology ont : importsClosure) { ont.accept(visitor); } visitor.verify(); shouldRefresh = false; }
/** @return current reasoner, operating over abox */ public OWLReasoner getReasoner() { synchronized (reasonerMutex) { if (reasoner == null) { // reasoner -> query -> abox -> tbox if (LOG.isDebugEnabled()) { LOG.debug( "Creating reasoner on " + queryOntology + " ImportsClosure=" + queryOntology.getImportsClosure()); } reasoner = reasonerFactory.createReasoner(queryOntology); } } return reasoner; }
/** * <b>Motivation</b>: OWL reasoners do not return superclass expressions If we want to find all * class expressions that may hold for a class then we must pre-coordinate all possible * expressions within the subset of OWL we care about. <br> * This class generates all satisfiable class expressions of the form r some c (for the * cross-product of R x C), as well as all class expressions that have been used (which may * include nested expressions) * * <p>The results are stored in queryClassMap * * @param precomputePropertyClassCombinations */ @Deprecated private void generateQueryOntology(boolean precomputePropertyClassCombinations) { queryClassMap = new HashMap<OWLClass, OWLClassExpression>(); getReasoner().flush(); if (precomputePropertyClassCombinations) { LOG.debug("Precomputing all OP x Class combos"); // cross-product of P x C // TODO - reflexivity and local reflexivity? for (OWLObjectProperty p : tboxOntology.getObjectPropertiesInSignature(true)) { LOG.debug(" materializing P some C for P=:" + p); for (OWLClass c : tboxOntology.getClassesInSignature(true)) { OWLObjectSomeValuesFrom r = getOWLDataFactory().getOWLObjectSomeValuesFrom(p, c); // LOG.debug(" QMAP:"+r); addClassExpressionToQueryMap(r); } } } // all expressions used in ontology for (OWLOntology ont : tboxOntology.getImportsClosure()) { LOG.debug("Finding all nested anonymous expressions"); for (OWLAxiom ax : ont.getAxioms()) { // TODO - check if this is the nest closure. ie (r some (r2 some (r3 some ...))) for (OWLClassExpression x : ax.getNestedClassExpressions()) { if (x.isAnonymous()) { // LOG.debug(" QMAP+:"+x); addClassExpressionToQueryMap(x); } } } } if (LOG.isDebugEnabled()) { for (OWLOntology ont : collectedAxioms.keySet()) { LOG.debug("TOTAL axioms in QMAP: " + collectedAxioms.get(ont).size()); } } LOG.debug("Adding collected axioms"); addCollectedAxioms(); LOG.debug("Flushing reasoner..."); reasoner.flush(); LOG.debug("Flushed reasoner"); }
private String parseWithOWLAPI( URL ontologyURL, boolean useOWLAPI, boolean considerImportedOntologies, boolean considerImportedClosure, boolean useReasoner) throws OWLOntologyCreationException, OWLOntologyStorageException, URISyntaxException { String result = ""; if (useOWLAPI) { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntology(IRI.create(ontologyURL.toString())); if (considerImportedClosure || considerImportedOntologies) { Set<OWLOntology> setOfImportedOntologies = new HashSet<OWLOntology>(); if (considerImportedOntologies) { setOfImportedOntologies.addAll(ontology.getDirectImports()); } else { setOfImportedOntologies.addAll(ontology.getImportsClosure()); } for (OWLOntology importedOntology : setOfImportedOntologies) { manager.addAxioms(ontology, importedOntology.getAxioms()); } } if (useReasoner) { ontology = parseWithReasoner(manager, ontology); } StringDocumentTarget parsedOntology = new StringDocumentTarget(); manager.saveOntology(ontology, new RDFXMLOntologyFormat(), parsedOntology); result = parsedOntology.toString(); } return result; }
protected OWLOntology getOntologyAsOWLOntology( OWLOntologyID ontologyId, boolean merge, IRI universalPrefix) { // if (merge) throw new UnsupportedOperationException("Merge not implemented yet for // OWLOntology."); // Remove the check below. It might be an unmanaged dependency (TODO remove from collector and // reintroduce check?). // if (!hasOntology(ontologyIri)) return null; OWLOntology o; o = ontologyProvider.getStoredOntology(ontologyId, OWLOntology.class, merge); if (merge) { final Set<OWLOntology> set = new HashSet<OWLOntology>(); log.debug("Merging {} with its imports, if any.", o); set.add(o); // Actually, if the provider already performed the merge, this won't happen for (OWLOntology impo : o.getImportsClosure()) { log.debug("Imported ontology {} will be merged with {}.", impo, o); set.add(impo); } OWLOntologySetProvider provider = new OWLOntologySetProvider() { @Override public Set<OWLOntology> getOntologies() { return set; } }; OWLOntologyMerger merger = new OWLOntologyMerger(provider); try { o = merger.createMergedOntology( OWLManager.createOWLOntologyManager(), ontologyId.getOntologyIRI()); } catch (OWLOntologyCreationException e) { log.error("Failed to merge imports for ontology " + ontologyId, e); // do not reassign the root ontology } } else { // Rewrite import statements List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>(); OWLDataFactory df = OWLManager.getOWLDataFactory(); /* * TODO manage import rewrites better once the container ID is fully configurable (i.e. instead of * going upOne() add "session" or "ontology" if needed). But only do this if we keep considering * imported ontologies as *not* managed. */ for (OWLImportsDeclaration oldImp : o.getImportsDeclarations()) { changes.add(new RemoveImport(o, oldImp)); String s = oldImp.getIRI().toString(); // FIXME Ugly way to check, but we'll get through with it if (s.contains("::")) s = s.substring(s.indexOf("::") + 2, s.length()); boolean managed = managedOntologies.contains(oldImp.getIRI()); // For space, always go up at least one String tid = getID(); if (backwardPathLength > 0) tid = tid.split("/")[0]; IRI target = IRI.create( (managed ? universalPrefix + "/" + tid + "/" : URIUtils.upOne(universalPrefix) + "/") + s); changes.add(new AddImport(o, df.getOWLImportsDeclaration(target))); } o.getOWLOntologyManager().applyChanges(changes); } return o; }
public static void main(String[] args) throws Exception { if (args.length == 0) { // args = new String[] { "/home/yzhou/backup/20141212/univ-bench-dl-queries.owl"}; args = new String[] {PagodaTester.onto_dir + "fly/fly-all-in-one_rolledUp.owl"}; // args = new String[] { PagodaTester.onto_dir + // "dbpedia/integratedOntology-all-in-one-minus-datatype.owl" }; // args = new String[] { PagodaTester.onto_dir + "npd/npd-all-minus-datatype.owl" }; // args = new String[] { PagodaTester.onto_dir + "bio2rdf/chembl/cco-noDPR.ttl" }; // args = new String[] { PagodaTester.onto_dir + // "bio2rdf/reactome/biopax-level3-processed.owl" }; // args = new String[] { PagodaTester.onto_dir + "bio2rdf/uniprot/core-processed-noDis.owl" // }; } // OWLOntology ontology = OWLHelper.getMergedOntology(args[0], null); // OWLHelper.correctDataTypeRangeAxioms(ontology); OWLOntology ontology = OWLHelper.loadOntology(args[0]); OWLOntologyManager manager = ontology.getOWLOntologyManager(); OWLDataFactory factory = manager.getOWLDataFactory(); // manager.saveOntology(ontology, new FileOutputStream(args[0].replace(".owl", // "_owlapi.owl"))); if (outputFile != null) Utility.redirectCurrentOut(outputFile); int queryID = 0; for (OWLClass cls : ontology.getClassesInSignature(true)) { if (cls.equals(factory.getOWLThing()) || cls.equals(factory.getOWLNothing())) continue; if (!cls.toStringID().contains("Query")) continue; System.out.println("^[Query" + ++queryID + "]"); System.out.println(template.replace("@CLASS", cls.toStringID())); System.out.println(); } for (OWLOntology onto : ontology.getImportsClosure()) for (OWLObjectProperty prop : onto.getObjectPropertiesInSignature()) { // if (!prop.toStringID().contains("Query")) continue; System.out.println("^[Query" + ++queryID + "]"); System.out.println("SELECT ?X ?Y"); System.out.println("WHERE {"); System.out.println("?X <" + prop.toStringID() + "> ?Y ."); System.out.println("}"); System.out.println(); } String[] answerVars = new String[] {"?X", "?Y"}; for (OWLOntology onto : ontology.getImportsClosure()) for (OWLObjectProperty prop : onto.getObjectPropertiesInSignature()) { // if (!prop.toStringID().contains("Query")) continue; for (int i = 0; i < answerVars.length; ++i) { System.out.println("^[Query" + ++queryID + "]"); System.out.println("SELECT " + answerVars[i]); System.out.println("WHERE {"); System.out.println("?X <" + prop.toStringID() + "> ?Y ."); System.out.println("}"); System.out.println(); } } if (outputFile != null) Utility.closeCurrentOut(); }