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 ValueFunction {
31  /*
32  	Precedence values
33  
34  	0	CONSB
35  	1	LVAR
36  	2	ELSE, IMPLY
37  	3	ARITH, FUNC, METH
38          4       COMP, LAND, LNOT, LOR
39  	5	AND
40  	6	OR
41  	7	CONS, FACT?
42  	8	FIELD
43          9
44  	10	VECT, BOOL, DATE, ID, INT, REAL, TYPE, TIME
45  */
46  
47     static final int ID_MIN = 0;
48     static final int ID_MAX = 23;
49  
50  
51     public static final int CONSB = 0;
52     public static final int LVAR  = 1;
53     public static final int ID    = 2;
54     public static final int TYPE  = 3;
55     public static final int FIELD = 4;
56     public static final int AND   = 5;
57     public static final int OR    = 6;
58     public static final int CONS  = 7;
59     public static final int IMPLY = 8;
60     public static final int ELSE  = 9;
61     public static final int LOR   = 10;
62     public static final int LAND  = 11;
63     public static final int LNOT  = 12;
64     public static final int COMP  = 13;
65     public static final int INT   = 14;
66     public static final int REAL  = 15;
67     public static final int DATE  = 16;
68     public static final int TIME  = 17;
69     public static final int BOOL  = 18;
70     public static final int ARITH = 19;
71     public static final int FACT  = 20;
72     public static final int FUNC  = 21;
73     public static final int METH  = 22;
74     public static final int VECT  = 23;
75  
76     static final int PD_MIN = 0;
77     static final int PD_MAX = 10;
78  
79     protected int type = -1;
80     protected int precedence = -1;
81  
82     public ValueFunction(int type, int precedence) {
83        Assert.notFalse(type >= ID_MIN && type <= ID_MAX);
84        Assert.notFalse(precedence >= PD_MIN && precedence <= PD_MAX);
85        this.type = type;
86        this.precedence = precedence;
87     }
88  
89     public final int getID() { return type; }
90     public final int getPD() { return precedence; }
91  
92     public abstract boolean isDeterminate();
93     public abstract boolean references(ValueFunction var);
94  
95     public final ValueFunction unifiesWith(ValueFunction fn, Bindings b) {
96        ValueFunction x, y, z;
97        
98        x = this.resolve(b);
99        y = fn.resolve(b);
100 
101       if ( x == null || y == null ) return null;
102 
103       x = x.normalize();
104       y = y.normalize();
105 
106       if ( x.equals(y) ) return y;
107 
108       z = (y.getPD() < x.getPD() ?  y.unify(x,b) : x.unify(y,b));
109 
110       if ( z == null )
111          return null;
112 
113       z = z.simplify();
114       b.set(x,z);
115       b.set(y,z);
116       return z;
117    }
118 
119    public ValueFunction resolve(Bindings b) {
120       return resolve(new ResolutionContext(), b);
121    }
122    public ValueFunction resolve(ResolutionContext c, Bindings b) {
123       return b.lookUp(this);
124    }
125    public Vector variables() {
126       return new Vector();
127    }
128    public ValueFunction duplicate(String name, GenSym genSym) {
129       DuplicationTable table = new DuplicationTable(name,genSym);
130       return duplicate(table);
131    }
132    public ValueFunction duplicate(DuplicationTable table) {
133       return this;
134    }
135    public ValueFunction evaluationFn() {
136       return this;
137    }
138    public int baseID() {
139       return type;
140    }
141 
142    abstract Object        getArg(int position);
143    abstract ValueFunction unify(ValueFunction fn, Bindings b);
144 
145    public abstract ValueFunction mirror();
146 
147    ValueFunction normalize()    { return this; }
148    ValueFunction simplify()     { return this; }
149 
150    final Object getArg()        { return getArg(0); }
151 
152    /*
153       List of methods associated with this ValueFunction type
154    */
155 
156    static final String[] METHOD_LIST = {
157       "isDeterminate", "0",
158       "toString", "0"
159    };
160 
161    static final int IS_DETERMINATE = 0;
162    static final int TO_STRING      = 2;
163 
164    ValueFunction invokeMethod(String method, Vector args) {
165       int arity;
166       int position = Misc.whichPosition(method,METHOD_LIST);
167 
168       if ( position != -1 ) {
169          arity = Integer.parseInt(METHOD_LIST[position+1]);
170          if ( args.size() != arity )
171             throw new IllegalArgumentException(
172                "Wrong number of arguments in method \'" + method + "/" +
173                 arity + "\'.");
174       }
175       else {
176          throw new IllegalArgumentException(
177             "Unknown method \'" + method + "\' invoked on \'" + this +"\'");
178       }
179 
180       switch( position ) {
181          case IS_DETERMINATE:
182               return isDeterminate() ? BoolFn.trueFn : BoolFn.falseFn;
183 
184          case TO_STRING:
185               return new IdFn(toString());
186 
187          default:
188               throw new IllegalArgumentException(
189                  "Unknown method \'" + method + "\' invoked on \'" + this +"\'");
190       }
191    }
192 }