rules2Prereq function
build prerequisites table from rules
Description by example
given set of logic rules:
a -> b, c b -> c
we build prerequisites (from what points something can be deduced):
b <- a c <- a, b
rules: {} of a -> b, c, ...
return: {} of c <- a, b, ...
Note however, that these prerequisites may not be enough to prove a fact. An example is 'a -> b' rule, where prereq(a) is b, and prereq(b) is a. That's because a=T -> b=T, and b=F -> a=F, but a=F -> b=?
Implementation
DefaultMap<Logic, Set<Logic>> rules2Prereq(
DefaultMap<(Logic, bool), Set<(Logic, bool)>> rules) {
final prereq = DefaultMap<Logic, Set<Logic>>(() => <Logic>{});
for (final entry in rules.entries) {
final (key, impl) = (entry.key, entry.value);
var (a, _) = key;
if (a is Not) {
a = a.arg;
}
for (var (i, _) in impl) {
if (i is Not) {
i = i.arg;
}
if (!prereq.containsKey(i)) {
prereq[i] = <Logic>{};
}
prereq[i].add(a);
}
}
return prereq;
}