]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
756818c331da6f3f36836c0f561b4a7034244ad9
[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.FocusListener;\r
25 import org.eclipse.swt.events.KeyListener;\r
26 import org.eclipse.swt.events.ModifyListener;\r
27 import org.eclipse.swt.graphics.Point;\r
28 import org.eclipse.swt.widgets.Composite;\r
29 import org.eclipse.swt.widgets.Label;\r
30 import org.eclipse.swt.widgets.Table;\r
31 import org.eclipse.swt.widgets.Text;\r
32 import org.simantics.db.ReadGraph;\r
33 import org.simantics.db.Resource;\r
34 import org.simantics.db.WriteGraph;\r
35 import org.simantics.db.common.request.WriteRequest;\r
36 import org.simantics.db.common.utils.ListUtils;\r
37 import org.simantics.db.exception.DatabaseException;\r
38 import org.simantics.db.layer0.util.RemoverUtil;\r
39 import org.simantics.db.request.Read;\r
40 import org.simantics.layer0.Layer0;\r
41 import org.simantics.sysdyn.SysdynResource;\r
42 import org.simantics.sysdyn.manager.SysdynModel;\r
43 import org.simantics.sysdyn.manager.SysdynModelManager;\r
44 import org.simantics.sysdyn.representation.Stock;\r
45 import org.simantics.sysdyn.representation.Valve;\r
46 import org.simantics.ui.SimanticsUI;\r
47 \r
48 public class StockExpression implements IExpression {\r
49 \r
50     private Text integral;\r
51     private ExpressionField expression;\r
52 \r
53     @Override\r
54     public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {\r
55         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
56         String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";\r
57         String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";\r
58 \r
59 \r
60         Label label = new Label(parent, SWT.NONE);\r
61         label.setText("Integral");\r
62 \r
63         integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
64         integral.setEditable(false);\r
65         integral.setText(integralEquation);\r
66         GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);\r
67 \r
68 \r
69         label = new Label(parent, SWT.NONE);\r
70         label.setText("Initial\nValue");\r
71 \r
72         expression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true);\r
73         expression.setExpression(initialEquation);\r
74 \r
75         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
76 \r
77     }\r
78 \r
79     @Override\r
80     public void focus() {\r
81         this.expression.focus();\r
82     }\r
83 \r
84     @Override\r
85     public List<ExpressionField> getExpressionFields() {\r
86         return Arrays.asList(this.expression);\r
87     }\r
88 \r
89     @Override\r
90     public void readData(final Resource expression, Map<String, Object> data) {\r
91         String initialEquation = null;\r
92 \r
93         if (expression != null && data.get("initialEquation") == null) {\r
94             try {\r
95                 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
96 \r
97                     @Override\r
98                     public String perform(ReadGraph graph) throws DatabaseException {\r
99                         SysdynResource sr = SysdynResource.getInstance(graph);\r
100                         if (graph.isInstanceOf(expression, sr.StockExpression)) {\r
101                             String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);\r
102                             return initialEquation != null ? initialEquation : "";\r
103                         } else {\r
104                             return "";\r
105                         }\r
106                     }\r
107 \r
108                 });\r
109             } catch (DatabaseException e1) {\r
110                 e1.printStackTrace();\r
111             }\r
112             data.put("initialEquation", initialEquation);\r
113         }\r
114 \r
115         data.put("integral", getIntegral(expression));\r
116 \r
117     }\r
118 \r
119     @Override\r
120     public void replaceSelection(String var) {\r
121         if(expression != null) {\r
122             IDocument doc = expression.getDocument();\r
123             try {\r
124                 Point selection = expression.getSelection();\r
125                 doc.replace(selection.x, selection.y, var);\r
126                 expression.setSelection(selection.x + var.length());\r
127             } catch (BadLocationException e) {\r
128                 e.printStackTrace();\r
129             }\r
130         }\r
131     }\r
132 \r
133     @Override\r
134     public void save(final Resource expression, Map<String, Object> data) {\r
135         final String currentText = this.expression.getExpression();\r
136         if(currentText != null) {\r
137             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
138 \r
139                 @Override\r
140                 public void perform(WriteGraph g)\r
141                 throws DatabaseException {\r
142                     SysdynResource sr = SysdynResource.getInstance(g);\r
143                     Layer0 l0 = Layer0.getInstance(g);\r
144                     \r
145                     if(!g.isInstanceOf(expression, sr.StockExpression)) {\r
146                         Resource variable = g.getPossibleObject(expression, l0.PartOf);\r
147                         Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList);\r
148                         Resource temp = g.newResource();\r
149                         ListUtils.replace(g, expressionList, expression, temp);\r
150                         \r
151                         for(Resource predicate : g.getPredicates(expression)) {\r
152                             g.deny(expression, predicate);\r
153                         }\r
154                         g.claim(expression, l0.InstanceOf, null, sr.StockExpression);\r
155 \r
156                         ListUtils.replace(g, expressionList, temp, expression);\r
157 \r
158                         RemoverUtil.remove(g, temp);\r
159 \r
160                         g.claim(expression, l0.PartOf, variable);\r
161                     }\r
162                     g.claimLiteral(expression, sr.StockExpression_initialEquation, currentText);\r
163                 }\r
164 \r
165             });\r
166         }\r
167         this.expression.setExpression(currentText);\r
168     }\r
169 \r
170     @Override\r
171     public void updateData(Map<String, Object> data) {\r
172         if(this.expression != null && this.expression.getExpression() != null)\r
173             data.put("initialEquation", this.expression.getExpression());\r
174         if(this.integral != null && this.integral.getText() != null)\r
175             data.put("integral", this.integral.getText());\r
176     }\r
177 \r
178 \r
179     private String getIntegral(final Resource expression) {\r
180         \r
181         String integral = "";\r
182         if(expression == null)\r
183                 return integral;\r
184         try {\r
185             integral = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
186 \r
187                 @Override\r
188                 public String perform(ReadGraph graph) throws DatabaseException {\r
189                     SysdynResource sr = SysdynResource.getInstance(graph);\r
190                     Layer0 l0 = Layer0.getInstance(graph);\r
191                     \r
192                     // find the variable\r
193                     Resource variable = graph.getPossibleObject(expression, l0.PartOf);\r
194                     if(variable == null)\r
195                         return "";\r
196                     \r
197                         SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());\r
198                         SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));\r
199                         model.update(graph);\r
200                         \r
201                         Stock stock = (Stock)model.getElement(variable);\r
202                     \r
203                         String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);\r
204                         if(range == null)\r
205                                 range = "";\r
206 \r
207                     StringBuilder builder = new StringBuilder();\r
208                     builder.append("");\r
209                     for(Valve in : stock.getIncomingValves()) {\r
210                         builder.append(" + " + in.getName() + range);\r
211                     }\r
212                     for(Valve out : stock.getOutgoingValves()) {\r
213                         builder.append(" - " + out.getName() + range);\r
214                     }\r
215                     if (builder.indexOf(" + ") == 0)\r
216                         builder.delete(0, 3);\r
217                     \r
218                     return builder.toString().trim();\r
219                 }\r
220 \r
221             });\r
222         } catch (DatabaseException e) {\r
223             e.printStackTrace();\r
224         }\r
225         return integral;\r
226     }\r
227 \r
228     @Override\r
229     public void addKeyListener(KeyListener listener) {\r
230         this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
231 \r
232     }\r
233 \r
234     @Override\r
235     public void addModifyListener(ModifyListener listener) {\r
236         this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);\r
237 \r
238     }\r
239 \r
240     @Override\r
241     public void addFocusListener(FocusListener listener) {\r
242         this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);\r
243     }\r
244     \r
245         @Override\r
246         public void addVerifyKeyListener(VerifyKeyListener listener) {\r
247                 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
248         }\r
249 \r
250 }\r