From 682334e108d8fe9e497020c75430d1484c5d0623 Mon Sep 17 00:00:00 2001 From: niemisto Date: Thu, 19 Nov 2009 14:36:21 +0000 Subject: [PATCH] modelica text viewer for system dynamics configuration, Expression + NormalExpression git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13148 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../simantics/sysdyn/ui/actions/Connect.java | 2 +- .../sysdyn/ui/equation/EquationView.java | 18 +++++++-- .../ui/modelica/SysdynModelicaEditor.java | 18 ++++++++- .../org/simantics/sysdyn/SysdynResource.java | 15 +++++-- .../sysdyn/modelica/ModelicaWriter.java | 40 +++++++++++++++---- .../sysdyn/representation/Auxiliary.java | 3 -- .../sysdyn/representation/Configuration.java | 3 +- .../sysdyn/representation/Dependency.java | 2 +- .../simantics/sysdyn/representation/Flow.java | 2 +- .../sysdyn/representation/IExpression.java | 5 +++ .../representation/NormalExpression.java | 15 +++++++ .../sysdyn/representation/Valve.java | 2 +- .../sysdyn/representation/Variable.java | 5 +++ 13 files changed, 108 insertions(+), 22 deletions(-) create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/Connect.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/Connect.java index 8b793f4e..e9551879 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/Connect.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/Connect.java @@ -81,7 +81,7 @@ public class Connect extends DragEventHandler { 0.5 * (fromOrigo.getY() + toOrigo.getY()) ); editor.getDiagram().addElement(new FlowElement(from, (ValveElement)newElement)); - editor.getDiagram().addElement(new FlowElement(to, (ValveElement)newElement)); + editor.getDiagram().addElement(new FlowElement((ValveElement)newElement, to)); } else if(from instanceof ValveElement && to instanceof AuxiliaryElement) newElement = new DependencyElement(to, from); 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 9a513a89..04474d57 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 @@ -47,8 +47,16 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void perform(WriteGraph g) throws DatabaseException { + Builtins b = g.getBuiltins(); SysdynResource sr = SysdynResource.getInstance(g); - g.claimValue(auxiliary, sr.HasValue, currentText); + + Resource expression = g.getPossibleObject(auxiliary, sr.HasExpression); + if(expression == null) { + expression = g.newResource(); + g.claim(expression, b.InstanceOf, sr.Expression); + g.claim(auxiliary, sr.HasExpression, expression); + } + g.claimValue(expression, sr.HasText, currentText); } }); @@ -83,7 +91,9 @@ public class EquationView extends ViewPart implements ISelectionListener { if(g.isInstanceOf(resource, sr.Auxiliary) || g.isInstanceOf(resource, sr.Valve)) { auxiliary = resource; final String name = g.getRelatedValue(resource, b.HasName); - final String value = g.getPossibleRelatedValue(resource, sr.HasValue); + Resource expression = g.getPossibleObject(resource, sr.HasExpression); + final String value = expression == null ? null : + (String)g.getPossibleRelatedValue(expression, sr.HasText); text.getDisplay().asyncExec(new Runnable() { @Override @@ -101,7 +111,9 @@ public class EquationView extends ViewPart implements ISelectionListener { else if(g.isInstanceOf(resource, sr.Stock)) { auxiliary = resource; final String name = g.getRelatedValue(resource, b.HasName); - final String value = g.getPossibleRelatedValue(resource, sr.HasValue); + Resource expression = g.getPossibleObject(resource, sr.HasExpression); + final String value = expression == null ? null : + (String)g.getPossibleRelatedValue(expression, sr.HasText); text.getDisplay().asyncExec(new Runnable() { @Override 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 200a4806..542861ae 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 @@ -17,6 +17,12 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.editors.text.TextEditor; import org.eclipse.ui.texteditor.AbstractDocumentProvider; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.modelica.ModelicaWriter; +import org.simantics.sysdyn.representation.Configuration; +import org.simantics.sysdyn.representation.LoadRepresentation; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.workbench.ResourceEditorInput; public class SysdynModelicaEditor extends TextEditor { @@ -38,7 +44,17 @@ public class SysdynModelicaEditor extends TextEditor { @Override protected IDocument createDocument(Object element) throws CoreException { - return new Document((String)getEditorInput().getAdapter(String.class)); + try { + Configuration configuration = + LoadRepresentation.load(SimanticsUI.getSession(), ((ResourceEditorInput)element).getResource()); + ModelicaWriter writer = new ModelicaWriter(); + writer.write(configuration); + + return new Document(writer.toString()); + } catch (DatabaseException e) { + e.printStackTrace(); + throw new CoreException(STATUS_ERROR); + } } @Override diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java index 01448ff3..efa65edc 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -22,16 +22,19 @@ public class SysdynResource { public final Resource Cloud; public final Resource Configuration; public final Resource Dependency; + public final Resource Expression; public final Resource Flow; public final Resource HasAngle; + public final Resource HasExpression; public final Resource HasHead; public final Resource HasTail; - public final Resource HasValue; + public final Resource HasText; 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 Stock; public final Resource SysdynProject; public final Resource Valve; @@ -42,16 +45,19 @@ public class SysdynResource { public static final String Cloud = "http://www.simantics.org/Sysdyn#Cloud"; public static final String Configuration = "http://www.simantics.org/Sysdyn#Configuration"; public static final String Dependency = "http://www.simantics.org/Sysdyn#Dependency"; + 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 HasExpression = "http://www.simantics.org/Sysdyn#HasExpression"; public static final String HasHead = "http://www.simantics.org/Sysdyn#HasHead"; public static final String HasTail = "http://www.simantics.org/Sysdyn#HasTail"; - public static final String HasValue = "http://www.simantics.org/Sysdyn#HasValue"; + public static final String HasText = "http://www.simantics.org/Sysdyn#HasText"; 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 Stock = "http://www.simantics.org/Sysdyn#Stock"; public static final String SysdynProject = "http://www.simantics.org/Sysdyn#SysdynProject"; public static final String Valve = "http://www.simantics.org/Sysdyn#Valve"; @@ -72,16 +78,19 @@ public class SysdynResource { Cloud = getResourceOrNull(graph, URIs.Cloud); Configuration = getResourceOrNull(graph, URIs.Configuration); Dependency = getResourceOrNull(graph, URIs.Dependency); + Expression = getResourceOrNull(graph, URIs.Expression); Flow = getResourceOrNull(graph, URIs.Flow); HasAngle = getResourceOrNull(graph, URIs.HasAngle); + HasExpression = getResourceOrNull(graph, URIs.HasExpression); HasHead = getResourceOrNull(graph, URIs.HasHead); HasTail = getResourceOrNull(graph, URIs.HasTail); - HasValue = getResourceOrNull(graph, URIs.HasValue); + HasText = getResourceOrNull(graph, URIs.HasText); 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); Stock = getResourceOrNull(graph, URIs.Stock); SysdynProject = getResourceOrNull(graph, URIs.SysdynProject); Valve = getResourceOrNull(graph, URIs.Valve); 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 a63897e5..23b1b8c1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -5,41 +5,62 @@ 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; + 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(Stock stock) { - b.append(" Real ").append(escape(stock.name)).append(";\n"); - } public void visit(Valve valve) { - // TODO + 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.value).append(";\n"); + .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"); + } + }; + 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"); + for(IElement element : conf.elements) + element.accept(initialEquationWriter); b.append("equation\n"); for(IElement element : conf.elements) element.accept(equationWriter); @@ -47,7 +68,12 @@ public class ModelicaWriter { } public String escape(String name) { - return name; + return name.replace(' ', '_'); + } + + @Override + public String toString() { + return b.toString(); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Auxiliary.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Auxiliary.java index 97cb4465..f325b446 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Auxiliary.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Auxiliary.java @@ -5,9 +5,6 @@ import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; @GraphType("http://www.simantics.org/Sysdyn#Auxiliary") public class Auxiliary extends Variable { - // TODO - public String value; - @Override public void accept(IElementVisitorVoid v) { v.visit(this); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java index 86c640cd..5cd87a81 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java @@ -3,6 +3,7 @@ package org.simantics.sysdyn.representation; import java.util.ArrayList; import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedElements; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.representation.visitors.ElementVisitorVoidAdapter; import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; @@ -13,7 +14,7 @@ public class Configuration { @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName") public String name = "Unnamed"; - @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#ConsistsOf") + @RelatedElements("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#ConsistsOf") public ArrayList elements = new ArrayList(); IElementVisitorVoid clearVisitor = new ElementVisitorVoidAdapter() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Dependency.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Dependency.java index 631d2229..ff1eb648 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Dependency.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Dependency.java @@ -4,7 +4,7 @@ import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedElement; import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; -@GraphType("http://www.simantics.org/Sysdyn#Cloud") +@GraphType("http://www.simantics.org/Sysdyn#Dependency") public class Dependency implements IElement { @RelatedElement("http://www.simantics.org/Sysdyn#HasTail") public IElement tail; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Flow.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Flow.java index 6148dbfc..c9a2aa63 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Flow.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Flow.java @@ -4,7 +4,7 @@ import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedElement; import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; -@GraphType("http://www.simantics.org/Sysdyn#Cloud") +@GraphType("http://www.simantics.org/Sysdyn#Flow") public class Flow implements IElement { @RelatedElement("http://www.simantics.org/Sysdyn#HasTail") public IElement tail; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java new file mode 100644 index 00000000..d0e5ec4e --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IExpression.java @@ -0,0 +1,5 @@ +package org.simantics.sysdyn.representation; + +public interface IExpression { + String getText(); +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java new file mode 100644 index 00000000..d9d609fa --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/NormalExpression.java @@ -0,0 +1,15 @@ +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/Valve.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Valve.java index ba45a7c9..b3674010 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Valve.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Valve.java @@ -4,7 +4,7 @@ import org.simantics.objmap.annotations.GraphType; import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; @GraphType("http://www.simantics.org/Sysdyn#Valve") -public class Valve implements IElement { +public class Valve extends Variable { @Override public void accept(IElementVisitorVoid v) { v.visit(this); 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 379edf5c..653c04ba 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java @@ -1,8 +1,13 @@ package org.simantics.sysdyn.representation; +import org.simantics.objmap.annotations.RelatedElement; import org.simantics.objmap.annotations.RelatedValue; public abstract class Variable implements IElement { @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName") public String name; + //@RelatedValue("http://www.simantics.org/Sysdyn#HasValue") + //public String value; + @RelatedElement("http://www.simantics.org/Sysdyn#HasExpression") + public IExpression expression; } -- 2.47.1