]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
ac8b74559e119d88439eb8d8d37448a0aa4cddb2
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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.equation.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.graphics.Point;\r
25 import org.eclipse.swt.widgets.Composite;\r
26 import org.eclipse.swt.widgets.Label;\r
27 import org.eclipse.swt.widgets.Text;\r
28 import org.simantics.db.Builtins;\r
29 import org.simantics.db.ReadGraph;\r
30 import org.simantics.db.Resource;\r
31 import org.simantics.db.WriteGraph;\r
32 import org.simantics.db.common.request.WriteRequest;\r
33 import org.simantics.db.exception.DatabaseException;\r
34 import org.simantics.db.request.Read;\r
35 import org.simantics.sysdyn.SysdynResource;\r
36 import org.simantics.ui.SimanticsUI;\r
37 \r
38 public class StockExpressionViewFactor implements IExpressionViewFactor {\r
39 \r
40     private Label integralLabel;\r
41     private Text integral;\r
42     private Label equationLabel;\r
43     private ExpressionField expression;\r
44 \r
45     @Override\r
46     public void createView(Composite parent, Map<String, Object> data) {\r
47 \r
48         final String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";\r
49         final String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";\r
50 \r
51         GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
52 \r
53         integralLabel = new Label(parent, SWT.NONE);\r
54         integralLabel.setFont(FONT);\r
55         integralLabel.setText("Integral");\r
56         GridDataFactory.fillDefaults().applyTo(integralLabel);\r
57 \r
58         integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
59         integral.setEditable(false);\r
60         integral.setFont(FONT);\r
61         integral.setText(integralEquation);\r
62         GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);\r
63 \r
64         equationLabel = new Label(parent, SWT.NONE);\r
65         equationLabel.setFont(FONT);\r
66         equationLabel.setText("Initial\nValue");\r
67         GridDataFactory.fillDefaults().applyTo(equationLabel);\r
68 \r
69 \r
70         expression = new ExpressionField(parent, SWT.BORDER);\r
71         expression.setFont(FONT);\r
72         expression.setExpression(initialEquation);\r
73         GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
74 \r
75     }\r
76 \r
77     @Override\r
78     public void readData(final Resource variable, Map<String, Object> data) {\r
79         String initialEquation = null;\r
80 \r
81         if (variable != null && data.get("initialEquation") == null) {\r
82             try {\r
83                 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
84 \r
85                     @Override\r
86                     public String perform(ReadGraph graph) throws DatabaseException {\r
87                         SysdynResource sr = SysdynResource.getInstance(graph);\r
88                         Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
89                         if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) {\r
90                             return graph.getRelatedValue(expression, sr.HasInitialEquation);\r
91                         } else {\r
92                             return "";\r
93                         }\r
94                     }\r
95 \r
96                 });\r
97             } catch (DatabaseException e1) {\r
98                 e1.printStackTrace();\r
99             }\r
100             data.put("initialEquation", initialEquation);\r
101         }\r
102 \r
103         data.put("integral", getIntegral(variable));\r
104     }\r
105 \r
106     @Override\r
107     public void writeData(final Resource variable, Map<String, Object> data) {\r
108         final String currentText = expression.getExpression();\r
109         if(currentText != null) {\r
110             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
111 \r
112                 @Override\r
113                 public void perform(WriteGraph g)\r
114                 throws DatabaseException {\r
115                     SysdynResource sr = SysdynResource.getInstance(g);\r
116                     Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
117                     Builtins b = g.getBuiltins();\r
118                     if(expression != null) {\r
119                         g.deny(variable, sr.HasExpression);\r
120                     }\r
121                     expression = g.newResource();\r
122                     g.claim(expression, b.InstanceOf, null, sr.StockExpression);\r
123                     g.claim(variable, sr.HasExpression, expression);\r
124                     g.claimValue(expression, sr.HasInitialEquation, currentText);\r
125                 }\r
126 \r
127             });\r
128         }\r
129     }\r
130 \r
131     private String getIntegral(final Resource variable) {\r
132         String integral = "";\r
133         try {\r
134             integral = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
135 \r
136                 @Override\r
137                 public String perform(ReadGraph graph) throws DatabaseException {\r
138                     SysdynResource sr = SysdynResource.getInstance(graph);\r
139                     Builtins b = graph.getBuiltins();\r
140                     Collection<Resource> heads = graph.getObjects(variable, sr.IsHeadOf);\r
141                     Collection<Resource> tails = graph.getObjects(variable, sr.IsTailOf);\r
142 \r
143                     StringBuilder builder = new StringBuilder();\r
144                     builder.append("");\r
145                     for (Resource r : heads) {\r
146                         if(graph.isInstanceOf(r, sr.Flow)) {\r
147                             Resource tail = graph.getPossibleObject(r, sr.HasTail);\r
148                             if(tail != null) {\r
149                                 Object name = graph.getPossibleRelatedValue(tail, b.HasName);\r
150                                 if (name != null)\r
151                                     builder.append(" + " + name);\r
152                             }\r
153                         }\r
154                     }\r
155                     for (Resource r : tails) {\r
156                         if(graph.isInstanceOf(r, sr.Flow)) {\r
157                             Resource head = graph.getPossibleObject(r, sr.HasHead);\r
158                             if(head != null) {\r
159                                 Object name = graph.getPossibleRelatedValue(head, b.HasName);\r
160                                 if (name != null)\r
161                                     builder.append(" - " + name);\r
162                             }\r
163                         }\r
164                     }\r
165                     if (builder.indexOf(" + ") == 0)\r
166                         builder.delete(0, 3);\r
167                     return builder.toString().trim();\r
168                 }\r
169 \r
170             });\r
171         } catch (DatabaseException e) {\r
172             e.printStackTrace();\r
173         }\r
174         return integral;\r
175     }\r
176 \r
177     @Override\r
178     public void focus() {\r
179         if(this.expression != null) this.expression.focus();\r
180     }\r
181 \r
182     @Override\r
183     public void replaceSelection(String var) {\r
184         if(expression != null) {\r
185             IDocument doc = expression.getDocument();\r
186             try {\r
187                 Point selection = expression.getSelection();\r
188                 doc.replace(selection.x, selection.y, var);\r
189                 expression.setSelection(selection.x + var.length());\r
190             } catch (BadLocationException e) {\r
191                 e.printStackTrace();\r
192             }\r
193         }\r
194     }\r
195 \r
196     @Override\r
197     public void updateData(Map<String, Object> data) {\r
198         if(this.expression != null && this.expression.getExpression() != null)\r
199             data.put("initialEquation", this.expression.getExpression());\r
200         if(this.integral != null && this.integral.getText() != null)\r
201             data.put("integral", this.integral.getText());\r
202     }\r
203 \r
204     @Override\r
205     public List<ExpressionField> getExpressionFields() {\r
206         return Arrays.asList(this.expression);\r
207     }\r
208 }\r