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