rulesexpressivity.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,433 行 · 第 1/3 页
JAVA
1,433 行
.getIndividual()).getURI(); result = result.concat(aux1.getFragment() + ")"); } } if (atom instanceof OWLRuleObjectPropertyAtom) { OWLObjectProperty prop = ((OWLRuleObjectPropertyAtom) atom) .getProperty(); URI aux = prop.getURI(); result = result.concat(aux.getFragment().toLowerCase() + "("); OWLRuleIObject var = ((OWLRuleObjectPropertyAtom) atom) .getFirstArgument(); if (var instanceof OWLRuleIVariable) { URI aux1 = ((OWLRuleIVariable) var).getURI(); result = result.concat("'$" + aux1.getFragment().toUpperCase() + "',"); } else { URI aux1 = ((OWLNamedObject) ((OWLRuleIndividual) var) .getIndividual()).getURI(); result = result.concat(aux1.getFragment() + ","); } OWLRuleIObject var2 = ((OWLRuleObjectPropertyAtom) atom) .getSecondArgument(); if (var2 instanceof OWLRuleIVariable) { URI aux1 = ((OWLRuleIVariable) var2).getURI(); result = result.concat("'$" + aux1.getFragment().toUpperCase() + "')"); } else { URI aux1 = ((OWLNamedObject) ((OWLRuleIndividual) var2) .getIndividual()).getURI(); result = result.concat(aux1.getFragment() + ")"); } } if (atom instanceof OWLRuleDataPropertyAtom) { OWLDataProperty prop = ((OWLRuleDataPropertyAtom) atom) .getProperty(); URI aux = prop.getURI(); result = result = result.concat(aux.getFragment().toLowerCase() + "("); OWLRuleDObject var = (OWLRuleDObject) ((OWLRuleDataPropertyAtom) atom) .getFirstArgument(); if (var instanceof OWLRuleDVariable) { URI aux1 = ((OWLRuleIVariable) var).getURI(); result = result.concat("'$" + aux1.getFragment().toUpperCase() + "',"); } else { URI aux1 = ((OWLNamedObject) ((OWLRuleIndividual) var) .getIndividual()).getURI(); result = result.concat(aux1.getFragment() + ","); } OWLRuleDObject var2 = ((OWLRuleDataPropertyAtom) atom) .getSecondArgument(); if (var2 instanceof OWLRuleIVariable) { URI aux1 = ((OWLRuleIVariable) var2).getURI(); result = result.concat("'$" + aux1.getFragment().toUpperCase() + "')"); } else { String aux1 = ((OWLRuleDataValue) var).toString(); result = result.concat(aux1 + ")"); } } } catch (Exception e) { e.printStackTrace(); } return result; } // Writing Allog constraint atoms private String writeConstraint(OWLRuleAtom atom) { String result = ""; try { if (atom instanceof OWLRuleClassAtom) { OWLDescription desc = ((OWLRuleClassAtom) atom) .getDescription(); URI aux = ((OWLClass) desc).getURI(); result = result.concat("restriction("); 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) .getIndividual()).getURI(); result = result.concat("'" + aux1.getFragment() + "',"); } result = result.concat("'" + aux.getFragment() + "')"); } } catch (Exception e) { e.printStackTrace(); } return result; } /* * (non-Javadoc) * * @see org.mindswap.swoop.SwoopModelListener#modelChanged(org.mindswap.swoop.ModelChangeEvent) */ public void modelChanged(ModelChangeEvent event) { // TODO Auto-generated method stub if ((swModel.getEnableRules()) && (event.getType() == ModelChangeEvent.ONTOLOGY_LOADED || event.getType() == ModelChangeEvent.ONTOLOGY_SEL_CHANGED || event.getType() == ModelChangeEvent.ONTOLOGY_RELOADED || event .getType() == ModelChangeEvent.ONTOLOGY_CHANGED)) { try { this.setRulesExpress(); } catch (OWLException e) { e.printStackTrace(); } } } public Set getRules(OWLObject obj) { if (ruleMap.containsKey(obj)) { return (HashSet) ruleMap.get(obj); } else return new HashSet(); } public String publishRulesToPychinko() { // need to print out header code here String n3 = "@prefix log: <http://www.w3.org/2000/10/swap/log#>.\n" + "@prefix str: <http://www.w3.org/2000/10/swap/string#>.\n" + "@prefix owl: <http://www.w3.org/2002/07/owl#>.\n" + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.\n" + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.\n" + "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.\n" + "@prefix : <http://counterterror.mindswap.org/2005/terrorism.owl#>.\n"; HashSet rules = (HashSet) getRulesFromMap(ruleMap); Iterator it = rules.iterator(); try { while (it.hasNext()) { n3 += "\n{ "; RuleValue rv = (RuleValue) it.next(); OWLRule rule = rv.getRule(); // print out the whole URI for predicates and classes // fragments for avariables only HashSet antecedents = (HashSet) rule.getAntecedents(); Iterator antIterator = antecedents.iterator(); while (antIterator.hasNext()) { OWLRuleAtom ant = (OWLRuleAtom) antIterator.next(); n3 += renderRuleAtom(ant); } // --------consequents n3 += "}\n=> \n{"; OWLRuleAtom consequent = (OWLRuleAtom) rule.getConsequents().iterator().next(); n3 += renderRuleAtom(consequent); n3 += "}.\n"; String consequentPredicate = getURIString( consequent ); String label = getConsequentLabel( consequent ); if ( label != null ) n3 += "<" + consequentPredicate + ">" + " rdfs:label " + "\"" + label + "\" ."; else n3 += "<" + consequentPredicate + ">" + " rdfs:label " + "\"" + guessConsequentLabel( consequent ) + "\" ."; } } catch (Exception e) { System.out.println(e.getMessage()); n3 += "\nERROR!"; } return n3; } public String toN3( OWLRule rule ) { // need to print out header code here String n3 = "@prefix log: <http://www.w3.org/2000/10/swap/log#>.\n" + "@prefix str: <http://www.w3.org/2000/10/swap/string#>.\n" + "@prefix owl: <http://www.w3.org/2002/07/owl#>.\n" + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.\n" + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.\n" + "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.\n"; try { n3 += "\n{ "; // print out the whole URI for predicates and classes // fragments for avariables only HashSet antecedents = (HashSet) rule.getAntecedents(); Iterator antIterator = antecedents.iterator(); while (antIterator.hasNext()) { OWLRuleAtom ant = (OWLRuleAtom) antIterator.next(); n3 += renderRuleAtom(ant); } // --------consequents n3 += "}\n=> \n{"; OWLRuleAtom consequent = (OWLRuleAtom) rule.getConsequents().iterator().next(); n3 += renderRuleAtom(consequent); n3 += "}.\n"; String consequentPredicate = getURIString( consequent ); String label = getLabel( rule ); n3 += "<" + consequentPredicate + ">" + " rdfs:label " + "\"" + label + "\" ."; } catch (Exception e) { System.out.println(e.getMessage()); n3 += "\nERROR!"; } return n3; } public String getPredicate( OWLRule rule ) { String consequentPredicate = null; try { OWLRuleAtom consequent = (OWLRuleAtom) rule.getConsequents().iterator().next(); consequentPredicate = getURIString( consequent ); } catch ( Exception e ) { e.printStackTrace(); } return consequentPredicate; } public String getLabel( OWLRule rule ) { String label = null; try { OWLRuleAtom consequent = (OWLRuleAtom) rule.getConsequents().iterator().next(); label = getConsequentLabel( consequent ); if ( label == null ) label = guessConsequentLabel( consequent ); } catch ( Exception e ) { e.printStackTrace(); } return label; } /* * Returns label for the given consequent (corresponding to SWOOP's rdfs:label for that entity) * If it doesn't exist, it returns null; */ private String getConsequentLabel( OWLRuleAtom atom ) { String label = null; try { // adding labels for rules if ( atom instanceof OWLRuleClassAtom ) { OWLDescription desc = ((OWLRuleClassAtom) atom ).getDescription(); URI classURI = ((OWLClass) desc).getURI(); Set set = desc.getAnnotations(); for (Iterator iter = set.iterator(); iter.hasNext();) { OWLAnnotationInstance oai = (OWLAnnotationInstance) iter.next(); URI uri = oai.getProperty().getURI(); if ( uri.toString().equals("http://www.w3.org/2000/01/rdf-schema#label") ) label = oai.getContent().toString(); } } else if ( atom instanceof OWLRuleObjectPropertyAtom ) { OWLObjectProperty prop = ((OWLRuleObjectPropertyAtom) atom).getProperty(); URI propURI = prop.getURI(); Set set = prop.getAnnotations(); for (Iterator iter = set.iterator(); iter.hasNext();) { OWLAnnotationInstance oai = (OWLAnnotationInstance) iter.next(); URI uri = oai.getProperty().getURI(); if ( uri.toString().equals("http://www.w3.org/2000/01/rdf-schema#label") ) { label = oai.getContent().toString(); } } } else // OWLRuleDataPropertyAtom { OWLDataProperty prop = ((OWLRuleDataPropertyAtom) atom).getProperty(); URI propURI = prop.getURI(); Set set = prop.getAnnotations(); for (Iterator iter = set.iterator(); iter.hasNext();) { OWLAnnotationInstance oai = (OWLAnnotationInstance) iter.next(); URI uri = oai.getProperty().getURI(); if ( uri.toString().equals("http://www.w3.org/2000/01/rdf-schema#label") ) label = oai.getContent().toString(); } } } catch ( Exception e ) { e.printStackTrace();} return label; } private String guessConsequentLabel( OWLRuleAtom consequent ) { String uriStr = getURIString( consequent ); int ind = uriStr.indexOf( "#" ); String guess = ""; if ( !( ind < 0 ) ) guess = uriStr.substring( ind + 1 ); else { ind = uriStr.lastIndexOf( "/" ); guess = uriStr.substring( ind + 1 ); } return guess; } private String getURIString( OWLRuleAtom atom ) { try { if ( atom instanceof OWLRuleClassAtom ) { OWLDescription desc = ((OWLRuleClassAtom) atom ).getDescription(); URI classURI = ((OWLClass) desc).getURI(); return classURI.toString(); } else if ( atom instanceof OWLRuleObjectPropertyAtom ) { OWLObjectProperty prop = ((OWLRuleObjectPropertyAtom) atom).getProperty(); URI propURI = prop.getURI(); return propURI.toString(); } else // OWLRuleDataPropertyAtom { OWLDataProperty prop = ((OWLRuleDataPropertyAtom) atom).getProperty(); URI propURI = prop.getURI(); return propURI.toString(); } } catch ( Exception e) { e.printStackTrace(); } return null; } /** * @param consequent * @return n3 representatio of the ruleatom */ private String renderRuleAtom(OWLRuleAtom atom) { String result = ""; try { if (atom instanceof OWLRuleClassAtom) { OWLDescription desc = ((OWLRuleClassAtom) atom).getDescription(); URI classURI = ((OWLClass) desc).getURI(); OWLRuleIObject var = ((OWLRuleClassAtom) atom).getArgument(); if (var instanceof OWLRuleIVariable) { // "?x rdf:type b" String varName = ((OWLRuleIVariable) var).getURI().toString(); result = result = result.concat("?" + varName + " rdf:type "); result = result.concat(" <" + classURI + "> "); } else { //not a variable: x rdf:type w URI indURI = ((OWLNamedObject) ((OWLRuleIndividual) var).getIndividual()).getURI(); result = result.concat("< " + indURI + "> rdf:Type "); result = result.concat(" <" + classURI + "> "); } } if (atom instanceof OWLRuleObjectPropertyAtom) { OWLObjectProperty prop = ((OWLRuleObjectPropertyAtom) atom).getProperty(); URI propURI = prop.getURI(); OWLRuleIObject var = ((OWLRuleObjectPropertyAtom) atom).getFirstArgument(); if (var instanceof OWLRuleIVariable) { String varName = ((OWLRuleIVariable) var).getURI().toString(); result = result.concat("?" + varName + " "); } else { URI indURI = ((OWLNamedObject) ((OWLRuleIndividual) var).getIndividual()).getURI(); result = result.concat("< " + indURI + "> "); } result = result.concat("<" + propURI + "> "); var = ((OWLRuleObjectPropertyAtom) atom).getSecondArgument(); if (var instanceof OWLRuleIVariable) { String varName = ((OWLRuleIVariable) var).getURI().toString(); result = result.concat("?" + varName + " "); } else { URI indURI = ((OWLNamedObject) ((OWLRuleIndividual) var).getIndividual()).getURI(); result = result.concat("< " + indURI + "> "); } } if (atom instanceof OWLRuleDataPropertyAtom) { /* OWLDataProperty prop = ((OWLRuleDataPropertyAtom) atom) .getProperty(); URI aux = prop.getURI(); result = result = result.concat(aux.getFragment().toLowerCase() + "("); OWLRuleDObject var = (OWLRuleDObject) ((OWLRuleDataPropertyAtom) atom) .getFirstArgument(); if (var instanceof OWLRuleDVariable) { URI aux1 = ((OWLRuleIVariable) var).getURI(); result = result.concat("'$" + aux1.getFragment().toUpperCase() + "',"); } else { URI aux1 = ((OWLNamedObject) ((OWLRuleIndividual) var) .getIndividual()).getURI(); result = result.concat(aux1.getFragment() + ","); } OWLRuleDObject var2 = ((OWLRuleDataPropertyAtom) atom) .getSecondArgument(); if (var2 instanceof OWLRuleIVariable) { URI aux1 = ((OWLRuleIVariable) var2).getURI(); result = result.concat("'$" + aux1.getFragment().toUpperCase() + "')"); } else { String aux1 = ((OWLRuleDataValue) var).toString(); result = result.concat(aux1 + ")"); } */ } } catch (Exception e) { e.printStackTrace(); } result = result.concat(".\n"); return result; } /** * @param rule */ public void removeRule(RuleValue ruleValue, OWLRuleAtom consAtom ) { HashSet rulesSet = new HashSet(); OWLObject key = null; Set antecedents; Set consequents; try { antecedents = ruleValue.getRule().getAntecedents(); consequents = ruleValue.getRule().getConsequents(); if (consAtom instanceof OWLRuleClassAtom) { key = ((OWLRuleClassAtom) consAtom).getDescription(); } else { if (consAtom instanceof OWLRuleDataPropertyAtom) { key = ((OWLRuleDataPropertyAtom) consAtom).getProperty(); } else { if (consAtom instanceof OWLRuleObjectPropertyAtom) { key = ((OWLRuleObjectPropertyAtom) consAtom) .getProperty(); } } } if (ruleMap.containsKey(key)) rulesSet = (HashSet) ruleMap.get(key); rulesSet.remove(ruleValue); ruleMap.put(key, rulesSet); } catch (Exception e) { e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?