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