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  
30  /*** 
31     * Rule is the class that defines what a Zeus rule actually looks like
32     * @see zeus.concepts.ReteKB 
33     * @see zeus.rete.ReteEngine
34     * @see zeus.rete.ConflictSet
35     * @see zeus.rete.TypeNode
36     * @see zeus.rete.ActionNode
37   * @author Divine Ndumu 
38   *@author Simon Thompson
39   * Changed to support rules as services 
40      */
41  public class Rule {
42     static final String IMPLIES = "=>";
43     static final String INITIAL_FACT = OntologyDb.ROOT;
44     static final ReteFact  initial_fact = new ReteFact(INITIAL_FACT);
45      /*** 
46          rule salience is the priority measure for a rule in the 
47          rulebase. 
48          */
49     public static final int MIN_SALIENCE  = 0;
50     public static final int MAX_SALIENCE  = 9;
51     public static final int NORM_SALIENCE = 5;
52  
53     String name = null;
54     int salience = NORM_SALIENCE;
55     // service indicates that this rule is a service
56     boolean service = false; 
57     Vector actions = new Vector();
58     Vector patterns = new Vector();
59     boolean can_add_patterns = true;
60  
61    
62     /*** 
63      * filp the service status
64      */
65     public void setService () { 
66         service = !service;
67     }
68     
69     public boolean isService () { 
70         return service;
71     }
72         
73     
74     
75     public Rule(String name) {
76        this.name = name;
77     }
78     
79     
80     protected Rule(String name, boolean state) {
81        this.name = name;
82        can_add_patterns = state;
83     }
84     
85     
86     public Rule(String name, int salience) {
87        this.name = name;
88        setSalience(salience);
89     }
90     
91     
92     protected Rule(String name, int salience, boolean state) {
93        this.name = name;
94        can_add_patterns = state;
95        setSalience(salience);
96     }
97     
98     
99     public Rule(Rule r) {
100       name = r.name;
101       salience = r.salience;
102       can_add_patterns = r.can_add_patterns;
103 
104       Pattern p;
105       Action a;
106       for(int i = 0; i < r.patterns.size(); i++ ) {
107          p = (Pattern)r.patterns.elementAt(i);
108          patterns.addElement(new Pattern(p));
109       }
110       for(int i = 0; i < r.actions.size(); i++ ) {
111          a = (Action)r.actions.elementAt(i);
112          actions.addElement(new Action(a));
113       }
114    }
115    
116    
117    public Rule duplicate(String name, GenSym genSym) {
118       DuplicationTable table = new DuplicationTable(name,genSym);
119       return duplicate(table);
120    }
121    
122    
123    public Rule duplicate(DuplicationTable table) {
124       Rule r = new Rule(name,salience,can_add_patterns);
125       Pattern p;
126       Action a;
127       for(int i = 0; i < patterns.size(); i++ ) {
128          p = (Pattern)patterns.elementAt(i);
129          p = p.duplicate(table);
130          r.patterns.addElement(p);
131       }
132       for(int i = 0; i < actions.size(); i++ ) {
133          a = (Action)actions.elementAt(i);
134          a = a.duplicate(table);
135          r.actions.addElement(a);
136       }
137       return r;
138    }
139    
140    
141    public void addPattern(Pattern p) {
142       Assert.notFalse( can_add_patterns );
143 
144       if ( patterns.isEmpty() ) {
145          switch(p.tag) {
146             case Pattern.NOT:
147             case Pattern.TEST:
148                  patterns.addElement(new Pattern(initial_fact));
149                  break;
150 
151             default:
152                  break;
153          }
154       }
155       patterns.addElement(p);
156    }
157    
158    
159    public void addAction(Action a) {
160       can_add_patterns = false;
161       if ( patterns.isEmpty() )
162          patterns.addElement(new Pattern(initial_fact));
163       actions.addElement(a);
164    }
165    
166    
167    int nTerminals() {
168       Pattern p;
169       int count = 0;
170       for(int i = 0; i < patterns.size(); i++ ) {
171          p = (Pattern)patterns.elementAt(i);
172          switch( p.tag ) {
173             case Pattern.NONE:
174             case Pattern.NOT:
175                  count++;
176                  break;
177             default:
178                  break;
179          }
180       }
181       return count;
182    }
183    
184    
185    public String getName() {
186       return name;
187    }
188    
189    
190    public int getSalience() {
191       return salience;
192    }
193    
194    
195    public void setName(String name) {
196       Core.ERROR(name,1,this);
197       this.name = name;
198    }
199    
200    
201    public Vector getPatterns(){
202       return patterns;
203    }
204 
205 
206 
207    public void setSalience(int salience) {
208       if ( salience < MIN_SALIENCE || salience > MAX_SALIENCE ) {
209          Core.USER_ERROR("Invalid salience level: " + salience);
210          return;
211       }
212       this.salience = salience;
213    }
214 
215 
216 
217    public Vector getActions(){
218       return actions;
219    }
220    
221 
222    public boolean resolve(Bindings b) {
223       boolean status = true;
224       Pattern p;
225       Action a;
226       for(int i = 0; status && i < patterns.size(); i++ ) {
227          p = (Pattern)patterns.elementAt(i);
228          status &= p.resolve(b);
229       }
230       for(int i = 0; status && i < actions.size(); i++ ) {
231          a = (Action)actions.elementAt(i);
232          status &= a.resolve(b);
233       }
234       return status;
235    }
236    
237    
238    public String toString() {
239       Pattern p;
240       Action a;
241 
242       String s = "(" + name + " ";
243       if ( salience != 0 )
244          s += salience + " ";
245       for(int i = 0; i < patterns.size(); i++ ) {
246          p = (Pattern)patterns.elementAt(i);
247          s += Misc.spaces(3) + p.toString();
248       }
249       s += " " + Misc.spaces(3) + IMPLIES + " ";
250       for(int i = 0; i < actions.size(); i++ ) {
251          a = (Action)actions.elementAt(i);
252          s += Misc.spaces(3) + a.toString();
253       }
254       s += ")";
255       return s;
256    }
257    
258    
259    public String pprint() {
260       return pprint(0);
261    }
262    
263    
264    public String pprint(int sp) {
265       Pattern p;
266       Action a;
267 
268       String s = Misc.spaces(sp) + "(" + name;
269       if ( salience != NORM_SALIENCE )
270          s += " " + salience;
271       for(int i = 0; i < patterns.size(); i++ ) {
272          p = (Pattern)patterns.elementAt(i);
273          s += "\n" + Misc.spaces(3+sp) + p.toString();
274       }
275       s += "\n" + Misc.spaces(3+sp) + IMPLIES;
276       for(int i = 0; i < actions.size(); i++ ) {
277          a = (Action)actions.elementAt(i);
278          s += "\n" + Misc.spaces(3+sp) + a.toString();
279       }
280       s += "\n" + Misc.spaces(sp) + ")";
281       return s;
282    }
283 }