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 FieldFn extends ValueFunction {
31     protected String arg = null;
32  
33     public FieldFn(String arg) {
34        super(FIELD,8);
35        this.arg = arg;
36     }
37     public ValueFunction mirror() {
38        return new FieldFn(arg);
39     }
40     public String toString() {
41        return arg;
42     }
43     Object getArg(int position) {
44        if (position != 0) throw new ArrayIndexOutOfBoundsException(position);
45        return arg;
46     }
47     String getBase() {
48        int index = arg.indexOf(Fact.A_CHR);
49        // ignore preceding Fact.V_CHR or Fact.F_CHR
50        return arg.substring(1,index); 
51     }
52     public ValueFunction duplicate(DuplicationTable table) {
53        // Example ?man12.name.firstname => ?var101.name.firstname.
54  
55        int index = arg.indexOf(Fact.A_CHR);
56        // ignore preceding Fact.V_CHR or Fact.F_CHR
57        String base =  arg.substring(1,index);
58  
59        base = table.getRef(base);
60  
61        String result = "" + arg.charAt(0) + base +
62                        arg.substring(index,arg.length());
63  
64        return new FieldFn(result);
65     }
66     public boolean references(ValueFunction var) {
67        return this.equals(var);
68     }
69     public Vector variables() {
70        Vector out = new Vector();
71        if ( arg.charAt(0) == Fact.V_CHR ) {
72           String s = "" + arg.charAt(0) + getBase();
73           out.addElement(new VarFn(s));
74        }
75        return out;
76     }
77     public boolean isDeterminate() {
78        return false;
79     }
80     public ValueFunction resolve(ResolutionContext c, Bindings b) {
81        StringTokenizer st = new StringTokenizer(arg,Fact.A_STR);
82        String attribute, fid;
83        ValueFunction value;
84        Fact f1;
85  
86        fid = st.nextToken();
87        f1 = c.lookUp(fid);
88        if ( f1 == null ) return this;
89  
90        while( st.hasMoreTokens() ) {
91           attribute = st.nextToken();
92           value = f1.getFn(attribute);
93           value = value.resolve(c,b);
94           if ( !st.hasMoreTokens() )
95              return b.lookUp(value);
96           else {
97              if ( value.isDeterminate() ) {
98                 value = value.evaluationFn();
99                 switch( value.getID() ) {
100                   case TYPE:
101                        fid = value.toString();
102                        f1 = c.lookUp(fid);
103                        break;
104 
105                   default:
106                        throw new IllegalArgumentException("Illegal " + 
107                          " attribute \'" + value + "\' in \'" + this + "\'");
108                }
109             }
110             else 
111                return b.lookUp(this);
112          }
113       }
114       throw new IllegalArgumentException("Cannot resolve \'" + this + "\'");
115    }
116    public boolean equals(Object any) {
117       if ( !(any instanceof FieldFn) ) return false;
118       FieldFn fn = (FieldFn)any;
119       return arg.equals((String)fn.getArg());
120    }
121    ValueFunction unify(ValueFunction fn, Bindings b) {
122       switch( fn.getID() ) {
123          case FIELD:
124          case VECT:
125          case BOOL:
126          case DATE:
127          case ID:
128          case INT:
129          case REAL:
130          case TYPE:
131          case TIME:
132               return new AndFn(this,fn);
133 
134          default:
135               throw new IllegalArgumentException("Cannot unify \'" + fn +
136                  "\' with \'" + this + "\'");
137       }
138    }
139 
140 }