001 package examples;
002
003 import com.hp.hpl.jena.ontology.Individual;
004 import com.hp.hpl.jena.ontology.OntClass;
005 import com.hp.hpl.jena.ontology.OntModel;
006 import com.hp.hpl.jena.ontology.OntModelSpec;
007 import com.hp.hpl.jena.rdf.model.ModelFactory;
008 import com.hp.hpl.jena.rdf.model.Resource;
009 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
010 import dlejena.DLEJenaReasoner;
011
012 /**
013 * In this example, we show how DLEJena can handle TBox and ABox updates
014 * AFTER INITIALIZATION (DLEJenaReasoner::initialize() method). ABox
015 * updates are treated seamlessly, since they can be handled by the
016 * entailments that have been generated for the initial TBox KB. However,
017 * TBox updates are tricky since the rule based for ABox reasoning
018 * should be updated in order to comply with the updated TBox. Currently, DLEJena
019 * does not consider any incremental ABox entailment generation procedure, and
020 * therefore, the ABox entailments are generated from scratch based on the new TBox.
021 * TBox updates are handled by Pellet.
022
023 * @author Georgios Meditskos <gmeditsk@csd.auth.gr>
024 */
025 public class Updates {
026
027 public static void main(String[] args) {
028
029 /*
030 * Create the base model for the asserted ontology axioms. Note that
031 * this model should not define any reasoning infrustructure. We just want
032 * a simple OntModel that will store the ontology (e.g. OWL_MEM).
033 */
034 OntModel base = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
035
036 /*
037 * Set basic properties
038 */
039 base.createOntology("http://dlejena/examples/updates.owl");
040 String uri = "http://dlejena/examples/updates.owl#";
041 base.setNsPrefix("", uri);
042
043 /*
044 * Define the ontology axioms. We define a class Publication
045 * subclass of the Document class. Furthermore we define the
046 * instance myThesis to belong to the Publication class.
047 */
048 System.out.println("Defining the ontology...");
049 OntClass publication = base.createClass(uri + "Publication");
050 System.out.println(" - Class created: " + publication.getLocalName());
051 OntClass document = base.createClass(uri + "Document");
052 System.out.println(" - Class created: " + document.getLocalName());
053 publication.addSuperClass(document);
054 System.out.println(" - Subclass relationship: " + publication.getLocalName() + " is subclass of " + document.getLocalName());
055 Individual myThesis = base.createIndividual(uri + "myThesis", publication);
056 System.out.println(" - Individual of " + publication.getLocalName() + " created: " + myThesis.getLocalName());
057 System.out.println("done!");
058
059 /*
060 * Define an instance of the DLEJena reasoner.
061 * Register the base OntModel and initialize the reasoner
062 */
063 System.out.println("");
064 System.out.print("Initializing DLEJena...");
065 DLEJenaReasoner dle = new DLEJenaReasoner();
066 dle.register(base);
067 dle.initialize();
068 System.out.println(" done!");
069
070 //Get the inferred TBox and ABox reference
071 OntModel tbox = dle.getTBox();
072 OntModel abox = dle.getABox();
073
074 /*
075 * Get the inferred types of myThesis
076 */
077 System.out.println("");
078 System.out.println("Get the inferred types of the instance " + myThesis.getLocalName());
079
080 //Note that the myThesis reference needs to be updated using the inferred abox model
081 //Otherwise, it would refer to the base model we have created in the beginning. The
082 //same holds for the other class, property and instance references too.
083 myThesis = abox.getIndividual(uri + "myThesis");
084 ExtendedIterator types = myThesis.listRDFTypes(false);
085 while (types.hasNext()) {
086 Resource type = (Resource) types.next();
087 if (!type.isAnon())
088 System.out.println(" - " + type.getLocalName());
089 }
090
091 /*
092 * ABOX UPDATE:
093 * We will update the ABox inferred model by creating one more instance
094 * of the class Publication without altering the inferred TBox model.
095 * Such updates are handled seamlessly by the entailment rules that have been
096 * generated for the current TBox. Note that there is no need to initialize
097 * the reasoner again or to prepare the abox model.
098 */
099 System.out.println("");
100 System.out.print("ABOX UPDATE: ");
101 Individual dleiswc08 = abox.createIndividual(uri + "dle-iswc08", publication);
102 System.out.println(" - Individual of " + publication.getLocalName() + " created: " + dleiswc08.getLocalName());
103
104 //Print the types of the new instance.
105 System.out.println("");
106 System.out.println("Get the inferred types of the instance " + dleiswc08.getLocalName());
107 types = dleiswc08.listRDFTypes(false);
108 while (types.hasNext()) {
109 Resource type = (Resource) types.next();
110 if (!type.isAnon())
111 System.out.println(" - " + type.getLocalName());
112 }
113
114 /*
115 * TBOX UPDATE:
116 * We will update the TBox inferred model by defining the class Thesis as subclass
117 * of the Publication class and the class PhDThesis as subclass of the Thesis class.
118 * After a TBox update, the *updateABoxRules()* method should be called in order to generate
119 * again the ABox entailment rules that correspond to
120 * the updated TBox. Otherwise, the rule base would not contain a rule able to handle, for example,
121 * the subclass relationship between the Publication and the Thesis classes. Note that,
122 * before initializing DLEJena, the Jena API can be freely used to define the TBox and
123 * ABox of the base ontology without any special precaution, as we have done in the beginning.
124 * The updateABoxRules method should be used only after reasoner initialization.
125 */
126 System.out.println("");
127 System.out.print("TBOX UPDATE: ");
128 OntClass thesis = tbox.createClass(uri + "Thesis");
129 System.out.println(" - Class created: " + thesis.getLocalName());
130 System.out.print("TBOX UPDATE: ");
131 OntClass phdthesis = tbox.createClass(uri + "PhDThesis");
132 System.out.println(" - Class created: " + phdthesis.getLocalName());
133 System.out.print("TBOX UPDATE: ");
134 publication = tbox.getOntClass(uri + "Publication");
135 thesis.addSuperClass(publication);
136 System.out.println(" - Subclass relationship: " + thesis.getLocalName() + " is subclass of " + publication.getLocalName());
137 System.out.print("TBOX UPDATE: ");
138 phdthesis.addSuperClass(thesis);
139 System.out.println(" - Subclass relationship: " + phdthesis.getLocalName() + " is subclass of " + thesis.getLocalName());
140
141 /*
142 * Print the number of the generated ABox entailments before updating the tbox
143 */
144 System.out.println("");
145 System.out.println("ABox entailments before updating the TBox : " + dle.getABoxRules().size());
146
147 //Update the set of the ABox entailment rules
148 dle.updateABoxRules();
149
150 /*
151 * Print the number of the generated ABox entailments after tbox update.
152 * We can observe that more rules are considered now.
153 */
154 System.out.println("ABox entailments after the TBox update: " + dle.getABoxRules().size());
155
156 /*
157 * ABOX UPDATE:
158 * We will update the ABox inferred model by defining the myThesis individual to
159 * belong also to the Thesis class.
160 */
161 System.out.println("");
162 System.out.print("ABOX UPDATE: ");
163 myThesis.addRDFType(phdthesis);
164 System.out.println(" - Individual of " + phdthesis.getLocalName() + " created: " + myThesis.getLocalName());
165
166 /*
167 * Print the final types of the myThesis instances
168 */
169 System.out.println("");
170 System.out.println("Get the inferred types of the instance " + myThesis.getLocalName());
171 types = myThesis.listRDFTypes(false);
172 while (types.hasNext()) {
173 Resource type = (Resource) types.next();
174 if (!type.isAnon())
175 System.out.println(" - " + type.getLocalName());
176 }
177 }
178 }
|