001: /******************************************************************
002: * File: RuleDerivation.java
003: * Created by: Dave Reynolds
004: * Created on: 06-Apr-03
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: RuleDerivation.java,v 1.12 2008/01/02 12:07:47 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.reasoner.Derivation;
013: import com.hp.hpl.jena.reasoner.InfGraph;
014: import com.hp.hpl.jena.util.PrintUtil;
015: import java.io.PrintWriter;
016: import java.util.*;
017:
018: /**
019: * Derivation records are used to determine how an inferred triple
020: * was derived from a set of source triples and a reasoner. SubClasses
021: * provide more specific information.
022: *
023: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
024: * @version $Revision: 1.12 $ on $Date: 2008/01/02 12:07:47 $
025: */
026: public class RuleDerivation implements Derivation {
027:
028: /** The rule which asserted this triple */
029: protected Rule rule;
030:
031: /** The triple which was asserted */
032: protected Triple conclusion;
033:
034: /** The list of triple matches that fired the rule */
035: protected List matches;
036:
037: /** The InfGraph which produced this derivation */
038: protected InfGraph infGraph;
039:
040: /**
041: * Constructor
042: * @param rule the rule which created this derivation
043: * @param conclusion the triple which the rule created
044: * @param matches a list of matching Triples corresponding to the rule body patterns
045: * @param infGraph the parent infGraph which was controlling the derivation
046: */
047: public RuleDerivation(Rule rule, Triple conclusion, List matches,
048: InfGraph infGraph) {
049: this .rule = rule;
050: this .conclusion = conclusion;
051: this .matches = matches;
052: this .infGraph = infGraph;
053: }
054:
055: /**
056: * Return a short-form description of this derivation.
057: */
058: public String toString() {
059: if (rule == null) {
060: return "DUMMY";
061: } else {
062: return "Rule " + rule.toShortString();
063: }
064: }
065:
066: /**
067: * Print a deep traceback of this derivation back to axioms and
068: * source assertions.
069: * @param out the stream to print the trace out to
070: * @param bindings set to true to print intermediate variable bindings for
071: * each stage in the derivation
072: */
073: public void printTrace(PrintWriter out, boolean bindings) {
074: printTrace(out, bindings, 0, new HashSet());
075: }
076:
077: /**
078: * Print a deep traceback of this derivation back to axioms and
079: * source assertions.
080: * @param out the stream to print the trace out to
081: * @param bindings set to true to print intermediate variable bindings for
082: * each stage in the derivation
083: * @param indent the number of indent spaces to use
084: * @param seen a HashSet of derviations that have already been listed
085: */
086: protected void printTrace(PrintWriter out, boolean bindings,
087: int indent, HashSet seen) {
088: PrintUtil.printIndent(out, indent);
089: out.print(this .toString());
090: if (bindings) {
091: out.print(" concluded " + PrintUtil.print(conclusion));
092: }
093: out.println(" <-");
094: int margin = indent + 4;
095: for (int i = 0; i < matches.size(); i++) {
096: Triple match = (Triple) matches.get(i);
097: Iterator derivations = infGraph.getDerivation(match);
098: if (derivations == null || !derivations.hasNext()) {
099: PrintUtil.printIndent(out, margin);
100: if (match == null) {
101: // A primitive
102: ClauseEntry term = rule.getBodyElement(i);
103: if (term instanceof Functor) {
104: out.println(((Functor) term).getName() + "()");
105: } else {
106: out.println("call to built in");
107: }
108: } else {
109: out.println("Fact " + PrintUtil.print(match));
110: }
111: } else {
112: RuleDerivation derivation = (RuleDerivation) derivations
113: .next();
114: if (seen.contains(derivation)) {
115: PrintUtil.printIndent(out, margin);
116: out.println("Known " + PrintUtil.print(match)
117: + " - already shown");
118: } else {
119: seen.add(derivation);
120: derivation.printTrace(out, bindings, margin, seen);
121: }
122: }
123: }
124: }
125:
126: /**
127: * @return the triple concluded by the derivation
128: */
129: public Triple getConclusion() {
130: return conclusion;
131: }
132:
133: /**
134: * @return the set of triples which were used in firing this rule derivation
135: */
136: public List getMatches() {
137: return matches;
138: }
139:
140: /**
141: * @return the rule which fired to create this derivation
142: */
143: public Rule getRule() {
144: return rule;
145: }
146:
147: /**
148: * Compare two derivations. This is a shallow comparison, two derivations
149: * are the same if they contain the same conclusion, rule and match list.
150: * They do not need to be derived from the same (or any) infGraph.
151: */
152: public boolean equals(Object other) {
153: if (other instanceof RuleDerivation) {
154: RuleDerivation otherD = (RuleDerivation) other;
155: return conclusion.equals(otherD.getConclusion())
156: && matches.equals(otherD.getMatches())
157: && rule.equals(otherD.getRule());
158: } else {
159: return false;
160: }
161: }
162: }
163:
164: /*
165: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
166: All rights reserved.
167:
168: Redistribution and use in source and binary forms, with or without
169: modification, are permitted provided that the following conditions
170: are met:
171:
172: 1. Redistributions of source code must retain the above copyright
173: notice, this list of conditions and the following disclaimer.
174:
175: 2. Redistributions in binary form must reproduce the above copyright
176: notice, this list of conditions and the following disclaimer in the
177: documentation and/or other materials provided with the distribution.
178:
179: 3. The name of the author may not be used to endorse or promote products
180: derived from this software without specific prior written permission.
181:
182: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
183: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
184: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
185: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
186: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
187: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
188: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
189: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
190: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
191: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
192: */
|