public static class FactNode extends StringNode {
public final Fact fact;
public FactNode(Fact f) {
super(f.toString());
this.fact = f;
}
public double getProbability() {
return fact.truth.getProbability();
}
}
public static class RuleNode extends StringNode {
public final Rule rule;
public RuleNode(Rule r) {
super(r.toString());
this.rule = r;
}
public double getW() {
return rule.getW();
}
}
public static class GeniferGraph extends MutableDirectedAdjacencyGraph<Node, ValueEdge<Node, Link>> {
private final Genifer gen;
public GeniferGraph(Genifer gen, int maxLevels) {
super();
this.gen = gen;
update(maxLevels);
}
protected void update(int maxLevels) {
clear();
for (Fact f : gen.getMemory().getFacts()) {
updateNode(f, maxLevels - 1);
}
for (Rule rule : gen.getMemory().getRules()) {
updateNode(rule, maxLevels - 1);
}
}
protected void updateNode(Rule r, int i) {
if (i == 0) {
return;
}
Formula formula = r.formula;
if (formula instanceof Sexp) {
Sexp s = (Sexp) formula;
Node parent = new RuleNode(r);
add(parent);
updateLispObject(parent, s.cons.car, i - 1);
updateLispObject(parent, s.cons.cdr, i - 1);
}
}
protected void updateNode(Fact f, int i) {
if (i == 0) {
return;
}
Formula formula = f.formula;
if (formula instanceof Sexp) {
Sexp s = (Sexp) formula;
Node parent = new FactNode(f);
add(parent);
updateLispObject(parent, s.cons.car, i - 1);
updateLispObject(parent, s.cons.cdr, i - 1);
}
}
protected void updateLispObject(Node parent, LispObject l, int i) {
if (i == 0) {
return;
}
if (l == null) {
return;
}
Node lNode = getNode(l);
add(lNode);
add(new ValueEdge<Node, Link>(new Next(), parent, lNode));
if (l instanceof Cons) {
Cons c = (Cons) l;
updateLispObject(lNode, c.car, i - 1);
updateLispObject(lNode, c.cdr, i - 1);
}
}
private Node getNode(LispObject l) {
if (l instanceof Cons) {
return new StringNode("cons-" + l.hashCode());
}
return new StringNode(l.writeToString());
}
}