1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 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.FocusListener;
\r
25 import org.eclipse.swt.events.KeyListener;
\r
26 import org.eclipse.swt.events.ModifyListener;
\r
27 import org.eclipse.swt.graphics.Point;
\r
28 import org.eclipse.swt.widgets.Composite;
\r
29 import org.eclipse.swt.widgets.Label;
\r
30 import org.eclipse.swt.widgets.Table;
\r
31 import org.eclipse.swt.widgets.Text;
\r
32 import org.simantics.db.ReadGraph;
\r
33 import org.simantics.db.Resource;
\r
34 import org.simantics.db.WriteGraph;
\r
35 import org.simantics.db.common.request.WriteRequest;
\r
36 import org.simantics.db.common.utils.ListUtils;
\r
37 import org.simantics.db.exception.DatabaseException;
\r
38 import org.simantics.db.layer0.util.RemoverUtil;
\r
39 import org.simantics.db.request.Read;
\r
40 import org.simantics.layer0.Layer0;
\r
41 import org.simantics.sysdyn.SysdynResource;
\r
42 import org.simantics.sysdyn.manager.SysdynModel;
\r
43 import org.simantics.sysdyn.manager.SysdynModelManager;
\r
44 import org.simantics.sysdyn.representation.Stock;
\r
45 import org.simantics.sysdyn.representation.Valve;
\r
46 import org.simantics.ui.SimanticsUI;
\r
48 public class StockExpression implements IExpression {
\r
50 private Text integral;
\r
51 private final ExpressionWidgetInput input;
\r
52 private ExpressionField expression;
\r
54 public StockExpression(ExpressionWidgetInput input) {
\r
59 public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {
\r
60 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
61 String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
62 String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";
\r
65 Label label = new Label(parent, SWT.NONE);
\r
66 label.setText("Integral");
\r
68 integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
69 integral.setEditable(false);
\r
70 integral.setText(integralEquation);
\r
71 GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);
\r
74 label = new Label(parent, SWT.NONE);
\r
75 label.setText("Initial\nValue");
\r
77 expression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true, input);
\r
78 expression.setExpression(initialEquation);
\r
80 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
85 public void focus() {
\r
86 this.expression.focus();
\r
90 public List<ExpressionField> getExpressionFields() {
\r
91 return Arrays.asList(this.expression);
\r
95 public void readData(final Resource expression, Map<String, Object> data) {
\r
96 String initialEquation = null;
\r
98 if (expression != null && data.get("initialEquation") == null) {
\r
100 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
103 public String perform(ReadGraph graph) throws DatabaseException {
\r
104 SysdynResource sr = SysdynResource.getInstance(graph);
\r
105 if (graph.isInstanceOf(expression, sr.StockExpression)) {
\r
106 String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);
\r
107 return initialEquation != null ? initialEquation : "";
\r
114 } catch (DatabaseException e1) {
\r
115 e1.printStackTrace();
\r
117 data.put("initialEquation", initialEquation);
\r
120 data.put("integral", getIntegral(expression));
\r
125 public void replaceSelection(String var) {
\r
126 if(expression != null) {
\r
127 IDocument doc = expression.getDocument();
\r
129 Point selection = expression.getSelection();
\r
130 doc.replace(selection.x, selection.y, var);
\r
131 expression.setSelection(selection.x + var.length());
\r
132 } catch (BadLocationException e) {
\r
133 e.printStackTrace();
\r
139 public void save(final Resource expression, Map<String, Object> data) {
\r
140 final String currentText = this.expression.getExpression();
\r
141 if(currentText != null && !currentText.equals(data.get("initialEquation"))) {
\r
142 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
145 public void perform(WriteGraph g)
\r
146 throws DatabaseException {
\r
147 SysdynResource sr = SysdynResource.getInstance(g);
\r
148 Layer0 l0 = Layer0.getInstance(g);
\r
150 if(!g.isInstanceOf(expression, sr.StockExpression)) {
\r
151 Resource variable = g.getPossibleObject(expression, l0.PartOf);
\r
152 Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList);
\r
153 Resource temp = g.newResource();
\r
154 ListUtils.replace(g, expressionList, expression, temp);
\r
156 for(Resource predicate : g.getPredicates(expression)) {
\r
157 g.deny(expression, predicate);
\r
159 g.claim(expression, l0.InstanceOf, null, sr.StockExpression);
\r
161 ListUtils.replace(g, expressionList, temp, expression);
\r
163 RemoverUtil.remove(g, temp);
\r
165 g.claim(expression, l0.PartOf, variable);
\r
167 g.claimLiteral(expression, sr.StockExpression_initialEquation, currentText);
\r
172 this.expression.setExpression(currentText);
\r
176 public void updateData(Map<String, Object> data) {
\r
177 if(this.expression != null && this.expression.getExpression() != null)
\r
178 data.put("initialEquation", this.expression.getExpression());
\r
179 if(this.integral != null && this.integral.getText() != null)
\r
180 data.put("integral", this.integral.getText());
\r
184 private String getIntegral(final Resource expression) {
\r
186 String integral = "";
\r
187 if(expression == null)
\r
190 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
193 public String perform(ReadGraph graph) throws DatabaseException {
\r
194 SysdynResource sr = SysdynResource.getInstance(graph);
\r
195 Layer0 l0 = Layer0.getInstance(graph);
\r
197 // find the variable
\r
198 Resource variable = graph.getPossibleObject(expression, l0.PartOf);
\r
199 if(variable == null)
\r
202 SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());
\r
203 SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));
\r
204 model.update(graph);
\r
206 Stock stock = (Stock)model.getElement(variable);
\r
208 String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);
\r
212 StringBuilder builder = new StringBuilder();
\r
213 builder.append("");
\r
214 for(Valve in : stock.getIncomingValves()) {
\r
215 builder.append(" + " + in.getName() + range);
\r
217 for(Valve out : stock.getOutgoingValves()) {
\r
218 builder.append(" - " + out.getName() + range);
\r
220 if (builder.indexOf(" + ") == 0)
\r
221 builder.delete(0, 3);
\r
223 return builder.toString().trim();
\r
227 } catch (DatabaseException e) {
\r
228 e.printStackTrace();
\r
234 public void addKeyListener(KeyListener listener) {
\r
235 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
240 public void addModifyListener(ModifyListener listener) {
\r
241 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
246 public void addFocusListener(FocusListener listener) {
\r
247 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
251 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
252 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r