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