use of suite.lp.kb.Prototype in project suite by stupidsing.
the class Chr method chr.
public Collection<Node> chr(Collection<Node> facts) {
State state = new State(IMap.empty());
for (Node fact : facts) {
Prototype prototype = Prototype.of(fact);
state = setFacts(state, prototype, getFacts(state, prototype).replace(fact));
}
State state1;
while ((state1 = chr(state)) != null) state = state1;
List<Node> nodes1 = new ArrayList<>();
for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) nodes1.addAll(To.list(e.t1));
return nodes1;
}
use of suite.lp.kb.Prototype in project suite by stupidsing.
the class Chr method chrThen.
private Streamlet<State> chrThen(Streamlet<State> states, Node then) {
Generalizer generalizer = new Generalizer();
Atom a = atom(".a"), b = atom(".b");
if (Binder.bind(then, generalizer.generalize(Suite.substitute(".0 = .1", a, b)), new Trail())) {
// built-in syntactic equality
Reference from = generalizer.getVariable(a);
Reference to = generalizer.getVariable(b);
states = states.map(new Fun<>() {
public State apply(State state) {
IMap<Prototype, ISet<Node>> factsByPrototype1 = IMap.empty();
for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) factsByPrototype1 = factsByPrototype1.put(e.t0, replace(e.t1));
return new State(factsByPrototype1);
}
private ISet<Node> replace(ISet<Node> facts) {
ISet<Node> facts1 = ISet.empty();
for (Node node : facts) facts1 = facts1.replace(rw.replace(from, to, node));
return facts1;
}
});
}
return states.map(state -> {
Prototype prototype = Prototype.of(then);
ISet<Node> facts = getFacts(state, prototype);
return setFacts(state, prototype, facts.replace(then));
});
}
use of suite.lp.kb.Prototype in project suite by stupidsing.
the class Chr method chrGiven.
private Streamlet<State> chrGiven(Streamlet<State> states, Trail trail, Node given) {
Prototype prototype = Prototype.of(given);
return states.filter(state -> {
ISet<Node> facts = getFacts(state, prototype);
Predicate<Node> bindFun = bindFun(trail, given);
return facts.streamlet().isAny(bindFun);
});
}
use of suite.lp.kb.Prototype 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.Prototype 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