swoopspeciesvalidator.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,398 行 · 第 1/3 页
JAVA
1,398 行
* an <code>URI</code> value * @return a <code>boolean</code> value */ public boolean isOWLDL(URI uri) { boolean result = false; try { /* Handler that's strict about OWLFullExceptions */ syntaxLevel = LITE; OWLRDFErrorHandler handler = new OWLRDFErrorHandler() { public void owlFullConstruct(int code, String message) throws SAXException { /* Doesn't throw an error, but keeps going.... */ setSyntaxLevel(FULL); explain(FULL, code, message); // throw new OWLFullConstructRDFException( message ); } public void error(String message) throws SAXException { throw new SAXException(message.toString()); } public void warning(String message) throws SAXException { message(message.toString()); } public void owlFullConstruct(int code, String message, Object obj) throws SAXException { // TODO Auto-generated method stub } }; OWLOntology o = parseFromURI(handler, uri); int species = species(o, true) | syntaxLevel; result = (species == DL || species == LITE); // releaseOntology( o ); } catch (ParserException ex) { explain(OTHER, UNKNOWN, ex.getMessage()); } catch (OWLException ex) { explain(OTHER, UNKNOWN, ex.getMessage()); } return result; } /** * Returns <code>true</code> if the ontology obtained by parsing the URI * is in OWL Full. Will report findings to the reporter as it goes. Note * that the inner workings of the validator assume that the ontology has * <strong>not</strong> already been parsed. * * @param uri * an <code>URI</code> value * @return a <code>boolean</code> value */ public boolean isOWLFull(URI uri) { /* * An ontology is OWL Full if: * * 1) There are OWL Full constructs used in the syntax, e.g. things have * not been explicitly typed * * or * * 2) The expressivity is Full. * */ boolean result = false; try { /* Handler that doesn't care about OWLFullExceptions */ syntaxLevel = LITE; OWLRDFErrorHandler handler = new OWLRDFErrorHandler() { public void owlFullConstruct(int code, String message) throws SAXException { /* * We know that there's some syntactic Full stuff going on, * but we don't necessarily want to throw an exception as * there may be stuff that comes up later that pushed us out * of Full, e.g. malformed RDF. */ setSyntaxLevel(FULL); explain(FULL, code, message); } public void error(String message) throws SAXException { throw new SAXException(message); } public void warning(String message) throws SAXException { message(message.toString()); } public void owlFullConstruct(int code, String message, Object obj) throws SAXException { // TODO Auto-generated method stub } }; OWLOntology o = parseFromURI(handler, uri); int species = species(o, true) | syntaxLevel; result = (species == DL || species == LITE || species == FULL); // releaseOntology( o ); } catch (ParserException ex) { explain(OTHER, UNKNOWN, ex.getMessage()); } catch (OWLException ex) { explain(OTHER, UNKNOWN, ex.getMessage()); } return result; } /** * Returns <code>true</code> if the ontology is OWL-Lite. Will report * findings to the reporter as it goes. * * * @param ontology * an <code>OWLOntology</code> value * @return a <code>boolean</code> value * @exception OWLException * if an error occurs */ public boolean isOWLLite(OWLOntology ontology) throws OWLException { return species(ontology, true) == LITE; } /** * Returns <code>true</code> if the ontology is OWL-DL. Will report * findings to the reporter as it goes. * * @param ontology * an <code>OWLOntology</code> value * @return a <code>boolean</code> value * @exception OWLException * if an error occurs */ public boolean isOWLDL(OWLOntology ontology) throws OWLException { int species = species(ontology, true); return (species == LITE || species == DL); } /** * Returns <code>true</code> if the ontology is OWL-Full. Will report * findings to the reporter as it goes. * * @param ontology * an <code>OWLOntology</code> value * @return a <code>boolean</code> value * @exception OWLException * if an error occurs */ public boolean isOWLFull(OWLOntology ontology) throws OWLException { int species = species(ontology, true); return (species == LITE || species == DL || species == FULL); } /** * Gather togther all the URIs that are used by this ontology. * */ private void gatherURIs() throws OWLException { /* Initialise the collections. */ this.classURIs = new HashSet(); this.individualURIs = new HashSet(); this.objectPropertyURIs = new HashSet(); this.dataPropertyURIs = new HashSet(); this.annotationPropertyURIs = new HashSet(); this.datatypeURIs = new HashSet(); this.allURIs = new HashSet(); /* Collect together all the URIs */ for (Iterator it = allOntologies.iterator(); it.hasNext();) { OWLOntology onto = (OWLOntology) it.next(); for (Iterator cit = onto.getClasses().iterator(); cit.hasNext();) { OWLNamedObject entity = (OWLNamedObject) cit.next(); classURIs.add(entity.getURI()); allURIs.add(entity.getURI()); } for (Iterator cit = onto.getIndividuals().iterator(); cit.hasNext();) { OWLNamedObject entity = (OWLNamedObject) cit.next(); individualURIs.add(entity.getURI()); allURIs.add(entity.getURI()); } for (Iterator cit = onto.getObjectProperties().iterator(); cit .hasNext();) { OWLNamedObject entity = (OWLNamedObject) cit.next(); objectPropertyURIs.add(entity.getURI()); allURIs.add(entity.getURI()); } for (Iterator cit = onto.getDataProperties().iterator(); cit .hasNext();) { OWLNamedObject entity = (OWLNamedObject) cit.next(); dataPropertyURIs.add(entity.getURI()); allURIs.add(entity.getURI()); } for (Iterator cit = onto.getAnnotationProperties().iterator(); cit .hasNext();) { OWLNamedObject entity = (OWLNamedObject) cit.next(); annotationPropertyURIs.add(entity.getURI()); allURIs.add(entity.getURI()); } for (Iterator cit = onto.getDatatypes().iterator(); cit.hasNext();) { OWLDataType entity = (OWLDataType) cit.next(); datatypeURIs.add(entity.getURI()); allURIs.add(entity.getURI()); } } } /** * Check that namespace separation has been correctly obeyed. In other * words, no URI has been used as both an individual and class name, or * property and class name etc. */ private void checkNamespaceSeparation() { try { /* Check that the collections are all disjoint */ for (Iterator it = classURIs.iterator(); it.hasNext();) { URI u = (URI) it.next(); if (individualURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as Class and Individual"); } else if (objectPropertyURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as Class and ObjectProperty"); } else if (dataPropertyURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as Class and DataProperty"); } else if (annotationPropertyURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as Class and AnnotationProperty"); } else if (datatypeURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as Class and Datatype"); } } for (Iterator it = individualURIs.iterator(); it.hasNext();) { URI u = (URI) it.next(); if (objectPropertyURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as Individual and Property"); } else if (dataPropertyURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as Individual and DataProperty"); } else if (annotationPropertyURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as Individual and AnnotationProperty"); } else if (datatypeURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as Individual and Datatype"); } } for (Iterator it = objectPropertyURIs.iterator(); it.hasNext();) { URI u = (URI) it.next(); if (dataPropertyURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as ObjectProperty and DataProperty"); } else if (annotationPropertyURIs.contains(u)) { namespacesSeparated = false; explain( FULL, SEPARATIONVIOLATION, encodeHLink(u.toString(), myModel.shortForm(u)) + "\t used as ObjectProperty and AnnotationProperty"); } else if (datatypeURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as ObjectProperty and Datatype"); } } for (Iterator it = dataPropertyURIs.iterator(); it.hasNext();) { URI u = (URI) it.next(); if (annotationPropertyURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as DataProperty and AnnotationProperty"); } else if (datatypeURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as DataProperty and Datatype"); } } for (Iterator it = annotationPropertyURIs.iterator(); it.hasNext();) { URI u = (URI) it.next(); if (datatypeURIs.contains(u)) { namespacesSeparated = false; explain(FULL, SEPARATIONVIOLATION, encodeHLink( u.toString(), myModel.shortForm(u)) + "\t used as AnnotationProperty and Datatype"); } } // namespacesSeparated = true; } catch (Exception ex) { ex.printStackTrace(); } return; } /** * Check the level of class axioms. Involves checking whether things like * disjoint axioms have been used. */ private void checkClassAxioms() throws OWLException { classAxiomLevel = LITE; /* Grab all the axioms and check their level */ for (Iterator it = allOntologies.iterator(); it.hasNext();) { OWLOntology onto = (OWLOntology) it.next(); for (Iterator axit = onto.getClassAxioms().iterator(); axit.hasNext();) { OWLObject oo = (OWLObject) axit.next(); SwoopSpeciesValidatorVisitor visitor = new SwoopSpeciesValidatorVisitor( this, objectRenderer); try { oo.accept(visitor); classAxiomLevel = classAxiomLevel | visitor.getLevel(); } catch (OWLException ex) { classAxiomLevel = OTHER; } } } } /** * Check that all individuals have been given at least one type. */ private void checkIndividualTyping() throws OWLException { try { /* Grab all the individuals and check their typing */ Set allIndividuals = new HashSet(); for (Iterator it = allOntologies.iterator(); it.hasNext();) { OWLOntology onto = (OWLOntology) it.next(); for (Iterator indit = onto.getIndividuals().iterator(); indit.hasNext();) { allIndividuals.add(indit.next()); } } for (Iterator it = allIndividuals.iterator(); it.hasNext();) { OWLIndividual i = (OWLIndividual) it.next(); if (i.getTypes(allOntologies).size() == 0) { individualsTyped = false; URI uri = i.getURI(); explain(FULL, UNTYPEDINDIVIDUAL, "Individual with no explicit type: " + encodeHLink(uri.toString(), myModel.shortForm(uri))); } } } catch (Exception ex) { ex.printStackTrace(); throw new OWLException(ex.getMessage()); } } private void checkPropertyAxioms() throws OWLException { /* * There isn't really anything to do here. Property axioms can be in all * species */ propertyAxiomLevel = LITE; }; private void checkExpressivity() throws OWLException { try { /* * Here, we need to look at all the expressions used anywhere within * the ontology and check their level. */ /* For each ontology, we need to check everything within it */ expressivityLevel = LITE; SwoopExpressionValidatorVisitor evv = new SwoopExpressionValidatorVisitor( this, objectRenderer); for (Iterator it = allOntologies.iterator(); it.hasNext();) { OWLOntology onto = (OWLOntology) it.next(); for (Iterator cit = onto.getClasses().iterator(); cit.hasNext();) { OWLClass clazz = (OWLClass) cit.next(); if (!clazz.getEnumerations(onto).isEmpty()) { /* We're in DL. */ expressivityLevel = expressivityLevel | DL; URI uri = clazz.getURI(); explain(DL, ONEOF, "Enumeration used: " + encodeHLink(uri.toString(), myModel.shortForm(uri))); } for (Iterator superit = clazz.getSuperClasses(onto).iterator(); superit.hasNext();) { /* Check the expressivity of any superclasses */ /* * Tricky bit here -- if there's an intersection used at * the top level, we're still ok for LITE. This is *not* * currently catered for, so we will get some stuff * wrong. */ OWLDescription description = (OWLDescription) superit.next(); evv.reset(); evv.setTopLevelDescription(true); try { description.accept(evv); expressivityLevel = expressivityLevel | evv.getLevel(); } catch (OWLException ex) { explain(OTHER, UNKNOWN, ex.getMessage()); expressivityLevel = OTHER; } } for (Iterator superit = clazz.getEquivalentClasses(onto).iterator(); superit.hasNext();) { /* Check the expressivity of any equivalences */ /* * This is tricky, as these expressions *can* be * intersections, as long as they're intersections of * Lite constructs. This is the only place that it can * happen in Lite. */ OWLDescription description = (OWLDescription) superit.next(); evv.reset(); evv.setTopLevelDescription(true); try { description.accept(evv); expressivityLevel = expressivityLevel | evv.getLevel(); } catch (OWLException ex) { explain(OTHER, UNKNOWN, ex.getMessage()); expressivityLevel = OTHER; } } } for (Iterator iit = onto.getObjectProperties().iterator(); iit.hasNext();) { OWLObjectProperty op = (OWLObjectProperty) iit.next(); for (Iterator dit = op.getDomains(onto).iterator(); dit.hasNext();) { /* Check the expressivity of any equivalences */ OWLDescription description = (OWLDescription) dit.next(); evv.reset(); try { description.accept(evv); expressivityLevel = expressivityLevel | evv.getLevel(); } catch (OWLException ex) { explain(OTHER, UNKNOWN, ex.getMessage()); expressivityLevel = OTHER; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?