1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
15 import java.util.Map;
\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
38 public class ParameterExpression extends BasicExpression {
\r
42 public ParameterExpression(Variable variable) {
\r
44 this.expressionType = SimanticsUI.getSession().syncRequest(new Read<Resource>() {
\r
47 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
48 return SysdynResource.getInstance(graph).ParameterExpression;
\r
51 } catch (DatabaseException e) {
\r
52 e.printStackTrace();
\r
55 this.variable = variable;
\r
60 public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {
\r
61 // Create the single field
\r
62 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
63 String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
65 Label l = new Label(parent, SWT.NONE);
\r
68 expression = new ExpressionField(parent, SWT.BORDER, null, false);
\r
69 expression.setExpression(equation);
\r
70 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
75 public void readData(final Resource expression, Map<String, Object> data) {
\r
76 IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
\r
77 IExperiment experiment = manager.getActiveExperiment();
\r
78 if(experiment == null || !(experiment instanceof SysdynGameExperiment)) {
\r
79 super.readData(expression, data);
\r
82 ExperimentState state = ((SysdynGameExperiment)experiment).getSysdynExperimentState();
\r
83 if(ExperimentState.RUNNING.equals(state) || ExperimentState.STOPPED.equals(state)) {
\r
85 value = SimanticsUI.getSession().syncRequest(new Read<Double>() {
\r
88 public Double perform(ReadGraph graph) throws DatabaseException {
\r
90 Variable valuesVariable = variable.browsePossible(graph, "#value#");
\r
91 double[] res = valuesVariable.getValue(graph);
\r
92 if(res != null && res.length == 1)
\r
96 } catch (DatabaseException e) {
\r
97 throw new DatabaseException(e.getMessage());
\r
102 data.put("equation", value.toString());
\r
103 } catch (DatabaseException e) {}
\r
105 super.readData(expression, data);
\r
111 public void save(final Resource expression, Map<String, Object> data) {
\r
113 IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
\r
114 IExperiment experiment = manager.getActiveExperiment();
\r
115 if(experiment != null && experiment instanceof SysdynGameExperiment) {
\r
116 final String currentText = this.expression.getExpression();
\r
117 final String oldEquation = (String)data.get("equation");
\r
118 if(oldEquation == null ||
\r
119 (currentText != null && expressionType != null)) {
\r
120 if(ExpressionUtils.isParameter(currentText)) {
\r
121 Boolean savedIntoFMU = false;
\r
123 ExperimentState state = ((SysdynGameExperiment)experiment).getSysdynExperimentState();
\r
124 // Set value to control only if the simulation is running or stopped, not before initialization
\r
125 if(ExperimentState.RUNNING.equals(state) || ExperimentState.STOPPED.equals(state)) {
\r
127 savedIntoFMU = SimanticsUI.getSession().syncRequest(new WriteResultRequest<Boolean>() {
\r
130 public Boolean perform(WriteGraph graph)
\r
131 throws DatabaseException {
\r
133 Variable valuesVariable = variable.browsePossible(graph, "#value#");
\r
134 if(valuesVariable == null)
\r
136 valuesVariable.setValue(graph, new double[] {Double.parseDouble(currentText)});
\r
138 } catch (Exception e) {
\r
144 } catch (DatabaseException e) {}
\r
148 data.put("equation", currentText);
\r
155 // If setting a parameter value was not succesful, use the normal save-method
\r
156 super.save(expression, data);
\r
159 public ParameterExpression() {
\r
161 this.expressionType = SimanticsUI.getSession().syncRequest(new Read<Resource>() {
\r
164 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
165 return SysdynResource.getInstance(graph).ParameterExpression;
\r
168 } catch (DatabaseException e) {
\r
169 e.printStackTrace();
\r