]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
4ef1734f087b6882b2a898197f847df0f82d587e
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.properties.widgets.expressions;\r
13 \r
14 import java.util.Arrays;\r
15 import java.util.List;\r
16 import java.util.Map;\r
17 \r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.jface.layout.GridLayoutFactory;\r
20 import org.eclipse.jface.text.BadLocationException;\r
21 import org.eclipse.jface.text.IDocument;\r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.custom.VerifyKeyListener;\r
24 import org.eclipse.swt.events.FocusAdapter;\r
25 import org.eclipse.swt.events.FocusEvent;\r
26 import org.eclipse.swt.events.FocusListener;\r
27 import org.eclipse.swt.events.KeyListener;\r
28 import org.eclipse.swt.events.ModifyListener;\r
29 import org.eclipse.swt.graphics.Point;\r
30 import org.eclipse.swt.widgets.Button;\r
31 import org.eclipse.swt.widgets.Composite;\r
32 import org.eclipse.swt.widgets.Event;\r
33 import org.eclipse.swt.widgets.Label;\r
34 import org.eclipse.swt.widgets.Listener;\r
35 import org.eclipse.swt.widgets.Table;\r
36 import org.simantics.db.ReadGraph;\r
37 import org.simantics.db.Resource;\r
38 import org.simantics.db.WriteGraph;\r
39 import org.simantics.db.common.request.WriteRequest;\r
40 import org.simantics.db.common.utils.ListUtils;\r
41 import org.simantics.db.exception.DatabaseException;\r
42 import org.simantics.db.layer0.util.RemoverUtil;\r
43 import org.simantics.db.request.Read;\r
44 import org.simantics.layer0.Layer0;\r
45 import org.simantics.sysdyn.SysdynResource;\r
46 import org.simantics.sysdyn.manager.SysdynModel;\r
47 import org.simantics.sysdyn.manager.SysdynModelManager;\r
48 import org.simantics.sysdyn.representation.Stock;\r
49 import org.simantics.sysdyn.representation.Valve;\r
50 import org.simantics.ui.SimanticsUI;\r
51 import org.simantics.utils.datastructures.Pair;\r
52 \r
53 public class StockExpression implements IExpression {\r
54 \r
55     private final ExpressionWidgetInput input;\r
56     private ExpressionField integralExpression, initialExpression;\r
57     private ExpressionField lastSelectedText;\r
58     \r
59     public StockExpression(ExpressionWidgetInput input) {\r
60         this.input = input;\r
61     }\r
62     \r
63     @Override\r
64     public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {\r
65         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
66         String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";\r
67         String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";\r
68 \r
69 \r
70         Label label = new Label(parent, SWT.NONE);\r
71         label.setText("Integral");\r
72 \r
73         Composite integralComposite = new Composite(parent, SWT.NONE);\r
74         GridDataFactory.fillDefaults().grab(true, true).applyTo(integralComposite);\r
75         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(integralComposite);\r
76         \r
77         integralExpression = new ExpressionField(integralComposite, SWT.BORDER, allowedVariables, true, input);\r
78         integralExpression.setExpression(integralEquation);\r
79         GridDataFactory.fillDefaults().grab(true, true).applyTo(integralExpression);\r
80         integralExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
81 \r
82             @Override\r
83             public void focusLost(FocusEvent e) {\r
84                 lastSelectedText = integralExpression;\r
85             }\r
86         });\r
87         \r
88         Button defaultButton = new Button(integralComposite, SWT.PUSH);\r
89         defaultButton.setText("Use default");\r
90         defaultButton.addListener(SWT.Selection, new Listener() {\r
91 \r
92                 @Override\r
93                 public void handleEvent(Event event) {\r
94                         switch (event.type) {\r
95                         case SWT.Selection:\r
96                                 StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression));\r
97                                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
98 \r
99                                         @Override\r
100                                         public void perform(WriteGraph graph)\r
101                                                         throws DatabaseException {\r
102                                                 // Delete the possible integral expression from the database to note \r
103                                                 // that we are in the "default" mode.\r
104                                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
105                                                 graph.deny(input.expression, sr.StockExpression_integralEquation);\r
106                                         }\r
107                                 });\r
108                                 break;\r
109                         }\r
110                 }\r
111 \r
112         });\r
113 \r
114         label = new Label(parent, SWT.NONE);\r
115         label.setText("Initial\nValue");\r
116 \r
117         initialExpression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true, input);\r
118         initialExpression.setExpression(initialEquation);\r
119         GridDataFactory.fillDefaults().grab(true, true).applyTo(initialExpression);\r
120         initialExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
121 \r
122             @Override\r
123             public void focusLost(FocusEvent e) {\r
124                 lastSelectedText = initialExpression;\r
125             }\r
126         });\r
127 \r
128         lastSelectedText = initialExpression;\r
129     }\r
130 \r
131     @Override\r
132     public void focus() {\r
133         this.lastSelectedText.focus();\r
134     }\r
135 \r
136     @Override\r
137     public List<ExpressionField> getExpressionFields() {\r
138         return Arrays.asList(this.integralExpression, this.initialExpression);\r
139     }\r
140 \r
141     @Override\r
142     public void readData(final Resource expression, Map<String, Object> data) {\r
143         Pair<String, String> equations = null;\r
144         if (expression != null && data.get("initialEquation") == null) {\r
145                 try {\r
146                     equations = SimanticsUI.getSession().syncRequest(new Read<Pair<String, String>>() {\r
147         \r
148                         @Override\r
149                         public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {\r
150                             SysdynResource sr = SysdynResource.getInstance(graph);\r
151                             if (graph.isInstanceOf(expression, sr.StockExpression)) {\r
152                                 String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);\r
153                                 String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation);\r
154                                 initialEquation = (initialEquation != null) ? initialEquation : "";\r
155                                 return new Pair<String, String>(integralEquation, initialEquation);\r
156                             } else {\r
157                                 return new Pair<String, String>(null, "");\r
158                             }\r
159                         }\r
160         \r
161                     });\r
162                 } catch (DatabaseException e1) {\r
163                     e1.printStackTrace();\r
164                 }\r
165                 data.put("initialEquation", equations.second);\r
166         }\r
167 \r
168         if (equations.first == null) {\r
169                 data.put("integral", getDefaultIntegral(expression));\r
170         } else {\r
171                 data.put("integral", equations.first);\r
172         }\r
173 \r
174     }\r
175 \r
176     @Override\r
177     public void replaceSelection(String var) {\r
178         if(lastSelectedText != null) {\r
179             IDocument doc = lastSelectedText.getDocument();\r
180             try {\r
181                 Point selection = lastSelectedText.getSelection();\r
182                 doc.replace(selection.x, selection.y, var);\r
183                 lastSelectedText.setSelection(selection.x + var.length());\r
184             } catch (BadLocationException e) {\r
185                 e.printStackTrace();\r
186             }\r
187         }\r
188     }\r
189 \r
190     @Override\r
191     public void save(final Resource expression, final Map<String, Object> data) {\r
192         final String currentInitial = this.initialExpression.getExpression();\r
193         final String currentIntegral = this.integralExpression.getExpression();\r
194         if(currentInitial == null \r
195                         || !currentInitial.equals(data.get("initialEquation"))\r
196                         || (currentIntegral != null\r
197                                 && !currentIntegral.equals(data.get("integral")))) {\r
198             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
199 \r
200                 @Override\r
201                 public void perform(WriteGraph g)\r
202                 throws DatabaseException {\r
203                     SysdynResource sr = SysdynResource.getInstance(g);\r
204                     Layer0 l0 = Layer0.getInstance(g);\r
205                     \r
206                     if(!g.isInstanceOf(expression, sr.StockExpression)) {\r
207                         Resource variable = g.getPossibleObject(expression, l0.PartOf);\r
208                         Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList);\r
209                         Resource temp = g.newResource();\r
210                         ListUtils.replace(g, expressionList, expression, temp);\r
211                         \r
212                         for(Resource predicate : g.getPredicates(expression)) {\r
213                             g.deny(expression, predicate);\r
214                         }\r
215                         g.claim(expression, l0.InstanceOf, null, sr.StockExpression);\r
216 \r
217                         ListUtils.replace(g, expressionList, temp, expression);\r
218 \r
219                         RemoverUtil.remove(g, temp);\r
220 \r
221                         g.claim(expression, l0.PartOf, variable);\r
222                     }\r
223                     g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial);\r
224                     \r
225                     // If the value is same as default, do not save to database.\r
226                     if (currentIntegral != null && !currentIntegral.equals(data.get("integral"))) {\r
227                         g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);\r
228                     }\r
229                 }\r
230 \r
231             });\r
232         }\r
233         this.initialExpression.setExpression(currentInitial);\r
234         if (currentIntegral != null)\r
235                 this.integralExpression.setExpression(currentIntegral);\r
236     }\r
237 \r
238     @Override\r
239     public void updateData(Map<String, Object> data) {\r
240         if(this.initialExpression != null && this.initialExpression.getExpression() != null)\r
241             data.put("initialEquation", this.initialExpression.getExpression());\r
242         if(this.integralExpression != null && this.integralExpression.getExpression() != null)\r
243             data.put("integral", this.integralExpression.getExpression());\r
244     }\r
245 \r
246     private String getDefaultIntegral(final Resource expression) {\r
247         \r
248         String integral = "";\r
249         if(expression == null)\r
250                 return integral;\r
251         try {\r
252             integral = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
253 \r
254                 @Override\r
255                 public String perform(ReadGraph graph) throws DatabaseException {\r
256                         SysdynResource sr = SysdynResource.getInstance(graph);\r
257                     Layer0 l0 = Layer0.getInstance(graph);\r
258                     \r
259                     // find the variable\r
260                     Resource variable = graph.getPossibleObject(expression, l0.PartOf);\r
261                     if(variable == null)\r
262                         return "";\r
263                     \r
264                         SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());\r
265                         SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));\r
266                         model.update(graph);\r
267                         \r
268                         Stock stock = (Stock)model.getElement(variable);\r
269                     \r
270                         String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);\r
271                         if(range == null)\r
272                                 range = "";\r
273                 \r
274                     StringBuilder builder = new StringBuilder();\r
275                     builder.append("");\r
276                     for(Valve in : stock.getIncomingValves()) {\r
277                         builder.append(" + " + in.getName() + range);\r
278                     }\r
279                     for(Valve out : stock.getOutgoingValves()) {\r
280                         builder.append(" - " + out.getName() + range);\r
281                     }\r
282                     if (builder.indexOf(" + ") == 0)\r
283                         builder.delete(0, 3);\r
284                     \r
285                     return builder.toString().trim();\r
286                 }\r
287 \r
288             });\r
289         } catch (DatabaseException e) {\r
290             e.printStackTrace();\r
291         }\r
292         return integral;\r
293     }\r
294 \r
295     @Override\r
296     public void addKeyListener(KeyListener listener) {\r
297         this.initialExpression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
298         this.integralExpression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
299 \r
300     }\r
301 \r
302     @Override\r
303     public void addModifyListener(ModifyListener listener) {\r
304         this.initialExpression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
305         this.integralExpression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
306 \r
307     }\r
308 \r
309     @Override\r
310     public void addFocusListener(FocusListener listener) {\r
311         this.initialExpression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
312         this.integralExpression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
313     }\r
314     \r
315         @Override\r
316         public void addVerifyKeyListener(VerifyKeyListener listener) {\r
317                 this.initialExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
318                 this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
319         }\r
320 \r
321 }\r