public void dispose() {
   try {
     reset();
   } catch (OWLException e) {
     logger.severe(e.getMessage());
   }
 }
  /**
   * 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 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 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;
  }
Beispiel #4
0
  public void renderOntology(OWLOntology ontology, Writer writer) throws RendererException {
    try {
      this.pw = new PrintWriter(writer);
      this.allURIs = OntologyHelper.allURIs(ontology);
      this.allOntologies = OntologyHelper.importClosure(ontology);
      this.visitor = new RenderingVisitor(this);
      this.definedURIs = new HashSet();
      generateShortNames();
      writeShortNames();
      sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
      gmt = java.util.TimeZone.getTimeZone("GMT");
      sdf.setTimeZone(gmt);

      doneThing = false;

      random = new Random();
      anonymousIndividuals = new HashMap();
      for (Iterator ontoIt = allOntologies.iterator(); ontoIt.hasNext(); ) {
        OWLOntology onto = (OWLOntology) ontoIt.next();

        pw.println(" ;; Ontology: " + onto.getURI());

        pw.println();
        pw.println(" ;; Classes ");
        pw.println();
        for (Iterator it = orderedEntities(onto.getClasses()).iterator(); it.hasNext(); ) {
          renderClass(onto, (OWLClass) it.next());
        }
        pw.println(" ;; Object Properties ");

        for (Iterator it = orderedEntities(onto.getObjectProperties()).iterator(); it.hasNext(); ) {
          renderObjectProperty(onto, (OWLObjectProperty) it.next());
        }
        pw.println(" ;; Data Properties ");

        for (Iterator it = orderedEntities(onto.getDataProperties()).iterator(); it.hasNext(); ) {
          renderDataProperty(onto, (OWLDataProperty) it.next());
        }
        pw.println(" ;; Individuals ");

        for (Iterator it = orderedEntities(onto.getIndividuals()).iterator(); it.hasNext(); ) {
          renderIndividual(onto, (OWLIndividual) it.next());
        }
        pw.println(" ;; Class Axioms ");

        for (Iterator it = orderedEntities(onto.getDatatypes()).iterator(); it.hasNext(); ) {
          renderDataType(onto, (OWLDataType) it.next());
        }
        pw.println(" ;;_Datatypes ");

        for (Iterator it = orderedEntities(onto.getClassAxioms()).iterator(); it.hasNext(); ) {
          renderClassAxiom((OWLClassAxiom) it.next());
        }
        pw.println(" ;; Property Axioms ");

        for (Iterator it = orderedEntities(onto.getPropertyAxioms()).iterator(); it.hasNext(); ) {
          renderPropertyAxiom((OWLPropertyAxiom) it.next());
        }

        pw.println(" ;; Individual Axioms ");
        for (Iterator it = orderedEntities(onto.getIndividualAxioms()).iterator(); it.hasNext(); ) {
          renderIndividualAxiom((OWLIndividualAxiom) it.next());
        }
      }

    } catch (OWLException ex) {
      throw new RendererException(ex.getMessage());
    }
  }
  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;
            }
          }
          for (Iterator dit = op.getRanges(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;
            }
          }
        }

        for (Iterator iit = onto.getDataProperties().iterator(); iit.hasNext(); ) {
          OWLDataProperty dp = (OWLDataProperty) iit.next();

          for (Iterator dit = dp.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;
            }
          }
          for (Iterator dit = dp.getRanges(onto).iterator(); dit.hasNext(); ) {
            /* Check the expressivity of any equivalences */
            OWLDataRange description = (OWLDataRange) dit.next();
            evv.reset();
            try {
              description.accept(evv);
              expressivityLevel = expressivityLevel | evv.getLevel();
            } catch (OWLException ex) {
              explain(OTHER, UNKNOWN, ex.getMessage());
              expressivityLevel = OTHER;
            }
          }
        }

        for (Iterator iit = onto.getIndividuals().iterator(); iit.hasNext(); ) {
          OWLIndividual ind = (OWLIndividual) iit.next();

          for (Iterator typeit = ind.getTypes(onto).iterator(); typeit.hasNext(); ) {
            /* Check the expressivity of any equivalences */
            OWLDescription description = (OWLDescription) typeit.next();

            evv.reset();
            try {
              description.accept(evv);
              expressivityLevel = expressivityLevel | evv.getLevel();
            } catch (OWLException ex) {
              explain(OTHER, UNKNOWN, ex.getMessage());
              expressivityLevel = OTHER;
            }
          }
        }
      }

      Set complexProperties = evv.getComplexProperties();
      /*
       * Gather all the properties that are known to be functional or
       * inverse functional
       */
      for (Iterator it = allOntologies.iterator(); it.hasNext(); ) {
        OWLOntology onto = (OWLOntology) it.next();
        for (Iterator pit = onto.getObjectProperties().iterator(); pit.hasNext(); ) {
          OWLObjectProperty prop = (OWLObjectProperty) pit.next();
          if (prop.isFunctional(onto) || prop.isInverseFunctional(onto)) {
            complexProperties.add(prop);
          }
        }
      }

      /*
       * We aren't doing everything yet as we still need to grab those
       * that have complex superproperties.
       */

      /*
       * Now check to see if they've been said to be transitive, in which
       * case we're in FULL.
       */
      for (Iterator pit = complexProperties.iterator(); pit.hasNext(); ) {
        OWLObjectProperty prop = (OWLObjectProperty) pit.next();
        for (Iterator it = allOntologies.iterator(); it.hasNext(); ) {
          OWLOntology onto = (OWLOntology) it.next();
          if (prop.isTransitive(onto)) {
            expressivityLevel = FULL;
            URI uri = prop.getURI();
            explain(
                FULL,
                COMPLEXTRANSITIVE,
                "Complex property "
                    + encodeHLink(uri.toString(), myModel.shortForm(uri))
                    + " asserted to be transitive.");
          }
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new OWLException(ex.getMessage());
    }
  }
  /**
   * 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;
  }