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