From 83037590871ace7159442cc3ce7dbfc41adec4be Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 22 Dec 2009 15:11:21 +0000 Subject: [PATCH] Expression types started git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13342 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/elements/FlowElement.java | 1 - .../sysdyn/ui/elements/TextElement.java | 3 + .../sysdyn/ui/equation/EquationView.java | 188 +++++++----------- .../equation/expressions/ExpressionView.java | 20 ++ .../expressions/NormalExpressionView.java | 123 ++++++++++++ .../expressions/StockExpressionView.java | 127 ++++++++++++ .../ui/modelica/SysdynModelicaEditor.java | 2 +- .../org/simantics/sysdyn/SysdynResource.java | 18 +- .../sysdyn/modelica/ModelicaWriter.java | 69 ++----- .../sysdyn/representation/IExpression.java | 5 - .../representation/LoadRepresentation.java | 16 +- .../representation/NormalExpression.java | 15 -- .../sysdyn/representation/SysdynSchema.java | 6 + .../sysdyn/representation/Variable.java | 13 +- .../expressions/Expression.java | 22 ++ .../expressions/IExpression.java | 11 + .../expressions/NormalExpression.java | 23 +++ .../expressions/ParameterExpression.java | 17 ++ .../expressions/StockExpression.java | 59 ++++++ sysdyn_ontologies/generate_sysdyn.bat | 2 +- sysdyn_ontologies/sysdyn.graph | 20 +- 21 files changed, 570 insertions(+), 190 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionView.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/NormalExpressionView.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionView.java delete mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java delete mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/FlowElement.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/FlowElement.java index 2675900c..656a3f76 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/FlowElement.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/FlowElement.java @@ -29,7 +29,6 @@ public class FlowElement extends Element implements IElementListener { Connectable tail; @RelatedElement("http://www.simantics.org/Sysdyn#HasHead") Connectable head; - double angle = 0.1; // Auxiliary double angle0; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java index e820dad2..474fbd7e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java @@ -34,6 +34,9 @@ public class TextElement extends RectangularElement implements Movable, Connecta // Properties @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName") public String label = "Unnamed"; + + @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasType") + public String type = "Real"; // Auxiliary fields double textX; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java index 713d39ee..095bec4e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java @@ -9,17 +9,15 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; -import org.eclipse.swt.events.FocusAdapter; import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; -import org.eclipse.swt.events.KeyAdapter; -import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.TabFolder; import org.eclipse.swt.widgets.TabItem; @@ -28,16 +26,18 @@ import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.ISelectionListener; import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.IWorkbenchPartReference; import org.eclipse.ui.part.ViewPart; import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.equation.expressions.ExpressionView; +import org.simantics.sysdyn.ui.equation.expressions.NormalExpressionView; +import org.simantics.sysdyn.ui.equation.expressions.StockExpressionView; import org.simantics.ui.SimanticsUI; import org.simantics.utils.ui.ExceptionUtils; @@ -52,8 +52,6 @@ public class EquationView extends ViewPart implements ISelectionListener { Text nameText; Composite equationComposite; - Label equationLabel; - Text text; String originalText; TabFolder tabFolder; @@ -62,14 +60,37 @@ public class EquationView extends ViewPart implements ISelectionListener { TabItem functions; Composite unitsAndRange; Composite variableType; + Composite emptyComposite; + ExpressionView expressionView; + org.eclipse.ui.IPartListener2 focusLostListener; @Override public void createPartControl(Composite parent) { + // Listeners getSite().getPage().addPostSelectionListener(this); + focusLostListener = new org.eclipse.ui.IPartListener2() + { + public void partInputChanged(IWorkbenchPartReference partRef) {} + public void partVisible(IWorkbenchPartReference partRef) {} + public void partHidden(IWorkbenchPartReference partRef) {} + public void partOpened(IWorkbenchPartReference partRef) {} + public void partDeactivated(IWorkbenchPartReference partRef) + { + if(partRef.getPart(false) instanceof EquationView) { + if(expressionView != null) expressionView.save(); + } + } + public void partClosed(IWorkbenchPartReference partRef) {} + public void partBroughtToTop(IWorkbenchPartReference partRef) {} + public void partActivated(IWorkbenchPartReference partRef) {} + }; + getViewSite().getPage().addPartListener(focusLostListener); GridLayoutFactory.fillDefaults().numColumns(4).margins(5, 5).applyTo(parent); + + // Upper part of the view: Name (and renaming) nameText = new Text(parent, SWT.SINGLE | SWT.BORDER); nameText.setFont(FONT); @@ -82,41 +103,30 @@ public class EquationView extends ViewPart implements ISelectionListener { variables.setText("Variables"); variableTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); variableTable.addMouseListener(new MouseListener(){ - + @Override public void mouseDoubleClick(MouseEvent e) { TableItem[] selection = variableTable.getSelection(); - final String var = selection[0].getText(); - text.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - int pos = text.getCaretPosition(); - String middle = var; - String start = text.getText(0, pos -1); - String end = text.getText(pos, text.getCharCount()-1); - if (pos != 0 && !text.getText(pos-1, pos).equals(" ")) - middle = " " + middle; - if (pos != text.getCharCount() && !text.getText(pos, pos+1).equals(" ")) - middle = middle + " "; - text.setText(start + middle + end); - text.forceFocus(); - text.setSelection(pos + middle.length()); - } - }); - } - - @Override - public void mouseDown(MouseEvent e) { + if(selection.length > 0) { + final String var = selection[0].getText(); + expressionView.getDisplay().asyncExec(new Runnable() { + @Override + public void run() { + expressionView.replaceSelection(var); + } + }); + } } @Override - public void mouseUp(MouseEvent e) { + public void mouseDown(MouseEvent e) {} - } + @Override + public void mouseUp(MouseEvent e) {} }); + variableTable.addFocusListener(new FocusListener() { @Override @@ -127,7 +137,7 @@ public class EquationView extends ViewPart implements ISelectionListener { public void focusLost(FocusEvent e) { variableTable.deselectAll(); } - + }); variables.setControl(variableTable); @@ -143,8 +153,10 @@ public class EquationView extends ViewPart implements ISelectionListener { functions.setControl(table); // Right side of the view: Details, comments - Composite emptyComposite = new Composite(parent, SWT.NONE); + emptyComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().span(1,4).grab(true, true).applyTo(emptyComposite); + GridLayoutFactory.fillDefaults().applyTo(emptyComposite); + // Below the variable name: Variable type variableType = new Composite(parent, SWT.NONE); @@ -172,53 +184,9 @@ public class EquationView extends ViewPart implements ISelectionListener { // Below variable type: Equation equationComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().span(2, 1).grab(true, true).applyTo(equationComposite); - GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(equationComposite); - equationLabel = new Label(equationComposite, SWT.NONE); - equationLabel.setFont(FONT); - equationLabel.setText("="); - GridDataFactory.fillDefaults().applyTo(equationLabel); - - text = new Text(equationComposite, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); - text.setFont(FONT); - GridDataFactory.fillDefaults().grab(true, true).applyTo(text); - text.addFocusListener(new FocusAdapter() { + GridLayoutFactory.fillDefaults().applyTo(equationComposite); - @Override - public void focusLost(FocusEvent e) { - final String currentText = text.getText(); - if(originalText != null && !originalText.equals(currentText)) { - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - @Override - public void perform(WriteGraph g) - throws DatabaseException { - Builtins b = g.getBuiltins(); - SysdynResource sr = SysdynResource.getInstance(g); - - Resource expression = g.getPossibleObject(variable, sr.HasExpression); - if(expression == null) { - expression = g.newResource(); - g.claim(expression, b.InstanceOf, sr.Expression); - g.claim(variable, sr.HasExpression, expression); - } - g.claimValue(expression, sr.HasText, currentText); - } - - }); - } - } - - }); - text.addKeyListener(new KeyAdapter() { - - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC) { - text.setText(originalText); - } - } - - }); // Lower part of the view: Units and Range unitsAndRange = new Composite(parent, SWT.NONE); @@ -243,9 +211,10 @@ public class EquationView extends ViewPart implements ISelectionListener { static class Auxiliary { ArrayList dependencies; - String eqLabel, name, value; + String name; + String type; } - + class UpdateViewRequest implements Read { Resource resource; @@ -256,26 +225,19 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public Auxiliary perform(ReadGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); Builtins b = graph.getBuiltins(); Auxiliary a = new Auxiliary(); - if (graph.isInstanceOf(resource, sr.Auxiliary) || graph.isInstanceOf(resource, sr.Valve)) - a.eqLabel = "="; - else if (graph.isInstanceOf(resource, sr.Stock)) - a.eqLabel = "initial ="; - else - return null; variable = resource; a.name = graph.getRelatedValue(resource, b.HasName); - Resource expression = graph.getPossibleObject(resource, sr.HasExpression); - a.value = expression == null ? null : - (String)graph.getPossibleRelatedValue(expression, sr.HasText); a.dependencies = getDependencies(resource); + SysdynResource sr = SysdynResource.getInstance(graph); + if (graph.isInstanceOf(variable, sr.Auxiliary) || graph.isInstanceOf(variable, sr.Valve)) + a.type = "NORMAL"; + else if (graph.isInstanceOf(variable, sr.Stock)) + a.type = "STOCK"; + return a; } - - - } @Override @@ -293,52 +255,55 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void exception(Throwable t) { // TODO Auto-generated method stub - + } @Override public void execute(final Auxiliary result) { - text.getDisplay().asyncExec(new Runnable() { + equationComposite.getDisplay().asyncExec(new Runnable() { @Override public void run(){ - + variableTable.removeAll(); TableItem item; for(String d : result.dependencies) { item = new TableItem(variableTable, SWT.NONE); item.setText(d); } - nameText.setText(result.name); - equationLabel.setText(result.eqLabel); - equationComposite.layout(); - if(result.value == null) - originalText = ""; - else - originalText = result.value; - text.setText(originalText); - + + for(Control c : equationComposite.getChildren()) { + c.dispose(); + } + ExpressionView ev = null; + if (result.type.equalsIgnoreCase("NORMAL")) + ev = new NormalExpressionView(equationComposite, SWT.NONE, variable); + else if (result.type.equalsIgnoreCase("STOCK")) + ev = new StockExpressionView(equationComposite, SWT.NONE, variable); + expressionView = ev; + equationComposite.layout(); + } }); - + } @Override public boolean isDisposed() { return disposed; } - + } - ); + ); } } } @Override public void setFocus() { - text.setFocus(); + //text.setFocus(); } // Returns the names of the related variables (dependencies) @@ -377,10 +342,11 @@ public class EquationView extends ViewPart implements ISelectionListener { return variables; } - + @Override public void dispose() { disposed = true; + getViewSite().getPage().removePartListener(focusLostListener); super.dispose(); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionView.java new file mode 100644 index 00000000..8b55b095 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionView.java @@ -0,0 +1,20 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.widgets.Composite; +import org.simantics.db.Resource; + +abstract public class ExpressionView extends Composite { + + static final Font FONT = new Font(null, "Courier New", 12, SWT.NORMAL); + protected Resource variable; + + public ExpressionView(Composite parent, int style, Resource variable) { + super(parent, style); + this.variable = variable; + } + + abstract public void save(); + abstract public void replaceSelection(String text); +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/NormalExpressionView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/NormalExpressionView.java new file mode 100644 index 00000000..7fb0bc65 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/NormalExpressionView.java @@ -0,0 +1,123 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; + +@GraphType("http://www.simantics.org/Sysdyn#NormalExpression") +public class NormalExpressionView extends ExpressionView { + + Label equationLabel; + Text expression; + Point lastSelection = new Point(0,0); + + @RelatedValue("http://www.simantics.org/Sysdyn#HasEquation") + private String equation; + + public NormalExpressionView(Composite parent, int style, final Resource variable) { + super(parent, style, variable); + + try { + SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource expression = graph.getPossibleObject(variable, sr.HasExpression); + if (expression != null && graph.isInstanceOf(expression, sr.NormalExpression)) { + equation = graph.getRelatedValue(expression, sr.HasEquation); + } else { + equation = ""; + } + return null; + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + + GridDataFactory.fillDefaults().grab(true, true).applyTo(this); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this); + equationLabel = new Label(this, SWT.NONE); + equationLabel.setFont(FONT); + equationLabel.setText("="); + GridDataFactory.fillDefaults().applyTo(equationLabel); + + + expression = new Text(this, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); + expression.setFont(FONT); + expression.setText(equation); + GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); + + expression.addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + lastSelection = expression.getSelection(); + } + }); + + expression.addKeyListener(new KeyAdapter() { + + @Override + public void keyPressed(KeyEvent e) { + if(e.keyCode == SWT.ESC) { + expression.setText(equation); + } + } + + }); + + } + + @Override + public void save() { + final String currentText = expression.getText(); + if(equation != null && !equation.equals(currentText)) { + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph g) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + Resource expression = g.getPossibleObject(variable, sr.HasExpression); + Builtins b = g.getBuiltins(); + if(expression == null) { + expression = g.newResource(); + g.claim(expression, b.InstanceOf, sr.NormalExpression); + g.claim(variable, sr.HasExpression, expression); + } + g.claimValue(expression, sr.HasEquation, currentText); + } + + }); + } + + } + + public void replaceSelection(String text) { + String oldExpression = expression.getText(); + String newExpression = oldExpression.substring(0, lastSelection.x) + text + oldExpression.substring(lastSelection.y); + expression.setText(newExpression); + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionView.java new file mode 100644 index 00000000..e640eedf --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionView.java @@ -0,0 +1,127 @@ +package org.simantics.sysdyn.ui.equation.expressions; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; + +@GraphType("http://www.simantics.org/Sysdyn#StockExpression") +public class StockExpressionView extends ExpressionView { + + Label equationLabel; + Text expression; + Point lastSelection = new Point(0,0); + + @RelatedValue("http://www.simantics.org/Sysdyn#HasInitialEquation") + private String initialEquation; + + public StockExpressionView(Composite parent, int style, final Resource variable) { + super(parent, style, variable); + + try { + SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource expression = graph.getPossibleObject(variable, sr.HasExpression); + if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) { + initialEquation = graph.getRelatedValue(expression, sr.HasInitialEquation); + } else { + initialEquation = ""; + } + return null; + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + + GridDataFactory.fillDefaults().grab(true, true).applyTo(this); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(this); + equationLabel = new Label(this, SWT.NONE); + equationLabel.setFont(FONT); + equationLabel.setText("Initial\nValue"); + GridDataFactory.fillDefaults().applyTo(equationLabel); + + + expression = new Text(this, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER); + expression.setFont(FONT); + expression.setText(initialEquation); + GridDataFactory.fillDefaults().grab(true, true).applyTo(expression); + + expression.addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + lastSelection = expression.getSelection(); + } + }); + + expression.addKeyListener(new KeyAdapter() { + + @Override + public void keyPressed(KeyEvent e) { + if(e.keyCode == SWT.ESC) { + expression.setText(initialEquation); + } + } + + }); + + } + + + @Override + public void replaceSelection(String text) { + String oldExpression = expression.getText(); + String newExpression = oldExpression.substring(0, lastSelection.x) + text + oldExpression.substring(lastSelection.y); + expression.setText(newExpression); + } + + @Override + public void save() { + final String currentText = expression.getText(); + if(initialEquation != null && !initialEquation.equals(currentText)) { + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph g) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + Resource expression = g.getPossibleObject(variable, sr.HasExpression); + Builtins b = g.getBuiltins(); + if(expression == null) { + expression = g.newResource(); + g.claim(expression, b.InstanceOf, sr.StockExpression); + g.claim(variable, sr.HasExpression, expression); + } + g.claimValue(expression, sr.HasInitialEquation, currentText); + } + + }); + } + + + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/modelica/SysdynModelicaEditor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/modelica/SysdynModelicaEditor.java index 542861ae..76df255e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/modelica/SysdynModelicaEditor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/modelica/SysdynModelicaEditor.java @@ -46,7 +46,7 @@ public class SysdynModelicaEditor extends TextEditor { throws CoreException { try { Configuration configuration = - LoadRepresentation.load(SimanticsUI.getSession(), ((ResourceEditorInput)element).getResource()); + LoadRepresentation.loadConfiguration(SimanticsUI.getSession(), ((ResourceEditorInput)element).getResource()); ModelicaWriter writer = new ModelicaWriter(); writer.write(configuration); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java index efa65edc..3546148f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -25,17 +25,21 @@ public class SysdynResource { public final Resource Expression; public final Resource Flow; public final Resource HasAngle; + public final Resource HasEquation; public final Resource HasExpression; public final Resource HasHead; + public final Resource HasInitialEquation; public final Resource HasTail; - public final Resource HasText; + public final Resource HasType; public final Resource HasX; public final Resource HasY; public final Resource IndependentVariable; public final Resource IsHeadOf; public final Resource IsTailOf; public final Resource NormalExpression; + public final Resource ParameterExpression; public final Resource Stock; + public final Resource StockExpression; public final Resource SysdynProject; public final Resource Valve; public final Resource Variable; @@ -48,17 +52,21 @@ public class SysdynResource { public static final String Expression = "http://www.simantics.org/Sysdyn#Expression"; public static final String Flow = "http://www.simantics.org/Sysdyn#Flow"; public static final String HasAngle = "http://www.simantics.org/Sysdyn#HasAngle"; + public static final String HasEquation = "http://www.simantics.org/Sysdyn#HasEquation"; public static final String HasExpression = "http://www.simantics.org/Sysdyn#HasExpression"; public static final String HasHead = "http://www.simantics.org/Sysdyn#HasHead"; + public static final String HasInitialEquation = "http://www.simantics.org/Sysdyn#HasInitialEquation"; public static final String HasTail = "http://www.simantics.org/Sysdyn#HasTail"; - public static final String HasText = "http://www.simantics.org/Sysdyn#HasText"; + public static final String HasType = "http://www.simantics.org/Sysdyn#HasType"; public static final String HasX = "http://www.simantics.org/Sysdyn#HasX"; public static final String HasY = "http://www.simantics.org/Sysdyn#HasY"; public static final String IndependentVariable = "http://www.simantics.org/Sysdyn#IndependentVariable"; public static final String IsHeadOf = "http://www.simantics.org/Sysdyn#IsHeadOf"; public static final String IsTailOf = "http://www.simantics.org/Sysdyn#IsTailOf"; public static final String NormalExpression = "http://www.simantics.org/Sysdyn#NormalExpression"; + public static final String ParameterExpression = "http://www.simantics.org/Sysdyn#ParameterExpression"; public static final String Stock = "http://www.simantics.org/Sysdyn#Stock"; + public static final String StockExpression = "http://www.simantics.org/Sysdyn#StockExpression"; public static final String SysdynProject = "http://www.simantics.org/Sysdyn#SysdynProject"; public static final String Valve = "http://www.simantics.org/Sysdyn#Valve"; public static final String Variable = "http://www.simantics.org/Sysdyn#Variable"; @@ -81,17 +89,21 @@ public class SysdynResource { Expression = getResourceOrNull(graph, URIs.Expression); Flow = getResourceOrNull(graph, URIs.Flow); HasAngle = getResourceOrNull(graph, URIs.HasAngle); + HasEquation = getResourceOrNull(graph, URIs.HasEquation); HasExpression = getResourceOrNull(graph, URIs.HasExpression); HasHead = getResourceOrNull(graph, URIs.HasHead); + HasInitialEquation = getResourceOrNull(graph, URIs.HasInitialEquation); HasTail = getResourceOrNull(graph, URIs.HasTail); - HasText = getResourceOrNull(graph, URIs.HasText); + HasType = getResourceOrNull(graph, URIs.HasType); HasX = getResourceOrNull(graph, URIs.HasX); HasY = getResourceOrNull(graph, URIs.HasY); IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable); IsHeadOf = getResourceOrNull(graph, URIs.IsHeadOf); IsTailOf = getResourceOrNull(graph, URIs.IsTailOf); NormalExpression = getResourceOrNull(graph, URIs.NormalExpression); + ParameterExpression = getResourceOrNull(graph, URIs.ParameterExpression); Stock = getResourceOrNull(graph, URIs.Stock); + StockExpression = getResourceOrNull(graph, URIs.StockExpression); SysdynProject = getResourceOrNull(graph, URIs.SysdynProject); Valve = getResourceOrNull(graph, URIs.Valve); Variable = getResourceOrNull(graph, URIs.Variable); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java index 23b1b8c1..f990c4c2 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -1,69 +1,42 @@ package org.simantics.sysdyn.modelica; -import org.simantics.sysdyn.representation.Auxiliary; import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.IElement; import org.simantics.sysdyn.representation.Stock; -import org.simantics.sysdyn.representation.Valve; import org.simantics.sysdyn.representation.Variable; -import org.simantics.sysdyn.representation.visitors.ElementVisitorVoidAdapter; -import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; public class ModelicaWriter { StringBuilder b = new StringBuilder(); - - IElementVisitorVoid componentWriter = new ElementVisitorVoidAdapter() { - public void visit(Auxiliary auxiliary) { - b.append(" Real ").append(escape(auxiliary.name)).append(";\n"); - } - public void visit(Valve valve) { - b.append(" Real ").append(escape(valve.name)).append(";\n"); - } - public void visit(Stock stock) { - b.append(" Real ").append(escape(stock.name)).append(";\n"); - } - }; - - IElementVisitorVoid equationWriter = new ElementVisitorVoidAdapter() { - public void visit(Auxiliary auxiliary) { - b.append(" ").append(escape(auxiliary.name)) - .append(" = ").append(auxiliary.expression.getText()).append(";\n"); - } - public void visit(Stock stock) { - b.append(" der(") - .append(escape(stock.name)) - .append(") ="); - for(Variable flow : stock.incomingFlows) - b.append("\n + ").append(escape(flow.name)); - for(Variable flow : stock.outgoingFlows) - b.append("\n - ").append(escape(flow.name)); - b.append(";\n"); - } - public void visit(Valve valve) { - b.append(" ").append(escape(valve.name)) - .append(" = ").append(valve.expression.getText()).append(";\n"); - } - }; - - IElementVisitorVoid initialEquationWriter = new ElementVisitorVoidAdapter() { - public void visit(Stock stock) { - b.append(" ").append(escape(stock.name)) - .append(" = ").append(stock.expression.getText()).append(";\n"); - } - }; + String app; + boolean initialEquations = false; public void write(Configuration conf) { conf.update(); b.append("model ").append(conf.name).append('\n'); for(IElement element : conf.elements) - element.accept(componentWriter); - b.append("initial equation\n"); + if(element instanceof Variable) { + app = ((Variable)element).expression.getDeclaration((Variable)element); + if (app != null) b.append(app); + } + for(IElement element : conf.elements) - element.accept(initialEquationWriter); + if(element instanceof Stock) { + app = ((Stock)element).expression.getInitialEquation((Stock)element); + if (app != null) { + if(initialEquations == false) { + initialEquations = true; + b.append("initial equation\n"); + } + b.append(app); + } + } b.append("equation\n"); for(IElement element : conf.elements) - element.accept(equationWriter); + if(element instanceof Variable) { + app = ((Variable)element).expression.getEquation((Variable)element); + if (app != null) b.append(app); + } b.append("end ").append(conf.name).append(";\n"); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java deleted file mode 100644 index d0e5ec4e..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.simantics.sysdyn.representation; - -public interface IExpression { - String getText(); -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/LoadRepresentation.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/LoadRepresentation.java index c02ac45a..1c7b861e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/LoadRepresentation.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/LoadRepresentation.java @@ -10,7 +10,7 @@ import org.simantics.objmap.Mappings; public class LoadRepresentation { - public static Configuration load(Session session, final Resource configuration) throws DatabaseException { + public static Configuration loadConfiguration(Session session, final Resource configuration) throws DatabaseException { return session.syncRequest(new Read() { @Override @@ -24,4 +24,18 @@ public class LoadRepresentation { }); } + public static IElement loadElement(Session session, final Resource element) throws DatabaseException { + return session.syncRequest(new Read() { + + @Override + public IElement perform(ReadGraph graph) + throws DatabaseException { + SysdynSchema schema = new SysdynSchema(graph); + IMapping mapping = Mappings.createWithoutListening(schema); + return (IElement)mapping.map(graph, element); + } + + }); + } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java deleted file mode 100644 index d9d609fa..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.simantics.sysdyn.representation; - -import org.simantics.objmap.annotations.GraphType; -import org.simantics.objmap.annotations.RelatedValue; - -@GraphType("http://www.simantics.org/Sysdyn#NormalExpression") -public class NormalExpression implements IExpression { - @RelatedValue("http://www.simantics.org/Sysdyn#HasText") - public String text; - - @Override - public String getText() { - return text; - } -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java index 58be1de4..b2294c38 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java @@ -4,6 +4,9 @@ import org.simantics.db.ReadGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.objmap.schema.MappingSchemas; import org.simantics.objmap.schema.SimpleSchema; +import org.simantics.sysdyn.representation.expressions.NormalExpression; +import org.simantics.sysdyn.representation.expressions.ParameterExpression; +import org.simantics.sysdyn.representation.expressions.StockExpression; public class SysdynSchema extends SimpleSchema { @@ -17,6 +20,9 @@ public class SysdynSchema extends SimpleSchema { addLinkType(MappingSchemas.fromAnnotations(g, Stock.class)); addLinkType(MappingSchemas.fromAnnotations(g, Valve.class)); addLinkType(MappingSchemas.fromAnnotations(g, NormalExpression.class)); + addLinkType(MappingSchemas.fromAnnotations(g, ParameterExpression.class)); + addLinkType(MappingSchemas.fromAnnotations(g, StockExpression.class)); + } catch (DatabaseException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java index 653c04ba..d9b44119 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java @@ -2,12 +2,23 @@ package org.simantics.sysdyn.representation; import org.simantics.objmap.annotations.RelatedElement; import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.sysdyn.representation.expressions.IExpression; public abstract class Variable implements IElement { @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName") - public String name; + protected String name; + @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasType") + protected String type; //@RelatedValue("http://www.simantics.org/Sysdyn#HasValue") //public String value; @RelatedElement("http://www.simantics.org/Sysdyn#HasExpression") public IExpression expression; + + public String getName() { + return this.name; + } + + public String getType() { + return this.type; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java new file mode 100644 index 00000000..aab3d0fa --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java @@ -0,0 +1,22 @@ +package org.simantics.sysdyn.representation.expressions; + +import org.simantics.sysdyn.representation.Variable; + +public abstract class Expression implements IExpression { + + @Override + public String getDeclaration(Variable variable) { + return null; + } + + @Override + public String getEquation(Variable variable) { + return null; + } + + @Override + public String getInitialEquation(Variable variable) { + return null; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java new file mode 100644 index 00000000..9260eadf --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java @@ -0,0 +1,11 @@ +package org.simantics.sysdyn.representation.expressions; + +import org.simantics.sysdyn.representation.Variable; + +public interface IExpression { + + String getDeclaration(Variable variable); + String getInitialEquation(Variable variable); + String getEquation(Variable variable); + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java new file mode 100644 index 00000000..c8242c54 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java @@ -0,0 +1,23 @@ +package org.simantics.sysdyn.representation.expressions; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.sysdyn.representation.Variable; + +@GraphType("http://www.simantics.org/Sysdyn#NormalExpression") +public class NormalExpression extends Expression { + + @RelatedValue("http://www.simantics.org/Sysdyn#HasEquation") + private String equation; + + @Override + public String getDeclaration(Variable variable) { + return " " + variable.getType() + " " + variable.getName() + ";\n"; + } + + @Override + public String getEquation(Variable variable) { + return " " + variable.getName() + " = " + equation + ";\n"; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java new file mode 100644 index 00000000..0c541fb6 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java @@ -0,0 +1,17 @@ +package org.simantics.sysdyn.representation.expressions; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.sysdyn.representation.Variable; + +@GraphType("http://www.simantics.org/Sysdyn#ParameterExpression") +public class ParameterExpression extends Expression { + + @RelatedValue("http://www.simantics.org/Sysdyn#HasEquation") + private String equation; + + @Override + public String getDeclaration(Variable variable) { + return " " + variable.getType() + " " + variable.getName() + " = " + equation + ";\n"; + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java new file mode 100644 index 00000000..5ab28c21 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java @@ -0,0 +1,59 @@ +package org.simantics.sysdyn.representation.expressions; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.sysdyn.representation.Stock; +import org.simantics.sysdyn.representation.Variable; + +@GraphType("http://www.simantics.org/Sysdyn#StockExpression") +public class StockExpression extends Expression { + + @RelatedValue("http://www.simantics.org/Sysdyn#HasInitialEquation") + private String initialEquation; + + @Override + public String getDeclaration(Variable variable) { + Double value = null; + try { + value = Double.parseDouble(initialEquation); + } catch (Exception e){ + // Is an initial equation + } + if (value == null) { + return " " + variable.getType() + " " + variable.getName() + "(fixed=false);\n"; + } else { + return " " + variable.getType() + " " + variable.getName() + "(start=" + value + ", fixed=true);\n"; + } + + } + + @Override + public String getEquation(Variable variable) { + StringBuilder b = new StringBuilder(); + b.append(" der(") + .append(variable.getName()) + .append(") ="); + for(Variable flow : ((Stock)variable).incomingFlows) + b.append("\n + ").append(flow.getName()); + for(Variable flow : ((Stock)variable).outgoingFlows) + b.append("\n - ").append(flow.getName()); + b.append(";\n"); + return b.toString(); + } + + @Override + public String getInitialEquation(Variable variable) { + Double value = null; + try { + value = Double.parseDouble(initialEquation); + } catch (Exception e){ + // Is an initial equation + } if (value != null) { + return null; + } else { + return " " + variable.getName() + " = " + initialEquation + ";\n"; + } + } + + +} diff --git a/sysdyn_ontologies/generate_sysdyn.bat b/sysdyn_ontologies/generate_sysdyn.bat index 32293b37..2c0814a0 100644 --- a/sysdyn_ontologies/generate_sysdyn.bat +++ b/sysdyn_ontologies/generate_sysdyn.bat @@ -1,2 +1,2 @@ @echo off -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 diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index ab012257..97cae434 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -49,9 +49,14 @@ HasY