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 class LogicalAndFn extends LogicalFn {
31     protected ValueFunction[] args = new ValueFunction[2];
32  
33     static final String[] legal_operands = {
34        "zeus.concepts.fn.DefinedFn",
35        "zeus.concepts.fn.MethodCallFn"
36     };
37  
38     public LogicalAndFn(ValueFunction lhs, ValueFunction rhs)
39        throws IllegalArgumentException {
40  
41        super(LAND,4);
42        String lhs_type = lhs.getClass().getName();
43        String rhs_type = rhs.getClass().getName();
44  
45        if ( (!(lhs instanceof LogicalFn) && !Misc.member(lhs_type,legal_operands)) ||
46             (!(rhs instanceof LogicalFn) && !Misc.member(rhs_type,legal_operands)) )
47           throw new IllegalArgumentException("Illegal operand type in function  \'" +
48                                              lhs + " && " + rhs + "\'");
49        args[0] = lhs;
50        args[1] = rhs;
51     }
52  
53     public String toString() {
54        return "(" + args[0] + " && " + args[1] + ")";
55     }
56     public ValueFunction mirror() {
57        return new LogicalAndFn(args[0].mirror(),args[1].mirror());
58     }
59     ValueFunction simplify() {
60        ValueFunction a, b;
61        a = args[0].simplify();
62        b = args[1].simplify();
63        return (a != args[0] || b != args[1])
64               ? new LogicalAndFn(a,b) : this;
65     }
66     Object getArg(int position) {
67        return args[position];
68     }
69     public boolean references(ValueFunction var) {
70        return args[0].references(var) || args[1].references(var);
71     }
72     public Vector variables() {
73        return Misc.union(args[0].variables(),args[1].variables());
74     }
75     public boolean isDeterminate() {
76        return args[0].isDeterminate() && args[1].isDeterminate();
77     }
78     ValueFunction normalize() {
79        ValueFunction a, b;
80        a = args[0].normalize();
81        b = args[1].normalize();
82        return (a != args[0] || b != args[1] )
83               ? new LogicalAndFn(a,b) : this;
84     }
85     public ValueFunction resolve(ResolutionContext c, Bindings b) {
86        ValueFunction x = args[0].resolve(c,b);
87        ValueFunction y = args[1].resolve(c,b);
88        return (new LogicalAndFn(x,y)).evaluationFn();
89     }
90  
91     public int evaluate() {
92        ValueFunction fn = evaluationFn();
93        if ( fn == this ) return UNKNOWN;
94        else if ( fn == BoolFn.trueFn ) return TRUE;
95        else return FALSE;
96     }
97  
98     public ValueFunction evaluationFn() {
99        if ( !isDeterminate() ) return this;
100 
101       BoolFn a, b;
102       a = (BoolFn) args[0].evaluationFn();
103       b = (BoolFn) args[1].evaluationFn();
104 
105       return a.equals(b) ? BoolFn.trueFn : BoolFn.falseFn;
106    }
107 
108    public ValueFunction duplicate(DuplicationTable table) {
109       return new LogicalAndFn(args[0].duplicate(table),args[1].duplicate(table));
110    }
111    public boolean equals(Object any) {
112       if ( !(any instanceof LogicalAndFn) ) return false;
113       LogicalAndFn fn = (LogicalAndFn)any;
114       ValueFunction a = this.simplify();
115       ValueFunction b = fn.simplify();
116       return ((ValueFunction)a.getArg(0)).equals((ValueFunction)b.getArg(0)) &&
117              ((ValueFunction)a.getArg(1)).equals((ValueFunction)b.getArg(1));
118    }
119 }