]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
53e51b9114703d7b892d474829a5ecfd63a48b50
[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.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") : getDefaultIntegral(input.expression);\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_useCustomIntegral);\r
106                                                 graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));\r
107                                         }\r
108                                 });\r
109                                 break;\r
110                         }\r
111                 }\r
112 \r
113         });\r
114 \r
115         label = new Label(parent, SWT.NONE);\r
116         label.setText("Initial\nValue");\r
117 \r
118         initialExpression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true, input);\r
119         initialExpression.setExpression(initialEquation);\r
120         GridDataFactory.fillDefaults().grab(true, true).applyTo(initialExpression);\r
121         initialExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
122 \r
123             @Override\r
124             public void focusLost(FocusEvent e) {\r
125                 lastSelectedText = initialExpression;\r
126             }\r
127         });\r
128 \r
129         lastSelectedText = initialExpression;\r
130     }\r
131 \r
132     @Override\r
133     public void focus() {\r
134         this.lastSelectedText.focus();\r
135     }\r
136 \r
137     @Override\r
138     public List<ExpressionField> getExpressionFields() {\r
139         return Arrays.asList(this.integralExpression, this.initialExpression);\r
140     }\r
141 \r
142     @Override\r
143     public void readData(final Resource expression, Map<String, Object> data) {\r
144         Pair<String, String> equations = null;\r
145         if (expression != null && data.get("initialEquation") == null) {\r
146                 try {\r
147                     equations = SimanticsUI.getSession().syncRequest(new Read<Pair<String, String>>() {\r
148         \r
149                         @Override\r
150                         public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {\r
151                             SysdynResource sr = SysdynResource.getInstance(graph);\r
152                             if (graph.isInstanceOf(expression, sr.StockExpression)) {\r
153                                 String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);\r
154                                 String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation);\r
155                                 initialEquation = (initialEquation != null) ? initialEquation : "";\r
156                                 return new Pair<String, String>(integralEquation, initialEquation);\r
157                             } else {\r
158                                 return new Pair<String, String>(null, "");\r
159                             }\r
160                         }\r
161         \r
162                     });\r
163                 } catch (DatabaseException e1) {\r
164                     e1.printStackTrace();\r
165                 }\r
166                 data.put("initialEquation", equations.second);\r
167         }\r
168 \r
169         if (equations.first == null) {\r
170                 try {\r
171                                 SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
172 \r
173                                         @Override\r
174                                         public void perform(WriteGraph graph) throws DatabaseException {\r
175                                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
176                                         graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));\r
177                                         }\r
178 \r
179                                 });\r
180                         } catch (DatabaseException e) {\r
181                                 // TODO Auto-generated catch block\r
182                                 e.printStackTrace();\r
183                         }\r
184                 \r
185                 data.put("integral", getDefaultIntegral(expression));\r
186         } else {\r
187                 data.put("integral", equations.first);\r
188         }\r
189 \r
190     }\r
191 \r
192     @Override\r
193     public void replaceSelection(String var) {\r
194         if(lastSelectedText != null) {\r
195             IDocument doc = lastSelectedText.getDocument();\r
196             try {\r
197                 Point selection = lastSelectedText.getSelection();\r
198                 doc.replace(selection.x, selection.y, var);\r
199                 lastSelectedText.setSelection(selection.x + var.length());\r
200             } catch (BadLocationException e) {\r
201                 e.printStackTrace();\r
202             }\r
203         }\r
204     }\r
205 \r
206     @Override\r
207     public void save(final Resource expression, final Map<String, Object> data) {\r
208         final String currentInitial = this.initialExpression.getExpression();\r
209         final String currentIntegral = this.integralExpression.getExpression();\r
210         if (currentIntegral == null) {\r
211                 this.integralExpression.setExpression(getDefaultIntegral(expression));\r
212         }\r
213         if(!currentInitial.equals(data.get("initialEquation"))\r
214                         || currentIntegral == null\r
215                         || !currentIntegral.equals(data.get("integral"))) {\r
216             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
217 \r
218                 @Override\r
219                 public void perform(WriteGraph g)\r
220                 throws DatabaseException {\r
221                     SysdynResource sr = SysdynResource.getInstance(g);\r
222                     Layer0 l0 = Layer0.getInstance(g);\r
223                     \r
224                     if(!g.isInstanceOf(expression, sr.StockExpression)) {\r
225                         Resource variable = g.getPossibleObject(expression, l0.PartOf);\r
226                         Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList);\r
227                         Resource temp = g.newResource();\r
228                         ListUtils.replace(g, expressionList, expression, temp);\r
229                         \r
230                         for(Resource predicate : g.getPredicates(expression)) {\r
231                             g.deny(expression, predicate);\r
232                         }\r
233                         g.claim(expression, l0.InstanceOf, null, sr.StockExpression);\r
234 \r
235                         ListUtils.replace(g, expressionList, temp, expression);\r
236 \r
237                         RemoverUtil.remove(g, temp);\r
238 \r
239                         g.claim(expression, l0.PartOf, variable);\r
240                     }\r
241                     g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial);\r
242                     \r
243                     if (!currentIntegral.equals(data.get("integral"))) {\r
244                         // If the value is not same as default, enable the custom tag\r
245                         g.claim(expression, sr.StockExpression_useCustomIntegral, expression);\r
246                     }\r
247                     g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);\r
248                 }\r
249 \r
250             });\r
251         }\r
252         this.initialExpression.setExpression(currentInitial);\r
253         this.integralExpression.setExpression(currentIntegral);\r
254     }\r
255 \r
256     @Override\r
257     public void updateData(Map<String, Object> data) {\r
258         if(this.initialExpression != null && this.initialExpression.getExpression() != null)\r
259             data.put("initialEquation", this.initialExpression.getExpression());\r
260         if(this.integralExpression != null && this.integralExpression.getExpression() != null)\r
261             data.put("integral", this.integralExpression.getExpression());\r
262     }\r
263 \r
264     \r
265     private static String getDefaultIntegral(ReadGraph graph, Resource expression) throws DatabaseException {\r
266         SysdynResource sr = SysdynResource.getInstance(graph);\r
267         Layer0 l0 = Layer0.getInstance(graph);\r
268     \r
269             // find the variable\r
270             Resource variable = graph.getPossibleObject(expression, l0.PartOf);\r
271             if(variable == null)\r
272                 return "";\r
273             \r
274                 SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());\r
275                 SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));\r
276                 model.update(graph);\r
277                 \r
278                 Stock stock = (Stock)model.getElement(variable);\r
279             \r
280                 String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);\r
281                 if(range == null)\r
282                         range = "";\r
283         \r
284             StringBuilder builder = new StringBuilder();\r
285             builder.append("");\r
286             for(Valve in : stock.getIncomingValves()) {\r
287                 builder.append(" + " + in.getName() + range);\r
288             }\r
289             for(Valve out : stock.getOutgoingValves()) {\r
290                 builder.append(" - " + out.getName() + range);\r
291             }\r
292             if (builder.indexOf(" + ") == 0)\r
293                 builder.delete(0, 3);\r
294             \r
295             return builder.toString().trim();   \r
296     }\r
297     \r
298     \r
299     private static String getDefaultIntegral(final Resource expression) {\r
300         \r
301         String integral = "";\r
302         if(expression == null)\r
303                 return integral;\r
304         try {\r
305             integral = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
306 \r
307                 @Override\r
308                 public String perform(ReadGraph graph) throws DatabaseException {\r
309                         return getDefaultIntegral(graph, expression);\r
310                 }\r
311 \r
312             });\r
313         } catch (DatabaseException e) {\r
314             e.printStackTrace();\r
315         }\r
316         return integral;\r
317     }\r
318 \r
319     @Override\r
320     public void addKeyListener(KeyListener listener) {\r
321         this.initialExpression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
322         this.integralExpression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
323 \r
324     }\r
325 \r
326     @Override\r
327     public void addModifyListener(ModifyListener listener) {\r
328         this.initialExpression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
329         this.integralExpression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
330 \r
331     }\r
332 \r
333     @Override\r
334     public void addFocusListener(FocusListener listener) {\r
335         this.initialExpression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
336         this.integralExpression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
337     }\r
338     \r
339         @Override\r
340         public void addVerifyKeyListener(VerifyKeyListener listener) {\r
341                 this.initialExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
342                 this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
343         }\r
344 \r
345 }\r