KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > RuleDerivation


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

10 package com.hp.hpl.jena.reasoner.rulesys;
11
12 import com.hp.hpl.jena.graph.*;
13 import com.hp.hpl.jena.reasoner.Derivation;
14 import com.hp.hpl.jena.reasoner.InfGraph;
15 import com.hp.hpl.jena.util.PrintUtil;
16 import java.io.PrintWriter JavaDoc;
17 import java.util.*;
18
19 /**
20  * Derivation records are used to determine how an inferred triple
21  * was derived from a set of source triples and a reasoner. SubClasses
22  * provide more specific information.
23  *
24  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
25  * @version $Revision: 1.8 $ on $Date: 2005/02/21 12:17:06 $
26  */

27 public class RuleDerivation implements Derivation {
28     
29     /** The rule which asserted this triple */
30     protected Rule rule;
31     
32     /** The triple which was asserted */
33     protected Triple conclusion;
34     
35     /** The list of triple matches that fired the rule */
36     protected List matches;
37
38     /** The InfGraph which produced this derivation */
39     protected InfGraph infGraph;
40     
41     /**
42      * Constructor
43      * @param rule the rule which created this derivation
44      * @param conclusion the triple which the rule created
45      * @param matches a list of matching Triples corresponding to the rule body patterns
46      * @param infGraph the parent infGraph which was controlling the derivation
47      */

48     public RuleDerivation(Rule rule, Triple conclusion, List matches, InfGraph infGraph) {
49         this.rule = rule;
50         this.conclusion = conclusion;
51         this.matches = matches;
52         this.infGraph = infGraph;
53     }
54     
55     /**
56      * Return a short-form description of this derivation.
57      */

58     public String JavaDoc toString() {
59         if (rule == null) {
60             return "DUMMY";
61         } else {
62             return "Rule " + rule.toShortString();
63         }
64     }
65     
66     /**
67      * Print a deep traceback of this derivation back to axioms and
68      * source assertions.
69      * @param out the stream to print the trace out to
70      * @param bindings set to true to print intermediate variable bindings for
71      * each stage in the derivation
72      */

73     public void printTrace(PrintWriter JavaDoc out, boolean bindings) {
74        printTrace(out, bindings, 0, new HashSet());
75     }
76     
77      /**
78      * Print a deep traceback of this derivation back to axioms and
79      * source assertions.
80      * @param out the stream to print the trace out to
81      * @param bindings set to true to print intermediate variable bindings for
82      * each stage in the derivation
83      * @param indent the number of indent spaces to use
84      * @param seen a HashSet of derviations that have already been listed
85      */

86     protected void printTrace(PrintWriter JavaDoc out, boolean bindings, int indent, HashSet seen) {
87         PrintUtil.printIndent(out, indent);
88         out.print(this.toString());
89         if (bindings) {
90             out.print(" concluded " + PrintUtil.print(conclusion));
91         }
92         out.println(" <-");
93         int margin = indent + 4;
94         for (int i = 0; i < matches.size(); i++) {
95             Triple match = (Triple)matches.get(i);
96             Iterator derivations = infGraph.getDerivation(match);
97             if (derivations == null || !derivations.hasNext()) {
98                 PrintUtil.printIndent(out, margin);
99                 if (match == null) {
100                     // A primitive
101
ClauseEntry term = rule.getBodyElement(i);
102                     if (term instanceof Functor) {
103                         out.println(((Functor)term).getName() + "()");
104                     } else {
105                         out.println("call to built in");
106                     }
107                 } else {
108                     out.println("Fact " + PrintUtil.print(match));
109                 }
110             } else {
111                 RuleDerivation derivation = (RuleDerivation)derivations.next();
112                 if (seen.contains(derivation)) {
113                     PrintUtil.printIndent(out, margin);
114                     out.println("Known " + PrintUtil.print(match) + " - already shown");
115                 } else {
116                     seen.add(derivation);
117                     derivation.printTrace(out, bindings, margin, seen);
118                 }
119             }
120         }
121     }
122     
123
124     /**
125      * @return the triple concluded by the derivation
126      */

127     public Triple getConclusion() {
128         return conclusion;
129     }
130
131     /**
132      * @return the set of triples which were used in firing this rule derivation
133      */

134     public List getMatches() {
135         return matches;
136     }
137
138     /**
139      * @return the rule which fired to create this derivation
140      */

141     public Rule getRule() {
142         return rule;
143     }
144
145 }
146
147 /*
148     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
149     All rights reserved.
150
151     Redistribution and use in source and binary forms, with or without
152     modification, are permitted provided that the following conditions
153     are met:
154
155     1. Redistributions of source code must retain the above copyright
156        notice, this list of conditions and the following disclaimer.
157
158     2. Redistributions in binary form must reproduce the above copyright
159        notice, this list of conditions and the following disclaimer in the
160        documentation and/or other materials provided with the distribution.
161
162     3. The name of the author may not be used to endorse or promote products
163        derived from this software without specific prior written permission.
164
165     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
166     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
167     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
168     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
169     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
170     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
171     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
172     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
173     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
174     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
175 */

176
Popular Tags