001    /*
002     * Created on Sep 18, 2004
003     *
004     */
005    package aima.logic.fol.parsing.ast;
006    
007    import java.util.ArrayList;
008    import java.util.Collections;
009    import java.util.List;
010    
011    import aima.logic.fol.parsing.FOLVisitor;
012    
013    /**
014     * @author Ravi Mohan
015     * @author Ciaran O'Reilly
016     */
017    public class QuantifiedSentence implements Sentence {
018            private String quantifier;
019            private List<Variable> variables = new ArrayList<Variable>();
020            private Sentence quantified;
021            private List<FOLNode> args = new ArrayList<FOLNode>();
022            private String stringRep = null;
023            private int hashCode = 0;
024    
025            public QuantifiedSentence(String quantifier, List<Variable> variables,
026                            Sentence quantified) {
027                    this.quantifier = quantifier;
028                    this.variables.addAll(variables);
029                    this.quantified = quantified;
030                    this.args.addAll(variables);
031                    this.args.add(quantified);
032            }
033    
034            public String getQuantifier() {
035                    return quantifier;
036            }
037    
038            public List<Variable> getVariables() {
039                    return Collections.unmodifiableList(variables);
040            }
041    
042            public Sentence getQuantified() {
043                    return quantified;
044            }
045    
046            //
047            // START-Sentence
048            public String getSymbolicName() {
049                    return getQuantifier();
050            }
051    
052            public boolean isCompound() {
053                    return true;
054            }
055    
056            public List<FOLNode> getArgs() {
057                    return Collections.unmodifiableList(args);
058            }
059    
060            public Object accept(FOLVisitor v, Object arg) {
061                    return v.visitQuantifiedSentence(this, arg);
062            }
063    
064            public QuantifiedSentence copy() {
065                    List<Variable> copyVars = new ArrayList<Variable>();
066                    for (Variable v : variables) {
067                            copyVars.add(v.copy());
068                    }
069                    return new QuantifiedSentence(quantifier, copyVars, quantified.copy());
070            }
071    
072            // END-Sentence
073            //
074    
075            @Override
076            public boolean equals(Object o) {
077    
078                    if (this == o) {
079                            return true;
080                    }
081                    if ((o == null) || (this.getClass() != o.getClass())) {
082                            return false;
083                    }
084                    QuantifiedSentence cs = (QuantifiedSentence) o;
085                    return cs.quantifier.equals(quantifier)
086                                    && cs.variables.equals(variables)
087                                    && cs.quantified.equals(quantified);
088            }
089    
090            @Override
091            public int hashCode() {
092                    if (0 == hashCode) {
093                            hashCode = 17;
094                            hashCode = 37 * hashCode + quantifier.hashCode();
095                            for (Variable v : variables) {
096                                    hashCode = 37 * hashCode + v.hashCode();
097                            }
098                            hashCode = hashCode * 37 + quantified.hashCode();
099                    }
100                    return hashCode;
101            }
102    
103            @Override
104            public String toString() {
105                    if (null == stringRep) {
106                            StringBuilder sb = new StringBuilder();
107                            sb.append(quantifier);
108                            sb.append(" ");
109                            for (Variable v : variables) {
110                                    sb.append(v.toString());
111                                    sb.append(" ");
112                            }
113                            sb.append(quantified.toString());
114                            stringRep = sb.toString();
115                    }
116                    return stringRep;
117            }
118    }