Connectable tail;\r
@RelatedElement("http://www.simantics.org/Sysdyn#HasHead")\r
Connectable head;\r
- double angle = 0.1;\r
\r
// Auxiliary \r
double angle0;\r
// Properties\r
@RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName")\r
public String label = "Unnamed";\r
+ \r
+ @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasType")\r
+ public String type = "Real";\r
\r
// Auxiliary fields\r
double textX;\r
import org.eclipse.jface.viewers.ISelection;\r
import org.eclipse.jface.viewers.IStructuredSelection;\r
import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.FocusAdapter;\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.layout.RowLayout;\r
import org.eclipse.swt.widgets.Combo;\r
import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
import org.eclipse.swt.widgets.Label;\r
import org.eclipse.swt.widgets.TabFolder;\r
import org.eclipse.swt.widgets.TabItem;\r
import org.eclipse.swt.widgets.Text;\r
import org.eclipse.ui.ISelectionListener;\r
import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.IWorkbenchPartReference;\r
import org.eclipse.ui.part.ViewPart;\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.procedure.Listener;\r
import org.simantics.db.request.Read;\r
import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.equation.expressions.ExpressionView;\r
+import org.simantics.sysdyn.ui.equation.expressions.NormalExpressionView;\r
+import org.simantics.sysdyn.ui.equation.expressions.StockExpressionView;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.utils.ui.ExceptionUtils;\r
\r
Text nameText;\r
\r
Composite equationComposite;\r
- Label equationLabel;\r
- Text text;\r
\r
String originalText;\r
TabFolder tabFolder;\r
TabItem functions;\r
Composite unitsAndRange;\r
Composite variableType;\r
+ Composite emptyComposite;\r
+ ExpressionView expressionView;\r
\r
+ org.eclipse.ui.IPartListener2 focusLostListener;\r
\r
@Override \r
public void createPartControl(Composite parent) {\r
+ // Listeners\r
getSite().getPage().addPostSelectionListener(this);\r
+ focusLostListener = new org.eclipse.ui.IPartListener2()\r
+ {\r
+ public void partInputChanged(IWorkbenchPartReference partRef) {}\r
+ public void partVisible(IWorkbenchPartReference partRef) {}\r
+ public void partHidden(IWorkbenchPartReference partRef) {}\r
+ public void partOpened(IWorkbenchPartReference partRef) {}\r
+ public void partDeactivated(IWorkbenchPartReference partRef)\r
+ {\r
+ if(partRef.getPart(false) instanceof EquationView) {\r
+ if(expressionView != null) expressionView.save();\r
+ }\r
+ }\r
+ public void partClosed(IWorkbenchPartReference partRef) {}\r
+ public void partBroughtToTop(IWorkbenchPartReference partRef) {}\r
+ public void partActivated(IWorkbenchPartReference partRef) {}\r
+ };\r
+ getViewSite().getPage().addPartListener(focusLostListener);\r
\r
GridLayoutFactory.fillDefaults().numColumns(4).margins(5, 5).applyTo(parent);\r
\r
+\r
+\r
// Upper part of the view: Name (and renaming)\r
nameText = new Text(parent, SWT.SINGLE | SWT.BORDER);\r
nameText.setFont(FONT);\r
variables.setText("Variables");\r
variableTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
variableTable.addMouseListener(new MouseListener(){\r
- \r
+\r
@Override\r
public void mouseDoubleClick(MouseEvent e) {\r
TableItem[] selection = variableTable.getSelection();\r
- final String var = selection[0].getText();\r
- text.getDisplay().asyncExec(new Runnable() {\r
- \r
- @Override\r
- public void run() {\r
- int pos = text.getCaretPosition();\r
- String middle = var;\r
- String start = text.getText(0, pos -1);\r
- String end = text.getText(pos, text.getCharCount()-1);\r
- if (pos != 0 && !text.getText(pos-1, pos).equals(" "))\r
- middle = " " + middle;\r
- if (pos != text.getCharCount() && !text.getText(pos, pos+1).equals(" "))\r
- middle = middle + " ";\r
- text.setText(start + middle + end);\r
- text.forceFocus();\r
- text.setSelection(pos + middle.length());\r
- }\r
- });\r
- }\r
-\r
- @Override\r
- public void mouseDown(MouseEvent e) {\r
+ if(selection.length > 0) {\r
+ final String var = selection[0].getText();\r
+ expressionView.getDisplay().asyncExec(new Runnable() {\r
\r
+ @Override\r
+ public void run() {\r
+ expressionView.replaceSelection(var);\r
+ }\r
+ });\r
+ }\r
}\r
\r
@Override\r
- public void mouseUp(MouseEvent e) {\r
+ public void mouseDown(MouseEvent e) {}\r
\r
- }\r
+ @Override\r
+ public void mouseUp(MouseEvent e) {}\r
\r
});\r
+\r
variableTable.addFocusListener(new FocusListener() {\r
\r
@Override\r
public void focusLost(FocusEvent e) {\r
variableTable.deselectAll();\r
}\r
- \r
+\r
});\r
variables.setControl(variableTable);\r
\r
functions.setControl(table);\r
\r
// Right side of the view: Details, comments\r
- Composite emptyComposite = new Composite(parent, SWT.NONE);\r
+ emptyComposite = new Composite(parent, SWT.NONE);\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
// Below variable type: Equation\r
equationComposite = new Composite(parent, SWT.NONE);\r
GridDataFactory.fillDefaults().span(2, 1).grab(true, true).applyTo(equationComposite);\r
- GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(equationComposite);\r
- equationLabel = new Label(equationComposite, SWT.NONE);\r
- equationLabel.setFont(FONT);\r
- equationLabel.setText("=");\r
- GridDataFactory.fillDefaults().applyTo(equationLabel);\r
-\r
- text = new Text(equationComposite, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
- text.setFont(FONT);\r
- GridDataFactory.fillDefaults().grab(true, true).applyTo(text);\r
- text.addFocusListener(new FocusAdapter() {\r
+ GridLayoutFactory.fillDefaults().applyTo(equationComposite);\r
\r
- @Override\r
- public void focusLost(FocusEvent e) {\r
- final String currentText = text.getText();\r
- if(originalText != null && !originalText.equals(currentText)) {\r
- SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
\r
- @Override\r
- public void perform(WriteGraph g)\r
- throws DatabaseException {\r
- Builtins b = g.getBuiltins();\r
- SysdynResource sr = SysdynResource.getInstance(g);\r
-\r
- Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
- if(expression == null) {\r
- expression = g.newResource();\r
- g.claim(expression, b.InstanceOf, sr.Expression);\r
- g.claim(variable, sr.HasExpression, expression);\r
- }\r
- g.claimValue(expression, sr.HasText, currentText);\r
- }\r
-\r
- });\r
- }\r
- } \r
-\r
- });\r
- text.addKeyListener(new KeyAdapter() { \r
-\r
- @Override\r
- public void keyPressed(KeyEvent e) {\r
- if(e.keyCode == SWT.ESC) {\r
- text.setText(originalText);\r
- } \r
- }\r
-\r
- });\r
\r
// Lower part of the view: Units and Range\r
unitsAndRange = new Composite(parent, SWT.NONE); \r
\r
static class Auxiliary {\r
ArrayList<String> dependencies;\r
- String eqLabel, name, value;\r
+ String name;\r
+ String type;\r
}\r
- \r
+\r
class UpdateViewRequest implements Read<Auxiliary> {\r
\r
Resource resource;\r
\r
@Override\r
public Auxiliary perform(ReadGraph graph) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
Builtins b = graph.getBuiltins();\r
Auxiliary a = new Auxiliary();\r
- if (graph.isInstanceOf(resource, sr.Auxiliary) || graph.isInstanceOf(resource, sr.Valve))\r
- a.eqLabel = "=";\r
- else if (graph.isInstanceOf(resource, sr.Stock))\r
- a.eqLabel = "initial =";\r
- else \r
- return null;\r
variable = resource;\r
a.name = graph.getRelatedValue(resource, b.HasName);\r
- Resource expression = graph.getPossibleObject(resource, sr.HasExpression); \r
- a.value = expression == null ? null :\r
- (String)graph.getPossibleRelatedValue(expression, sr.HasText);\r
a.dependencies = getDependencies(resource);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ if (graph.isInstanceOf(variable, sr.Auxiliary) || graph.isInstanceOf(variable, sr.Valve))\r
+ a.type = "NORMAL";\r
+ else if (graph.isInstanceOf(variable, sr.Stock))\r
+ a.type = "STOCK";\r
+ \r
return a;\r
}\r
-\r
-\r
-\r
}\r
\r
@Override\r
@Override\r
public void exception(Throwable t) {\r
// TODO Auto-generated method stub\r
- \r
+\r
}\r
\r
@Override\r
public void execute(final Auxiliary result) {\r
- text.getDisplay().asyncExec(new Runnable() {\r
+ equationComposite.getDisplay().asyncExec(new Runnable() {\r
\r
@Override\r
public void run(){\r
- \r
+\r
variableTable.removeAll();\r
TableItem item;\r
for(String d : result.dependencies) {\r
item = new TableItem(variableTable, SWT.NONE);\r
item.setText(d);\r
}\r
- \r
nameText.setText(result.name);\r
- equationLabel.setText(result.eqLabel);\r
- equationComposite.layout();\r
- if(result.value == null)\r
- originalText = "";\r
- else\r
- originalText = result.value;\r
- text.setText(originalText);\r
- \r
+\r
+ for(Control c : equationComposite.getChildren()) {\r
+ c.dispose();\r
+ } \r
\r
+ ExpressionView ev = null;\r
+ if (result.type.equalsIgnoreCase("NORMAL"))\r
+ ev = new NormalExpressionView(equationComposite, SWT.NONE, variable);\r
+ else if (result.type.equalsIgnoreCase("STOCK"))\r
+ ev = new StockExpressionView(equationComposite, SWT.NONE, variable);\r
+ expressionView = ev;\r
+ equationComposite.layout();\r
+\r
}\r
}); \r
- \r
+\r
}\r
\r
@Override\r
public boolean isDisposed() {\r
return disposed;\r
}\r
- \r
+\r
}\r
- );\r
+ );\r
}\r
}\r
}\r
\r
@Override\r
public void setFocus() {\r
- text.setFocus();\r
+ //text.setFocus();\r
}\r
\r
// Returns the names of the related variables (dependencies)\r
\r
return variables;\r
}\r
- \r
+\r
@Override\r
public void dispose() {\r
disposed = true;\r
+ getViewSite().getPage().removePartListener(focusLostListener);\r
super.dispose();\r
}\r
\r
--- /dev/null
+package org.simantics.sysdyn.ui.equation.expressions;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.Font;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.simantics.db.Resource;\r
+\r
+abstract public class ExpressionView extends Composite {\r
+\r
+ static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL);\r
+ protected Resource variable;\r
+ \r
+ public ExpressionView(Composite parent, int style, Resource variable) {\r
+ super(parent, style);\r
+ this.variable = variable;\r
+ }\r
+ \r
+ abstract public void save();\r
+ abstract public void replaceSelection(String text);\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.equation.expressions;\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.events.FocusAdapter;\r
+import org.eclipse.swt.events.FocusEvent;\r
+import org.eclipse.swt.events.KeyAdapter;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.graphics.Point;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Text;\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.request.Read;\r
+import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+@GraphType("http://www.simantics.org/Sysdyn#NormalExpression")\r
+public class NormalExpressionView extends ExpressionView {\r
+\r
+ Label equationLabel;\r
+ Text expression;\r
+ Point lastSelection = new Point(0,0);\r
+\r
+ @RelatedValue("http://www.simantics.org/Sysdyn#HasEquation")\r
+ private String equation;\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
+ }\r
+ return null;\r
+ }\r
+\r
+ });\r
+ } catch (DatabaseException e1) {\r
+ e1.printStackTrace();\r
+ }\r
+\r
+ GridDataFactory.fillDefaults().grab(true, true).applyTo(this);\r
+ GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this);\r
+ equationLabel = new Label(this, SWT.NONE);\r
+ equationLabel.setFont(FONT);\r
+ equationLabel.setText("=");\r
+ GridDataFactory.fillDefaults().applyTo(equationLabel);\r
+\r
+\r
+ expression = new Text(this, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
+ expression.setFont(FONT);\r
+ expression.setText(equation);\r
+ GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
+ \r
+ expression.addFocusListener(new FocusAdapter() {\r
+\r
+ @Override\r
+ public void focusLost(FocusEvent e) {\r
+ lastSelection = expression.getSelection();\r
+ }\r
+ });\r
+\r
+ expression.addKeyListener(new KeyAdapter() { \r
+\r
+ @Override\r
+ public void keyPressed(KeyEvent e) {\r
+ if(e.keyCode == SWT.ESC) {\r
+ expression.setText(equation);\r
+ } \r
+ }\r
+\r
+ });\r
+\r
+ }\r
+\r
+ @Override\r
+ public void save() { \r
+ final String currentText = expression.getText();\r
+ if(equation != null && !equation.equals(currentText)) {\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
+ Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
+ Builtins b = g.getBuiltins();\r
+ if(expression == null) {\r
+ expression = g.newResource();\r
+ g.claim(expression, b.InstanceOf, sr.NormalExpression);\r
+ g.claim(variable, sr.HasExpression, expression);\r
+ }\r
+ g.claimValue(expression, sr.HasEquation, currentText);\r
+ }\r
+\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
+ }\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.equation.expressions;\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.events.FocusAdapter;\r
+import org.eclipse.swt.events.FocusEvent;\r
+import org.eclipse.swt.events.KeyAdapter;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.graphics.Point;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Text;\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.request.Read;\r
+import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+@GraphType("http://www.simantics.org/Sysdyn#StockExpression")\r
+public class StockExpressionView extends ExpressionView {\r
+\r
+ Label equationLabel;\r
+ Text expression;\r
+ Point lastSelection = new Point(0,0);\r
+\r
+ @RelatedValue("http://www.simantics.org/Sysdyn#HasInitialEquation")\r
+ private String initialEquation;\r
+ \r
+ public StockExpressionView(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.StockExpression)) {\r
+ initialEquation = graph.getRelatedValue(expression, sr.HasInitialEquation);\r
+ } else {\r
+ initialEquation = "";\r
+ }\r
+ return null;\r
+ }\r
+\r
+ });\r
+ } catch (DatabaseException e1) {\r
+ e1.printStackTrace();\r
+ }\r
+ \r
+ GridDataFactory.fillDefaults().grab(true, true).applyTo(this);\r
+ GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this);\r
+ equationLabel = new Label(this, SWT.NONE);\r
+ equationLabel.setFont(FONT);\r
+ equationLabel.setText("Initial\nValue");\r
+ GridDataFactory.fillDefaults().applyTo(equationLabel);\r
+\r
+\r
+ expression = new Text(this, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);\r
+ expression.setFont(FONT);\r
+ expression.setText(initialEquation);\r
+ GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);\r
+ \r
+ expression.addFocusListener(new FocusAdapter() {\r
+\r
+ @Override\r
+ public void focusLost(FocusEvent e) {\r
+ lastSelection = expression.getSelection();\r
+ }\r
+ });\r
+\r
+ expression.addKeyListener(new KeyAdapter() { \r
+\r
+ @Override\r
+ public void keyPressed(KeyEvent e) {\r
+ if(e.keyCode == SWT.ESC) {\r
+ expression.setText(initialEquation);\r
+ } \r
+ }\r
+\r
+ });\r
+\r
+ }\r
+\r
+\r
+ @Override\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
+ }\r
+\r
+ @Override\r
+ public void save() {\r
+ final String currentText = expression.getText();\r
+ if(initialEquation != null && !initialEquation.equals(currentText)) {\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
+ Resource expression = g.getPossibleObject(variable, sr.HasExpression);\r
+ Builtins b = g.getBuiltins();\r
+ if(expression == null) {\r
+ expression = g.newResource();\r
+ g.claim(expression, b.InstanceOf, sr.StockExpression);\r
+ g.claim(variable, sr.HasExpression, expression);\r
+ }\r
+ g.claimValue(expression, sr.HasInitialEquation, currentText);\r
+ }\r
+\r
+ });\r
+ }\r
+\r
+ \r
+ }\r
+\r
+}\r
throws CoreException {\r
try {\r
Configuration configuration =\r
- LoadRepresentation.load(SimanticsUI.getSession(), ((ResourceEditorInput)element).getResource());\r
+ LoadRepresentation.loadConfiguration(SimanticsUI.getSession(), ((ResourceEditorInput)element).getResource());\r
ModelicaWriter writer = new ModelicaWriter();\r
writer.write(configuration);\r
\r
public final Resource Expression;\r
public final Resource Flow;\r
public final Resource HasAngle;\r
+ public final Resource HasEquation;\r
public final Resource HasExpression;\r
public final Resource HasHead;\r
+ public final Resource HasInitialEquation;\r
public final Resource HasTail;\r
- public final Resource HasText;\r
+ public final Resource HasType;\r
public final Resource HasX;\r
public final Resource HasY;\r
public final Resource IndependentVariable;\r
public final Resource IsHeadOf;\r
public final Resource IsTailOf;\r
public final Resource NormalExpression;\r
+ public final Resource ParameterExpression;\r
public final Resource Stock;\r
+ public final Resource StockExpression;\r
public final Resource SysdynProject;\r
public final Resource Valve;\r
public final Resource Variable;\r
public static final String Expression = "http://www.simantics.org/Sysdyn#Expression";\r
public static final String Flow = "http://www.simantics.org/Sysdyn#Flow";\r
public static final String HasAngle = "http://www.simantics.org/Sysdyn#HasAngle";\r
+ public static final String HasEquation = "http://www.simantics.org/Sysdyn#HasEquation";\r
public static final String HasExpression = "http://www.simantics.org/Sysdyn#HasExpression";\r
public static final String HasHead = "http://www.simantics.org/Sysdyn#HasHead";\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 HasText = "http://www.simantics.org/Sysdyn#HasText";\r
+ public static final String HasType = "http://www.simantics.org/Sysdyn#HasType";\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
public static final String IsHeadOf = "http://www.simantics.org/Sysdyn#IsHeadOf";\r
public static final String IsTailOf = "http://www.simantics.org/Sysdyn#IsTailOf";\r
public static final String NormalExpression = "http://www.simantics.org/Sysdyn#NormalExpression";\r
+ public static final String ParameterExpression = "http://www.simantics.org/Sysdyn#ParameterExpression";\r
public static final String Stock = "http://www.simantics.org/Sysdyn#Stock";\r
+ public static final String StockExpression = "http://www.simantics.org/Sysdyn#StockExpression";\r
public static final String SysdynProject = "http://www.simantics.org/Sysdyn#SysdynProject";\r
public static final String Valve = "http://www.simantics.org/Sysdyn#Valve";\r
public static final String Variable = "http://www.simantics.org/Sysdyn#Variable";\r
Expression = getResourceOrNull(graph, URIs.Expression);\r
Flow = getResourceOrNull(graph, URIs.Flow);\r
HasAngle = getResourceOrNull(graph, URIs.HasAngle);\r
+ HasEquation = getResourceOrNull(graph, URIs.HasEquation);\r
HasExpression = getResourceOrNull(graph, URIs.HasExpression);\r
HasHead = getResourceOrNull(graph, URIs.HasHead);\r
+ HasInitialEquation = getResourceOrNull(graph, URIs.HasInitialEquation);\r
HasTail = getResourceOrNull(graph, URIs.HasTail);\r
- HasText = getResourceOrNull(graph, URIs.HasText);\r
+ HasType = getResourceOrNull(graph, URIs.HasType);\r
HasX = getResourceOrNull(graph, URIs.HasX);\r
HasY = getResourceOrNull(graph, URIs.HasY);\r
IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable);\r
IsHeadOf = getResourceOrNull(graph, URIs.IsHeadOf);\r
IsTailOf = getResourceOrNull(graph, URIs.IsTailOf);\r
NormalExpression = getResourceOrNull(graph, URIs.NormalExpression);\r
+ ParameterExpression = getResourceOrNull(graph, URIs.ParameterExpression);\r
Stock = getResourceOrNull(graph, URIs.Stock);\r
+ StockExpression = getResourceOrNull(graph, URIs.StockExpression);\r
SysdynProject = getResourceOrNull(graph, URIs.SysdynProject);\r
Valve = getResourceOrNull(graph, URIs.Valve);\r
Variable = getResourceOrNull(graph, URIs.Variable);\r
package org.simantics.sysdyn.modelica;\r
\r
-import org.simantics.sysdyn.representation.Auxiliary;\r
import org.simantics.sysdyn.representation.Configuration;\r
import org.simantics.sysdyn.representation.IElement;\r
import org.simantics.sysdyn.representation.Stock;\r
-import org.simantics.sysdyn.representation.Valve;\r
import org.simantics.sysdyn.representation.Variable;\r
-import org.simantics.sysdyn.representation.visitors.ElementVisitorVoidAdapter;\r
-import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
\r
public class ModelicaWriter {\r
\r
StringBuilder b = new StringBuilder();\r
- \r
- IElementVisitorVoid componentWriter = new ElementVisitorVoidAdapter() {\r
- public void visit(Auxiliary auxiliary) {\r
- b.append(" Real ").append(escape(auxiliary.name)).append(";\n");\r
- }\r
- public void visit(Valve valve) {\r
- b.append(" Real ").append(escape(valve.name)).append(";\n");\r
- }\r
- public void visit(Stock stock) {\r
- b.append(" Real ").append(escape(stock.name)).append(";\n");\r
- } \r
- };\r
- \r
- IElementVisitorVoid equationWriter = new ElementVisitorVoidAdapter() {\r
- public void visit(Auxiliary auxiliary) {\r
- b.append(" ").append(escape(auxiliary.name))\r
- .append(" = ").append(auxiliary.expression.getText()).append(";\n");\r
- }\r
- public void visit(Stock stock) {\r
- b.append(" der(")\r
- .append(escape(stock.name))\r
- .append(") =");\r
- for(Variable flow : stock.incomingFlows)\r
- b.append("\n + ").append(escape(flow.name));\r
- for(Variable flow : stock.outgoingFlows)\r
- b.append("\n - ").append(escape(flow.name));\r
- b.append(";\n");\r
- } \r
- public void visit(Valve valve) {\r
- b.append(" ").append(escape(valve.name))\r
- .append(" = ").append(valve.expression.getText()).append(";\n");\r
- }\r
- };\r
- \r
- IElementVisitorVoid initialEquationWriter = new ElementVisitorVoidAdapter() {\r
- public void visit(Stock stock) {\r
- b.append(" ").append(escape(stock.name))\r
- .append(" = ").append(stock.expression.getText()).append(";\n");\r
- } \r
- };\r
+ String app;\r
+ boolean initialEquations = false;\r
\r
public void write(Configuration conf) {\r
conf.update();\r
b.append("model ").append(conf.name).append('\n');\r
for(IElement element : conf.elements)\r
- element.accept(componentWriter);\r
- b.append("initial equation\n");\r
+ if(element instanceof Variable) {\r
+ app = ((Variable)element).expression.getDeclaration((Variable)element);\r
+ if (app != null) b.append(app);\r
+ }\r
+ \r
for(IElement element : conf.elements)\r
- element.accept(initialEquationWriter);\r
+ if(element instanceof Stock) {\r
+ app = ((Stock)element).expression.getInitialEquation((Stock)element);\r
+ if (app != null) {\r
+ if(initialEquations == false) {\r
+ initialEquations = true;\r
+ b.append("initial equation\n");\r
+ }\r
+ b.append(app);\r
+ }\r
+ }\r
b.append("equation\n");\r
for(IElement element : conf.elements)\r
- element.accept(equationWriter);\r
+ if(element instanceof Variable) {\r
+ app = ((Variable)element).expression.getEquation((Variable)element);\r
+ if (app != null) b.append(app);\r
+ }\r
b.append("end ").append(conf.name).append(";\n");\r
}\r
\r
+++ /dev/null
-package org.simantics.sysdyn.representation;\r
-\r
-public interface IExpression {\r
- String getText();\r
-}\r
\r
public class LoadRepresentation {\r
\r
- public static Configuration load(Session session, final Resource configuration) throws DatabaseException {\r
+ public static Configuration loadConfiguration(Session session, final Resource configuration) throws DatabaseException {\r
return session.syncRequest(new Read<Configuration>() {\r
\r
@Override\r
});\r
}\r
\r
+ public static IElement loadElement(Session session, final Resource element) throws DatabaseException {\r
+ return session.syncRequest(new Read<IElement>() {\r
+\r
+ @Override\r
+ public IElement perform(ReadGraph graph)\r
+ throws DatabaseException {\r
+ SysdynSchema schema = new SysdynSchema(graph);\r
+ IMapping mapping = Mappings.createWithoutListening(schema);\r
+ return (IElement)mapping.map(graph, element); \r
+ }\r
+ \r
+ });\r
+ }\r
+ \r
}\r
+++ /dev/null
-package org.simantics.sysdyn.representation;\r
-\r
-import org.simantics.objmap.annotations.GraphType;\r
-import org.simantics.objmap.annotations.RelatedValue;\r
-\r
-@GraphType("http://www.simantics.org/Sysdyn#NormalExpression")\r
-public class NormalExpression implements IExpression {\r
- @RelatedValue("http://www.simantics.org/Sysdyn#HasText")\r
- public String text;\r
-\r
- @Override\r
- public String getText() {\r
- return text;\r
- }\r
-}\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.objmap.schema.MappingSchemas;\r
import org.simantics.objmap.schema.SimpleSchema;\r
+import org.simantics.sysdyn.representation.expressions.NormalExpression;\r
+import org.simantics.sysdyn.representation.expressions.ParameterExpression;\r
+import org.simantics.sysdyn.representation.expressions.StockExpression;\r
\r
public class SysdynSchema extends SimpleSchema {\r
\r
addLinkType(MappingSchemas.fromAnnotations(g, Stock.class));\r
addLinkType(MappingSchemas.fromAnnotations(g, Valve.class));\r
addLinkType(MappingSchemas.fromAnnotations(g, NormalExpression.class));\r
+ addLinkType(MappingSchemas.fromAnnotations(g, ParameterExpression.class));\r
+ addLinkType(MappingSchemas.fromAnnotations(g, StockExpression.class));\r
+ \r
} catch (DatabaseException e) {\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
\r
import org.simantics.objmap.annotations.RelatedElement;\r
import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.sysdyn.representation.expressions.IExpression;\r
\r
public abstract class Variable implements IElement {\r
@RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName")\r
- public String name;\r
+ protected String name;\r
+ @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasType")\r
+ protected String type;\r
//@RelatedValue("http://www.simantics.org/Sysdyn#HasValue")\r
//public String value;\r
@RelatedElement("http://www.simantics.org/Sysdyn#HasExpression")\r
public IExpression expression;\r
+ \r
+ public String getName() {\r
+ return this.name;\r
+ }\r
+ \r
+ public String getType() {\r
+ return this.type;\r
+ }\r
}\r
--- /dev/null
+package org.simantics.sysdyn.representation.expressions;\r
+\r
+import org.simantics.sysdyn.representation.Variable;\r
+\r
+public abstract class Expression implements IExpression {\r
+ \r
+ @Override\r
+ public String getDeclaration(Variable variable) {\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public String getEquation(Variable variable) {\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public String getInitialEquation(Variable variable) {\r
+ return null;\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.representation.expressions;\r
+\r
+import org.simantics.sysdyn.representation.Variable;\r
+\r
+public interface IExpression {\r
+\r
+ String getDeclaration(Variable variable);\r
+ String getInitialEquation(Variable variable);\r
+ String getEquation(Variable variable);\r
+\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.representation.expressions;\r
+\r
+import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.sysdyn.representation.Variable;\r
+\r
+@GraphType("http://www.simantics.org/Sysdyn#NormalExpression")\r
+public class NormalExpression extends Expression {\r
+\r
+ @RelatedValue("http://www.simantics.org/Sysdyn#HasEquation")\r
+ private String equation;\r
+ \r
+ @Override\r
+ public String getDeclaration(Variable variable) {\r
+ return " " + variable.getType() + " " + variable.getName() + ";\n";\r
+ }\r
+\r
+ @Override\r
+ public String getEquation(Variable variable) {\r
+ return " " + variable.getName() + " = " + equation + ";\n";\r
+ }\r
+ \r
+}\r
--- /dev/null
+package org.simantics.sysdyn.representation.expressions;\r
+\r
+import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.sysdyn.representation.Variable;\r
+\r
+@GraphType("http://www.simantics.org/Sysdyn#ParameterExpression")\r
+public class ParameterExpression extends Expression {\r
+ \r
+ @RelatedValue("http://www.simantics.org/Sysdyn#HasEquation")\r
+ private String equation;\r
+ \r
+ @Override\r
+ public String getDeclaration(Variable variable) {\r
+ return " " + variable.getType() + " " + variable.getName() + " = " + equation + ";\n";\r
+ }\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.representation.expressions;\r
+\r
+import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.sysdyn.representation.Stock;\r
+import org.simantics.sysdyn.representation.Variable;\r
+\r
+@GraphType("http://www.simantics.org/Sysdyn#StockExpression")\r
+public class StockExpression extends Expression {\r
+ \r
+ @RelatedValue("http://www.simantics.org/Sysdyn#HasInitialEquation")\r
+ private String initialEquation;\r
+ \r
+ @Override\r
+ public String getDeclaration(Variable variable) {\r
+ Double value = null;\r
+ try {\r
+ value = Double.parseDouble(initialEquation);\r
+ } catch (Exception e){\r
+ // Is an initial equation\r
+ }\r
+ if (value == null) {\r
+ return " " + variable.getType() + " " + variable.getName() + "(fixed=false);\n";\r
+ } else {\r
+ return " " + variable.getType() + " " + variable.getName() + "(start=" + value + ", fixed=true);\n";\r
+ }\r
+\r
+ }\r
+\r
+ @Override\r
+ public String getEquation(Variable variable) {\r
+ StringBuilder b = new StringBuilder();\r
+ b.append(" der(")\r
+ .append(variable.getName())\r
+ .append(") =");\r
+ for(Variable flow : ((Stock)variable).incomingFlows)\r
+ b.append("\n + ").append(flow.getName());\r
+ for(Variable flow : ((Stock)variable).outgoingFlows)\r
+ b.append("\n - ").append(flow.getName());\r
+ b.append(";\n");\r
+ return b.toString();\r
+ }\r
+\r
+ @Override\r
+ public String getInitialEquation(Variable variable) {\r
+ Double value = null;\r
+ try {\r
+ value = Double.parseDouble(initialEquation);\r
+ } catch (Exception e){\r
+ // Is an initial equation\r
+ } if (value != null) {\r
+ return null;\r
+ } else {\r
+ return " " + variable.getName() + " = " + initialEquation + ";\n";\r
+ }\r
+ }\r
+\r
+\r
+}\r
@echo off\r
-call "%~dp0..\og\generate2" "%~dp0" ..\foundation_ontologies\foundation.graph ..\sysdyn_ontologies\sysdyn.graph ..\modeling_ontologies\modeling.graph ..\modeling_ontologies\devs.graph ..\webmon_ontologies\webmon.graph
\ No newline at end of file
+call "%~dp0..\org.simantics.db.build\build" "%~dp0" ..\foundation_ontologies\foundation.graph ..\sysdyn_ontologies\sysdyn.graph ..\modeling_ontologies\modeling.graph ..\modeling_ontologies\devs.graph ..\webmon_ontologies\webmon.graph
\ No newline at end of file
L0.HasRange L0.Double \r
HasExpression <R L0.IsRelatedTo : L0.FunctionalRelation\r
L0.HasRange Expression\r
-\r
+ \r
+HasType <R L0.HasProperty\r
+ L0.HasRange L0.String\r
+ \r
IndependentVariable <T Variable\r
[L0.HasName card "1"]\r
+ [HasType card "1"]\r
+ \r
\r
Auxiliary <T IndependentVariable\r
[HasExpression card "0..1"]\r
# Expression\r
######################################################################\r
\r
-HasText <R L0.HasProperty\r
+HasEquation <R L0.HasProperty\r
+ L0.HasRange L0.String \r
+ \r
+HasInitialEquation <R L0.HasProperty\r
L0.HasRange L0.String \r
\r
Expression <T L0.Entity\r
\r
NormalExpression <T Expression\r
- [HasText card "1"] \r
+ [HasEquation card "1"] \r
\r
+ParameterExpression <T Expression\r
+ [HasEquation card "1"] \r
+\r
+StockExpression <T Expression\r
+ [HasInitialEquation card "1"]\r
+ \r
######################################################################\r
# Test configuration\r
######################################################################\r