use of suite.lp.kb.Rule 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.Rule in project suite by stupidsing.
the class ImportUtil method importFrom.
public synchronized boolean importFrom(RuleSet ruleSet, Node node) {
List<Rule> rules = new ArrayList<>();
for (Node elem : Tree.iter(node, TermOp.NEXT__)) rules.add(Rule.of(elem));
Prover prover = new Prover(ruleSet);
boolean b = true;
IList<Node> importing0 = importing.get();
try {
importing.set(IList.cons(node, importing0));
for (Rule rule : rules) if (rule.head != Atom.NIL)
ruleSet.addRule(rule);
else {
Node goal = SewingGeneralizerImpl.generalize(rule.tail);
b &= prover.prove(goal);
}
} finally {
importing.set(importing0);
}
if (// check after all files are imported
importing0.isEmpty())
new Checker().check(ruleSet.getRules());
return b;
}
use of suite.lp.kb.Rule in project suite by stupidsing.
the class SingletonVariableChecker method check.
public void check(List<Rule> rules) {
ListMultimap<Prototype, Rule> rulesByPrototype = Prototype.multimap(rules);
for (Pair<Prototype, Rule> e : rulesByPrototype.entries()) {
Prototype prototype = e.t0;
Rule rule = e.t1;
Scanner scanner = new Scanner();
scanner.scan(rule.head);
scanner.scan(rule.tail);
scanner.warn(prototype);
}
}
use of suite.lp.kb.Rule in project suite by stupidsing.
the class TypeChecker method check.
public void check(List<Rule> rules) {
Map<Prototype, Integer> nElementsByPrototype = checkerUtil.getNumberOfElements(rules);
Map<Pair<Prototype, Integer>, Reference> types = new HashMap<>();
Read.from(rules).concatMap(rule -> {
Generalizer generalizer = new Generalizer();
Node head = generalizer.generalize(rule.head);
Node tail = generalizer.generalize(rule.tail);
return checkerUtil.scan(tail).cons(head);
}).forEach(pred -> {
Prototype prototype = Prototype.of(pred);
Integer nElements = prototype != null ? nElementsByPrototype.get(prototype) : null;
Node[] ps = nElements != null ? TreeUtil.elements(pred, nElements) : new Node[0];
try {
if (nElements != null)
for (int i = 1; i < nElements; i++) {
Pair<Prototype, Integer> key = Pair.of(prototype, i);
Node p = ps[i];
Node type0 = types.computeIfAbsent(key, k -> new Reference());
Node type1 = getType(p);
bind(type0, type1);
}
} catch (Exception ex) {
Fail.t("in predicate " + prototype, ex);
}
});
trail.unwindAll();
}
Aggregations