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.OntProperty;
007 import com.hp.hpl.jena.rdf.model.NodeIterator;
008 import com.hp.hpl.jena.rdf.model.RDFNode;
009 import com.hp.hpl.jena.rdf.model.Resource;
010 import com.hp.hpl.jena.reasoner.rulesys.Rule;
011 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
012 import dlejena.DLEJenaParameters;
013 import dlejena.DLEJenaReasoner;
014 import java.io.File;
015 import java.net.URI;
016 import java.util.List;
017
018 /**
019 * This is a simple example of loading an ontology in DLEJena. We exeplify also
020 * on the way we can retrieve the rule base of the ABox rule reasoner (forwardRete) and
021 * on some trivial TBox and ABox queries using the Jena API over the inferred models.
022 *
023 * @author Georgios Meditskos <gmeditsk@csd.auth.gr>
024 */
025 public class LoadOntology {
026
027 public static void main(String[] args) {
028
029 /*
030 * Enable some basic messages in the standard output.
031 */
032 DLEJenaParameters.SHOW_MESSAGES_IN_STANDARD_OUTPUT = true;
033
034 /*
035 * The physical URI (local path or Web address) of the ontology.
036 */
037 URI ontology = new File("src/examples/man.owl").toURI();
038 //or something like URI ontology = URI.create("http://127.0.0.1/man.owl");
039
040 /*
041 * Hold the ontology logical URI needed in queries
042 */
043 String uri = "http://dlejena/examples/man.owl#";
044
045 /*
046 * Define an instance of the DLEJenaReasoner class.
047 * This class provides the necessary nethods for
048 * using the DLEJena library
049 */
050 DLEJenaReasoner dle = new DLEJenaReasoner();
051
052 /*
053 * Register the ontology to the reasoner.
054 */
055 dle.register(ontology);
056
057 /*
058 * Initiate the reasoner. This phase involves the
059 * separation of the TBox from ABox axioms (using the OWLAPI),
060 * the TBox reasoning procedure (using Pellet), the generation of the
061 * dynamic entailments and the ABox reasoning procedure (using
062 * the forwardRete rule engine of Jena).
063 */
064 dle.initialize();
065
066 /*
067 * Validate the ABox model based against a set of validation rules
068 * that are dynamically generated. Note that
069 * any TBox incosistency is determined by Pellet.
070 */
071 dle.validateABox();
072
073 /*
074 * Obtain references to the TBox and ABox inferred models.
075 */
076 OntModel tbox = dle.getTBox();
077 OntModel abox = dle.getABox();
078
079
080 /*
081 * Retrieve and print the set of the rules that have been used
082 * for ABox reasoning.
083 * The rules that are returned by this method are not
084 * always the same: it depends on the semantics of the loaded ontologies.
085 * The rule name of every dynamic rule starts with D_.
086 */
087 List<Rule> aboxRules = dle.getABoxRules();
088 System.out.println("");
089 System.out.println("-------------------------");
090 System.out.println("The ABox entailment rules");
091 System.out.println("-------------------------");
092 dlejena.utils.Print.printRuleSet(aboxRules);
093
094 /*
095 * All the TBox-related queries should use the TBox inferred model (tbox)
096 */
097 System.out.println("");
098 System.out.println("The named superclasses of the class Man:");
099 OntClass man = tbox.getOntClass(uri + "Man");
100 ExtendedIterator superclasses = man.listSuperClasses();
101 while (superclasses.hasNext()) {
102 OntClass c = (OntClass) superclasses.next();
103 if (!c.isAnon())
104 System.out.println(" - " + c.getURI());
105 }
106
107 /*
108 * Get the types of the instance gmeditsk. This query should use the ABox
109 * inferred model of the dle instance, since it is an ABox-related query
110 */
111 Individual gmeditsk = abox.getIndividual(uri + "gmeditsk");
112 System.out.println("");
113 System.out.println("All the named classes where gmeditsk belongs to:");
114
115 /*
116 * The listRDFTypes method should be used instead of the listOntClasses
117 * method in order to retrieve the types of an instance, treating the
118 * results as Resources. Otherwise, an exception will be thrown, since
119 * Jena would not be able to view (cast) any restriction class as an OntClass
120 * (this information exists in the TBox model).
121 */
122 ExtendedIterator types = gmeditsk.listRDFTypes(false);
123 while (types.hasNext()) {
124 Resource type = (Resource) types.next();
125 if (!type.isAnon())
126 System.out.println(" - " + type.getURI());
127 }
128
129 /*
130 * Retrieve the value of the property hasSex of the gmeditsk instance
131 */
132 System.out.println("");
133 System.out.println("gmeditsk has sex: ");
134 OntProperty hasSex = tbox.getOntProperty(uri + "hasSex");
135 NodeIterator values = gmeditsk.listPropertyValues(hasSex);
136 while (values.hasNext()) {
137 RDFNode value = (RDFNode) values.next();
138 System.out.println(" - " + value.toString());
139 }
140 }
141 }
|