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.rete;
25  
26  import java.util.*;
27  import zeus.util.*;
28  import zeus.concepts.*;
29  import zeus.concepts.fn.*;
30  
31  
32  public class Pattern {
33     static final String FACT_MARKER = "<-";
34     static final String NEGATION    = "not";
35     static final String TEST_NAME   = "test";
36  
37     public static final int NONE = 0;
38     public static final int NOT  = 1;
39     public static final int TEST = 2;
40     public static final int CMD  = 3;
41  
42     VarFn  id = null;
43     int    tag  = NONE;
44     Object data = null;
45  
46     public Pattern(ReteFact t) {
47        this.data = t;
48     }
49     
50     
51     public Pattern(int tag, ReteFact t) {
52        Assert.notFalse(tag == NOT || tag == NONE);
53        this.tag = tag;
54        this.data = t;
55     }
56     
57     
58     public Pattern(int tag, ValueFunction constraint) {
59        Assert.notFalse(tag == TEST);
60        this.tag = tag;
61        this.data = constraint;
62     }
63     
64     
65     public Pattern(Pattern p) {
66        ValueFunction v;
67  
68        tag = p.tag;
69        id = p.id;
70        switch(p.tag) {
71           case NONE:
72           case NOT:
73                data = new ReteFact((ReteFact)p.data);
74                break;
75  
76           case TEST:
77                data = ((ValueFunction)p.data).mirror();
78                break;
79        }
80     }
81  
82  
83     public void setId(VarFn id) { this.id = id; }
84  
85  
86     Pattern duplicate(String name, GenSym genSym) {
87        DuplicationTable table = new DuplicationTable(name,genSym);
88        return duplicate(table);
89     }
90     
91     
92     Pattern duplicate(DuplicationTable table) {
93        ReteFact f;
94        Pattern p;
95        ValueFunction v;
96  
97        switch(tag) {
98           case NONE:
99           case NOT:
100               f = (ReteFact)data;
101               f = f.duplicate(table);
102               p = new Pattern(tag,f);
103               if ( id != null ) p.id = (VarFn)id.duplicate(table);
104               return p;
105 
106          case TEST:
107               v = (ValueFunction)data;
108               v = v.duplicate(table);
109               p = new Pattern(tag,v);
110               return p;
111       }
112       return this;
113    }
114    
115    
116    public boolean resolve(Bindings b) {
117       ReteFact f;
118       ValueFunction v;
119 
120       if ( id != null ) {
121          if ( (id = (VarFn)id.resolve(b)) == null )
122             return false;
123       }
124      
125       switch(tag) {
126          case NONE:
127          case NOT:
128               f = (ReteFact)data;
129               return f.resolve(b);
130 
131          case TEST:
132               v = (ValueFunction)data;
133               return ( (v = v.resolve(b)) != null );
134       }
135       return false;
136    }
137    
138    
139    public String toString() {
140       switch(tag) {
141          case NONE:
142               if ( id == null ) 
143                  return data.toString();
144               else
145                  return id.toString() + " " + 
146                         FACT_MARKER + " " + data.toString();
147 
148          case NOT:
149               return "(" + NEGATION + " " + data.toString() + ")";
150 
151          case TEST:
152               return "(" + TEST_NAME + " " + data.toString() + ")";
153       }
154       Core.ERROR(null,1,this);
155       return null;
156    }
157    
158    
159    public String pprint() {
160       return pprint(0);
161    }
162    
163    
164    public String pprint(int sp) {
165       switch(tag) {
166          case NONE:
167               return Misc.spaces(sp) + 
168                      data.toString();
169 
170          case NOT:
171               return Misc.spaces(sp) + 
172                      "(" + NEGATION + " " + data.toString() + ")";
173 
174          case TEST:
175               return Misc.spaces(sp) + 
176                      "(" + TEST_NAME + " " + data.toString() + ")";
177       }
178       Core.ERROR(null,1,this);
179       return null;
180    }
181 }