001
002 package examples;
003
004 import com.hp.hpl.jena.ontology.HasValueRestriction;
005 import com.hp.hpl.jena.ontology.Individual;
006 import com.hp.hpl.jena.ontology.OntClass;
007 import com.hp.hpl.jena.ontology.OntModel;
008 import com.hp.hpl.jena.ontology.OntModelSpec;
009 import com.hp.hpl.jena.ontology.OntProperty;
010 import com.hp.hpl.jena.rdf.model.ModelFactory;
011 import com.hp.hpl.jena.rdf.model.NodeIterator;
012 import com.hp.hpl.jena.rdf.model.RDFList;
013 import com.hp.hpl.jena.rdf.model.RDFNode;
014 import com.hp.hpl.jena.rdf.model.Resource;
015 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
016 import com.hp.hpl.jena.vocabulary.OWL;
017 import dlejena.DLEJenaParameters;
018 import dlejena.DLEJenaReasoner;
019
020 /**
021 * This example shows how the Jena API can be used in order to
022 * create an ontology and to load it in DLEJena, in contrast to the
023 * LoadOntology.java example where the ontology is loaded from a file.
024 * DLEJena can fully exploit the well-known Jena API methods.
025 *
026 * @author Georgios Meditskos <gmeditsk@csd.auth.gr>
027 */
028 public class CreateOntology {
029
030 public static void main(String[] args) {
031
032 /*
033 * Create the base model for the asserted ontology axioms. Note that
034 * this model should not define any reasoning infrustructure. We just want
035 * a simple OntModel that will store the ontology (e.g. OWL_MEM).
036 */
037 OntModel base = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
038
039 /*
040 * Set basic ontology properties
041 */
042 base.createOntology("http://dlejena/examples/createOntology.owl");
043 String uri = "http://dlejena/examples/createOntology.owl#";
044 base.setNsPrefix("", uri);
045
046 /*
047 * Define the ontology axioms. We create an ontology that defines the
048 * class Man to be equivalent to the intersection of the class Human and
049 * of the class of all instances that have the value 'male' in the 'hasSex'
050 * property (restriction class). Furthermore, we define the class Sex
051 * as the enumeration of the instances 'male' and 'female'.
052 */
053 OntClass man = base.createClass(uri + "Man");
054 OntClass human = base.createClass(uri + "Human");
055 Individual male = base.createIndividual(uri + "male", OWL.Thing);
056 Individual female = base.createIndividual(uri + "female", OWL.Thing);
057 RDFList enums = base.createList(); enums = enums.cons(male); enums = enums.cons(female);
058 OntClass sex = base.createEnumeratedClass(uri + "Sex", enums);
059 OntProperty hasSex = base.createObjectProperty(uri + "hasSex");
060 HasValueRestriction hasValue = base.createHasValueRestriction(null, hasSex, male);
061 RDFList inters = base.createList(); inters = inters.cons(human); inters = inters.cons(hasValue);
062 man.addEquivalentClass(base.createIntersectionClass(null, inters));
063 Individual gmeditsk = base.createIndividual(uri + "gmeditsk", OWL.Thing);
064 gmeditsk.addProperty(hasSex, male);
065 gmeditsk.addRDFType(human);
066
067 /*
068 * Define an instance of the DLEJena reasoner.
069 * Register the base OntModel and initialize the reasoner
070 */
071 DLEJenaReasoner dle = new DLEJenaReasoner();
072 dle.register(base);
073 dle.initialize();
074
075 /*
076 * Get references to the ABox and TBox models
077 */
078 OntModel tbox = dle.getTBox();
079 OntModel abox = dle.getABox();
080
081 /*
082 * All the TBox-related queries should use the TBox inferred model
083 */
084 System.out.println("");
085 System.out.println("The named superclasses of the class Man:");
086
087 //Caution: The refernce to the Man OntClass should be updated in order to refer to
088 //the inferred tbox (and not to the base model). The same holds for the other resources as well.
089 man = tbox.getOntClass(uri + "Man");
090 ExtendedIterator superclasses = man.listSuperClasses();
091 while (superclasses.hasNext()) {
092 OntClass c = (OntClass) superclasses.next();
093 if (!c.isAnon())
094 System.out.println(" - " + c.getURI());
095 }
096
097 /*
098 * Get the inferred types of the instance gmeditsk. This query should use the ABox
099 * inferred model of the dle reasoner, since it is an ABox-related query
100 */
101 System.out.println("");
102 System.out.println("All the named classes where gmeditsk belongs to:");
103
104 /*
105 * The listRDFTypes method should be used instead of the listOntClasses
106 * method in order to retrieve the types of an instance, treating the
107 * results as Resources. Otherwise, an exception will be thrown, since
108 * Jena would not be able to view (cast) any restriction class as an OntClass
109 * (this information exists in the TBox model).
110 */
111 gmeditsk = abox.getIndividual(uri + "gmeditsk");
112 ExtendedIterator types = gmeditsk.listRDFTypes(false);
113 while (types.hasNext()) {
114 Resource type = (Resource) types.next();
115 if (!type.isAnon())
116 System.out.println(" - " + type.getURI());
117 }
118
119 /*
120 * Retrieve the values of the property hasSex of the gmeditsk instance
121 */
122 hasSex = tbox.getOntProperty(uri + "hasSex");
123 System.out.println("");
124 System.out.println("gmeditsk has sex: ");
125 NodeIterator values = gmeditsk.listPropertyValues(hasSex);
126 while (values.hasNext()) {
127 RDFNode value = (RDFNode) values.next();
128 System.out.println(" - " + value.toString());
129 }
130 }
131 }
|