import org.simantics.h2d.node.ITextListener;\r
import org.simantics.h2d.node.TextNode;\r
import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedElement;\r
import org.simantics.objmap.annotations.RelatedValue;\r
import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.sysdyn.ui.equation.expressions.ExpressionView;\r
\r
@GraphType("http://www.simantics.org/Sysdyn#Auxiliary")\r
public class TextElement extends RectangularElement implements Movable, Connectable, IEventHandler {\r
\r
@RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasType")\r
public String type = "Real";\r
+ \r
+ @RelatedElement("http://www.simantics.org/Sysdyn#HasExpression")\r
+ public ExpressionView expression;\r
\r
// Auxiliary fields\r
double textX;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.events.FocusEvent;\r
import org.eclipse.swt.events.FocusListener;\r
+import org.eclipse.swt.events.KeyAdapter;\r
+import org.eclipse.swt.events.KeyEvent;\r
import org.eclipse.swt.events.MouseEvent;\r
import org.eclipse.swt.events.MouseListener;\r
import org.eclipse.swt.graphics.Font;\r
+import org.eclipse.swt.graphics.Point;\r
import org.eclipse.swt.layout.RowLayout;\r
import org.eclipse.swt.widgets.Combo;\r
import org.eclipse.swt.widgets.Composite;\r
import org.simantics.db.Builtins;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
+import org.simantics.db.exception.ServiceException;\r
import org.simantics.db.procedure.Listener;\r
import org.simantics.db.request.Read;\r
import org.simantics.sysdyn.SysdynResource;\r
Resource variable;\r
ArrayList<String> dependencies;\r
boolean disposed = false;\r
+ \r
+ Composite parent;\r
\r
Text nameText;\r
\r
Composite variableType;\r
Composite emptyComposite;\r
ExpressionView expressionView;\r
+ Composite unitComposite;\r
+ UnitSelector unitSelector;\r
\r
org.eclipse.ui.IPartListener2 focusLostListener;\r
\r
@Override \r
public void createPartControl(Composite parent) {\r
+ this.parent = parent;\r
// Listeners\r
getSite().getPage().addPostSelectionListener(this);\r
focusLostListener = new org.eclipse.ui.IPartListener2()\r
public void partDeactivated(IWorkbenchPartReference partRef)\r
{\r
if(partRef.getPart(false) instanceof EquationView) {\r
+ // Save expressions\r
if(expressionView != null) expressionView.save();\r
+ // Save unit\r
+ if(unitSelector != null) unitSelector.save();\r
}\r
+\r
}\r
public void partClosed(IWorkbenchPartReference partRef) {}\r
public void partBroughtToTop(IWorkbenchPartReference partRef) {}\r
// Upper part of the view: Name (and renaming)\r
nameText = new Text(parent, SWT.SINGLE | SWT.BORDER);\r
nameText.setFont(FONT);\r
- GridDataFactory.fillDefaults().span(2, 1).applyTo(nameText);\r
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);\r
\r
+ // After the variable name: Variable type\r
+ variableType = new Composite(parent, SWT.NONE);\r
+ GridLayoutFactory.fillDefaults().numColumns(3).spacing(3, 0).applyTo(variableType);\r
+ GridDataFactory.fillDefaults().applyTo(variableType);\r
+ Label label = new Label(variableType, SWT.SINGLE);\r
+ label.setText("Type:");\r
+ Combo comboDropDown = new Combo(variableType, SWT.DROP_DOWN | SWT.BORDER);\r
+ comboDropDown.add("Auxiliary");\r
+ comboDropDown.add("Parameter");\r
+ comboDropDown.add("Constant");\r
+\r
+ unitComposite = new Composite(variableType, SWT.NONE);\r
+ GridDataFactory.fillDefaults().applyTo(unitComposite);\r
+ GridLayoutFactory.fillDefaults().applyTo(unitComposite);\r
+ unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable);\r
+ \r
// Center side of the view: (related) variables and possible functions\r
TabFolder tabFolder = new TabFolder (parent, SWT.NONE);\r
GridDataFactory.fillDefaults().span(1, 4).applyTo(tabFolder);\r
\r
@Override\r
public void mouseDoubleClick(MouseEvent e) {\r
- TableItem[] selection = variableTable.getSelection();\r
- if(selection.length > 0) {\r
- final String var = selection[0].getText();\r
+ TableItem item = variableTable.getItem(new Point(e.x, e.y));\r
+ if(item != null) {\r
+ final String var = item.getText();\r
expressionView.getDisplay().asyncExec(new Runnable() {\r
\r
@Override\r
}\r
\r
@Override\r
- public void mouseDown(MouseEvent e) {}\r
+ public void mouseDown(MouseEvent e) {\r
+ expressionView.focus();\r
+ }\r
\r
@Override\r
- public void mouseUp(MouseEvent e) {}\r
+ public void mouseUp(MouseEvent e) {\r
+ expressionView.focus();\r
+ }\r
\r
});\r
\r
GridDataFactory.fillDefaults().span(1,4).grab(true, true).applyTo(emptyComposite);\r
GridLayoutFactory.fillDefaults().applyTo(emptyComposite);\r
\r
-\r
- // Below the variable name: Variable type\r
- variableType = new Composite(parent, SWT.NONE);\r
- GridLayoutFactory.fillDefaults().numColumns(5).spacing(3, 0).applyTo(variableType);\r
- GridDataFactory.fillDefaults().span(2, 1).applyTo(variableType);\r
- Label label = new Label(variableType, SWT.SINGLE);\r
- label.setText("Type:");\r
- Combo comboDropDown = new Combo(variableType, SWT.DROP_DOWN | SWT.BORDER);\r
- comboDropDown.add("Auxiliary");\r
- comboDropDown.add("Lookup");\r
- comboDropDown.add("Delay");\r
- comboDropDown = new Combo(variableType, SWT.DROP_DOWN | SWT.BORDER);\r
- comboDropDown.add("Normal");\r
- comboDropDown.add("With Lookup");\r
-\r
- label = new Label(variableType, SWT.SINGLE );\r
- label.setText("Unit:");\r
- label.setAlignment(SWT.RIGHT);\r
- GridDataFactory.fillDefaults().grab(true, false).align(SWT.END, SWT.CENTER).applyTo(label);\r
- comboDropDown = new Combo(variableType, SWT.DROP_DOWN | SWT.BORDER);\r
- comboDropDown.add("m");\r
- comboDropDown.add("V");\r
- comboDropDown.add("euro");\r
-\r
- // Below variable type: Equation\r
+ // Below variable name: Equation\r
equationComposite = new Composite(parent, SWT.NONE);\r
GridDataFactory.fillDefaults().span(2, 1).grab(true, true).applyTo(equationComposite);\r
GridLayoutFactory.fillDefaults().applyTo(equationComposite);\r
-\r
-\r
+ expressionView = new NormalExpressionView(equationComposite, SWT.NONE, variable); \r
\r
// Lower part of the view: Units and Range\r
unitsAndRange = new Composite(parent, SWT.NONE); \r
a.type = "NORMAL";\r
else if (graph.isInstanceOf(variable, sr.Stock))\r
a.type = "STOCK";\r
- \r
+\r
return a;\r
}\r
}\r
}\r
nameText.setText(result.name);\r
\r
+ if(unitSelector!= null) unitSelector.dispose();\r
+ unitSelector = new UnitSelector(unitComposite, SWT.NONE, variable);\r
+ unitComposite.layout();\r
+ \r
for(Control c : equationComposite.getChildren()) {\r
c.dispose();\r
- } \r
+ } \r
\r
ExpressionView ev = null;\r
if (result.type.equalsIgnoreCase("NORMAL"))\r
ev = new StockExpressionView(equationComposite, SWT.NONE, variable);\r
expressionView = ev;\r
equationComposite.layout();\r
-\r
}\r
}); \r
\r
\r
@Override\r
public void setFocus() {\r
- //text.setFocus();\r
+ if(expressionView != null)\r
+ expressionView.focus();\r
}\r
\r
// Returns the names of the related variables (dependencies)\r
Collection<Resource> dependencies = graph.getObjects(r, sr.IsHeadOf);\r
\r
for(Resource d : dependencies) {\r
- Resource tail = graph.getPossibleObject(d, sr.HasTail);\r
- if(tail != null) {\r
- Object name = graph.getPossibleRelatedValue(tail, b.HasName);\r
- if(name != null)\r
- variables.add((String)name);\r
+ if(graph.isInstanceOf(d, sr.Dependency)) {\r
+ Resource tail = graph.getPossibleObject(d, sr.HasTail);\r
+ if(tail != null) {\r
+ Object name = graph.getPossibleRelatedValue(tail, b.HasName);\r
+ if(name != null)\r
+ variables.add((String)name);\r
+ }\r
}\r
}\r
\r
--- /dev/null
+package org.simantics.sysdyn.ui.equation;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.FontMetrics;\r
+import org.eclipse.swt.graphics.GC;\r
+import org.eclipse.swt.widgets.Combo;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class UnitSelector extends Composite {\r
+\r
+ Combo unitCombo;\r
+ Resource variable;\r
+ String unit = "";\r
+\r
+ public UnitSelector(Composite parent, int style, final Resource variable) {\r
+ super(parent, style);\r
+ this.variable = variable;\r
+ GridDataFactory.fillDefaults().applyTo(this);\r
+ GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this);\r
+\r
+ if(variable != null) {\r
+ try {\r
+ SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ unit = graph.getPossibleRelatedValue(variable, sr.HasUnit);\r
+ return null;\r
+ }\r
+ });\r
+ } catch (DatabaseException e1) {\r
+ e1.printStackTrace();\r
+ }\r
+ }\r
+\r
+\r
+ Label label = new Label(this, SWT.SINGLE );\r
+ label.setFont(EquationView.FONT);\r
+ label.setText("Unit:");\r
+ //label.setAlignment(SWT.RIGHT);\r
+ GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
+\r
+\r
+ unitCombo = new Combo(this, SWT.DROP_DOWN | SWT.BORDER);\r
+ unitCombo.setFont(EquationView.FONT);\r
+ \r
+ int columns = 6;\r
+ GC gc = new GC (unitCombo);\r
+ FontMetrics fm = gc.getFontMetrics ();\r
+ int width = columns * fm.getAverageCharWidth ();\r
+ int height = fm.getHeight ();\r
+ gc.dispose (); \r
+ GridDataFactory.fillDefaults().hint(unitCombo.computeSize(width, height)).applyTo(unitCombo);\r
+ \r
+ unitCombo.add("m");\r
+ unitCombo.add("V");\r
+ unitCombo.add("euro");\r
+ if (unit != null)\r
+ if(unitCombo.indexOf(unit) >= 0)\r
+ unitCombo.select(unitCombo.indexOf(unit));\r
+ else\r
+ unitCombo.setText(unit);\r
+ }\r
+\r
+ public void save(){\r
+ final String unitNew = unitCombo.getText();\r
+ if(unitNew != null && !unitNew.equals(unit)) {\r
+ SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph g)\r
+ throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(g);\r
+ if (!unitNew.equals(""))\r
+ g.claimValue(variable, sr.HasUnit, unitNew);\r
+ else if (g.getPossibleRelatedValue(variable, sr.HasUnit) != null)\r
+ g.deny(variable, sr.HasUnit);\r
+ }\r
+ });\r
+ }\r
+\r
+ }\r
+\r
+}\r
\r
abstract public void save();\r
abstract public void replaceSelection(String text);\r
+ abstract public void focus();\r
}\r
public NormalExpressionView(Composite parent, int style, final Resource variable) {\r
super(parent, style, variable);\r
\r
- try {\r
- SimanticsUI.getSession().syncRequest(new Read<String>() {\r
-\r
- @Override\r
- public String perform(ReadGraph graph) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
- if (expression != null && graph.isInstanceOf(expression, sr.NormalExpression)) {\r
- equation = graph.getRelatedValue(expression, sr.HasEquation);\r
- } else {\r
- equation = "";\r
+ if (variable != null) {\r
+ try {\r
+ SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
+ if (expression != null && graph.isInstanceOf(expression, sr.NormalExpression)) {\r
+ equation = graph.getRelatedValue(expression, sr.HasEquation);\r
+ } else {\r
+ equation = "";\r
+ }\r
+ return null;\r
}\r
- return null;\r
- }\r
\r
- });\r
- } catch (DatabaseException e1) {\r
- e1.printStackTrace();\r
+ });\r
+ } catch (DatabaseException e1) {\r
+ e1.printStackTrace();\r
+ }\r
+ } else {\r
+ equation = "";\r
}\r
\r
GridDataFactory.fillDefaults().grab(true, true).applyTo(this);\r
expression.setFont(FONT);\r
expression.setText(equation);\r
GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
- \r
+\r
expression.addFocusListener(new FocusAdapter() {\r
\r
@Override\r
}\r
\r
}\r
- \r
+\r
public void replaceSelection(String text) {\r
String oldExpression = expression.getText();\r
String newExpression = oldExpression.substring(0, lastSelection.x) + text + oldExpression.substring(lastSelection.y);\r
expression.setText(newExpression);\r
+ expression.setSelection(lastSelection.x + text.length());\r
+ }\r
+\r
+ public void focus() {\r
+ expression.forceFocus();\r
}\r
}\r
package org.simantics.sysdyn.ui.equation.expressions;\r
\r
+import java.util.Collection;\r
+\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
import org.eclipse.swt.SWT;\r
@GraphType("http://www.simantics.org/Sysdyn#StockExpression")\r
public class StockExpressionView extends ExpressionView {\r
\r
+ Label integralLabel;\r
+ Text integral;\r
Label equationLabel;\r
Text expression;\r
Point lastSelection = new Point(0,0);\r
\r
GridDataFactory.fillDefaults().grab(true, true).applyTo(this);\r
GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this);\r
+\r
+ integralLabel = new Label(this, SWT.NONE);\r
+ integralLabel.setFont(FONT);\r
+ integralLabel.setText("Integral");\r
+ GridDataFactory.fillDefaults().applyTo(integralLabel);\r
+ \r
+ integral = new Text(this, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
+ integral.setEditable(false);\r
+ integral.setFont(FONT);\r
+ integral.setText(getIntegral());\r
+ GridDataFactory.fillDefaults().grab(true, true).applyTo(integral);\r
+ \r
equationLabel = new Label(this, SWT.NONE);\r
equationLabel.setFont(FONT);\r
equationLabel.setText("Initial\nValue");\r
public void replaceSelection(String text) {\r
String oldExpression = expression.getText();\r
String newExpression = oldExpression.substring(0, lastSelection.x) + text + oldExpression.substring(lastSelection.y);\r
- expression.setText(newExpression); \r
+ expression.setText(newExpression); \r
+ expression.setSelection(lastSelection.x + text.length());\r
}\r
\r
@Override\r
}\r
\r
});\r
- }\r
+ } \r
+ }\r
+ \r
+ public void focus() {\r
+ expression.forceFocus();\r
+ }\r
+ \r
+ private String getIntegral() {\r
+ String integral = "";\r
+ try {\r
+ integral = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
\r
- \r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Builtins b = graph.getBuiltins();\r
+ Collection<Resource> heads = graph.getObjects(variable, sr.IsHeadOf);\r
+ Collection<Resource> tails = graph.getObjects(variable, sr.IsTailOf);\r
+ \r
+ StringBuilder builder = new StringBuilder();\r
+ builder.append("");\r
+ for (Resource r : heads) {\r
+ if(graph.isInstanceOf(r, sr.Flow)) {\r
+ Resource tail = graph.getPossibleObject(r, sr.HasTail);\r
+ if(tail != null) {\r
+ Object name = graph.getPossibleRelatedValue(tail, b.HasName);\r
+ if (name != null) \r
+ builder.append(" + " + name);\r
+ }\r
+ } \r
+ }\r
+ for (Resource r : tails) {\r
+ if(graph.isInstanceOf(r, sr.Flow)) {\r
+ Resource head = graph.getPossibleObject(r, sr.HasHead);\r
+ if(head != null) {\r
+ Object name = graph.getPossibleRelatedValue(head, b.HasName);\r
+ if (name != null) \r
+ builder.append(" - " + name);\r
+ }\r
+ } \r
+ }\r
+ if (builder.indexOf(" + ") == 0)\r
+ builder.delete(0, 3);\r
+ return builder.toString().trim();\r
+ }\r
+ \r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ } \r
+ return integral;\r
}\r
\r
}\r
public final Resource HasInitialEquation;\r
public final Resource HasTail;\r
public final Resource HasType;\r
+ public final Resource HasUnit;\r
public final Resource HasX;\r
public final Resource HasY;\r
public final Resource IndependentVariable;\r
public static final String HasInitialEquation = "http://www.simantics.org/Sysdyn#HasInitialEquation";\r
public static final String HasTail = "http://www.simantics.org/Sysdyn#HasTail";\r
public static final String HasType = "http://www.simantics.org/Sysdyn#HasType";\r
+ public static final String HasUnit = "http://www.simantics.org/Sysdyn#HasUnit";\r
public static final String HasX = "http://www.simantics.org/Sysdyn#HasX";\r
public static final String HasY = "http://www.simantics.org/Sysdyn#HasY";\r
public static final String IndependentVariable = "http://www.simantics.org/Sysdyn#IndependentVariable";\r
HasInitialEquation = getResourceOrNull(graph, URIs.HasInitialEquation);\r
HasTail = getResourceOrNull(graph, URIs.HasTail);\r
HasType = getResourceOrNull(graph, URIs.HasType);\r
+ HasUnit = getResourceOrNull(graph, URIs.HasUnit);\r
HasX = getResourceOrNull(graph, URIs.HasX);\r
HasY = getResourceOrNull(graph, URIs.HasY);\r
IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable);\r
\r
HasType <R L0.HasProperty\r
L0.HasRange L0.String\r
- \r
+\r
+HasUnit <R L0.HasProperty\r
+ L0.HasRange L0.String\r
+\r
IndependentVariable <T Variable\r
[L0.HasName card "1"]\r
[HasType card "1"]\r
+ [HasUnit card "0..1"]\r
+ [HasExpression card "0..1"]\r
\r
\r
Auxiliary <T IndependentVariable\r
- [HasExpression card "0..1"]\r
+ \r
Stock <T IndependentVariable\r
- [HasExpression card "0..1"]\r
+\r
Valve <T IndependentVariable\r
- [HasExpression card "0..1"]\r
+\r
Cloud <T IndependentVariable \r
\r
######################################################################\r