dependencyreasoner.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 904 行 · 第 1/2 页
JAVA
904 行
StringWriter st = new StringWriter(); rdfRend.renderOntology(source, st); copy = this.loadOntologyInRDF(new StringReader(st.toString()), source.getURI(), true); } catch (Exception ex) { ex.printStackTrace(); } return copy; } Set getAllPropValueChains(OWLClass cla) { allPC = new HashSet(); /*** reseting this is key for each class ***/ try { // trace all equivalent axioms for prop-value chains for (Iterator iter = cla.getEquivalentClasses(ontologies).iterator(); iter.hasNext();) { OWLDescription equCla = (OWLDescription) iter.next(); propChain = new ArrayList(); inUnion = false; getDependency(equCla); } // trace all superclass axioms for prop-value chains for (Iterator iter = cla.getSuperClasses(ontologies).iterator(); iter.hasNext();) { OWLDescription supCla = (OWLDescription) iter.next(); propChain = new ArrayList(); inUnion = false; getDependency(supCla); } } catch (OWLException ex) { ex.printStackTrace(); } return allPC; } Set getDependency(OWLClass cla) { Set dep = new HashSet(); try {// if (preprocessing) {// // do nothing here// }// else { // trace dependencies of all equivalent axioms // consider inferred equivalents using RDFS reasoner for (Iterator iter = rdfsReasoner.equivalentClassesOf(cla).iterator(); iter.hasNext();) { OWLDescription equCla = (OWLDescription) iter.next(); propChain = new ArrayList(); if (unsat.contains(equCla)) { if (derivedClasses.contains(equCla)) { if (unfoldSet((Set) dependencyMap.get(equCla)).size()>2) { dep.add(equCla); } } else dep.add(equCla); } } // consider asserted equivalents for (Iterator iter = cla.getEquivalentClasses(ontologies).iterator(); iter.hasNext();) { OWLDescription equCla = (OWLDescription) iter.next(); propChain = new ArrayList(); Set d = getDependency(equCla); if (!d.isEmpty()) dep.add(d); } // trace dependencies of all superclass axioms for (Iterator iter = cla.getSuperClasses(ontologies).iterator(); iter.hasNext();) { OWLDescription supCla = (OWLDescription) iter.next(); propChain = new ArrayList(); Set d = getDependency(supCla); if (!d.isEmpty()) dep.add(d); } // finally if cla itself is unsat add it to dep if (unsat.contains(cla)) { dep.add(cla); }// } } catch (OWLException ex) { ex.printStackTrace(); } return dep; } Set getDependency(OWLDescription desc) { if (desc instanceof OWLClass) { return getDependency((OWLClass) desc); } if (desc instanceof OWLRestriction) { return getDependency((OWLRestriction) desc); } Set dep = new HashSet(); try { if (desc instanceof OWLAnd) { // simply recurse along nested class description operands // do not recurse on named classes! instead check for unsat and add them for (Iterator iter = ((OWLAnd) desc).getOperands().iterator(); iter.hasNext();) { OWLDescription op = (OWLDescription) iter.next(); // copy propChain before recursing List copyPropChain = new ArrayList(propChain); if (!(op instanceof OWLClass)) { // recurse Set d = getDependency(op); if (!d.isEmpty()) dep.add(d); } else { // add named class as dependency if unsatisfiable if (unsat.contains(op)) { if (preprocessing) { if (this.lastPropAll) { List endPropChain = new ArrayList(propChain); endPropChain.add(op); if (inUnion) endPropChain.add("o"); else endPropChain.add("d"); allPC.add(endPropChain); } } else { dep.add(op); } } } // and restore propChain after recursing propChain = new ArrayList(copyPropChain); } } else if (desc instanceof OWLOr) { if (preprocessing) { /*** entering union */ inUnion = true; // now recurse along nested class description operands // do not recurse on named classes! ignore them in preprocessing for (Iterator iter = ((OWLOr) desc).getOperands().iterator(); iter.hasNext();) { OWLDescription op = (OWLDescription) iter.next(); // copy propChain before recursing List copyPropChain = new ArrayList(propChain); if (!(op instanceof OWLClass)) getDependency(op); else if (unsat.contains(op)) { if (this.lastPropAll) { List endPropChain = new ArrayList(propChain); endPropChain.add(op); endPropChain.add("o"); allPC.add(endPropChain); } } // and restore propChain after recursing propChain = new ArrayList(copyPropChain); } /*** leaving union */ inUnion = false; } else { // recurse along nested class description operands // do not recurse on named classes, instead check for unsat for (Iterator iter = ((OWLOr) desc).getOperands().iterator(); iter.hasNext();) { OWLDescription op = (OWLDescription) iter.next(); Set d = new HashSet(); // copy propChain before recursing List copyPropChain = new ArrayList(propChain); if (!(op instanceof OWLClass)) d = getDependency(op); else if (unsat.contains(op)) d.add(op); // and restore propChain after recursing propChain = new ArrayList(copyPropChain); if (d.isEmpty()) return new HashSet(); else dep.add(d); } } } } catch (OWLException ex) { ex.printStackTrace(); } return dep; } Set getDependency(OWLRestriction res) { Set dep = new HashSet(); try { if (preprocessing) { // only check for some/all on object properties if (res instanceof OWLObjectQuantifiedRestriction) { propChain.add(res.getProperty()); //adds object property // get value of restriction OWLDescription desc = ((OWLObjectQuantifiedRestriction) res).getDescription(); if (res instanceof OWLObjectAllRestriction) { // if value is a class, append value to propChain // and add it to allPC if (desc instanceof OWLClass) { // check if unsatisfiable now: if (unsat.contains(desc)) { // class now becomes terminal of current propChain List endPropChain = new ArrayList(propChain); endPropChain.add(desc); /* also check inUnion or not and tag as optional */ if (inUnion) { endPropChain.add("o"); // optional } else endPropChain.add("d"); // definite allPC.add(endPropChain); } } this.lastPropAll = true; } else this.lastPropAll = false; // recurse on nested class descriptions if (!(desc instanceof OWLClass)) getDependency(desc); } } else { propChain.add(res.getProperty()); //adds property ? (more generic than during preprocessing) if (res instanceof OWLObjectSomeRestriction) { // existential restriction - check for dependencies on must exist property Set d = getDependency(res.getProperty()); if (!d.isEmpty()) dep.add(d); //*** and check for dependencies on value OWLDescription value = ((OWLObjectSomeRestriction) res).getDescription(); if (!(value instanceof OWLClass)) { d = getDependency(value); if (!d.isEmpty()) dep.add(d); } else if (unsat.contains(value)) dep.add(value); } else if (res instanceof OWLCardinalityRestriction) { // minCard >0 restriction - check for dependencies on must exist property int value = ((OWLCardinalityRestriction) res).getAtLeast(); if (value>0) { Set d = getDependency(res.getProperty()); if (!d.isEmpty()) dep.add(d); } } else if (res instanceof OWLDataValueRestriction || res instanceof OWLObjectValueRestriction) { // hasValue restriction - check for dependencies on must exist property Set d = getDependency(res.getProperty()); if (!d.isEmpty()) dep.add(d); } } } catch (OWLException ex) { ex.printStackTrace(); } return dep; } Set getDependency(OWLProperty prop) { Set dep = new HashSet(); try { if (preprocessing) { // do nothing here } else { // check if current propChain is present in allPC OWLClass value = null; if ((value=checkChain())!=null) { dep.add(value); } // check for domain for (Iterator iter = prop.getDomains(ontologies).iterator(); iter.hasNext();) { OWLDescription dom = (OWLDescription) iter.next(); if (!(dom instanceof OWLClass)) { Set d = getDependency(dom); if (!d.isEmpty()) dep.add(d); } else if (unsat.contains(dom)) dep.add(dom); } // get dependencies of all asserted super props for (Iterator iter = prop.getSuperProperties(ontologies).iterator(); iter.hasNext();) { OWLProperty supProp = (OWLProperty) iter.next(); Set d = getDependency(supProp); if (!d.isEmpty()) dep.add(d); } // get dependencies on all inverse properties if (prop instanceof OWLObjectProperty) { for (Iterator iter = ((OWLObjectProperty) prop).getInverses(ontologies).iterator(); iter.hasNext();) { OWLObjectProperty invProp = (OWLObjectProperty) iter.next(); Set d = getInverseDependency(invProp); if (!d.isEmpty()) dep.add(d); } } } } catch (OWLException ex) { ex.printStackTrace(); } return dep; } private Set getInverseDependency(OWLObjectProperty prop) { Set dep = new HashSet(); try { if (preprocessing) { // do nothing here } else { // check for range for (Iterator iter = prop.getRanges(ontologies).iterator(); iter.hasNext();) { OWLDescription ran = (OWLDescription) iter.next(); if (!(ran instanceof OWLClass)) { Set d = getDependency(ran); if (!d.isEmpty()) dep.add(d); } else if (unsat.contains(ran)) dep.add(ran); } // get dependencies of all asserted super props for (Iterator iter = prop.getSuperProperties(ontologies).iterator(); iter.hasNext();) { OWLProperty supProp = (OWLProperty) iter.next(); Set d = getDependency(supProp); if (!d.isEmpty()) dep.add(d); } } } catch (OWLException ex) { ex.printStackTrace(); } return dep; } /* * Check if current propChain is a prefix of any allPC chain */ private OWLClass checkChain() { OWLClass value = null; for (Iterator iter = allPC.iterator(); iter.hasNext();) { List allPCchain = (ArrayList) iter.next(); /* note last two values of chain are terminal class and optional definite string tag */ boolean match = true; if (propChain.size()==(allPCchain.size()-2)) { for (int i=0; i<allPCchain.size()-2; i++) { if (!allPCchain.get(i).equals(propChain.get(i))) { match = false; break; } } if (match) { value = (OWLClass) allPCchain.get(allPCchain.size()-2); break; } } } return value; } public OWLOntology loadOntologyInRDF(Reader reader, URI uri, boolean importing) { OWLOntology ontology = null; try { OWLRDFParser parser = new OWLRDFParser(); parser.setImporting(importing); parser.setOWLRDFErrorHandler(new OWLRDFErrorHandler() { public void owlFullConstruct(int code, String message) throws SAXException { } public void error(String message) throws SAXException { throw new SAXException(message.toString()); } public void warning(String message) throws SAXException { System.out.println("RDFParser: " + message.toString()); } public void owlFullConstruct(int code, String message, Object obj) throws SAXException { // TODO Auto-generated method stub } }); OWLConnection connection = new OWLConnectionImpl(); parser.setConnection(connection); // PARSE THE ONTOLOGY! ontology = parser.parseOntology(reader, uri); } catch (Exception e) { e.printStackTrace(); } return ontology; } private String getName(OWLEntity entity) { try { String name = entity.getURI().toString(); if (name.indexOf("#")>=0) name = name.substring(name.indexOf("#")+1, name.length()); else name = name.substring(name.lastIndexOf("/")+1, name.length()); return name; } catch (OWLException ex) { ex.printStackTrace(); } return ""; } protected Set unfoldSet(Set set) { Set unfold = new HashSet(); for (Iterator iter = set.iterator(); iter.hasNext();) { Object obj = iter.next(); if (obj instanceof Set) unfold.addAll(unfoldSet((Set) obj)); else if (obj instanceof OWLEntity) unfold.add(obj); else if (obj instanceof String) unfold.add(obj); } return unfold; } /* * count the no. of derived classes under each root */ public void countDependencies() { childMap = new HashMap(); for (Iterator iter = rootClasses.iterator(); iter.hasNext();) { OWLClass root = (OWLClass) iter.next(); childMap.put(root, getChildren(root)); } } /* * create a child-map for each root class */ private Set getChildren(OWLClass cla) { Set children = new HashSet(); for (Iterator iter = derivedClasses.iterator(); iter.hasNext();) { OWLClass der = (OWLClass) iter.next(); if (!der.equals(cla)) { Set dep = (HashSet) this.dependencyMap.get(der); dep = this.unfoldSet(dep); if (dep.contains(cla)) { children.add(der); children.addAll(getChildren(der)); } } } return children; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?