1 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 ; 17 import java.util.*; 18 19 27 public class RuleDerivation implements Derivation { 28 29 30 protected Rule rule; 31 32 33 protected Triple conclusion; 34 35 36 protected List matches; 37 38 39 protected InfGraph infGraph; 40 41 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 58 public String toString() { 59 if (rule == null) { 60 return "DUMMY"; 61 } else { 62 return "Rule " + rule.toShortString(); 63 } 64 } 65 66 73 public void printTrace(PrintWriter out, boolean bindings) { 74 printTrace(out, bindings, 0, new HashSet()); 75 } 76 77 86 protected void printTrace(PrintWriter 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 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 127 public Triple getConclusion() { 128 return conclusion; 129 } 130 131 134 public List getMatches() { 135 return matches; 136 } 137 138 141 public Rule getRule() { 142 return rule; 143 } 144 145 } 146 147 176 | Popular Tags |