View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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; // no update is needed
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; // no change
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         // parsing the input string
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         // comparing with the rule's variables
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         // adding the new vars:
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  * $Log$
173  * Revision 1.12  2006/11/22 14:11:06  alex_simov
174  * Code refactoring: the plug-in is synchronized with the latest wsmo4j changes
175  *
176  * Revision 1.11  2006/06/22 13:21:06  vassil_momtchev
177  * method WsmoFactory.createVariable(String) moved to LogicalExpressionFactory.createVariable(String)
178  *
179  * Revision 1.10  2006/04/18 07:14:37  alex_simov
180  * Adapting to the recent choreography-api changes
181  *
182  * Revision 1.9  2006/02/17 09:49:12  alex_simov
183  * new variables input string not parsed precisely enough
184  *
185  * Revision 1.8  2006/02/16 13:41:27  alex_simov
186  * bugfix [1426200]: namespace definitions were not used in choreography rule
187  *  editors
188  *
189  * Revision 1.7  2006/02/14 15:07:52  alex_simov
190  * bugfix[1431322]: quantified variables lost during rule updating
191  *
192  * Revision 1.6  2006/01/09 12:51:11  alex_simov
193  * Copyright message in header updated
194  *
195  * Revision 1.5  2005/12/21 15:25:35  alex_simov
196  * choreography managment implementation completed
197  *
198  * Revision 1.4  2005/12/19 15:35:54  alex_simov
199  * update
200  *
201  * Revision 1.3  2005/11/24 13:48:16  alex_simov
202  * logical expression field contains now wrapped text
203  * (instead of horizontal scroll bar)
204  * [bug issue 1365441]
205  *
206  */