Zhi# Wiki
Brought to you by:
alexpaar
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.
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#!
}
}
}
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());
}
}
}
Anonymous