1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: OntoText Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.choreography.editors;
26
27 import java.util.*;
28
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.*;
31 import org.eclipse.swt.layout.*;
32 import org.eclipse.swt.widgets.*;
33 import org.omwg.ontology.Variable;
34 import org.wsmo.factory.LogicalExpressionFactory;
35 import org.wsmo.service.choreography.rule.*;
36 import org.wsmo.service.rule.ChoreographyRule;
37 import org.wsmo.service.rule.Condition;
38 import org.wsmostudio.choreography.ChoreographyPlugin;
39 import org.wsmostudio.choreography.editors.model.ChoreographyModel;
40 import org.wsmostudio.runtime.WSMORuntime;
41 import org.wsmostudio.ui.Utils;
42
43 public class QuantRuleEditor extends RuleFormEditor {
44
45 private Text exprContainer;
46 private Text varsContainer;
47
48 private boolean dirty = false;
49
50
51 public QuantRuleEditor(ChoreographyRule rule, ChoreographyModel model) {
52 super(rule, model);
53 }
54
55 public Composite initUI(Composite parent) {
56 Composite mainContainer = new Composite(parent, SWT.BORDER);
57 mainContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
58 mainContainer.setLayout(new GridLayout(1, false));
59 Composite header = new Composite(mainContainer, SWT.NONE);
60 header.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
61 header.setLayout(new GridLayout(2, false));
62
63 new Label(header, SWT.NONE).setImage(RulesManagementPanel.iconForRule(rule));
64 Label lab = new Label(header, SWT.NONE);
65 lab.setText(labelForRule());
66 lab.setForeground(
67 parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
68 lab.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
69
70 new Label(mainContainer, SWT.NONE).setText("Variable(s) :");
71
72 varsContainer = new Text(mainContainer, SWT.BORDER | SWT.SINGLE);
73 varsContainer.setText(
74 RulesManagementPanel.varsAsString(((ChoreographyQuantifiedRule)rule).listVariables()));
75 varsContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
76 varsContainer.addModifyListener(new ModifyListener() {
77 public void modifyText(ModifyEvent me) {
78 uiModel.setChanged();
79 dirty = true;
80 }
81 });
82
83 new Label(mainContainer, SWT.NONE).setText("Condition :");
84
85 exprContainer = new Text(mainContainer,
86 SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
87 exprContainer.setText(
88 ((ChoreographyQuantifiedRule)rule).getCondition().getRestrictedLogicalExpression().toString(
89 Utils.findTopContainer(uiModel)));
90 exprContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
91 exprContainer.addModifyListener(new ModifyListener() {
92 public void modifyText(ModifyEvent me) {
93 uiModel.setChanged();
94 dirty = true;
95 }
96 });
97
98 return mainContainer;
99 }
100
101 public void updateRule() throws Exception {
102 if (dirty == false) {
103 return;
104 }
105 String newConditionText = exprContainer.getText().trim();
106 ChoreographyQuantifiedRule theRule = (ChoreographyQuantifiedRule)getRule();
107 boolean varsChange = updateVars(theRule);
108 if (theRule.getCondition() != null
109 && theRule.getCondition().toString().trim().equals(newConditionText)
110 && false == varsChange) {
111 return;
112 }
113
114 LogicalExpressionFactory leFactory = WSMORuntime.getRuntime().getLogExprFactory();
115 Condition condition =
116 ChoreographyPlugin.getFactory().transitionRules.createConditionFromLogicalExpression(
117 leFactory.createLogicalExpression(newConditionText,
118 Utils.findTopContainer(uiModel)));
119
120 theRule.setCondition(condition);
121 }
122
123 private boolean updateVars(ChoreographyQuantifiedRule rule) throws Exception {
124 Set<String> newVars = new HashSet<String>();
125 boolean modified = false;
126
127
128 StringTokenizer toker = new StringTokenizer(varsContainer.getText()," ,?");
129 while (toker.hasMoreTokens()) {
130 String varName = toker.nextToken();
131 for (int i = 0; i < varName.length(); i++ ) {
132 if (false == Character.isLetterOrDigit(varName.charAt(i))) {
133 throw new Exception("Invalid character '"
134 + varName.charAt(i)
135 + "' in variable '"
136 + varName
137 + "'!");
138 }
139 }
140 newVars.add(varName);
141 }
142
143 Set<Variable> oldOnes = new HashSet<Variable>(rule.listVariables());
144 for(Iterator<Variable> it = oldOnes.iterator(); it.hasNext();) {
145 Variable varObj = it.next();
146 if (newVars.contains(varObj.getName())) {
147 newVars.remove(varObj.getName());
148 }
149 else {
150 rule.removeVariable(varObj);
151 modified = true;
152 }
153 }
154
155 for(Iterator it = newVars.iterator(); it.hasNext();) {
156 Variable newVar = WSMORuntime.getRuntime().getLogExprFactory().createVariable((String)it.next());
157 rule.addVariable(newVar);
158 modified = true;
159 }
160 return modified;
161 }
162
163 @Override
164 public void dispose() {
165 varsContainer.dispose();
166 exprContainer.dispose();
167 }
168
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206