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 ImplyFn extends ValueFunction {
31     protected ValueFunction[] args = new ValueFunction[2];
32  
33     public ImplyFn(ValueFunction lhsArg, ValueFunction rhsArg) {
34        super(IMPLY,2);
35        if ( !(lhsArg instanceof LogicalFn) )
36           throw new IllegalArgumentException("Illegal argument \'" + lhsArg +
37              "\' in function \'(if " + lhsArg + " then " + rhsArg + ")\'");
38  
39        args[0] = lhsArg;
40        args[1] = rhsArg;
41     }
42     public String toString() {
43        String out = "(if " + args[0] + " then " + args[1] + ")";
44        return out;
45     }
46     ValueFunction simplify() {
47        ValueFunction a, b;
48        a = args[0].simplify();
49        b = args[1].simplify();
50        return (a != args[0] || b != args[1]) ? new ImplyFn(a,b) : this;
51     }
52     Object getArg(int position) {
53        return args[position];
54     }
55     public boolean references(ValueFunction var) {
56        return args[0].references(var) || args[1].references(var);
57     }
58     public Vector variables() {
59        return Misc.union(args[0].variables(),args[1].variables());
60     }
61     public boolean isDeterminate() {
62        return args[0].isDeterminate() && args[1].isDeterminate();
63     }
64     ValueFunction normalize() {
65        return new ImplyFn(args[0].normalize(),args[1].normalize());
66     }
67     public ValueFunction mirror() {
68        return new ImplyFn(args[0].mirror(),args[1].mirror());
69     }
70     public ValueFunction resolve(ResolutionContext c, Bindings bb) {
71        ValueFunction a = args[0].resolve(c,bb);
72        ValueFunction b = args[1].resolve(c,bb);
73        return (new ImplyFn(a,b)).evaluationFn();
74     }
75     public ValueFunction evaluationFn() {
76        switch( ((LogicalFn)args[0]).evaluate() ) {
77           case LogicalFn.TRUE:
78                return args[1];
79           case LogicalFn.FALSE:
80                return null;
81           case LogicalFn.UNKNOWN:
82                return this;
83        }
84        return this;
85     }
86     public ValueFunction duplicate(DuplicationTable table) {
87        return new ImplyFn(args[0].duplicate(table),args[1].duplicate(table));
88     }
89     public boolean equals(Object any) {
90        if ( !(any instanceof ImplyFn) ) return false;
91        ImplyFn fn = (ImplyFn)any;
92        ValueFunction a = this.simplify();
93        ValueFunction b = fn.simplify();
94        return ((ValueFunction)a.getArg(0)).equals((ValueFunction)b.getArg(0)) &&
95               ((ValueFunction)a.getArg(1)).equals((ValueFunction)b.getArg(1));
96     }
97     ValueFunction unify(ValueFunction fn, Bindings b) {
98        ValueFunction x = null, y = null;
99  
100       if ( (x = evaluationFn()) == null )
101          return null;
102       else if ( x != this )
103          return x.unifiesWith(fn,b);
104 
105       switch( fn.getID() ) {
106          case IMPLY:
107               if ( (y = ((ImplyFn)fn).evaluationFn()) == null )
108                  return null;
109               else if ( y != fn )
110                  return this.unifiesWith(y,b);
111 
112               if ( (x = args[1].unifiesWith(fn,b)) == null )
113                  return null;
114               else
115                  return new ImplyFn(args[0],x);
116 
117          case ELSE:
118               if ( (y = ((ElseFn)fn).evaluationFn()) == null )
119                  return null;
120               else if ( y != fn )
121                  return this.unifiesWith(y,b);
122 
123               if ( (x = args[1].unifiesWith(fn,b)) == null )
124                  return null;
125               else
126                  return new ImplyFn(args[0],x);
127 
128          default:
129               if ( (x = args[1].unifiesWith(fn,b)) == null )
130                  return null;
131               else
132                  return new ImplyFn(args[0],x);
133       }
134       
135    }
136    public int baseID() {
137       // must be called when Fn is known to be determinate
138       return args[1].baseID();
139    }
140 }