use of suite.lp.kb.Prototype in project suite by stupidsing.
the class Prover method prove0.
public boolean prove0(Node query) {
rem = OK;
alt = FAIL;
while (true) {
// logUtil.info(Formatter.dump(query));
query = query.finalNode();
if (query instanceof Tree) {
Tree tree = (Tree) query;
Node left = tree.getLeft(), right = tree.getRight();
switch((TermOp) tree.getOperator()) {
case OR____:
int pit = trail.getPointInTime();
Node bt = new Data<Source<Boolean>>(() -> {
trail.unwind(pit);
return Boolean.TRUE;
});
alt = andTree(bt, orTree(andTree(right, rem), alt));
query = left;
continue;
case AND___:
rem = andTree(right, rem);
query = left;
continue;
case EQUAL_:
query = isSuccess(bind(left, right));
break;
default:
}
} else if (query instanceof Data) {
query = isSuccess(Data.<Source<Boolean>>get(query).source());
continue;
}
Boolean b = systemPredicates.call(query);
if (b != null)
query = isSuccess(b);
// not handled above
if (query == OK)
if (rem != OK) {
query = rem;
rem = OK;
} else
return true;
else if (query == FAIL)
if (alt != FAIL) {
query = alt;
alt = FAIL;
rem = OK;
} else
return false;
else {
boolean isTrace = config.isTrace();
if (isTrace) {
Set<String> whites = Suite.tracePredicates;
Set<String> blacks = Suite.noTracePredicates;
Prototype prototype = Prototype.of(query);
Node head = prototype != null ? prototype.head : null;
Atom atom = head instanceof Atom ? (Atom) head : null;
String name = atom != null ? atom.name : null;
isTrace &= whites == null || whites.contains(name);
isTrace &= blacks == null || !blacks.contains(name);
}
if (!isTrace)
query = expand(query);
else
query = tracer.expandWithTrace(query, this, this::expand);
}
}
}
use of suite.lp.kb.Prototype in project suite by stupidsing.
the class QueryRewriter method rewriteQuery.
private Node rewriteQuery(Node node0) {
Node nodex;
Prototype prototype = Prototype.of(node0);
PrototypeInfo pi;
if ((pi = infoByPrototype.get(prototype)) != null) {
int length = pi.length;
if (length <= 0)
nodex = node0;
else {
Node[] ps = TreeUtil.elements(node0, length);
if (pi.isSkipFirst)
ps = Arrays.copyOfRange(ps, 1, ps.length, Node[].class);
nodex = Tuple.of(ps);
}
} else
nodex = node0;
return nodex;
}
use of suite.lp.kb.Prototype in project suite by stupidsing.
the class SewingProverImpl method compileAll.
private void compileAll() {
isHasCutByPrototype = rules.listEntries().mapValue(this::isHasCut).toMap();
for (Pair<Prototype, List<Rule>> e : rules.listEntries()) {
Prototype prototype = e.t0;
List<Rule> rules = new ArrayList<>(e.t1);
TraceLevel traceLevel = traceLevel(prototype);
// second-level indexing optimization
Map<Prototype, List<Rule>> rulesByProto1;
if (6 <= rules.size()) {
Map<Prototype, List<Rule>> rulesByProto_ = Read.from(rules).toListMap(rule -> Prototype.of(rule, 1));
rulesByProto1 = !rulesByProto_.containsKey(null) ? rulesByProto_ : null;
} else
rulesByProto1 = null;
if (isHasCutByPrototype.get(prototype)) {
Trampoline tr0 = compileTrRules(prototype, rules, traceLevel);
Trampoline tr;
if (rulesByProto1 != null) {
Map<Prototype, Trampoline> trByProto1 = //
Read.from2(//
rulesByProto1).mapValue(//
rules_ -> compileTrRules(prototype, rules_, traceLevel)).toMap();
tr = rt -> {
Prototype proto = Prototype.of(rt.query, 1);
if (proto != null) {
Trampoline tr_ = trByProto1.get(proto);
return tr_ != null ? tr_ : fail;
} else
return tr0;
};
} else
tr = tr0;
getTrampolineByPrototype(prototype).set(tr);
} else {
Cps cps0 = compileCpsRules(prototype, rules, traceLevel);
Cps cps;
if (rulesByProto1 != null) {
Map<Prototype, Cps> cpsByProto1 = //
Read.from2(//
rulesByProto1).mapValue(//
rules_ -> compileCpsRules(prototype, rules_, traceLevel)).toMap();
cps = rt -> {
Prototype proto = Prototype.of(rt.query, 1);
return proto != null ? cpsByProto1.get(proto) : cps0;
};
} else
cps = cps0;
getCpsByPrototype(prototype).set(cps);
}
}
}
use of suite.lp.kb.Prototype in project suite by stupidsing.
the class SewingProverImpl method compileTrCallPredicate.
private Trampoline compileTrCallPredicate(BinderFactory bf, Node node) {
Prototype prototype = Prototype.of(node);
if (rules.containsKey(prototype)) {
Clone_ f = bf.cloner(node);
Trampoline tr;
if (isHasCutByPrototype.get(prototype)) {
Mutable<Trampoline> mtr = getTrampolineByPrototype(prototype);
tr = rt -> {
rt.query = f.apply(rt.env);
return mtr.get()::prove;
};
} else {
Mutable<Cps> mcps = getCpsByPrototype(prototype);
Cps cpsx = rt -> {
IList<Trampoline> rems = rt.rems;
rt.rems = IList.cons(fail, IList.end());
new Runtime(rt, rt_ -> {
rt_.rems = rems;
return okay;
}).trampoline();
return null;
};
tr = rt -> {
Cps cps0 = rt.cps;
rt.cps = rt_ -> {
rt.cps = cps0;
return cpsx;
};
rt.query = f.apply(rt.env);
rt.cont(mcps.get());
return fail;
};
}
return tr;
} else
return Fail.t("cannot find predicate " + prototype);
}
use of suite.lp.kb.Prototype in project suite by stupidsing.
the class Chr method chrIf.
private Streamlet<State> chrIf(Streamlet<State> states, Trail trail, Node if_) {
Prototype prototype = Prototype.of(if_);
Fun<State, Streamlet<State>> fun = state -> {
ISet<Node> facts = getFacts(state, prototype);
Predicate<Node> bindFun = bindFun(trail, if_);
return facts.streamlet().filter(bindFun).map(node -> setFacts(state, prototype, facts.remove(node)));
};
return states.concatMap(fun);
}
Aggregations