001    /*
002     * Created on Sep 15, 2003 by Ravi Mohan
003     *  
004     */
005    package aima.logic.propositional.parsing.ast;
006    
007    import aima.logic.propositional.parsing.PLVisitor;
008    import aima.logic.propositional.parsing.ast.Sentence;
009    
010    /**
011     * @author Ravi Mohan
012     * 
013     */
014    
015    public class BinarySentence extends ComplexSentence {
016            private String operator;
017    
018            private Sentence first;
019    
020            private Sentence second;
021    
022            public BinarySentence(String operator, Sentence first, Sentence second) {
023                    this.operator = operator;
024                    this.first = first;
025                    this.second = second;
026    
027            }
028    
029            public Sentence getFirst() {
030                    return first;
031            }
032    
033            public String getOperator() {
034                    return operator;
035            }
036    
037            public Sentence getSecond() {
038                    return second;
039            }
040    
041            @Override
042            public boolean equals(Object o) {
043    
044                    if (this == o) {
045                            return true;
046                    }
047                    if ((o == null) || (this.getClass() != o.getClass())) {
048                            return false;
049                    }
050                    BinarySentence bs = (BinarySentence) o;
051                    return ((bs.getOperator().equals(getOperator()))
052                                    && (bs.getFirst().equals(first)) && (bs.getSecond()
053                                    .equals(second)));
054    
055            }
056    
057            @Override
058            public int hashCode() {
059                    int result = 17;
060                    result = 37 * result + first.hashCode();
061                    result = 37 * result + second.hashCode();
062                    return result;
063            }
064    
065            @Override
066            public String toString() {
067                    return " ( " + first.toString() + " " + operator + " "
068                                    + second.toString() + " )";
069            }
070    
071            @Override
072            public Object accept(PLVisitor plv, Object arg) {
073                    return plv.visitBinarySentence(this, arg);
074            }
075    
076            public boolean isOrSentence() {
077                    return (getOperator().equals("OR"));
078            }
079    
080            public boolean isAndSentence() {
081                    return (getOperator().equals("AND"));
082            }
083    
084            public boolean isImplication() {
085                    return (getOperator().equals("=>"));
086            }
087    
088            public boolean isBiconditional() {
089                    return (getOperator().equals("<=>"));
090            }
091    
092            public boolean firstTermIsAndSentence() {
093                    return (getFirst() instanceof BinarySentence)
094                                    && (((BinarySentence) getFirst()).isAndSentence());
095            }
096    
097            public boolean secondTermIsAndSentence() {
098                    return (getSecond() instanceof BinarySentence)
099                                    && (((BinarySentence) getSecond()).isAndSentence());
100            }
101    }