document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.     public static class FactNode extends StringNode {
  2.  
  3.         public final Fact fact;
  4.  
  5.         public FactNode(Fact f) {
  6.             super(f.toString());
  7.             this.fact = f;
  8.         }
  9.  
  10.         public double getProbability() {
  11.             return fact.truth.getProbability();
  12.         }
  13.     }
  14.  
  15.     public static class RuleNode extends StringNode {
  16.  
  17.         public final Rule rule;
  18.  
  19.         public RuleNode(Rule r) {
  20.             super(r.toString());
  21.             this.rule = r;
  22.         }
  23.  
  24.         public double getW() {
  25.             return rule.getW();
  26.         }
  27.     }
  28.  
  29.     public static class GeniferGraph extends MutableDirectedAdjacencyGraph<Node, ValueEdge<Node, Link>> {
  30.  
  31.         private final Genifer gen;
  32.  
  33.         public GeniferGraph(Genifer gen, int maxLevels) {
  34.             super();
  35.             this.gen = gen;
  36.             update(maxLevels);
  37.         }
  38.  
  39.         protected void update(int maxLevels) {
  40.             clear();
  41.             for (Fact f : gen.getMemory().getFacts()) {
  42.                 updateNode(f, maxLevels - 1);
  43.             }
  44.             for (Rule rule : gen.getMemory().getRules()) {
  45.                 updateNode(rule, maxLevels - 1);
  46.             }
  47.         }
  48.  
  49.         protected void updateNode(Rule r, int i) {
  50.             if (i == 0) {
  51.                 return;
  52.             }
  53.  
  54.  
  55.             Formula formula = r.formula;
  56.             if (formula instanceof Sexp) {
  57.                 Sexp s = (Sexp) formula;
  58.                 Node parent = new RuleNode(r);
  59.                 add(parent);
  60.                 updateLispObject(parent, s.cons.car, i - 1);
  61.                 updateLispObject(parent, s.cons.cdr, i - 1);
  62.             }
  63.  
  64.         }
  65.  
  66.         protected void updateNode(Fact f, int i) {
  67.             if (i == 0) {
  68.                 return;
  69.             }
  70.  
  71.             Formula formula = f.formula;
  72.             if (formula instanceof Sexp) {
  73.                 Sexp s = (Sexp) formula;
  74.                 Node parent = new FactNode(f);
  75.                 add(parent);
  76.                 updateLispObject(parent, s.cons.car, i - 1);
  77.                 updateLispObject(parent, s.cons.cdr, i - 1);
  78.             }
  79.  
  80.         }
  81.  
  82.         protected void updateLispObject(Node parent, LispObject l, int i) {
  83.             if (i == 0) {
  84.                 return;
  85.             }
  86.             if (l == null) {
  87.                 return;
  88.             }
  89.  
  90.             Node lNode = getNode(l);
  91.             add(lNode);
  92.             add(new ValueEdge<Node, Link>(new Next(), parent, lNode));
  93.  
  94.             if (l instanceof Cons) {
  95.                 Cons c = (Cons) l;
  96.                 updateLispObject(lNode, c.car, i - 1);
  97.                 updateLispObject(lNode, c.cdr, i - 1);
  98.             }
  99.         }
  100.  
  101.         private Node getNode(LispObject l) {
  102.             if (l instanceof Cons) {
  103.                 return new StringNode("cons-" + l.hashCode());
  104.             }
  105.  
  106.             return new StringNode(l.writeToString());
  107.         }
  108.     }
');