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