KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > impl > OWLRuleTranslationHook


1 /******************************************************************
2  * File: OWLRuleTranslationHook.java
3  * Created by: Dave Reynolds
4  * Created on: 22-Jun-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: OWLRuleTranslationHook.java,v 1.7 2005/02/21 12:17:57 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.impl;
11
12 import com.hp.hpl.jena.reasoner.rulesys.*;
13 import com.hp.hpl.jena.reasoner.*;
14 import com.hp.hpl.jena.graph.*;
15 import com.hp.hpl.jena.vocabulary.*;
16
17 import java.util.*;
18
19 /**
20  * A rule preprocessor that scans the data looking for interesection
21  * definitions and augements the rule base by translations of the
22  * intersection statement.
23  *
24  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
25  * @version $Revision: 1.7 $ on $Date: 2005/02/21 12:17:57 $
26  */

27 public class OWLRuleTranslationHook implements RulePreprocessHook {
28
29     /**
30      * Invoke the preprocessing hook. This will be called during the
31      * preparation time of the hybrid reasoner.
32      * @param infGraph the inference graph which is being prepared,
33      * the hook code can use this to addDeductions or add additional
34      * rules (using addRuleDuringPrepare).
35      * @param dataFind the finder which packages up the raw data (both
36      * schema and data bind) and any cached transitive closures.
37      * @param inserts a temporary graph into which the hook should insert
38      * all new deductions that should be seen by the rules.
39      */

40     public void run(FBRuleInfGraph infGraph, Finder dataFind, Graph inserts) {
41         Iterator it = dataFind.find(new TriplePattern(null, OWL.intersectionOf.asNode(), null));
42         while (it.hasNext()) {
43             Triple decl = (Triple)it.next();
44             Node className = decl.getSubject();
45             List elements = new ArrayList();
46             translateIntersectionList(decl.getObject(), dataFind, elements);
47             // Generate the corresponding ruleset
48
List recognitionBody = new ArrayList();
49             Node var = new Node_RuleVariable("?x", 0);
50             for (Iterator i = elements.iterator(); i.hasNext(); ) {
51                 Node description = (Node)i.next();
52                 // Implication rule
53
Rule ir = new Rule("intersectionImplication", new ClauseEntry[] {
54                                     new TriplePattern(className, RDFS.subClassOf.asNode(), description)
55                                     }, new ClauseEntry[0]);
56                 ir.setBackward(false);
57 // System.out.println("translation hook => " + ir);
58
infGraph.addRuleDuringPrepare(ir);
59                // Recognition rule elements
60
recognitionBody.add(new TriplePattern(var, RDF.type.asNode(), description));
61             }
62             List recognitionHead = new ArrayList(1);
63             recognitionHead.add(new TriplePattern(var, RDF.type.asNode(), className));
64             Rule rr = new Rule("intersectionRecognition", recognitionHead, recognitionBody);
65             rr.setBackward(true);
66 // System.out.println("translation hook => " + rr);
67
infGraph.addRuleDuringPrepare(rr);
68         }
69     }
70     
71     /**
72      * Translation code to translate a list of intersection elements into a
73      * Java list of corresponding class names or restriction functors.
74      * @param node the list node currently being processed
75      * @param data the source data to use as a context for this processing
76      * @param elements the list of elements found so far
77      */

78     protected static void translateIntersectionList(Node node, Finder dataFind, List elements) {
79         if (node == null) {
80             throw new ReasonerException("Illegal list structure in owl:intersectionOf");
81         }
82         if (node.equals(RDF.nil.asNode())) {
83             return; // end of list
84
}
85         Node description = Util.getPropValue(node, RDF.first.asNode(), dataFind);
86         if (description == null) {
87             throw new ReasonerException("Illegal list structure in owl:intersectionOf");
88         }
89         // Translate the first description element
90
/* - temp comment out during debugging
91         if (dataFind.contains(new TriplePattern(description, RDF.type.asNode(), OWL.Restriction.asNode()))) {
92             // Process a restriction element
93             Node onprop = Util.getPropValue(description, OWL.onProperty.asNode(), dataFind);
94             Node value;
95             if ((value = Util.getPropValue(description, OWL.allValuesFrom.asNode(), dataFind)) != null) {
96                 elements.add(Functor.makeFunctorNode("all", new Node[] {onprop, value}));
97             } else if ((value = Util.getPropValue(description, OWL.someValuesFrom.asNode(), dataFind)) != null) {
98                 elements.add(Functor.makeFunctorNode("some", new Node[] {onprop, value}));
99             } else if ((value = Util.getPropValue(description, OWL.hasValue.asNode(), dataFind)) != null) {
100                 elements.add(Functor.makeFunctorNode("hasValue", new Node[] {onprop, value}));
101             } else if ((value = Util.getPropValue(description, OWL.minCardinality.asNode(), dataFind)) != null) {
102                 elements.add(Functor.makeFunctorNode("min", new Node[] {onprop, value}));
103             } else if ((value = Util.getPropValue(description, OWL.maxCardinality.asNode(), dataFind)) != null) {
104                 elements.add(Functor.makeFunctorNode("max", new Node[] {onprop, value}));
105             } else if ((value = Util.getPropValue(description, OWL.cardinality.asNode(), dataFind)) != null) {
106                 elements.add(Functor.makeFunctorNode("max", new Node[] {onprop, value}));
107                 elements.add(Functor.makeFunctorNode("min", new Node[] {onprop, value}));
108             } else {
109                 elements.add(description);
110             }
111         } else {
112             // Assume its a class name
113             elements.add(description);
114         }
115         */

116         // Above used to translated intersections into direct functor tests but in fact the
117
// references to the restriction bNode is sufficent and a better match to the current rules
118
elements.add(description);
119         // Process the list tail
120
Node next = Util.getPropValue(node, RDF.rest.asNode(), dataFind);
121         translateIntersectionList(next, dataFind, elements);
122     }
123
124 }
125
126
127
128 /*
129     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
130     All rights reserved.
131
132     Redistribution and use in source and binary forms, with or without
133     modification, are permitted provided that the following conditions
134     are met:
135
136     1. Redistributions of source code must retain the above copyright
137        notice, this list of conditions and the following disclaimer.
138
139     2. Redistributions in binary form must reproduce the above copyright
140        notice, this list of conditions and the following disclaimer in the
141        documentation and/or other materials provided with the distribution.
142
143     3. The name of the author may not be used to endorse or promote products
144        derived from this software without specific prior written permission.
145
146     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
148     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
149     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
150     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
151     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
152     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
153     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
154     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
155     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
156 */
Popular Tags