]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
772f083b2767c48ee8462467b2d08fca3781f547
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2012 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 \r
15 import java.util.Map;\r
16 \r
17 import org.eclipse.jface.layout.GridDataFactory;\r
18 import org.eclipse.jface.layout.GridLayoutFactory;\r
19 import org.eclipse.swt.SWT;\r
20 import org.eclipse.swt.widgets.Composite;\r
21 import org.eclipse.swt.widgets.Label;\r
22 import org.eclipse.swt.widgets.Table;\r
23 import org.simantics.db.ReadGraph;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.WriteGraph;\r
26 import org.simantics.db.common.CommentMetadata;\r
27 import org.simantics.db.common.request.WriteResultRequest;\r
28 import org.simantics.db.exception.DatabaseException;\r
29 import org.simantics.db.layer0.variable.Variable;\r
30 import org.simantics.db.request.Read;\r
31 import org.simantics.simulation.experiment.ExperimentState;\r
32 import org.simantics.simulation.experiment.IExperiment;\r
33 import org.simantics.simulation.project.IExperimentManager;\r
34 import org.simantics.sysdyn.SysdynResource;\r
35 import org.simantics.sysdyn.manager.SysdynGameExperimentBase;\r
36 import org.simantics.sysdyn.ui.utils.ExpressionUtils;\r
37 import org.simantics.ui.SimanticsUI;\r
38 \r
39 public class ParameterExpression extends BasicExpression {\r
40 \r
41         Variable variable;\r
42 \r
43         public ParameterExpression(ExpressionWidgetInput input) {\r
44             super(input);\r
45             try {\r
46                         this.expressionType = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
47 \r
48                                 @Override\r
49                                 public Resource perform(ReadGraph graph) throws DatabaseException {\r
50                                         return SysdynResource.getInstance(graph).ParameterExpression;\r
51                                 }\r
52                         });\r
53                 } catch (DatabaseException e) {\r
54                         e.printStackTrace();\r
55                 }               \r
56             \r
57             this.variable = input.variable;\r
58         }\r
59 \r
60 \r
61         @Override\r
62         public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {\r
63                 // Create the single field\r
64                 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
65                 String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
66 \r
67                 Label l = new Label(parent, SWT.NONE);\r
68                 l.setText("=");\r
69 \r
70                 expression = new ExpressionField(parent, SWT.BORDER, null, false, input);\r
71                 expression.setExpression(equation);\r
72                 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
73 \r
74         }\r
75 \r
76         @Override\r
77         public void readData(final Resource expression, Map<String, Object> data) {\r
78                 IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
79                 IExperiment experiment = manager.getActiveExperiment();\r
80                 if(experiment == null || !(experiment instanceof SysdynGameExperimentBase)) {\r
81                         super.readData(expression, data);\r
82                 } else {\r
83                         Double value;\r
84                         ExperimentState state = ((SysdynGameExperimentBase)experiment).getSysdynExperimentState();\r
85                         if(ExperimentState.RUNNING.equals(state) || ExperimentState.STOPPED.equals(state)) {\r
86                             try {\r
87                                 value = SimanticsUI.getSession().syncRequest(new Read<Double>() {\r
88 \r
89                                     @Override\r
90                                     public Double perform(ReadGraph graph) throws DatabaseException {\r
91                                         try {\r
92                                             Variable valuesVariable = variable.browsePossible(graph, "#value#");\r
93                                             double[] res = valuesVariable.getValue(graph);\r
94                                             if(res != null && res.length == 1)\r
95                                                 return res[0];\r
96                                             else\r
97                                                 return null;\r
98                                         } catch (DatabaseException e) {\r
99                                             throw new DatabaseException(e.getMessage());\r
100                                         } \r
101                                     }\r
102                                 });\r
103                                 if(value != null)\r
104                                     data.put("equation", value.toString());\r
105                             } catch (DatabaseException e) {}\r
106                         }\r
107                         super.readData(expression, data);\r
108 \r
109                 }\r
110         }\r
111 \r
112         @Override\r
113         public void save(final Resource expression, Map<String, Object> data) {\r
114 \r
115                 IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
116                 IExperiment experiment = manager.getActiveExperiment();\r
117                 if(experiment != null && experiment instanceof SysdynGameExperimentBase) {\r
118                         final String currentText = this.expression.getExpression();\r
119                         final String oldEquation = (String)data.get("equation");\r
120                         if(oldEquation == null || \r
121                                 (currentText != null && oldEquation != null && !currentText.equals(oldEquation) && expressionType != null)) {\r
122                             if(ExpressionUtils.isParameter(currentText)) {\r
123                                 Boolean savedIntoFMU = false;\r
124 \r
125                                 ExperimentState state = ((SysdynGameExperimentBase)experiment).getSysdynExperimentState();\r
126                                 // Set value to control only if the simulation is running or stopped, not before initialization\r
127                                 if(ExperimentState.RUNNING.equals(state) || ExperimentState.STOPPED.equals(state)) {\r
128                                     try {\r
129                                         savedIntoFMU = SimanticsUI.getSession().syncRequest(new WriteResultRequest<Boolean>() {\r
130 \r
131                                             @Override\r
132                                             public Boolean perform(WriteGraph graph)\r
133                                                     throws DatabaseException {\r
134                                                 try {\r
135                                                     Variable valuesVariable = variable.browsePossible(graph, "#value#");\r
136                                                     if(valuesVariable == null)\r
137                                                         return false;\r
138                                                     valuesVariable.setValue(graph, new double[] {Double.parseDouble(currentText)});\r
139                                                     \r
140                                                     graph.markUndoPoint();\r
141                                                                 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
142                                                                 graph.addMetadata(cm.add("Set parameter"));\r
143                                                     \r
144                                                     return true;\r
145                                                 } catch (Exception e) {\r
146                                                     return false;\r
147                                                 }\r
148                                             }\r
149 \r
150                                         });\r
151                                     } catch (DatabaseException e) {}\r
152                                 }\r
153 \r
154                                 if(savedIntoFMU) {\r
155                                                 data.put("equation", currentText);\r
156                                                 return;\r
157                                         }\r
158                                 }\r
159                         }\r
160                 }\r
161 \r
162                 // If setting a parameter value was not succesful, use the normal save-method\r
163                 super.save(expression, data);\r
164         }\r
165 \r
166         public ParameterExpression() {\r
167             super(null);\r
168                 try {\r
169                         this.expressionType = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
170 \r
171                                 @Override\r
172                                 public Resource perform(ReadGraph graph) throws DatabaseException {\r
173                                         return SysdynResource.getInstance(graph).ParameterExpression;\r
174                                 }\r
175                         });\r
176                 } catch (DatabaseException e) {\r
177                         e.printStackTrace();\r
178                 }\r
179         }\r
180 \r
181 \r
182 }\r