View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3    Copyright (c) 2004, DERI Innsbruck
4    This library is free software; you can redistribute it and/or modify it under
5    the terms of the GNU Lesser General Public License as published by the Free
6    Software Foundation; either version 2.1 of the License, or (at your option)
7    any later version.
8    This library is distributed in the hope that it will be useful, but WITHOUT
9    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11   details.
12   You should have received a copy of the GNU Lesser General Public License along
13   with this library; if not, write to the Free Software Foundation, Inc.,
14   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15   */
16  package org.deri.wsmo4j.logicalexpression;
17  
18  
19  import java.util.List;
20  import java.util.Vector;
21  
22  import org.deri.wsmo4j.logicalexpression.util.ExpressionContainmentUtil;
23  import org.deri.wsmo4j.logicalexpression.util.SetUtil;
24  import org.omwg.logicalexpression.*;
25  
26  
27  /**
28   * This class reunites all binary logical expressions
29   * (e.g LogExpr1 op LogExpr2)
30   * @author DERI Innsbruck, reto.krummenacher@deri.org
31   * @author DERI Innsbruck, thomas.haselwanter@deri.org
32   * @version $Revision: 1946 $ $Date: 2007-04-02 15:13:28 +0300 (Mon, 02 Apr 2007) $
33   * @see org.omwg.logicalexpression.Binary
34   */
35  public abstract class BinaryImpl extends LogicalExpressionImpl
36          implements Binary {
37  
38      protected LogicalExpression exprLeft;
39  
40      protected LogicalExpression exprRight;
41  
42      /**
43       * @param exprLeft the logical expression left of the operator
44       * @param exprRight the logical expression right of the operator
45       * @throws IllegalArgumentException
46       * <p>in case one of the two logical expressions is null
47       * <p>in case the operator is different from AND, EQUIVALENT, IMPLIEDBY, IMPLIES,
48       * LP_IMPL or OR</p>
49       * <p>in case one of the two logical expressions contains a nested CONSTRAINT</p>
50       */
51      public BinaryImpl(LogicalExpression exprLeft, LogicalExpression exprRight)
52              throws IllegalArgumentException {
53          if (exprLeft == null || exprRight == null) {
54              throw new IllegalArgumentException("null not allowed for Arguments of Binary operator");
55          }
56          if (ExpressionContainmentUtil.contains(exprLeft, Constraint.class)
57              || ExpressionContainmentUtil.contains(exprRight, Constraint.class)) {
58              throw new IllegalArgumentException("Nested Constraint not allowed in Binary Operator");
59          }
60          this.exprLeft = exprLeft;
61          this.exprRight = exprRight;
62      }
63  
64      public LogicalExpression getLeftOperand() {
65          return exprLeft;
66      }
67  
68      public LogicalExpression getRightOperand() {
69          return exprRight;
70      }
71  
72      /**
73       * @see CompoundExpression#listOperands()
74       */
75      public List <LogicalExpression> listOperands() {
76          Vector <LogicalExpression> ret = new Vector <LogicalExpression>();
77          ret.add(exprLeft);
78          ret.add(exprRight);
79          return ret;
80      }
81  
82      public int hashCode() {
83          return exprLeft.hashCode() + exprRight.hashCode();
84      }
85  
86      /* (non-Javadoc)
87       * @see org.omwg.logicalexpression.Binary#setLeftOperand(org.omwg.logicalexpression.LogicalExpression)
88       */
89      public void setLeftOperand(LogicalExpression le) {
90          if (le == null) {
91              throw new IllegalArgumentException("null not allowed for Arguments of Binary operator");
92          }
93          if (ExpressionContainmentUtil.contains(le, Constraint.class)) {
94              throw new IllegalArgumentException("Nested Constraint not allowed in Binary Operator");
95          }
96          exprLeft = le;
97          
98      }
99  
100     /* (non-Javadoc)
101      * @see org.omwg.logicalexpression.Binary#setRightOperand(org.omwg.logicalexpression.LogicalExpression)
102      */
103     public void setRightOperand(LogicalExpression le) {
104         if (le == null) {
105             throw new IllegalArgumentException("null not allowed for Arguments of Binary operator");
106         }
107         if (ExpressionContainmentUtil.contains(le, Constraint.class)) {
108             throw new IllegalArgumentException("Nested Constraint not allowed in Binary Operator");
109         }
110         exprRight = le;  
111     }
112 
113     /* (non-Javadoc)
114      * @see org.omwg.logicalexpression.CompoundExpression#setOperands(java.util.List)
115      */
116     public void setOperands(List operands) {
117         if (operands.size()!=2){
118             throw new IllegalArgumentException("For a Binary Operator exactly 2 expressions are necessary!");
119         }
120         if (!SetUtil.allOfType(operands, LogicalExpression.class)){
121             throw new IllegalArgumentException("Operands of a Binary must be Logical Expressions!!");
122         }
123         setLeftOperand((LogicalExpression)operands.get(0));
124         setRightOperand((LogicalExpression)operands.get(1));
125     }
126 }