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  import zeus.util.*;
28  import zeus.concepts.fn.*;
29  import zeus.rete.Rule;
30  
31  /***
32       this is the definition of a kb in Zeus for the Rulebase tasks. 
33       @see zeus.rete.Rule
34       @see zeus.rete.ReteEngine
35       */
36  public class ReteKB extends AbstractTask {
37     protected Vector rules = new Vector();
38  
39     public ReteKB() {
40        type = BEHAVIOUR;
41     }
42  
43     public ReteKB(String name) {
44        type = BEHAVIOUR;
45        setName(name);
46     }
47     public ReteKB(String name, Vector rules) {
48        type = BEHAVIOUR;
49        setName(name);
50        setRules(rules);
51     }
52     public ReteKB(String name, Rule[] rules) {
53        type = BEHAVIOUR;
54        setName(name);
55        setRules(rules);
56     }
57     public ReteKB(ReteKB kb) {
58        type = BEHAVIOUR;
59        name = kb.getName();
60        setRules(kb.getRules());
61     }
62  
63     public Rule getRule(String rulename) {
64        Rule rule = null;
65        for(int j = 0; j < rules.size(); j++ ) {
66           rule = (Rule)rules.elementAt(j);
67           if ( rule.getName().equals(rulename) )
68              return rule;
69        }
70        return null;
71     }
72  
73     public Rule getRule(int position) {
74        return (Rule)rules.elementAt(position);
75     }
76  
77     public Rule[] getRules() {
78        Rule[] out = new Rule[rules.size()];
79        for(int j = 0; j < rules.size(); j++ )
80           out[j] = new Rule((Rule)rules.elementAt(j));
81        return out;
82     }
83  
84     public Rule removeRule(String rulename) {
85        Rule rule = null;
86        for(int j = 0; j < rules.size(); j++ ) {
87           rule = (Rule)rules.elementAt(j);
88           if ( rule.getName().equals(rulename) ) {
89              rules.removeElementAt(j--);
90              return rule;
91           }
92        }
93        return null;
94     }
95  
96     public Rule removeRule(int position) {
97        Rule rule = (Rule)rules.elementAt(position);
98        rules.removeElementAt(position);
99        return rule;
100    }
101 
102    public void addRule(Rule rule) {
103       rules.addElement(new Rule(rule));
104    }
105 
106    public void setRules(Vector List) {
107       rules.removeAllElements();
108       for(int i = 0; i < List.size(); i++ )
109          rules.addElement(new Rule((Rule)List.elementAt(i)));
110    }
111    public void setRules(Rule[] List) {
112       rules.removeAllElements();
113       for(int i = 0; i < List.length; i++ )
114          rules.addElement(new Rule(List[i]));
115    }
116 
117    public boolean resolve(Bindings bindings) {
118       Rule rule;
119       for(int i = 0; i < rules.size(); i++ ) {
120          rule = (Rule)rules.elementAt(i);
121          if ( !rule.resolve(bindings) )
122             return false;
123       }
124       return true;
125    }
126 
127    public boolean isValid() {
128       return true;
129    }
130 
131    public String toString() {
132       String s = "(:" + TaskTypes[type] + " " + name + " ";
133 
134       for(int i = 0; i < rules.size(); i++ )
135          s += rules.elementAt(i) + " ";
136       return s.trim() + ")";
137    }
138 
139    public String pprint(int sp) {
140       String suffix, prefix;
141       String tabs = Misc.spaces(sp);
142       String eol  = "\n" + tabs + " ";
143 
144       String s = "(:" + TaskTypes[type] + " " + name + "\n";
145       for(int i = 0; i < rules.size(); i++ )
146          s += ((Rule)rules.elementAt(i)).pprint(sp+3) + "\n";
147       return tabs + s + tabs + ")";
148    }
149 
150    public AbstractTask duplicate(DuplicationTable table) {
151       Rule[]  Xrules = new Rule[rules.size()];
152       for(int i = 0; i < rules.size(); i++ )
153          Xrules[i] = ((Rule)rules.elementAt(i)).duplicate(table);
154       return new ReteKB(name,Xrules);
155    }
156 }