]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
14f87e966c5bbb535a650126c394ea38df230d1e
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2014 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.jface.text.IUndoManager;\r
23 import org.eclipse.jface.text.TextViewerUndoManager;\r
24 import org.eclipse.swt.SWT;\r
25 import org.eclipse.swt.custom.VerifyKeyListener;\r
26 import org.eclipse.swt.events.FocusAdapter;\r
27 import org.eclipse.swt.events.FocusEvent;\r
28 import org.eclipse.swt.events.FocusListener;\r
29 import org.eclipse.swt.events.KeyListener;\r
30 import org.eclipse.swt.events.ModifyListener;\r
31 import org.eclipse.swt.graphics.Point;\r
32 import org.eclipse.swt.widgets.Button;\r
33 import org.eclipse.swt.widgets.Composite;\r
34 import org.eclipse.swt.widgets.Event;\r
35 import org.eclipse.swt.widgets.Label;\r
36 import org.eclipse.swt.widgets.Listener;\r
37 import org.eclipse.swt.widgets.Table;\r
38 import org.simantics.Simantics;\r
39 import org.simantics.databoard.Bindings;\r
40 import org.simantics.db.ReadGraph;\r
41 import org.simantics.db.Resource;\r
42 import org.simantics.db.WriteGraph;\r
43 import org.simantics.db.common.CommentMetadata;\r
44 import org.simantics.db.common.request.ReadRequest;\r
45 import org.simantics.db.common.request.WriteRequest;\r
46 import org.simantics.db.common.utils.ListUtils;\r
47 import org.simantics.db.exception.DatabaseException;\r
48 import org.simantics.db.layer0.util.Layer0Utils;\r
49 import org.simantics.db.layer0.util.RemoverUtil;\r
50 import org.simantics.db.request.Read;\r
51 import org.simantics.layer0.Layer0;\r
52 import org.simantics.sysdyn.SysdynResource;\r
53 import org.simantics.sysdyn.manager.SysdynModel;\r
54 import org.simantics.sysdyn.manager.SysdynModelManager;\r
55 import org.simantics.sysdyn.representation.Stock;\r
56 import org.simantics.sysdyn.representation.Valve;\r
57 import org.simantics.utils.datastructures.Pair;\r
58 \r
59 public class StockExpression implements IExpression {\r
60 \r
61     private final ExpressionWidgetInput input;\r
62     private ExpressionField integralExpression, initialExpression;\r
63     private ExpressionField lastSelectedText;\r
64     private TextViewerUndoManager undoManager;\r
65     \r
66     public StockExpression(ExpressionWidgetInput input) {\r
67         this.input = input;\r
68     }\r
69     \r
70     @Override\r
71     public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {\r
72         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
73         String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";\r
74         String integralEquation = data.get("integral") != null ? (String)data.get("integral") : getDefaultIntegral(input.expression);\r
75 \r
76 \r
77         Label label = new Label(parent, SWT.NONE);\r
78         label.setText("Integral");\r
79 \r
80         Composite integralComposite = new Composite(parent, SWT.NONE);\r
81         GridDataFactory.fillDefaults().grab(true, true).applyTo(integralComposite);\r
82         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(integralComposite);\r
83         \r
84         integralExpression = new ExpressionField(integralComposite, SWT.BORDER, allowedVariables, true, input);\r
85         integralExpression.setExpression(integralEquation);\r
86         GridDataFactory.fillDefaults().grab(true, true).applyTo(integralExpression);\r
87         integralExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
88 \r
89             @Override\r
90             public void focusLost(FocusEvent e) {\r
91                 lastSelectedText = integralExpression;\r
92             }\r
93         });\r
94         \r
95         undoManager = new TextViewerUndoManager(100);\r
96         integralExpression.getSourceViewer().setUndoManager(undoManager);\r
97         undoManager.connect(integralExpression.getSourceViewer());\r
98         \r
99         Button defaultButton = new Button(integralComposite, SWT.PUSH);\r
100         defaultButton.setText("Use default");\r
101         defaultButton.addListener(SWT.Selection, new Listener() {\r
102 \r
103                 @Override\r
104                 public void handleEvent(Event event) {\r
105                         switch (event.type) {\r
106                         case SWT.Selection:\r
107                                 StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression));\r
108                                 Simantics.getSession().asyncRequest(new WriteRequest() {\r
109 \r
110                                         @Override\r
111                                         public void perform(WriteGraph graph)\r
112                                                         throws DatabaseException {\r
113                                                 // Delete the possible integral expression from the database to note \r
114                                                 // that we are in the "default" mode.\r
115                                                 graph.markUndoPoint();\r
116                                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
117                                                 graph.deny(input.expression, sr.StockExpression_useCustomIntegral);\r
118                                                 graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));\r
119                                                 \r
120                                         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
121                                         graph.addMetadata(cm.add("Use default"));\r
122                                                 \r
123                                         }\r
124                                 });\r
125                                 break;\r
126                         }\r
127                 }\r
128 \r
129         });\r
130 \r
131         label = new Label(parent, SWT.NONE);\r
132         label.setText("Initial\nValue");\r
133 \r
134         initialExpression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true, input);\r
135         initialExpression.setExpression(initialEquation);\r
136         GridDataFactory.fillDefaults().grab(true, true).applyTo(initialExpression);\r
137         initialExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
138 \r
139             @Override\r
140             public void focusLost(FocusEvent e) {\r
141                 lastSelectedText = initialExpression;\r
142             }\r
143         });\r
144 \r
145         lastSelectedText = initialExpression;\r
146     }\r
147 \r
148     @Override\r
149     public void focus() {\r
150         this.lastSelectedText.focus();\r
151     }\r
152 \r
153     @Override\r
154     public List<ExpressionField> getExpressionFields() {\r
155         return Arrays.asList(this.integralExpression, this.initialExpression);\r
156     }\r
157 \r
158     @Override\r
159     public void readData(final Resource expression, final Map<String, Object> data) {\r
160         Pair<String, String> equations = null;\r
161         if (expression != null && data.get("initialEquation") == null) {\r
162                 try {\r
163                     equations = Simantics.getSession().syncRequest(new Read<Pair<String, String>>() {\r
164         \r
165                         @Override\r
166                         public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {\r
167                             SysdynResource sr = SysdynResource.getInstance(graph);\r
168                             if (graph.isInstanceOf(expression, sr.StockExpression)) {\r
169                                 String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);\r
170                                 String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation);\r
171                                 initialEquation = (initialEquation != null) ? initialEquation : "";\r
172                                 return new Pair<String, String>(integralEquation, initialEquation);\r
173                             } else {\r
174                                 return new Pair<String, String>(null, "");\r
175                             }\r
176                         }\r
177         \r
178                     });\r
179                 } catch (DatabaseException e1) {\r
180                     e1.printStackTrace();\r
181                 }\r
182                 data.put("initialEquation", equations.second);\r
183         }\r
184 \r
185         if (equations.first == null) {\r
186                 try {\r
187                                 Simantics.getSession().syncRequest(new WriteRequest() {\r
188 \r
189                                         @Override\r
190                                         public void perform(WriteGraph graph) throws DatabaseException {\r
191                                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
192                                         graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));\r
193                                         graph.markUndoPoint();\r
194                                 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
195                                 graph.addMetadata(cm.add("Set expression"));\r
196                                         }\r
197 \r
198                                 });\r
199                         } catch (DatabaseException e) {\r
200                                 // TODO Auto-generated catch block\r
201                                 e.printStackTrace();\r
202                         }\r
203                 \r
204                 data.put("integral", getDefaultIntegral(expression));\r
205         } else {\r
206                 try {\r
207                         final String integral = equations.first;\r
208                                 Simantics.getSession().syncRequest(new ReadRequest() {\r
209 \r
210                                         @Override\r
211                                         public void run(ReadGraph graph) throws DatabaseException {\r
212                                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
213                                         if (graph.hasStatement(input.expression, sr.StockExpression_useCustomIntegral)) {\r
214                                                 data.put("integral", integral);\r
215                                         } else {\r
216                                                 data.put("integral", getDefaultIntegral(graph, expression));\r
217                                         }\r
218                                         }\r
219 \r
220                                 });\r
221                         } catch (DatabaseException e) {\r
222                                 data.put("integral", equations.first);\r
223                                 e.printStackTrace();\r
224                         }\r
225         }\r
226 \r
227     }\r
228 \r
229     @Override\r
230     public void replaceSelection(String var) {\r
231         if(lastSelectedText != null) {\r
232             IDocument doc = lastSelectedText.getDocument();\r
233             try {\r
234                 Point selection = lastSelectedText.getSelection();\r
235                 doc.replace(selection.x, selection.y, var);\r
236                 lastSelectedText.setSelection(selection.x + var.length());\r
237             } catch (BadLocationException e) {\r
238                 e.printStackTrace();\r
239             }\r
240         }\r
241     }\r
242 \r
243     @Override\r
244     public void save(final Resource expression, final Map<String, Object> data) {\r
245         final String currentInitial = this.initialExpression.getExpression();\r
246         final String currentIntegral = this.integralExpression.getExpression();\r
247         if (currentIntegral == null) {\r
248                 this.integralExpression.setExpression(getDefaultIntegral(expression));\r
249         }\r
250         if(!currentInitial.equals(data.get("initialEquation"))\r
251                         || currentIntegral == null\r
252                         || !currentIntegral.equals(data.get("integral"))) {\r
253             Simantics.getSession().asyncRequest(new WriteRequest() {\r
254 \r
255                 @Override\r
256                 public void perform(WriteGraph g)\r
257                 throws DatabaseException {\r
258                     SysdynResource sr = SysdynResource.getInstance(g);\r
259                     Layer0 l0 = Layer0.getInstance(g);\r
260                     \r
261                     if(!g.isInstanceOf(expression, sr.StockExpression)) {\r
262                         Resource variable = g.getPossibleObject(expression, l0.PartOf);\r
263                         Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList);\r
264                         Resource temp = g.newResource();\r
265                         ListUtils.replace(g, expressionList, expression, temp);\r
266                         \r
267                         for(Resource predicate : g.getPredicates(expression)) {\r
268                             g.deny(expression, predicate);\r
269                         }\r
270                         g.claim(expression, l0.InstanceOf, null, sr.StockExpression);\r
271 \r
272                         ListUtils.replace(g, expressionList, temp, expression);\r
273 \r
274                         RemoverUtil.remove(g, temp);\r
275 \r
276                         g.claim(expression, l0.PartOf, variable);\r
277                     }\r
278                     g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial);\r
279                     \r
280                     if (!currentIntegral.equals(g.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation, Bindings.STRING))) {\r
281                         // If the value is not same as default, enable the custom tag\r
282                         g.claim(expression, sr.StockExpression_useCustomIntegral, expression);\r
283                     }\r
284                     g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);\r
285                     \r
286                     updateData(data);\r
287                     \r
288                     g.markUndoPoint();\r
289                     \r
290                     Layer0Utils.addCommentMetadata(g, "Saved Stock Expression " + currentIntegral + " " + expression.toString() + " with Initial Value " + currentInitial);\r
291                 }\r
292 \r
293             });\r
294         }\r
295         this.initialExpression.setExpression(currentInitial);\r
296         this.integralExpression.setExpression(currentIntegral);\r
297     }\r
298 \r
299     @Override\r
300     public void updateData(Map<String, Object> data) {\r
301         if(this.initialExpression != null && this.initialExpression.getExpression() != null)\r
302             data.put("initialEquation", this.initialExpression.getExpression());\r
303         if(this.integralExpression != null && this.integralExpression.getExpression() != null)\r
304             data.put("integral", this.integralExpression.getExpression());\r
305     }\r
306 \r
307     \r
308     private static String getDefaultIntegral(ReadGraph graph, Resource expression) throws DatabaseException {\r
309         SysdynResource sr = SysdynResource.getInstance(graph);\r
310         Layer0 l0 = Layer0.getInstance(graph);\r
311     \r
312             // find the variable\r
313             Resource variable = graph.getPossibleObject(expression, l0.PartOf);\r
314             if(variable == null)\r
315                 return "";\r
316             \r
317                 SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());\r
318                 SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));\r
319                 model.update(graph);\r
320                 \r
321                 Stock stock = (Stock)model.getElement(variable);\r
322             \r
323                 String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);\r
324                 if(range == null)\r
325                         range = "";\r
326         \r
327             StringBuilder builder = new StringBuilder();\r
328             builder.append("");\r
329             for(Valve in : stock.getIncomingValves()) {\r
330                 builder.append(" + " + in.getName() + range);\r
331             }\r
332             for(Valve out : stock.getOutgoingValves()) {\r
333                 builder.append(" - " + out.getName() + range);\r
334             }\r
335             if (builder.indexOf(" + ") == 0)\r
336                 builder.delete(0, 3);\r
337             \r
338             return builder.toString().trim();   \r
339     }\r
340     \r
341     \r
342     private static String getDefaultIntegral(final Resource expression) {\r
343         \r
344         String integral = "";\r
345         if(expression == null)\r
346                 return integral;\r
347         try {\r
348             integral = Simantics.getSession().syncRequest(new Read<String>() {\r
349 \r
350                 @Override\r
351                 public String perform(ReadGraph graph) throws DatabaseException {\r
352                         return getDefaultIntegral(graph, expression);\r
353                 }\r
354 \r
355             });\r
356         } catch (DatabaseException e) {\r
357             e.printStackTrace();\r
358         }\r
359         return integral;\r
360     }\r
361 \r
362     @Override\r
363     public void addKeyListener(KeyListener listener) {\r
364         this.initialExpression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
365         this.integralExpression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
366 \r
367     }\r
368 \r
369     @Override\r
370     public void addModifyListener(ModifyListener listener) {\r
371         this.initialExpression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
372         this.integralExpression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
373 \r
374     }\r
375 \r
376     @Override\r
377     public void addFocusListener(FocusListener listener) {\r
378         this.initialExpression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
379         this.integralExpression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
380     }\r
381     \r
382         @Override\r
383         public void addVerifyKeyListener(VerifyKeyListener listener) {\r
384                 this.initialExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
385                 this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
386         }\r
387 \r
388     @Override\r
389     public IUndoManager getUndoManager() {\r
390         return undoManager;\r
391     }\r
392 \r
393 }\r