|
| 1 | +package aima.logic.fol; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import aima.logic.fol.kb.data.Chain; |
| 9 | +import aima.logic.fol.kb.data.Clause; |
| 10 | +import aima.logic.fol.kb.data.Literal; |
| 11 | +import aima.logic.fol.parsing.FOLVisitor; |
| 12 | +import aima.logic.fol.parsing.ast.ConnectedSentence; |
| 13 | +import aima.logic.fol.parsing.ast.Constant; |
| 14 | +import aima.logic.fol.parsing.ast.Function; |
| 15 | +import aima.logic.fol.parsing.ast.NotSentence; |
| 16 | +import aima.logic.fol.parsing.ast.Predicate; |
| 17 | +import aima.logic.fol.parsing.ast.QuantifiedSentence; |
| 18 | +import aima.logic.fol.parsing.ast.Sentence; |
| 19 | +import aima.logic.fol.parsing.ast.Term; |
| 20 | +import aima.logic.fol.parsing.ast.TermEquality; |
| 21 | +import aima.logic.fol.parsing.ast.Variable; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Ciaran O'Reilly |
| 25 | + * |
| 26 | + */ |
| 27 | +public class StandardizeApartInPlace { |
| 28 | + // |
| 29 | + private static CollectAllVariables _collectAllVariables = new CollectAllVariables(); |
| 30 | + |
| 31 | + public static int standardizeApart(Chain c, int saIdx) { |
| 32 | + List<Variable> variables = new ArrayList<Variable>(); |
| 33 | + for (Literal l : c.getLiterals()) { |
| 34 | + collectAllVariables(l.getAtomicSentence(), variables); |
| 35 | + } |
| 36 | + |
| 37 | + return standardizeApart(variables, c, saIdx); |
| 38 | + } |
| 39 | + |
| 40 | + public static int standardizeApart(Clause c, int saIdx) { |
| 41 | + List<Variable> variables = new ArrayList<Variable>(); |
| 42 | + for (Literal l : c.getLiterals()) { |
| 43 | + collectAllVariables(l.getAtomicSentence(), variables); |
| 44 | + } |
| 45 | + |
| 46 | + return standardizeApart(variables, c, saIdx); |
| 47 | + } |
| 48 | + |
| 49 | + // |
| 50 | + // PRIVATE METHODS |
| 51 | + // |
| 52 | + private static int standardizeApart(List<Variable> variables, Object expr, int saIdx) { |
| 53 | + Map<String, Integer> indexicals = new HashMap<String, Integer>(); |
| 54 | + for (Variable v : variables) { |
| 55 | + if (!indexicals.containsKey(v.getIndexedValue())) { |
| 56 | + indexicals.put(v.getIndexedValue(), saIdx++); |
| 57 | + } |
| 58 | + } |
| 59 | + for (Variable v : variables) { |
| 60 | + Integer i = indexicals.get(v.getIndexedValue()); |
| 61 | + if (null == i) { |
| 62 | + throw new RuntimeException("ERROR: duplicate var=" + v + ", expr=" + expr); |
| 63 | + } else { |
| 64 | + v.setIndexical(i); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return saIdx; |
| 69 | + } |
| 70 | + |
| 71 | + private static void collectAllVariables(Sentence s, List<Variable> vars) { |
| 72 | + s.accept(_collectAllVariables, vars); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class CollectAllVariables implements FOLVisitor { |
| 77 | + public CollectAllVariables() { |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @SuppressWarnings("unchecked") |
| 82 | + public Object visitVariable(Variable var, Object arg) { |
| 83 | + List<Variable> variables = (List<Variable>) arg; |
| 84 | + variables.add(var); |
| 85 | + return var; |
| 86 | + } |
| 87 | + |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + public Object visitQuantifiedSentence(QuantifiedSentence sentence, |
| 90 | + Object arg) { |
| 91 | + // Ensure I collect quantified variables too |
| 92 | + List<Variable> variables = (List<Variable>) arg; |
| 93 | + variables.addAll(sentence.getVariables()); |
| 94 | + |
| 95 | + sentence.getQuantified().accept(this, arg); |
| 96 | + |
| 97 | + return sentence; |
| 98 | + } |
| 99 | + |
| 100 | + public Object visitPredicate(Predicate predicate, Object arg) { |
| 101 | + for (Term t : predicate.getTerms()) { |
| 102 | + t.accept(this, arg); |
| 103 | + } |
| 104 | + return predicate; |
| 105 | + } |
| 106 | + |
| 107 | + public Object visitTermEquality(TermEquality equality, Object arg) { |
| 108 | + equality.getTerm1().accept(this, arg); |
| 109 | + equality.getTerm2().accept(this, arg); |
| 110 | + return equality; |
| 111 | + } |
| 112 | + |
| 113 | + public Object visitConstant(Constant constant, Object arg) { |
| 114 | + return constant; |
| 115 | + } |
| 116 | + |
| 117 | + public Object visitFunction(Function function, Object arg) { |
| 118 | + for (Term t : function.getTerms()) { |
| 119 | + t.accept(this, arg); |
| 120 | + } |
| 121 | + return function; |
| 122 | + } |
| 123 | + |
| 124 | + public Object visitNotSentence(NotSentence sentence, Object arg) { |
| 125 | + sentence.getNegated().accept(this, arg); |
| 126 | + return sentence; |
| 127 | + } |
| 128 | + |
| 129 | + public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) { |
| 130 | + sentence.getFirst().accept(this, arg); |
| 131 | + sentence.getSecond().accept(this, arg); |
| 132 | + return sentence; |
| 133 | + } |
| 134 | +} |
0 commit comments