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.Text;
\r
31 import org.simantics.db.ReadGraph;
\r
32 import org.simantics.db.Resource;
\r
33 import org.simantics.db.WriteGraph;
\r
34 import org.simantics.db.common.request.WriteRequest;
\r
35 import org.simantics.db.common.utils.OrderedSetUtils;
\r
36 import org.simantics.db.exception.DatabaseException;
\r
37 import org.simantics.db.request.Read;
\r
38 import org.simantics.layer0.Layer0;
\r
39 import org.simantics.sysdyn.SysdynResource;
\r
40 import org.simantics.sysdyn.manager.SysdynModel;
\r
41 import org.simantics.sysdyn.manager.SysdynModelManager;
\r
42 import org.simantics.sysdyn.representation.Stock;
\r
43 import org.simantics.sysdyn.representation.Valve;
\r
44 import org.simantics.ui.SimanticsUI;
\r
46 public class StockExpression implements IExpression {
\r
48 private Text integral;
\r
49 private ExpressionField expression;
\r
52 public void createExpressionFields(Composite parent, Map<String, Object> data) {
\r
53 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
54 String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
55 String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";
\r
58 Label label = new Label(parent, SWT.NONE);
\r
59 label.setText("Integral");
\r
61 integral = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
\r
62 integral.setEditable(false);
\r
63 integral.setText(integralEquation);
\r
64 GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);
\r
67 label = new Label(parent, SWT.NONE);
\r
68 label.setText("Initial\nValue");
\r
70 expression = new ExpressionField(parent, SWT.BORDER);
\r
71 expression.setExpression(initialEquation);
\r
73 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
78 public void focus() {
\r
79 this.expression.focus();
\r
83 public List<ExpressionField> getExpressionFields() {
\r
84 return Arrays.asList(this.expression);
\r
88 public void readData(final Resource expression, Map<String, Object> data) {
\r
89 String initialEquation = null;
\r
91 if (expression != null && data.get("initialEquation") == null) {
\r
93 initialEquation = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
96 public String perform(ReadGraph graph) throws DatabaseException {
\r
97 SysdynResource sr = SysdynResource.getInstance(graph);
\r
98 if (graph.isInstanceOf(expression, sr.StockExpression)) {
\r
99 return graph.getRelatedValue(expression, sr.HasInitialEquation);
\r
106 } catch (DatabaseException e1) {
\r
107 e1.printStackTrace();
\r
109 data.put("initialEquation", initialEquation);
\r
112 data.put("integral", getIntegral(expression));
\r
117 public void replaceSelection(String var) {
\r
118 if(expression != null) {
\r
119 IDocument doc = expression.getDocument();
\r
121 Point selection = expression.getSelection();
\r
122 doc.replace(selection.x, selection.y, var);
\r
123 expression.setSelection(selection.x + var.length());
\r
124 } catch (BadLocationException e) {
\r
125 e.printStackTrace();
\r
131 public void save(final Resource expression, Map<String, Object> data) {
\r
132 final String currentText = this.expression.getExpression();
\r
133 if(currentText != null) {
\r
134 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
137 public void perform(WriteGraph g)
\r
138 throws DatabaseException {
\r
139 SysdynResource sr = SysdynResource.getInstance(g);
\r
140 Layer0 l0 = Layer0.getInstance(g);
\r
142 if(!g.isInstanceOf(expression, sr.StockExpression)) {
\r
143 Resource expressionList = g.getSingleObject(expression, l0.HasNext);
\r
144 Resource temp = g.newResource();
\r
145 OrderedSetUtils.replace(g, expressionList, expression, temp);
\r
146 for(Resource predicate : g.getPredicates(expression)) {
\r
147 g.deny(expression, predicate);
\r
149 g.claim(expression, l0.InstanceOf, null, sr.StockExpression);
\r
150 OrderedSetUtils.replace(g, expressionList, temp, expression);
\r
152 g.claimLiteral(expression, sr.HasInitialEquation, currentText);
\r
157 this.expression.setExpression(currentText);
\r
161 public void updateData(Map<String, Object> data) {
\r
162 if(this.expression != null && this.expression.getExpression() != null)
\r
163 data.put("initialEquation", this.expression.getExpression());
\r
164 if(this.integral != null && this.integral.getText() != null)
\r
165 data.put("integral", this.integral.getText());
\r
169 private String getIntegral(final Resource expression) {
\r
171 String integral = "";
\r
172 if(expression == null)
\r
175 integral = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
178 public String perform(ReadGraph graph) throws DatabaseException {
\r
179 SysdynResource sr = SysdynResource.getInstance(graph);
\r
180 Layer0 l0 = Layer0.getInstance(graph);
\r
182 // find the variable
\r
183 Resource expressionList = OrderedSetUtils.getSingleOwnerList(graph, expression);
\r
184 Resource variable = graph.getPossibleObject(expressionList, sr.HasExpressions_Inverse);
\r
185 if(variable == null)
\r
188 SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());
\r
189 SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));
\r
190 model.update(graph);
\r
192 Stock stock = (Stock)model.getElement(variable);
\r
194 String range = graph.getPossibleRelatedValue(expression, sr.HasArrayRange);
\r
198 StringBuilder builder = new StringBuilder();
\r
199 builder.append("");
\r
200 for(Valve in : stock.getIncomingValves()) {
\r
201 builder.append(" + " + in.getName() + range);
\r
203 for(Valve out : stock.getOutgoingValves()) {
\r
204 builder.append(" - " + out.getName() + range);
\r
206 if (builder.indexOf(" + ") == 0)
\r
207 builder.delete(0, 3);
\r
209 return builder.toString().trim();
\r
213 } catch (DatabaseException e) {
\r
214 e.printStackTrace();
\r
220 public void addKeyListener(KeyListener listener) {
\r
221 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
226 public void addModifyListener(ModifyListener listener) {
\r
227 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
232 public void addFocusListener(FocusListener listener) {
\r
233 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
237 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
238 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r