rulesexpressivity.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,433 行 · 第 1/3 页
JAVA
1,433 行
for (Iterator i = consequent.iterator(); i.hasNext();) { OWLRuleAtom consAtom = (OWLRuleAtom) i.next(); if (isDLAtom(consAtom, onto)) { return false; } } } catch (OWLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } /** * Checks if a given SWRL rule is DL safe It checks that each variable in r * occurs in a non-DL atom in the rule body */ public boolean checkDLSafe(OWLRule rule, OWLOntology onto) { try { Set antecedent = rule.getAntecedents(); Set consequent = rule.getConsequents(); HashSet vars = new HashSet(); HashSet bodyNonDLVars = new HashSet(); // Constructs sets of variables and variables in non-DL atoms in the // antecedent of the rule for (Iterator i = antecedent.iterator(); i.hasNext();) { OWLRuleAtom antAtom = (OWLRuleAtom) i.next(); if (antAtom instanceof OWLRuleClassAtom) { OWLRuleIObject arg = ((OWLRuleClassAtom) antAtom) .getArgument(); if (arg instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg).getURI(); vars.add(aux); if (!(isDLAtom(antAtom, onto))) bodyNonDLVars.add(aux); } } if (antAtom instanceof OWLRuleObjectPropertyAtom) { OWLRuleIObject arg1 = ((OWLRuleObjectPropertyAtom) antAtom) .getFirstArgument(); if (arg1 instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg1).getURI(); vars.add(aux); if (!(isDLAtom(antAtom, onto))) bodyNonDLVars.add(aux); } OWLRuleIObject arg2 = ((OWLRuleObjectPropertyAtom) antAtom) .getSecondArgument(); if (arg2 instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg2).getURI(); vars.add(aux); if (!(isDLAtom(antAtom, onto))) bodyNonDLVars.add(aux); } } } // Adds variables of consequent to set of variables for (Iterator j = consequent.iterator(); j.hasNext();) { OWLRuleAtom consAtom = (OWLRuleAtom) j.next(); if (consAtom instanceof OWLRuleClassAtom) { OWLRuleIObject arg = ((OWLRuleClassAtom) consAtom) .getArgument(); if (arg instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg).getURI(); vars.add(aux); } } if (consAtom instanceof OWLRuleObjectPropertyAtom) { OWLRuleIObject arg1 = ((OWLRuleObjectPropertyAtom) consAtom) .getFirstArgument(); if (arg1 instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg1).getURI(); vars.add(aux); } OWLRuleIObject arg2 = ((OWLRuleObjectPropertyAtom) consAtom) .getSecondArgument(); if (arg2 instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg2).getURI(); vars.add(aux); } } } // Finds if each variable in a rule appeaars in a non-DL atom in the // body of rule if (vars.equals(bodyNonDLVars)) return true; else return false; } catch (Exception e) { e.printStackTrace(); } return true; } public boolean checkALLog(OWLRule rule, OWLOntology onto) { try { Set antecedent = rule.getAntecedents(); Set consequent = rule.getConsequents(); for (Iterator i = consequent.iterator(); i.hasNext();) { OWLRuleAtom consAtom = (OWLRuleAtom) i.next(); if (isDLAtom(consAtom, onto)) { return false; } } for (Iterator j = antecedent.iterator(); j.hasNext();) { OWLRuleAtom antAtom = (OWLRuleAtom) j.next(); if (antAtom instanceof OWLRuleObjectPropertyAtom && isDLAtom(antAtom, onto)) { return false; } } } catch (OWLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } /** * Checks if a given SWRL rule can be expressed in OWL * */ public boolean checkSyntSugar(OWLRule rule) { String answer = ""; try { Set antecedent = rule.getAntecedents(); Set consequent = rule.getConsequents(); if (antecedent.isEmpty() || (consequent.isEmpty())) { return true; } else { HashSet antVars = new HashSet(); HashSet consVars = new HashSet(); HashSet antInds = new HashSet(); HashSet consInds = new HashSet(); // Constructs sets of variables and individuals in antecedent for (Iterator i = antecedent.iterator(); i.hasNext();) { OWLRuleAtom antAtom = (OWLRuleAtom) i.next(); if (antAtom instanceof OWLRuleClassAtom) { OWLRuleIObject arg = ((OWLRuleClassAtom) antAtom) .getArgument(); if (arg instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg).getURI(); antVars.add(aux); } else { OWLIndividual aux = ((OWLRuleIndividual) arg) .getIndividual(); antInds.add(aux); } } if (antAtom instanceof OWLRuleObjectPropertyAtom) { OWLRuleIObject arg1 = ((OWLRuleObjectPropertyAtom) antAtom) .getFirstArgument(); if (arg1 instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg1).getURI(); antVars.add(aux); } else { OWLIndividual aux = ((OWLRuleIndividual) arg1) .getIndividual(); antInds.add(aux); } OWLRuleIObject arg2 = ((OWLRuleObjectPropertyAtom) antAtom) .getSecondArgument(); if (arg2 instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg2).getURI(); antVars.add(aux); } else { OWLIndividual aux = ((OWLRuleIndividual) arg2) .getIndividual(); antInds.add(aux); } } } // Construct sets of variables and individuals in consequent for (Iterator j = consequent.iterator(); j.hasNext();) { OWLRuleAtom consAtom = (OWLRuleAtom) j.next(); if (consAtom instanceof OWLRuleClassAtom) { OWLRuleIObject arg = ((OWLRuleClassAtom) consAtom) .getArgument(); if (arg instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg).getURI(); consVars.add(aux); } else { OWLIndividual aux = ((OWLRuleIndividual) arg) .getIndividual(); consInds.add(aux); } } if (consAtom instanceof OWLRuleObjectPropertyAtom) { OWLRuleIObject arg1 = ((OWLRuleObjectPropertyAtom) consAtom) .getFirstArgument(); if (arg1 instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg1).getURI(); consVars.add(aux); } else { OWLIndividual aux = ((OWLRuleIndividual) arg1) .getIndividual(); consInds.add(aux); } OWLRuleIObject arg2 = ((OWLRuleObjectPropertyAtom) consAtom) .getSecondArgument(); if (arg2 instanceof OWLRuleIVariable) { URI aux = ((OWLRuleIVariable) arg2).getURI(); consVars.add(aux); } else { OWLIndividual aux = ((OWLRuleIndividual) arg2) .getIndividual(); consInds.add(aux); } } } // Finds number of common variables and common individuals. // A rule can be expressed in OWL if it has exactly one common // variable, or // if it has no common variables and at least one common // individual. antVars.retainAll(consVars); antInds.retainAll(consInds); int varsSize = antVars.size(); int indsSize = antInds.size(); if (varsSize == 0) { if (indsSize > 0) return true; } else if (varsSize == 1) return true; else return false; } } catch (Exception e) { e.printStackTrace(); } return true; } public static boolean isDLAtom(OWLRuleAtom atom, OWLOntology ont) throws OWLException { //If the class is defined in terms of others in the ontology, // or used elsewhere in the ontology then it is a DL-atom if (atom instanceof OWLRuleClassAtom) { OWLDescription desc = ((OWLRuleClassAtom) atom).getDescription(); // A complex description will always be a DL atom if (!(desc instanceof OWLClass)) { return true; } //Check if it is used anywhere in the ontology Set aux = OntologyHelper.entityUsage(ont, (OWLClass) desc); // Create a new set of usage that excludes the instances of the // class Set aux1 = new HashSet(); for (Iterator it = aux.iterator(); it.hasNext();) { OWLObject object = (OWLObject) it.next(); if (!(object instanceof OWLIndividual)) { aux1.add(object); } } // If it os defined in terms of others in the ontology Set aux2 = ((OWLClass) desc).getSuperClasses(ont); Set aux3 = ((OWLClass) desc).getEquivalentClasses(ont); aux1.addAll(aux2); aux1.addAll(aux3); if (aux1.isEmpty()) return false; else return true; } // An object property that: //1) is used in a restriction in the ontology, OR // 2)it is the inverse of a property that is used in a restriction OR // 3)it has a subProperty or a superProperty that is used in a restr // 4) it is stated to be transitive, functional or inverse functional //Then it is a DL-atom if (atom instanceof OWLRuleObjectPropertyAtom) { OWLObjectProperty prop = ((OWLRuleObjectPropertyAtom) atom) .getProperty(); if (prop.isTransitive(ont) || prop.isSymmetric(ont) || prop.isFunctional(ont)) return true; Set aux = OntologyHelper.entityUsage(ont, prop); // Create a new set without the assetions on the properties Set aux1 = new HashSet(); for (Iterator it = aux.iterator(); it.hasNext();) { OWLObject object = (OWLObject) it.next(); if (!(object instanceof OWLIndividual)) { aux1.add(object); } } Set aux2 = prop.getSuperProperties(ont); Set aux3 = prop.getInverses(ont); aux1.addAll(aux2); aux1.addAll(aux3); if (aux1.isEmpty()) { return false; } else return true; } // If the data property is defined in terms of others in the ontology, // or it has super properties // then it is a DL-atom if (atom instanceof OWLRuleDataPropertyAtom) { OWLDataProperty prop = ((OWLRuleDataPropertyAtom) atom) .getProperty(); Set aux = OntologyHelper.entityUsage(ont, prop); Set aux1 = new HashSet(); for (Iterator it = aux.iterator(); it.hasNext();) { OWLObject object = (OWLObject) it.next(); if (!(object instanceof OWLIndividual)) { aux1.add(object); } } Set aux2 = prop.getSuperProperties(ont); aux1.addAll(aux2); if (aux1.isEmpty()) { return false; } else return true; } return false; } // Write the allog database with the non-dl atoms rules and facts private void writeAllog(OWLRule rule, OWLOntology onto) { try { String allogCons = ""; String allogRule = ""; String allogFact = ""; allogRule = "rule("; OWLRuleAtom consAtom = (OWLRuleAtom) rule.getConsequents() .iterator().next(); allogRule = allogRule.concat(writeAtom(consAtom)); Object[] antecedents = rule.getAntecedents().toArray(); allogRule = allogRule.concat(writeAntecedents(antecedents, onto) + ")." + "\n"); allogStream.write(allogRule); } catch (Exception e) { e.printStackTrace(); } } private String writeAntecedents(Object[] atoms, OWLOntology onto) { String cons = ""; try { int length = atoms.length; OWLRuleAtom atom = (OWLRuleAtom) atoms[0]; if (!(isDLAtom(atom, onto))) { //System.out.println("non-dl-atom " + atom.toString()); if (!(nonDLAtoms.contains(atom.toString()))) { System.out.println("non atom " + atom.toString()); nonDLAtoms.add(atom.toString()); writeFacts(atom, onto); } if (length == 1) cons = cons.concat("," + writeAtom(atom)); else { Object[] atoms1 = new Object[length - 1]; for (int i = 1; i < length; i++) { atoms1[i - 1] = atoms[i]; } cons = cons.concat(",(" + writeAtom(atom) + writeAntecedents(atoms1, onto) + ")"); } } else { if (length == 1) cons = cons.concat("," + writeConstraint(atom)); else { Object[] atoms1 = new Object[length - 1]; for (int i = 1; i < length; i++) { atoms1[i - 1] = atoms[i]; } cons = cons.concat(",(" + writeConstraint(atom) + writeAntecedents(atoms1, onto) + ")"); } } } catch (Exception e) { e.printStackTrace(); } return cons; } private void writeFacts(OWLRuleAtom atom, OWLOntology onto) { try { if (atom instanceof OWLRuleClassAtom) { OWLDescription desc = ((OWLRuleClassAtom) atom) .getDescription(); OWLClass clazz = (OWLClass) desc; Set objects1 = clazz.getUsage(onto); for (Iterator it = objects1.iterator(); it.hasNext();) { OWLEntity entity = (OWLEntity) it.next(); if (entity instanceof OWLIndividual) { allogStream .write("fact(" + clazz.getURI().getFragment() .toLowerCase() + "(" + entity.getURI().getFragment() + ")).\n"); } } } if (atom instanceof OWLRuleObjectPropertyAtom) { OWLObjectProperty prop = (OWLObjectProperty) ((OWLRuleObjectPropertyAtom) atom) .getProperty(); Set objects1 = prop.getUsage(onto); for (Iterator it = objects1.iterator(); it.hasNext();) { OWLEntity entity = (OWLEntity) it.next(); if (entity instanceof OWLIndividual) { Map objects2 = ((OWLIndividual) entity) .getObjectPropertyValues(onto); for (Iterator it1 = ((Set) objects2.get(prop)) .iterator(); it1.hasNext();) { allogStream.write("fact(" + prop.getURI().getFragment().toLowerCase() + "(" + entity.getURI().getFragment() + "," + ((OWLEntity) it1.next()).getURI() .getFragment() + ")).\n"); } } } } if (atom instanceof OWLRuleDataPropertyAtom) { OWLDataProperty prop = (OWLDataProperty) ((OWLRuleDataPropertyAtom) atom) .getProperty(); Set objects1 = prop.getUsage(onto); for (Iterator it = objects1.iterator(); it.hasNext();) { OWLEntity entity = (OWLEntity) it.next(); if (entity instanceof OWLIndividual) { Map objects2 = ((OWLIndividual) entity) .getDataPropertyValues(onto); for (Iterator it1 = ((Set) objects2.get(prop)) .iterator(); it1.hasNext();) { allogStream.write("fact(" + prop.getURI().getFragment().toLowerCase() + "(" + entity.getURI().getFragment() + "," + ((OWLEntity) it1.next()).toString() + ")).\n"); } } } } } catch (Exception e) { e.printStackTrace(); } } private String writeAtom(OWLRuleAtom atom) { String result = ""; try { if (atom instanceof OWLRuleClassAtom) { OWLDescription desc = ((OWLRuleClassAtom) atom) .getDescription(); URI aux = ((OWLClass) desc).getURI(); result = result.concat(aux.getFragment().toLowerCase() + "("); OWLRuleIObject var = ((OWLRuleClassAtom) atom).getArgument(); if (var instanceof OWLRuleIVariable) { URI aux1 = ((OWLRuleIVariable) var).getURI(); result = result = result.concat("'$" + aux1.getFragment().toUpperCase() + "')"); } else { URI aux1 = ((OWLNamedObject) ((OWLRuleIndividual) var)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?