swoopspeciesvalidator.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,398 行 · 第 1/3 页
JAVA
1,398 行
package org.mindswap.swoop.renderer.ontology;import java.io.Reader;import java.net.URI;import java.net.URISyntaxException;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import org.apache.commons.lang.StringEscapeUtils;import org.mindswap.swoop.SwoopModel;import org.semanticweb.owl.io.ParserException;import org.semanticweb.owl.io.abstract_syntax.ObjectRenderer;import org.semanticweb.owl.io.owl_rdf.OWLRDFErrorConstants;import org.semanticweb.owl.io.owl_rdf.OWLRDFErrorHandler;import org.semanticweb.owl.io.owl_rdf.OWLRDFParser;import org.semanticweb.owl.io.vocabulary.OWLVocabularyAdapter;import org.semanticweb.owl.model.OWLClass;import org.semanticweb.owl.model.OWLDataProperty;import org.semanticweb.owl.model.OWLDataRange;import org.semanticweb.owl.model.OWLDataType;import org.semanticweb.owl.model.OWLDescription;import org.semanticweb.owl.model.OWLException;import org.semanticweb.owl.model.OWLIndividual;import org.semanticweb.owl.model.OWLNamedObject;import org.semanticweb.owl.model.OWLObject;import org.semanticweb.owl.model.OWLObjectProperty;import org.semanticweb.owl.model.OWLOntology;import org.semanticweb.owl.model.helper.OntologyHelper;import org.semanticweb.owl.util.OWLConnection;import org.semanticweb.owl.util.OWLManager;import org.semanticweb.owl.validation.SpeciesValidatorReporter;import org.xml.sax.SAXException;//import uk.ac.man.cs.img.owl.validation.SpeciesValidator;/** * @author Evren Sirin modiefied by Dave */public class SwoopSpeciesValidator implements org.semanticweb.owl.validation.SpeciesValidator, org.semanticweb.owl.validation.OWLValidationConstants { // popular registered URI transfer scheme names (see // http://www.iana.org/assignments/uri-schemes) // private static final String [] POPULAR_SCHEME_NAMES = {"http://"};//, // "ftp://", "gopher://", "https://", "tftp://" }; private static final String SPACE = " "; private static final String LT = "<"; private static final String GT = ">"; protected static final int OTHER = 7; /* 0111 */ protected SwoopModel myModel = null; private SpeciesValidatorReporter reporter; private Map options; private OWLConnection connection; private OWLOntology ontology; private Set allOntologies; private Set reservedVocabulary; /* * Those things from the reserved vocabulary that can be defined as classes. * rdf:Statement, rdf:Seq, rdf:Bag, and rdf:Alt */ private Set classOnlyVocabulary; /* * Those things from the reserved vocabulary that can be defined as classes. * rdf:subject, rdf:predicate, rdf:object, and all the container membership * properties, i.e., rdf:_1, rdf:_2, etc. */ private Set propertyOnlyVocabulary; /** * Indicates whether the separation of names for classes, individuals and * properties has been observed */ private boolean namespacesSeparated; /** * Indicates whether the conditions regarding the usage and redefinition of * elements from the RDF, RDFS and OWL vocabularies have been observed. */ private boolean correctOWLUsage; private boolean correctOWLNamespaceUsage; /** Indicates whether all individuals have at least one explicit type. */ private boolean individualsTyped; /* * Indicates whether classAxioms from a particular species are used. */ private int classAxiomLevel; /* * Indicates whether propertyAxioms from a particular species are used. */ private int propertyAxiomLevel; /* * Indicates the level of expressivity used in expressions within the * ontology. */ private int expressivityLevel; /* * Indicates whether syntactic constraints have been violated which pushes * us into a particular level, e.g. untyped URIs => Full */ private int syntaxLevel; /* Collections of URIs taken from the ontology. */ private Set allURIs; private Set classURIs; private Set individualURIs; private Set objectPropertyURIs; private Set dataPropertyURIs; private Set annotationPropertyURIs; /* * There may still be some nasty corner cases that slip through the net * here.... */ private Set datatypeURIs; private ObjectRenderer objectRenderer; private OWLRDFParser parser; /** * Create a new validator. Will report to stdout by default. * * */ public SwoopSpeciesValidator(SwoopModel model) throws OWLException { myModel = model; /* Sets up a default reporter that writes to stdout. */ setReporter(new SpeciesValidatorReporter() { public void ontology(OWLOntology onto) { // System.out.println( onto.getURI() ); } public void done(String str) { // System.out.println( str ); } public void message(String str) { System.out.println(str); } public void explain(int l, int code, String str) { System.out.println(level(l) + " [" + readableCode(code) + "]:\t" + str); // System.out.println( level( l ) + ":\t" + str ); } }); reservedVocabulary = OWLVocabularyAdapter.INSTANCE.getReservedVocabulary(); classOnlyVocabulary = new HashSet(); classOnlyVocabulary.add(OWLVocabularyAdapter.INSTANCE.getStatement()); classOnlyVocabulary.add(OWLVocabularyAdapter.INSTANCE.getSeq()); classOnlyVocabulary.add(OWLVocabularyAdapter.INSTANCE.getBag()); classOnlyVocabulary.add(OWLVocabularyAdapter.INSTANCE.getAlt()); options = new HashMap(); /* Get a default connection */ connection = OWLManager.getOWLConnection(); parser = new OWLRDFParser(); /* * Tell the parser to ignore annotation content. This is not needed for * validation. */ Map options = new HashMap(); options.put("includeAnnotationContent", new Boolean(false)); parser.setOptions(options); } /** * Set the connection (e.g. the implementation that the validator will * choose to use when constructing ontologies. */ public void setConnection(OWLConnection connection) { this.connection = connection; } /** * Set the reporter that this speciesValidator will use. By default, the * validator will write to stdout. If you want to stop this happening, set * the reporter to null */ public void setReporter(SpeciesValidatorReporter reporter) { this.reporter = reporter; } /** * Provide an explanation as to why the validator considers the ontology to * be in a particular species. * * @param l * an <code>int</code> value * @param str * a <code>String</code> value */ public void explain(int l, int code, String str) { if (reporter != null) { reporter.explain(l, code, str); } } /** * Write a message. * * @param str * a <code>String</code> value */ public void message(String str) { if (reporter != null) { reporter.message(str); } } protected static String level(int l) { if (l == LITE) { return "OWL-Lite"; } else if (l == DL) { return "OWL-DL "; } else if (l == FULL) { return "OWL-Full"; } else { return "OTHER "; } } /** * Set the ontology that the validator will work with. Note that this * performs some initialisation. This particular implementation does not * track ontology changes, so if the ontology is changed before validation * takes place, the results may not be as expected. * * * * @param ontology * an <code>OWLOntology</code> value * @param checkImport * if true, grab the imports closure and check the species of any * imported ontologies. If false, just look here. Allows us to * catch situations where an ontology is imported that has a * higher expressivity, but the classes involved in that aren't * explicitly used in the importer. */ private int species(OWLOntology ontology, boolean checkImport) { int result = LITE; try { this.ontology = ontology; // logger.info( "Validating: " // + (checkImport?"[imports] ":"") // + ontology.getURI() ); if (reporter != null) { reporter.ontology(ontology); } /* Find the import closure */ this.allOntologies = OntologyHelper.importClosure(ontology); /* Do some initial processing */ gatherURIs(); /* Set up all the variables */ this.namespacesSeparated = true; this.correctOWLUsage = true; this.correctOWLNamespaceUsage = true; this.individualsTyped = true; this.classAxiomLevel = FULL; this.propertyAxiomLevel = FULL; this.expressivityLevel = FULL; /* A helper used when reporting stuff. */ objectRenderer = new ObjectRenderer(ontology); /* Now do all the relevant checks */ checkNamespaceSeparation(); checkCorrectOWLUsage(); checkCorrectOWLNamespaceUsage(); /* This should be done during parsing */ // checkIndividualTyping(); checkClassAxioms(); checkPropertyAxioms(); checkExpressivity(); if (!correctOWLNamespaceUsage) { /* * If there are things in the OWL namespace, we're in OTHER. See * http://lists.w3.org/Archives/Public/www-webont-wg/2003Feb/0157.html */ /* * This doesn't seem right though. I think it's actually the * case that any RDF document is an OWL FULL document. See * Section 1.3 of the Overview. */ // result = OTHER; result = FULL; } else if (!namespacesSeparated || !correctOWLUsage || !individualsTyped) { /* * If namespaces aren't separated, or redefinitions have * occurred, or individuals aren't all explicitly typed, we're * in Full */ result = FULL; } else { /* * Otherwise, it's the highest level that's used for classes, * properties and expressivity */ result = (classAxiomLevel | propertyAxiomLevel | expressivityLevel); } if (reporter != null) { reporter.done(level(result)); } } catch (OWLException e) { result = FULL; reporter.explain(FULL, UNKNOWN, "Exception occurred: " + e.getMessage()); } return result; } /** * Set options for this validator * * @param options * a <code>Map</code> value. Should contain a map from * {@link String String}s to {@link String String}s. */ public void setOptions(Map options) { options = new HashMap(options); }; /** * * Get options for this validator * * @return a <code>Map</code> value. Contains a map from * {@link String String}s to {@link String String}s. */ public Map getOptions() { return options; } /** Record the fact that some particular syntax has been noticed. */ private void setSyntaxLevel(int l) { syntaxLevel = l; } /** * Parse an ontology from a given URI. * * @param handler * an <code>OWLRDFErrorHandler</code> value * @param uri * an <code>URI</code> value * @return an <code>OWLOntology</code> value * @exception ParserException * if an error occurs * @exception OWLException * if an error occurs */ private OWLOntology parseFromURI(OWLRDFErrorHandler handler, URI uri) throws ParserException, OWLException { parser.setConnection(connection); /* Error handler for the parser */ parser.setOWLRDFErrorHandler(handler); // OWLOntology onto = connection.createOWLOntology( uri,uri ); OWLOntology onto = parser.parseOntology(uri); // message( onto.toString() ); return onto; } /** * Returns <code>true</code> if the ontology obtained by parsing the URI * is in OWL Lite. 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 isOWLLite(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 == 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 DL. 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
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?