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;
25  
26  import java.util.*;
27  
28  import zeus.util.*;
29  import zeus.concepts.fn.*;
30  
31  
32  public class AttributeList extends Hashtable {
33     AttributeList() {
34     }
35  
36     AttributeList(AttributeList List) {
37        this();
38        String attribute;
39        ValueFunction value;
40  
41        for(Enumeration enum = List.keys(); enum.hasMoreElements(); ) {
42           attribute = (String) enum.nextElement();
43           value = List.getFn(attribute);
44           this.setValue(attribute,value.mirror());
45        }
46     }
47  
48    
49    /*** 
50      getNames produces a list of the names of the attributes. 
51      You can iterate through this list using the getValue call to find the 
52      values of the attributes
53      *@since 1.21
54      *@author Simon Thompson
55      */
56    public String [] getNames () { 
57      return this.listAttributes(); 
58    }
59  
60  
61     public String[] listAttributes() {
62        String[] result = new String[size()];
63  
64        Enumeration enum = this.keys();
65        for(int i = 0; enum.hasMoreElements(); i++ )
66           result[i] = (String) enum.nextElement();
67        return result;
68     }
69  
70  
71  
72  
73     ValueFunction[] listValues() {
74        ValueFunction[] result = new ValueFunction[size()];
75  
76        Enumeration enum = this.elements();
77        for(int i = 0; enum.hasMoreElements(); i++ )
78           result[i] = (ValueFunction) enum.nextElement();
79        return result;
80     }
81     
82     
83     Vector variables() {
84        Vector data = new Vector(100);
85        Enumeration enum = this.elements();
86        while( enum.hasMoreElements() )
87          data = Misc.union(data,((ValueFunction)enum.nextElement()).variables());
88        return data;
89     }
90  
91  
92     public void setValue(String attribute, ValueFunction value) {
93        this.put(attribute,value);
94     }
95  
96  
97     public String getValue(String attribute) {
98        ValueFunction fn = (ValueFunction)get(attribute);
99        if ( fn != null ) return fn.toString();
100       else return null;
101    }
102 
103 
104    public ValueFunction getFn(String attribute) {
105       return (ValueFunction)get(attribute);
106    }
107 
108 
109    public String toString() {
110       String results = new String("(");
111       String attribute;
112       ValueFunction value;
113       for(Enumeration enum = this.keys(); enum.hasMoreElements(); ) {
114          attribute = (String) enum.nextElement();
115          value = (ValueFunction) this.get(attribute);
116 
117          results += "(" + attribute + " " + value + ")";
118       }
119       return results.trim() + ")";
120    }
121 
122         /***
123                 added by Simon on 20/02/02 - rough and ready implementation as an
124                 experiment
125                 */
126    public String toSL() {
127       String results = new String("");
128       String attribute;
129       ValueFunction value;
130       for(Enumeration enum = this.keys(); enum.hasMoreElements(); ) {
131          attribute = (String) enum.nextElement();
132          value = (ValueFunction) this.get(attribute);
133 
134          results += ":" + attribute + " " + value + " ";
135       }
136       return results.trim() ;
137    }
138 
139 
140    String pprint() {
141       return pprint(0);
142    }
143    
144    
145    String pprint(int sp) {
146       String tabs = Misc.spaces(sp);
147       String eol  = "\n" + tabs + " ";
148       String results = new String("(");
149       String attribute;
150       ValueFunction value;
151 
152       for(Enumeration enum = this.keys(); enum.hasMoreElements(); ) {
153          attribute = (String) enum.nextElement();
154          value = (ValueFunction)this.get(attribute);
155 
156          results += "(" + attribute + " " + value +")" + eol;
157       }
158       return results.trim() + "\n" + tabs + ")";
159    }
160 
161 
162    boolean unifiesWith(AttributeList List, Bindings bindings) {
163       String attribute;
164       ValueFunction value, m_value;
165 
166       Bindings b = new Bindings(bindings);
167       for( Enumeration enum = this.keys(); enum.hasMoreElements(); ) {
168          attribute = (String) enum.nextElement();
169          value = List.getFn(attribute);
170          m_value = getFn(attribute);
171 
172          Core.ERROR(value,1,this);
173          Core.ERROR(m_value,2,this);
174 
175          if ( !attribute.equals(OntologyDb.NUMBER) ) {
176             if ( m_value.unifiesWith(value,b) == null )
177                return false;
178          }
179          else {
180             // attribute = OntologyDb.NUMBER
181             // unify only if one of them is not a Primitive Numeric Fn
182             if ( !(m_value instanceof NumericFn) ||
183                  !(value instanceof NumericFn) ) {
184                if ( m_value.unifiesWith(value,b) == null )
185                   return false;
186             }
187          }
188       }
189       bindings.set(b);
190       return true;
191    }
192 
193 
194     /*** 
195         somewhat changed to allow less constrained matches
196         @author Simon Thompson
197         @since 1.2.2
198         *//*
199    public boolean equals(AttributeList List) {
200       String attribute;
201       ValueFunction value, m_value;
202       
203       if ( this.size() != List.size() ) return false;
204 
205      
206       for(Enumeration enum = this.keys(); enum.hasMoreElements(); ) {
207          attribute = (String)enum.nextElement();
208          value = List.getFn(attribute);
209          // test for nullness
210          if ( (m_value = List.getFn(attribute)) == null )
211             return false;
212          else if ( !value.equals(m_value) )
213             return false;
214       }
215       return true;
216    }*/
217    
218    
219    
220    
221    public boolean equals(AttributeList List) {
222       String attribute;
223       ValueFunction value, m_value;
224       
225       if ( this.size() != List.size() ) return false;
226 
227      
228       for(Enumeration enum = List.keys(); enum.hasMoreElements(); ) {
229          attribute = (String)enum.nextElement();
230          value = List.getFn(attribute);
231          if ( (m_value = this.getFn(attribute)) == null )
232             return false;
233          else if ( !value.equals(m_value) )
234             return false;
235       }
236       return true;
237    }
238    
239    
240    /*
241 
242 
243     public boolean equals (AttributeList list ) { 
244         String attribute = null; 
245         Enumeration allThis   
246     }*/
247 
248    boolean isDeterminate() {
249       ValueFunction value;
250       Enumeration enum = this.elements();
251       while( enum.hasMoreElements() ) {
252          value = (ValueFunction) enum.nextElement();
253          if ( !value.isDeterminate() ) return false;
254       }
255       return true;
256    }
257    
258    
259    boolean isDeterminate(String attribute) {
260       ValueFunction value = this.getFn(attribute);
261       return value.isDeterminate();
262    }
263 
264 
265    boolean resolve(ResolutionContext context, Bindings bindings) {
266       String attribute;
267       ValueFunction value;
268 
269       for(Enumeration enum = this.keys(); enum.hasMoreElements(); ) {
270          attribute = (String) enum.nextElement();
271          value = getFn(attribute);
272          value = value.resolve(context,bindings);
273          if ( value == null ) return false;
274          this.setValue(attribute,value);
275       }
276       return true;
277    }
278    
279    
280    void disjoin(AttributeList List) {
281       String attribute;
282       ValueFunction value, m_value;
283 
284       for( Enumeration enum = List.keys(); enum.hasMoreElements(); ) {
285          attribute = (String) enum.nextElement();
286          if ( !attribute.equals(OntologyDb.NUMBER) ) {
287             value = List.getFn(attribute);
288             m_value = getFn(attribute);
289             if ( m_value == null || value.equals(m_value) )
290                setValue(attribute,value);
291             else
292                setValue(attribute,new OrFn(m_value,value));
293          }
294       }
295    }
296 
297 
298    AttributeList duplicate(String name, GenSym genSym) {
299       DuplicationTable table = new DuplicationTable(name,genSym);
300       return duplicate(table);
301    }
302    
303    
304    AttributeList duplicate(DuplicationTable table) {
305       AttributeList attr = new AttributeList();
306       String attribute;
307       ValueFunction value, your_value;
308 
309       for( Enumeration enum = this.keys(); enum.hasMoreElements(); ) {
310          attribute = (String) enum.nextElement();
311          value = this.getFn(attribute);
312             your_value = value.duplicate(table);
313          attr.setValue(attribute,your_value);
314       }
315       return attr;
316    }
317 }