View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.concepts.fn;
25  
26  import java.util.*;
27  import zeus.util.*;
28  import zeus.concepts.*;
29  
30  public abstract class LogicalFn extends ValueFunction {
31     public static final int TRUE = 0;
32     public static final int FALSE = 1;
33     public static final int UNKNOWN = 2;
34  
35     public LogicalFn(int type, int precedence) {
36        super(type,precedence);
37     }
38     public LogicalFn(int type) {
39        super(type,4);
40     }
41  
42     public abstract int evaluate();
43  
44     public int baseID() {
45        return BOOL;
46     }
47  
48     ValueFunction unify(ValueFunction fn, Bindings b) {
49        int x;
50  
51        switch( fn.getID() ) {
52           case ID:
53           case TYPE:
54           case INT:
55           case REAL:
56           case DATE:
57           case TIME:
58           case ARITH:
59           case VECT:
60                break;
61  
62           // These have higher unification precedence
63           // case LVAR:
64           // case IMPLY:
65           // case ELSE:
66           // case CONSB:
67           // case FUNC:
68           // case METH:
69  
70           case FIELD:
71           case AND:
72           case OR:
73                return new AndFn(evaluationFn(),fn);
74  
75           case CONS:
76                if ( fn.isDeterminate() ) {
77                   if (fn.baseID() != BOOL)
78                      break;
79  
80                   switch( evaluate() ) {
81                      case TRUE:
82                      case FALSE:
83                           return ConstraintFn.compare((ConstraintFn)fn,
84                                    (PrimitiveFn)evaluationFn());
85                      case UNKNOWN:
86                           return new AndFn(evaluationFn(),fn);
87                   }
88                }
89                else
90                   return new AndFn(evaluationFn(),fn);
91  
92           case LOR:
93           case LAND:
94           case LNOT:
95           case COMP:
96           case BOOL:
97                x = ((LogicalFn)fn).evaluate();
98                switch( evaluate() ) {
99                   case TRUE:
100                       switch( x ) {
101                          case TRUE:
102                               return evaluationFn();
103                          case FALSE:
104                               return null;
105                          case UNKNOWN:
106                               return new AndFn(evaluationFn(),fn);
107                       }
108                       break;
109 
110                  case FALSE:
111                       switch( x ) {
112                          case TRUE:
113                               return null;
114                          case FALSE:
115                               return evaluationFn();
116                          case UNKNOWN:
117                               return new AndFn(evaluationFn(),fn);
118                       }
119                       break;
120 
121                  case UNKNOWN:
122                       return new AndFn(this,((LogicalFn)fn).evaluationFn());
123               }
124               break;
125 
126          default:
127               break;
128       }
129       throw new IllegalArgumentException("Unification type clash " +
130          " attempting to unify \'" + fn + "\' with \'" + this + "\'");
131    }
132 }