001 package examples;
002
003 import com.hp.hpl.jena.ontology.Individual;
004 import com.hp.hpl.jena.ontology.OntProperty;
005 import com.hp.hpl.jena.rdf.model.NodeIterator;
006 import com.hp.hpl.jena.reasoner.rulesys.Rule;
007 import dlejena.DLEJenaReasoner;
008 import java.io.File;
009 import java.net.URI;
010 import java.util.Collections;
011
012 /**
013 * This example shows how custom template rules can be added in DLEJena in order to
014 * define more complex ABox entailments beyond OWL2 RL.
015 *
016 * The template rules are rules that dynamically generate the ABox inferencing rules.
017 * These rules are executed over the inferred TBox model (PelletInfGraph). Therefore, the ABox rule
018 * base of DLEJena is dynamically defined, according to the TBox model.
019 *
020 * There are two ways of adding template rules in DLEJena. The first is to add directly
021 * the custom template rules in the templates.rules file that exists inside dlejena.jar.
022 * The second approach is to add them procedurally through the DLEJena API. The present example
023 * follows the second approach.
024 *
025 * @author Georgios Meditskos <gmeditsk@csd.auth.gr>
026 */
027 public class CustomTemplate {
028
029 public static void main(String[] args) {
030
031
032 /*
033 * The physical URI (local path or Web address) of the ontology.
034 */
035 URI ontology = new File("src/examples/parent.owl").toURI();
036
037 /*
038 * Hold the ontology logical URI needed in queries
039 */
040 String uri = "http://dlejena/examples/customTemplateRules.owl#";
041
042 /*
043 * Define an instance of the DLEJenaReasoner class.
044 * This class provides the necessary nethods for
045 * using the DLEJena library
046 */
047 DLEJenaReasoner dle = new DLEJenaReasoner();
048
049 /*
050 * Register the ontology to the reasoner.
051 */
052 dle.register(ontology);
053
054 /*
055 * Register the min cardinality template rule.
056 * Note that this procedure should take place prior to
057 * DLEJena initializaion.
058 */
059 dle.registerTemplate(Collections.singletonList(minCardinalityTemplate()));
060
061 /*
062 * Initiate the reasoner. This phase involves the
063 * separation of the TBox from ABox axioms (using the OWLAPI),
064 * the TBox reasoning procedure (using Pellet), the generation of the
065 * dynamic entailments and the ABox reasoning procedure (using
066 * the forwardRete rule engine of Jena).
067 */
068 dle.initialize();
069
070 /*
071 * The parent.owl ontology defines a class Parent as the equivalent
072 * class to the class of all the instances that have at least
073 * one value for the property hasChild (minCardinality 1). The restrictions
074 * imposed by the OWL2 RL Profile are designed so as to avoid the need to
075 * infer the existence of individuals not explicitly present in the KB.
076 * By adding this custom template rule, we are able to infer anonymous
077 * individuals for min cardinality restrictions, in cases where such
078 * instances do not exist, as it happens in the example ontology. The ontology
079 * defines an instance of the class Parent without a value in the hasChild
080 * property and an anonymous instance is generated. DLEJena can
081 * be extended with more template rules, according to the requirements.
082 */
083
084 System.out.println("");
085 System.out.println("The hasChild values of the nick instance: ");
086 Individual nick = dle.getABox().getIndividual(uri + "nick");
087 OntProperty hasChild = dle.getTBox().getOntProperty(uri + "hasChild");
088 NodeIterator values = nick.listPropertyValues(hasChild);
089 while(values.hasNext()){
090 Individual v = (Individual) values.nextNode().as(Individual.class);
091 if(v.isAnon()){
092 System.out.println(" - The anonymous instance (BNode) " + v.toString() + " of types " + v.listRDFTypes(false).toList());
093 }
094 }
095 }
096
097 /**
098 * This method defines the template rule for the following generic entailment:
099 * [min:
100 * (?R owl:minCardinality 1),
101 * (?R owl:onProperty ?P),
102 * (?X rdf:type ?R),
103 * noValue(?X ?P), makeTemp(?T)
104 * -> (?X ?P ?T)]
105 *
106 * Note that the triples that refer to TBox information should exist in the
107 * body of the template rule.
108 *
109 * @return The parsed rule
110 */
111 public static Rule minCardinalityTemplate() {
112 String rule = "[min: (?R owl:minCardinality 1), " +
113 "(?R owl:onProperty ?P) " +
114 "-> [D_min:(?X rdf:type ?R), noValue(?X ?P ), makeTemp(?T) -> (?X ?P ?T)]]";
115 return Rule.parseRule(rule);
116
117 }
118 }
|