|
| 1 | +package week4.baselangs.bool; |
| 2 | + |
| 3 | +import week4.baselangs.bool.ast.*; |
| 4 | + |
| 5 | +import java.util.Set; |
| 6 | +import java.util.TreeSet; |
| 7 | + |
| 8 | +import static week4.baselangs.bool.BoolMaster.DecisionTree.FALSE; |
| 9 | +import static week4.baselangs.bool.BoolMaster.DecisionTree.TRUE; |
| 10 | +import static week4.baselangs.bool.ast.BoolNode.not; |
| 11 | +import static week4.baselangs.bool.ast.BoolNode.or; |
| 12 | + |
| 13 | +public class BoolMaster { |
| 14 | + |
| 15 | + // Meil on antud järgnev klass andmete kogumiseks: |
| 16 | + public static final class Stats { |
| 17 | + private final Set<Character> variables = new TreeSet<>(); |
| 18 | + private boolean found = false; |
| 19 | + |
| 20 | + public Set<Character> getVariables() { |
| 21 | + return variables; |
| 22 | + } |
| 23 | + |
| 24 | + public boolean containsImp() { |
| 25 | + return found; |
| 26 | + } |
| 27 | + |
| 28 | + public void addVar(BoolVar node) { |
| 29 | + variables.add(node.name()); |
| 30 | + } |
| 31 | + |
| 32 | + public void foundImp() { |
| 33 | + found = true; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // Analüüsida avaldist kasutades Stats klassi meetodid. |
| 38 | + public static Stats analyze(BoolNode node) { |
| 39 | + Stats stats = new Stats(); |
| 40 | + analyze(node, stats); |
| 41 | + return stats; |
| 42 | + } |
| 43 | + |
| 44 | + private static void analyze(BoolNode node, Stats stats) { |
| 45 | + switch (node) { |
| 46 | + case BoolVar var -> stats.addVar(var); |
| 47 | + case BoolNot(BoolNode exp) -> analyze(exp, stats); |
| 48 | + case BoolOr(BoolNode left, BoolNode right) -> { |
| 49 | + analyze(left, stats); |
| 50 | + analyze(right, stats); |
| 51 | + } |
| 52 | + case BoolImp(BoolNode antedecent, BoolNode consequent) -> { |
| 53 | + stats.foundImp(); |
| 54 | + analyze(antedecent, stats); |
| 55 | + analyze(consequent, stats); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + // Teisendada avaldises implikatsiooni teiste operaatoritega. |
| 62 | + public static BoolNode transform(BoolNode node) { |
| 63 | + return switch (node) { |
| 64 | + case BoolVar var -> var; |
| 65 | + case BoolNot(BoolNode expr) -> not(transform(expr)); |
| 66 | + case BoolOr(BoolNode left, BoolNode right) -> or(transform(left), transform(right)); |
| 67 | + case BoolImp(BoolNode antedecent, BoolNode consequent) -> or( |
| 68 | + not(transform(antedecent)), |
| 69 | + transform(consequent) |
| 70 | + ); |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + // Teha avaldisest otsustuspuu. |
| 76 | + public static DecisionTree createDecisionTree(BoolNode node) { |
| 77 | + return switch (node) { |
| 78 | + case BoolVar(char name) -> DecisionTree.decision(name, TRUE, FALSE); |
| 79 | + case BoolNot(BoolNode expr) -> createDecisionTree(expr).compose(FALSE, TRUE); |
| 80 | + case BoolOr(BoolNode left, BoolNode right) -> |
| 81 | + createDecisionTree(left).compose(TRUE, createDecisionTree(right)); |
| 82 | + case BoolImp(BoolNode antedecent, BoolNode consequent) -> { |
| 83 | + DecisionTree cons = createDecisionTree(consequent); |
| 84 | + yield createDecisionTree(antedecent).compose(cons, TRUE); |
| 85 | + } |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Otsustuspuu on tõeväärtusavaldise puukujuline esitus, kus vahetipudeks on ainult muutujad. |
| 91 | + * See esitab see avaldise tõeväärtustabelit, vt. meetod eval! |
| 92 | + * <p> |
| 93 | + * Puu loomiseks võib kasutada meetod compose, mis jätkab otsustuspuud |
| 94 | + * x.compose(y,z) tulemuseks on selline puu, et |
| 95 | + * kui x on tõene, siis on tulemuseks y. |
| 96 | + * vastasel korral on tulemuseks z. |
| 97 | + * Seega on näiteks avaldise x eitus lihtsalt x.compose(FALSE, TRUE). |
| 98 | + */ |
| 99 | + public abstract static class DecisionTree { |
| 100 | + |
| 101 | + public static final DecisionTree TRUE = new TrueNode(); |
| 102 | + public static final DecisionTree FALSE = new FalseNode(); |
| 103 | + |
| 104 | + public static DecisionTree decision(char c, DecisionTree tc, DecisionTree fc) { |
| 105 | + return new DecisionNode(c, tc, fc); |
| 106 | + } |
| 107 | + |
| 108 | + public abstract boolean eval(Set<Character> tv); |
| 109 | + |
| 110 | + public abstract DecisionTree compose(DecisionTree trueDecision, DecisionTree falseDecision); |
| 111 | + |
| 112 | + |
| 113 | + private static class DecisionNode extends DecisionTree { |
| 114 | + private final Character variable; |
| 115 | + private final DecisionTree trueTree; |
| 116 | + private final DecisionTree falseTree; |
| 117 | + |
| 118 | + public DecisionNode(Character variable, DecisionTree trueTree, DecisionTree falseTree) { |
| 119 | + this.variable = variable; |
| 120 | + this.trueTree = trueTree; |
| 121 | + this.falseTree = falseTree; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public boolean eval(Set<Character> tv) { |
| 126 | + return tv.contains(variable) ? trueTree.eval(tv) : falseTree.eval(tv); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public DecisionTree compose(DecisionTree trueDecision, DecisionTree falseDecision) { |
| 131 | + return new DecisionNode(variable, |
| 132 | + trueTree.compose(trueDecision, falseDecision), |
| 133 | + falseTree.compose(trueDecision, falseDecision)); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public String toString() { |
| 138 | + return "(" + variable + " ? " + trueTree + " : " + falseTree + ")"; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static class TrueNode extends DecisionTree { |
| 143 | + @Override |
| 144 | + public boolean eval(Set<Character> tv) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public DecisionTree compose(DecisionTree trueDecision, DecisionTree falseDecision) { |
| 150 | + return trueDecision; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public String toString() { |
| 155 | + return "true"; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static class FalseNode extends DecisionTree { |
| 160 | + @Override |
| 161 | + public boolean eval(Set<Character> tv) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public DecisionTree compose(DecisionTree trueDecision, DecisionTree falseDecision) { |
| 167 | + return falseDecision; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public String toString() { |
| 172 | + return "false"; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + } |
| 177 | +} |
0 commit comments