com.bpodgursky.jbool_expressions.rules.RuleSet Maven / Gradle / Ivy
package com.bpodgursky.jbool_expressions.rules;
import java.util.List;
import java.util.Map;
import com.bpodgursky.jbool_expressions.Expression;
import com.bpodgursky.jbool_expressions.Not;
import com.google.common.collect.Lists;
public class RuleSet {
public static List> simplifyRules(){
List> rules = Lists.newArrayList();
rules.add(new SimplifyAnd());
rules.add(new SimplifyOr());
rules.add(new SimplifyNot());
rules.add(new CombineAnd());
rules.add(new CombineOr());
rules.add(new SimplifyNExpression());
rules.add(new SimplifyNExprChildren());
return rules;
}
public static List> toSopRules(){
List> rules = Lists.newArrayList(RuleSet.simplifyRules());
rules.add(new ToSOP());
rules.add(new DeMorgan());
return rules;
}
public static List> demorganRules(){
List> rules = Lists.newArrayList(RuleSet.simplifyRules());
rules.add(new DeMorgan());
return rules;
}
public static Expression applyAll(Expression e, List> rules){
Expression orig = e;
Expression simplified = applyAllSingle(orig, rules);
while(!orig.equals(simplified)){
orig = simplified;
simplified = applyAllSingle(orig, rules);
}
return simplified;
}
private static Expression applyAllSingle(Expression e, List> rules){
Expression tmp = e.apply(rules);
for(Rule, K> r: rules){
tmp = r.apply(tmp);
}
return tmp;
}
public static Expression simplify(Expression root){
return applySet(root, RuleSet.simplifyRules());
}
public static Expression toSop(Expression root){
return applySet(root, RuleSet.toSopRules());
}
public static Expression toPos(Expression root){
// not + simplify
Not inverse = Not.of(root);
Expression sopInv = toSop(inverse);
// not + demorgan
Not inverse2 = Not.of(sopInv);
return applySet(inverse2, RuleSet.demorganRules());
}
public static Expression assign(Expression root, Map values){
List> rules = Lists.newArrayList(RuleSet.simplifyRules());
rules.add(new Assign(values));
return applySet(root, rules);
}
public static Expression applySet(Expression root, List> allRules){
return applyAll(root, allRules);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy