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.jface.text.IUndoManager;
\r
23 import org.eclipse.jface.text.TextViewerUndoManager;
\r
24 import org.eclipse.swt.SWT;
\r
25 import org.eclipse.swt.custom.VerifyKeyListener;
\r
26 import org.eclipse.swt.events.FocusAdapter;
\r
27 import org.eclipse.swt.events.FocusEvent;
\r
28 import org.eclipse.swt.events.FocusListener;
\r
29 import org.eclipse.swt.events.KeyListener;
\r
30 import org.eclipse.swt.events.ModifyListener;
\r
31 import org.eclipse.swt.graphics.Point;
\r
32 import org.eclipse.swt.widgets.Button;
\r
33 import org.eclipse.swt.widgets.Composite;
\r
34 import org.eclipse.swt.widgets.Event;
\r
35 import org.eclipse.swt.widgets.Label;
\r
36 import org.eclipse.swt.widgets.Listener;
\r
37 import org.eclipse.swt.widgets.Table;
\r
38 import org.simantics.Simantics;
\r
39 import org.simantics.databoard.Bindings;
\r
40 import org.simantics.db.ReadGraph;
\r
41 import org.simantics.db.Resource;
\r
42 import org.simantics.db.WriteGraph;
\r
43 import org.simantics.db.common.CommentMetadata;
\r
44 import org.simantics.db.common.request.ReadRequest;
\r
45 import org.simantics.db.common.request.WriteRequest;
\r
46 import org.simantics.db.common.utils.ListUtils;
\r
47 import org.simantics.db.exception.DatabaseException;
\r
48 import org.simantics.db.layer0.util.Layer0Utils;
\r
49 import org.simantics.db.layer0.util.RemoverUtil;
\r
50 import org.simantics.db.request.Read;
\r
51 import org.simantics.layer0.Layer0;
\r
52 import org.simantics.sysdyn.SysdynResource;
\r
53 import org.simantics.sysdyn.manager.SysdynModel;
\r
54 import org.simantics.sysdyn.manager.SysdynModelManager;
\r
55 import org.simantics.sysdyn.representation.Stock;
\r
56 import org.simantics.sysdyn.representation.Valve;
\r
57 import org.simantics.utils.datastructures.Pair;
\r
59 public class StockExpression implements IExpression {
\r
61 private final ExpressionWidgetInput input;
\r
62 private ExpressionField integralExpression, initialExpression;
\r
63 private ExpressionField lastSelectedText;
\r
64 private TextViewerUndoManager undoManager;
\r
66 public StockExpression(ExpressionWidgetInput input) {
\r
71 public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {
\r
72 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);
\r
73 String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";
\r
74 String integralEquation = data.get("integral") != null ? (String)data.get("integral") : getDefaultIntegral(input.expression);
\r
77 Label label = new Label(parent, SWT.NONE);
\r
78 label.setText("Integral");
\r
80 Composite integralComposite = new Composite(parent, SWT.NONE);
\r
81 GridDataFactory.fillDefaults().grab(true, true).applyTo(integralComposite);
\r
82 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(integralComposite);
\r
84 integralExpression = new ExpressionField(integralComposite, SWT.BORDER, allowedVariables, true, input);
\r
85 integralExpression.setExpression(integralEquation);
\r
86 GridDataFactory.fillDefaults().grab(true, true).applyTo(integralExpression);
\r
87 integralExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
90 public void focusLost(FocusEvent e) {
\r
91 lastSelectedText = integralExpression;
\r
95 undoManager = new TextViewerUndoManager(100);
\r
96 integralExpression.getSourceViewer().setUndoManager(undoManager);
\r
97 undoManager.connect(integralExpression.getSourceViewer());
\r
99 Button defaultButton = new Button(integralComposite, SWT.PUSH);
\r
100 defaultButton.setText("Use default");
\r
101 defaultButton.addListener(SWT.Selection, new Listener() {
\r
104 public void handleEvent(Event event) {
\r
105 switch (event.type) {
\r
106 case SWT.Selection:
\r
107 StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression));
\r
108 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
111 public void perform(WriteGraph graph)
\r
112 throws DatabaseException {
\r
113 // Delete the possible integral expression from the database to note
\r
114 // that we are in the "default" mode.
\r
115 graph.markUndoPoint();
\r
116 SysdynResource sr = SysdynResource.getInstance(graph);
\r
117 graph.deny(input.expression, sr.StockExpression_useCustomIntegral);
\r
118 graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));
\r
120 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
121 graph.addMetadata(cm.add("Use default"));
\r
131 label = new Label(parent, SWT.NONE);
\r
132 label.setText("Initial\nValue");
\r
134 initialExpression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true, input);
\r
135 initialExpression.setExpression(initialEquation);
\r
136 GridDataFactory.fillDefaults().grab(true, true).applyTo(initialExpression);
\r
137 initialExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
140 public void focusLost(FocusEvent e) {
\r
141 lastSelectedText = initialExpression;
\r
145 lastSelectedText = initialExpression;
\r
149 public void focus() {
\r
150 this.lastSelectedText.focus();
\r
154 public List<ExpressionField> getExpressionFields() {
\r
155 return Arrays.asList(this.integralExpression, this.initialExpression);
\r
159 public void readData(final Resource expression, final Map<String, Object> data) {
\r
160 Pair<String, String> equations = null;
\r
161 if (expression != null && data.get("initialEquation") == null) {
\r
163 equations = Simantics.getSession().syncRequest(new Read<Pair<String, String>>() {
\r
166 public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {
\r
167 SysdynResource sr = SysdynResource.getInstance(graph);
\r
168 if (graph.isInstanceOf(expression, sr.StockExpression)) {
\r
169 String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);
\r
170 String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation);
\r
171 initialEquation = (initialEquation != null) ? initialEquation : "";
\r
172 return new Pair<String, String>(integralEquation, initialEquation);
\r
174 return new Pair<String, String>(null, "");
\r
179 } catch (DatabaseException e1) {
\r
180 e1.printStackTrace();
\r
182 data.put("initialEquation", equations.second);
\r
185 if (equations.first == null) {
\r
187 Simantics.getSession().syncRequest(new WriteRequest() {
\r
190 public void perform(WriteGraph graph) throws DatabaseException {
\r
191 SysdynResource sr = SysdynResource.getInstance(graph);
\r
192 graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));
\r
193 graph.markUndoPoint();
\r
194 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
\r
195 graph.addMetadata(cm.add("Set expression"));
\r
199 } catch (DatabaseException e) {
\r
200 // TODO Auto-generated catch block
\r
201 e.printStackTrace();
\r
204 data.put("integral", getDefaultIntegral(expression));
\r
207 final String integral = equations.first;
\r
208 Simantics.getSession().syncRequest(new ReadRequest() {
\r
211 public void run(ReadGraph graph) throws DatabaseException {
\r
212 SysdynResource sr = SysdynResource.getInstance(graph);
\r
213 if (graph.hasStatement(input.expression, sr.StockExpression_useCustomIntegral)) {
\r
214 data.put("integral", integral);
\r
216 data.put("integral", getDefaultIntegral(graph, expression));
\r
221 } catch (DatabaseException e) {
\r
222 data.put("integral", equations.first);
\r
223 e.printStackTrace();
\r
230 public void replaceSelection(String var) {
\r
231 if(lastSelectedText != null) {
\r
232 IDocument doc = lastSelectedText.getDocument();
\r
234 Point selection = lastSelectedText.getSelection();
\r
235 doc.replace(selection.x, selection.y, var);
\r
236 lastSelectedText.setSelection(selection.x + var.length());
\r
237 } catch (BadLocationException e) {
\r
238 e.printStackTrace();
\r
244 public void save(final Resource expression, final Map<String, Object> data) {
\r
245 final String currentInitial = this.initialExpression.getExpression();
\r
246 final String currentIntegral = this.integralExpression.getExpression();
\r
247 if (currentIntegral == null) {
\r
248 this.integralExpression.setExpression(getDefaultIntegral(expression));
\r
250 if(!currentInitial.equals(data.get("initialEquation"))
\r
251 || currentIntegral == null
\r
252 || !currentIntegral.equals(data.get("integral"))) {
\r
253 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
256 public void perform(WriteGraph g)
\r
257 throws DatabaseException {
\r
258 SysdynResource sr = SysdynResource.getInstance(g);
\r
259 Layer0 l0 = Layer0.getInstance(g);
\r
261 if(!g.isInstanceOf(expression, sr.StockExpression)) {
\r
262 Resource variable = g.getPossibleObject(expression, l0.PartOf);
\r
263 Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList);
\r
264 Resource temp = g.newResource();
\r
265 ListUtils.replace(g, expressionList, expression, temp);
\r
267 for(Resource predicate : g.getPredicates(expression)) {
\r
268 g.deny(expression, predicate);
\r
270 g.claim(expression, l0.InstanceOf, null, sr.StockExpression);
\r
272 ListUtils.replace(g, expressionList, temp, expression);
\r
274 RemoverUtil.remove(g, temp);
\r
276 g.claim(expression, l0.PartOf, variable);
\r
278 g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial);
\r
280 if (!currentIntegral.equals(g.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation, Bindings.STRING))) {
\r
281 // If the value is not same as default, enable the custom tag
\r
282 g.claim(expression, sr.StockExpression_useCustomIntegral, expression);
\r
284 g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);
\r
290 Layer0Utils.addCommentMetadata(g, "Saved Stock Expression " + currentIntegral + " " + expression.toString() + " with Initial Value " + currentInitial);
\r
295 this.initialExpression.setExpression(currentInitial);
\r
296 this.integralExpression.setExpression(currentIntegral);
\r
300 public void updateData(Map<String, Object> data) {
\r
301 if(this.initialExpression != null && this.initialExpression.getExpression() != null)
\r
302 data.put("initialEquation", this.initialExpression.getExpression());
\r
303 if(this.integralExpression != null && this.integralExpression.getExpression() != null)
\r
304 data.put("integral", this.integralExpression.getExpression());
\r
308 private static String getDefaultIntegral(ReadGraph graph, Resource expression) throws DatabaseException {
\r
309 SysdynResource sr = SysdynResource.getInstance(graph);
\r
310 Layer0 l0 = Layer0.getInstance(graph);
\r
312 // find the variable
\r
313 Resource variable = graph.getPossibleObject(expression, l0.PartOf);
\r
314 if(variable == null)
\r
317 SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());
\r
318 SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));
\r
319 model.update(graph);
\r
321 Stock stock = (Stock)model.getElement(variable);
\r
323 String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);
\r
327 StringBuilder builder = new StringBuilder();
\r
328 builder.append("");
\r
329 for(Valve in : stock.getIncomingValves()) {
\r
330 builder.append(" + " + in.getName() + range);
\r
332 for(Valve out : stock.getOutgoingValves()) {
\r
333 builder.append(" - " + out.getName() + range);
\r
335 if (builder.indexOf(" + ") == 0)
\r
336 builder.delete(0, 3);
\r
338 return builder.toString().trim();
\r
342 private static String getDefaultIntegral(final Resource expression) {
\r
344 String integral = "";
\r
345 if(expression == null)
\r
348 integral = Simantics.getSession().syncRequest(new Read<String>() {
\r
351 public String perform(ReadGraph graph) throws DatabaseException {
\r
352 return getDefaultIntegral(graph, expression);
\r
356 } catch (DatabaseException e) {
\r
357 e.printStackTrace();
\r
363 public void addKeyListener(KeyListener listener) {
\r
364 this.initialExpression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
365 this.integralExpression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
370 public void addModifyListener(ModifyListener listener) {
\r
371 this.initialExpression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
372 this.integralExpression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
377 public void addFocusListener(FocusListener listener) {
\r
378 this.initialExpression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
379 this.integralExpression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
383 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
384 this.initialExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
385 this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
389 public IUndoManager getUndoManager() {
\r
390 return undoManager;
\r