Expression result;
actual = equation;
addToContext(actual);
actual = new Equality(actual.lhs().evaluate(), actual.rhs().evaluate()); // simplify both sides
System.out.printf(" simplified: %s%n", actual);
addToContext(actual);
rule = rules.get(0);
while (!terminated()) {
if (rule == null) {
throw new EquationSolveException("No more matching rule");
}
System.out.printf(" rule: %s%n", rule);
if (isApplicable()) {
if (rule instanceof EquationRule) {
return fork(((EquationRule) rule).convertedEquations());
}
result = rule.apply();
if (result instanceof Equality) {
actual = (Equality) result;
} else if (result instanceof Set) {
System.out.printf(" root(s): %s%n", result);
return (Set) result;
}
if (actual instanceof Equation && !actual.getClass().equals(equation.getClass())) {
return new RuleMachine((Equation) actual).execute();
} else {
System.out.printf(" applied: %s%n", actual);
addToContext(actual);
actual = new Equality(actual.lhs().evaluate(), actual.rhs().evaluate());
System.out.printf(" eval'd: %s%n", actual);
addToContext(actual);
rule = nextRule(true);
}