Examples of Use

Alexander Paar

Examples of Use

Examples for the advantage of OWL aware compilation in the Zhi# programming language over an API-based use of ontology management systems can be shown based on the following programming tasks, which are all frequent for ontology-based applications. Assume the following TBox.

TBox

  • Task 1: Make an ontology available in a computer program.
  • Task 2: Create individual instances a, o, b, and c of classes Top, B, and C.
  • Task 3: Add individual o as a value for property R of individual a.
  • Task 4a): List the RDF types of individual o.
  • Task 4b): Check whether individual o is included by class description B.
  • Task 4c): List all individuals in the extension of class description B.
  • Task 5: Add individual c as a value for property R of individual a, which causes an inconsistent ABox since class descriptions B (range of property R) and C (which includes c) are disjoint.
  • Task 6: Add individual b as a value for property R of individual a and test if individuals o and b are equal (i.e. is there an inferred sameAs(o, b) statement in the ontology?).
  • Task 7: Add literals 23, -23, and string literal NaN as values for property U of individual o, where -23 and NaN are invalid values for the given TBox.

Zhi#-based implementation

import OWL ont = http://www.zhimantic.com/eval;
import XML xsd = http://www.w3.org/2001/XMLSchema;
namespace N {
  class Program {
    static void Main () {
      #owl#Thing a = new #owl#Thing("http://www.w3.org/2002/07/owl#a");
      #owl#Thing o = new #owl#Thing("http://www.w3.org/2002/07/owl#o");
      #ont#B b = new #ont#B("http://www.zhimantic.com/eval#b");
      #ont#C c = new #ont#C("c");
      a.#ont#R = o;
      foreach (string T in o.Types) {
        Console.WriteLine(T);
      }
      Console.WriteLine(o + " is" + (o is #ont#B ? "" : " not") + " a 'B'!");
      foreach(#ont#B v in #ont#B.Individuals) {
        Console.WriteLine(v);
      }
      a.#ont#R = c; // Compile-time error in Zhi#!
      a.#ont#R = b;
      Console.WriteLine("Individuals 'o' and 'b' are" + (o == b ? "" : " not") +
                            " identical!");
      #xsd#positiveInteger xpi = 23;
      #xsd#integer xi = -23;
      #xsd#string xs = "NaN";
      o.#ont#U = xpi;
      o.#ont#U = xi; // Compile-time error in Zhi#!
      o.#ont#U = xs; // Compile-time error in Zhi#!
      }
    }
  }

Jena-based Implementation

import Jena and Pellet namespaces;

public class Program {
  public static void main(String[] args) {
    String eval = "http://www.zhimantic.com/eval#";

    OntModel m = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
    m.read(new FileInputStream("Evaluation.owl"), "");

    Individual a = m.getOntClass("http://www.w3.org/2002/07/owl#Thing").
      createIndividual(eval + "a");
    Individual o = m.getOntClass("http://www.w3.org/2002/07/owl#Thing").
      createIndividual(eval + "o");
    Individual b = m.getOntClass(eval + "B").createIndividual(eval + "b");
    Individual c = m.getOntClass(eval + "C").createIndividual(eval + "c");

    a.addProperty(m.getObjectProperty(eval + "R"), o);

    for (Iterator it = o.listRDFTypes(false); it.hasNext();) {
      System.out.println(((Resource) it.next()).getURI());
    }

    System.out.println(o + " is" + (m.contains(b, RDF.type, m.getOntClass(eval + "B")) ?
      "" : " not") + " a 'B'!");

    for (Iterator it = m.getOntClass(eval + "B").listInstances(); it.hasNext();) {
      System.out.println(((Individual) it.next()).getURI());
    }

    // Runtime error with Jena! Program would not execute properly beyond this point.
    a.addProperty(m.getObjectProperty(eval + "R"), c);

    a.addProperty(m.getObjectProperty(eval + "R"), b);
    System.out.println("Individuals 'o' and 'b' are" + (o.isSameAs(b) ?
      "" : " not") + " identical!");
    // Equality test with *isSameAs* is not symmetric:
    // b.isSameAs(null) evaluates to true,
    // null.isSameAs(b) evaluates to a NullPointerException!
    System.out.println("b == null: " + b.isSameAs(null));

    // Invalid statements with properties -23 and NaN are all added...
    o.addProperty(m.getDatatypeProperty(eval + "T"),
      m.createTypedLiteral("23", "http://www.w3.org/2001/XMLSchema#positiveInteger"));
    o.addProperty(m.getDatatypeProperty(eval + "T"),
      m.createTypedLiteral("-23", "http://www.w3.org/2001/XMLSchema#positiveInteger"));
    o.addProperty(m.getDatatypeProperty(eval + "T"),
      m.createTypedLiteral("NaN", "http://www.w3.org/2001/XMLSchema#positiveInteger"));

    // ... but the following query fails at runtime if invalid statements were added!
    for (Iterator it = o.listPropertyValues(m.getDatatypeProperty(eval + "T"));
           it.hasNext();) {
      Literal v = (Literal) it.next();
      System.out.println(v.getValue() + " of type " + v.getDatatypeURI());
    }
  }
}

Discussion


Anonymous

Cancel  Add attachments