1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2014 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
14 import java.util.Arrays;
\r
15 import java.util.List;
\r
16 import java.util.Map;
\r
18 import org.eclipse.jface.layout.GridDataFactory;
\r
19 import org.eclipse.jface.layout.GridLayoutFactory;
\r
20 import org.eclipse.jface.text.BadLocationException;
\r
21 import org.eclipse.jface.text.IDocument;
\r
22 import org.eclipse.swt.SWT;
\r
23 import org.eclipse.swt.custom.VerifyKeyListener;
\r
24 import org.eclipse.swt.events.FocusAdapter;
\r
25 import org.eclipse.swt.events.FocusEvent;
\r
26 import org.eclipse.swt.events.FocusListener;
\r
27 import org.eclipse.swt.events.KeyListener;
\r
28 import org.eclipse.swt.events.ModifyListener;
\r
29 import org.eclipse.swt.graphics.Point;
\r
30 import org.eclipse.swt.widgets.Button;
\r
31 import org.eclipse.swt.widgets.Composite;
\r
32 import org.eclipse.swt.widgets.Event;
\r
33 import org.eclipse.swt.widgets.Label;
\r
34 import org.eclipse.swt.widgets.Listener;
\r
35 import org.eclipse.swt.widgets.Table;
\r
36 import org.simantics.db.ReadGraph;
\r
37 import org.simantics.db.Resource;
\r
38 import org.simantics.db.WriteGraph;
\r
39 import org.simantics.db.common.request.WriteRequest;
\r
40 import org.simantics.db.common.utils.ListUtils;
\r
41 import org.simantics.db.exception.DatabaseException;
\r
42 import org.simantics.db.layer0.util.RemoverUtil;
\r
43 import org.simantics.db.request.Read;
\r
44 import org.simantics.layer0.Layer0;
\r
45 import org.simantics.sysdyn.SysdynResource;
\r
46 import org.simantics.sysdyn.manager.SysdynModel;
\r
47 import org.simantics.sysdyn.manager.SysdynModelManager;
\r
48 import org.simantics.sysdyn.representation.Stock;
\r
49 import org.simantics.sysdyn.representation.Valve;
\r
50 import org.simantics.ui.SimanticsUI;
\r
51 import org.simantics.utils.datastructures.Pair;
\r
53 public class StockExpression implements IExpression {
\r
55 private final ExpressionWidgetInput input;
\r
56 private ExpressionField integralExpression, initialExpression;
\r
57 private ExpressionField lastSelectedText;
\r
59 public StockExpression(ExpressionWidgetInput input) {
\r
64 public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {
\r
65 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
66 String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
67 String integralEquation = data.get("integral") != null ? (String)data.get("integral") : getDefaultIntegral(input.expression);
\r
70 Label label = new Label(parent, SWT.NONE);
\r
71 label.setText("Integral");
\r
73 Composite integralComposite = new Composite(parent, SWT.NONE);
\r
74 GridDataFactory.fillDefaults().grab(true, true).applyTo(integralComposite);
\r
75 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(integralComposite);
\r
77 integralExpression = new ExpressionField(integralComposite, SWT.BORDER, allowedVariables, true, input);
\r
78 integralExpression.setExpression(integralEquation);
\r
79 GridDataFactory.fillDefaults().grab(true, true).applyTo(integralExpression);
\r
80 integralExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
83 public void focusLost(FocusEvent e) {
\r
84 lastSelectedText = integralExpression;
\r
88 Button defaultButton = new Button(integralComposite, SWT.PUSH);
\r
89 defaultButton.setText("Use default");
\r
90 defaultButton.addListener(SWT.Selection, new Listener() {
\r
93 public void handleEvent(Event event) {
\r
94 switch (event.type) {
\r
96 StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression));
\r
97 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
100 public void perform(WriteGraph graph)
\r
101 throws DatabaseException {
\r
102 // Delete the possible integral expression from the database to note
\r
103 // that we are in the "default" mode.
\r
104 SysdynResource sr = SysdynResource.getInstance(graph);
\r
105 graph.deny(input.expression, sr.StockExpression_useCustomIntegral);
\r
106 graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));
\r
115 label = new Label(parent, SWT.NONE);
\r
116 label.setText("Initial\nValue");
\r
118 initialExpression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true, input);
\r
119 initialExpression.setExpression(initialEquation);
\r
120 GridDataFactory.fillDefaults().grab(true, true).applyTo(initialExpression);
\r
121 initialExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
124 public void focusLost(FocusEvent e) {
\r
125 lastSelectedText = initialExpression;
\r
129 lastSelectedText = initialExpression;
\r
133 public void focus() {
\r
134 this.lastSelectedText.focus();
\r
138 public List<ExpressionField> getExpressionFields() {
\r
139 return Arrays.asList(this.integralExpression, this.initialExpression);
\r
143 public void readData(final Resource expression, Map<String, Object> data) {
\r
144 Pair<String, String> equations = null;
\r
145 if (expression != null && data.get("initialEquation") == null) {
\r
147 equations = SimanticsUI.getSession().syncRequest(new Read<Pair<String, String>>() {
\r
150 public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {
\r
151 SysdynResource sr = SysdynResource.getInstance(graph);
\r
152 if (graph.isInstanceOf(expression, sr.StockExpression)) {
\r
153 String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);
\r
154 String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation);
\r
155 initialEquation = (initialEquation != null) ? initialEquation : "";
\r
156 return new Pair<String, String>(integralEquation, initialEquation);
\r
158 return new Pair<String, String>(null, "");
\r
163 } catch (DatabaseException e1) {
\r
164 e1.printStackTrace();
\r
166 data.put("initialEquation", equations.second);
\r
169 if (equations.first == null) {
\r
171 SimanticsUI.getSession().syncRequest(new WriteRequest() {
\r
174 public void perform(WriteGraph graph) throws DatabaseException {
\r
175 SysdynResource sr = SysdynResource.getInstance(graph);
\r
176 graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));
\r
180 } catch (DatabaseException e) {
\r
181 // TODO Auto-generated catch block
\r
182 e.printStackTrace();
\r
185 data.put("integral", getDefaultIntegral(expression));
\r
187 data.put("integral", equations.first);
\r
193 public void replaceSelection(String var) {
\r
194 if(lastSelectedText != null) {
\r
195 IDocument doc = lastSelectedText.getDocument();
\r
197 Point selection = lastSelectedText.getSelection();
\r
198 doc.replace(selection.x, selection.y, var);
\r
199 lastSelectedText.setSelection(selection.x + var.length());
\r
200 } catch (BadLocationException e) {
\r
201 e.printStackTrace();
\r
207 public void save(final Resource expression, final Map<String, Object> data) {
\r
208 final String currentInitial = this.initialExpression.getExpression();
\r
209 final String currentIntegral = this.integralExpression.getExpression();
\r
210 if (currentIntegral == null) {
\r
211 this.integralExpression.setExpression(getDefaultIntegral(expression));
\r
213 if(!currentInitial.equals(data.get("initialEquation"))
\r
214 || currentIntegral == null
\r
215 || !currentIntegral.equals(data.get("integral"))) {
\r
216 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
219 public void perform(WriteGraph g)
\r
220 throws DatabaseException {
\r
221 SysdynResource sr = SysdynResource.getInstance(g);
\r
222 Layer0 l0 = Layer0.getInstance(g);
\r
224 if(!g.isInstanceOf(expression, sr.StockExpression)) {
\r
225 Resource variable = g.getPossibleObject(expression, l0.PartOf);
\r
226 Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList);
\r
227 Resource temp = g.newResource();
\r
228 ListUtils.replace(g, expressionList, expression, temp);
\r
230 for(Resource predicate : g.getPredicates(expression)) {
\r
231 g.deny(expression, predicate);
\r
233 g.claim(expression, l0.InstanceOf, null, sr.StockExpression);
\r
235 ListUtils.replace(g, expressionList, temp, expression);
\r
237 RemoverUtil.remove(g, temp);
\r
239 g.claim(expression, l0.PartOf, variable);
\r
241 g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial);
\r
243 if (!currentIntegral.equals(data.get("integral"))) {
\r
244 // If the value is not same as default, enable the custom tag
\r
245 g.claim(expression, sr.StockExpression_useCustomIntegral, expression);
\r
247 g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);
\r
252 this.initialExpression.setExpression(currentInitial);
\r
253 this.integralExpression.setExpression(currentIntegral);
\r
257 public void updateData(Map<String, Object> data) {
\r
258 if(this.initialExpression != null && this.initialExpression.getExpression() != null)
\r
259 data.put("initialEquation", this.initialExpression.getExpression());
\r
260 if(this.integralExpression != null && this.integralExpression.getExpression() != null)
\r
261 data.put("integral", this.integralExpression.getExpression());
\r
265 private static String getDefaultIntegral(ReadGraph graph, Resource expression) throws DatabaseException {
\r
266 SysdynResource sr = SysdynResource.getInstance(graph);
\r
267 Layer0 l0 = Layer0.getInstance(graph);
\r
269 // find the variable
\r
270 Resource variable = graph.getPossibleObject(expression, l0.PartOf);
\r
271 if(variable == null)
\r
274 SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());
\r
275 SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));
\r
276 model.update(graph);
\r
278 Stock stock = (Stock)model.getElement(variable);
\r
280 String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);
\r
284 StringBuilder builder = new StringBuilder();
\r
285 builder.append("");
\r
286 for(Valve in : stock.getIncomingValves()) {
\r
287 builder.append(" + " + in.getName() + range);
\r
289 for(Valve out : stock.getOutgoingValves()) {
\r
290 builder.append(" - " + out.getName() + range);
\r
292 if (builder.indexOf(" + ") == 0)
\r
293 builder.delete(0, 3);
\r
295 return builder.toString().trim();
\r
299 private static String getDefaultIntegral(final Resource expression) {
\r
301 String integral = "";
\r
302 if(expression == null)
\r
305 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
308 public String perform(ReadGraph graph) throws DatabaseException {
\r
309 return getDefaultIntegral(graph, expression);
\r
313 } catch (DatabaseException e) {
\r
314 e.printStackTrace();
\r
320 public void addKeyListener(KeyListener listener) {
\r
321 this.initialExpression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
322 this.integralExpression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
327 public void addModifyListener(ModifyListener listener) {
\r
328 this.initialExpression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
329 this.integralExpression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
334 public void addFocusListener(FocusListener listener) {
\r
335 this.initialExpression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
336 this.integralExpression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
340 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
341 this.initialExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
342 this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r