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.generator.task.rulebase;
25
26 import java.util.*;
27 import javax.swing.*;
28 import javax.swing.text.*;
29 import javax.swing.event.*;
30 import java.awt.Color;
31
32 public class Rule implements DocumentListener {
33
34 String name;
35 int priority;
36 PlainDocument lhs;
37 PlainDocument rhs;
38 protected EventListenerList changeListeners = new EventListenerList();
39 //------------------------------------------------------------------------------
40 public Rule(String name, int priority) {
41 this.name = name;
42 this.priority = priority;
43 lhs = new PlainDocument();
44 lhs.addDocumentListener(this);
45 rhs = new PlainDocument();
46 rhs.addDocumentListener(this);
47 }
48 //--------------------------------------------------------------------------
49 public String getName() {
50 return name;
51 }
52 //--------------------------------------------------------------------------
53 public int getPriority() {
54 return priority;
55 }
56 //--------------------------------------------------------------------------
57 public PlainDocument getLHS() {
58 return lhs;
59 }
60 //--------------------------------------------------------------------------
61 public PlainDocument getRHS() {
62 return rhs;
63 }
64 //--------------------------------------------------------------------------
65 public void setName(String name) {
66 this.name = name;
67 }
68 //--------------------------------------------------------------------------
69 public void setPriority(int priority) {
70 this.priority = priority;
71 }
72 //--------------------------------------------------------------------------
73 public String getCondition() {
74 try {
75 return lhs.getText(0,lhs.getLength());
76 }
77 catch (BadLocationException e) {
78 // e.printStackTrace();
79 }
80 return null;
81 }
82 //--------------------------------------------------------------------------
83 public String getConclusion() {
84 try {
85 return rhs.getText(0,rhs.getLength());
86 }
87 catch (BadLocationException e) {
88 // e.printStackTrace();
89 }
90 return null;
91 }
92 //--------------------------------------------------------------------------
93 public void setPatterns(Vector patterns) {
94 zeus.rete.Pattern pattern;
95 int pos = lhs.getStartPosition().getOffset();
96
97 for(int i = 0; i < patterns.size(); i++ ) {
98 pattern = (zeus.rete.Pattern) patterns.elementAt(i);
99 try {
100 lhs.insertString(pos,pattern.toString(),null);
101 }
102 catch (BadLocationException e) {
103 // System.out.println("Error (VVV" + pos +")"+ pattern );
104 //e.printStackTrace();
105 }
106 pos = lhs.getLength();
107
108 try {
109 lhs.insertString(pos,"\n",null);
110 }
111 catch (BadLocationException e) {
112 // System.out.println("Error (VVV"+ pos + ")"+ pattern);
113 //e.printStackTrace();
114 }
115 pos = lhs.getLength();
116 }
117 }
118 //--------------------------------------------------------------------------
119 public void setActions(Vector actions) {
120 zeus.rete.Action action;
121 int pos = rhs.getStartPosition().getOffset();
122
123 for(int i = 0; i < actions.size(); i++ ) {
124 action = (zeus.rete.Action) actions.elementAt(i);
125 try {
126 rhs.insertString(pos,action.toString(),null);
127 }
128 catch (BadLocationException e) {
129 // System.out.println("Error (XXX"+ pos + ")"+ action);
130 //e.printStackTrace();
131 }
132
133 pos = rhs.getLength();
134
135 try {
136 rhs.insertString(pos,"\n",null);
137 }
138 catch (BadLocationException e) {
139 // System.out.println("Error (XXX"+ pos + ")"+ action);
140 //e.printStackTrace();
141 }
142 pos = rhs.getLength();
143 }
144 }
145 //--------------------------------------------------------------------------
146 String getPatterns() {
147 try {
148 return lhs.getText(0, lhs.getLength());
149 }
150 catch(BadLocationException e) {
151 // e.printStackTrace();
152 }
153 return "";
154 }
155 //--------------------------------------------------------------------------
156 String getActions() {
157 try {
158 return rhs.getText(0, rhs.getLength());
159 }
160 catch(BadLocationException e) {
161 // e.printStackTrace();
162 }
163 return "";
164 }
165 //------------------------------------------------------------------------------
166 public void insertUpdate(DocumentEvent e) {
167 fireChanged();
168 }
169 //------------------------------------------------------------------------------
170 public void removeUpdate(DocumentEvent e) {
171 fireChanged();
172 }
173 //------------------------------------------------------------------------------
174 public void changedUpdate(DocumentEvent e) {
175 fireChanged();
176 }
177 //------------------------------------------------------------------------------
178 public void addChangeListener(ChangeListener x) {
179 changeListeners.add(ChangeListener.class, x);
180 }
181 //------------------------------------------------------------------------------
182 public void removeChangeListener(ChangeListener x) {
183 changeListeners.remove(ChangeListener.class, x);
184 }
185 //------------------------------------------------------------------------------
186 protected void fireChanged() {
187 ChangeEvent c = new ChangeEvent(this);
188 Object[] listeners = changeListeners.getListenerList();
189 for(int i= listeners.length-2; i >= 0; i -=2) {
190 if (listeners[i] == ChangeListener.class) {
191 ChangeListener cl = (ChangeListener)listeners[i+1];
192 cl.stateChanged(c);
193 }
194 }
195 }
196 //------------------------------------------------------------------------------
197
198 }