0.5 * (fromOrigo.getY() + toOrigo.getY())\r
);\r
editor.getDiagram().addElement(new FlowElement(from, (ValveElement)newElement));\r
- editor.getDiagram().addElement(new FlowElement(to, (ValveElement)newElement)); \r
+ editor.getDiagram().addElement(new FlowElement((ValveElement)newElement, to)); \r
}\r
else if(from instanceof ValveElement && to instanceof AuxiliaryElement)\r
newElement = new DependencyElement(to, from);\r
@Override\r
public void perform(WriteGraph g)\r
throws DatabaseException {\r
+ Builtins b = g.getBuiltins();\r
SysdynResource sr = SysdynResource.getInstance(g);\r
- g.claimValue(auxiliary, sr.HasValue, currentText);\r
+ \r
+ Resource expression = g.getPossibleObject(auxiliary, sr.HasExpression);\r
+ if(expression == null) {\r
+ expression = g.newResource();\r
+ g.claim(expression, b.InstanceOf, sr.Expression);\r
+ g.claim(auxiliary, sr.HasExpression, expression);\r
+ }\r
+ g.claimValue(expression, sr.HasText, currentText);\r
}\r
\r
});\r
if(g.isInstanceOf(resource, sr.Auxiliary) || g.isInstanceOf(resource, sr.Valve)) {\r
auxiliary = resource;\r
final String name = g.getRelatedValue(resource, b.HasName);\r
- final String value = g.getPossibleRelatedValue(resource, sr.HasValue);\r
+ Resource expression = g.getPossibleObject(resource, sr.HasExpression); \r
+ final String value = expression == null ? null :\r
+ (String)g.getPossibleRelatedValue(expression, sr.HasText);\r
text.getDisplay().asyncExec(new Runnable() {\r
\r
@Override\r
else if(g.isInstanceOf(resource, sr.Stock)) {\r
auxiliary = resource;\r
final String name = g.getRelatedValue(resource, b.HasName);\r
- final String value = g.getPossibleRelatedValue(resource, sr.HasValue);\r
+ Resource expression = g.getPossibleObject(resource, sr.HasExpression); \r
+ final String value = expression == null ? null :\r
+ (String)g.getPossibleRelatedValue(expression, sr.HasText);\r
text.getDisplay().asyncExec(new Runnable() {\r
\r
@Override\r
import org.eclipse.ui.PlatformUI;\r
import org.eclipse.ui.editors.text.TextEditor;\r
import org.eclipse.ui.texteditor.AbstractDocumentProvider;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.sysdyn.modelica.ModelicaWriter;\r
+import org.simantics.sysdyn.representation.Configuration;\r
+import org.simantics.sysdyn.representation.LoadRepresentation;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.workbench.ResourceEditorInput;\r
\r
\r
public class SysdynModelicaEditor extends TextEditor {\r
@Override\r
protected IDocument createDocument(Object element)\r
throws CoreException {\r
- return new Document((String)getEditorInput().getAdapter(String.class));\r
+ try {\r
+ Configuration configuration =\r
+ LoadRepresentation.load(SimanticsUI.getSession(), ((ResourceEditorInput)element).getResource());\r
+ ModelicaWriter writer = new ModelicaWriter();\r
+ writer.write(configuration);\r
+ \r
+ return new Document(writer.toString());\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ throw new CoreException(STATUS_ERROR);\r
+ } \r
}\r
\r
@Override\r
public final Resource Cloud;\r
public final Resource Configuration;\r
public final Resource Dependency;\r
+ public final Resource Expression;\r
public final Resource Flow;\r
public final Resource HasAngle;\r
+ public final Resource HasExpression;\r
public final Resource HasHead;\r
public final Resource HasTail;\r
- public final Resource HasValue;\r
+ public final Resource HasText;\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 Stock;\r
public final Resource SysdynProject;\r
public final Resource Valve;\r
public static final String Cloud = "http://www.simantics.org/Sysdyn#Cloud";\r
public static final String Configuration = "http://www.simantics.org/Sysdyn#Configuration";\r
public static final String Dependency = "http://www.simantics.org/Sysdyn#Dependency";\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 HasExpression = "http://www.simantics.org/Sysdyn#HasExpression";\r
public static final String HasHead = "http://www.simantics.org/Sysdyn#HasHead";\r
public static final String HasTail = "http://www.simantics.org/Sysdyn#HasTail";\r
- public static final String HasValue = "http://www.simantics.org/Sysdyn#HasValue";\r
+ public static final String HasText = "http://www.simantics.org/Sysdyn#HasText";\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 Stock = "http://www.simantics.org/Sysdyn#Stock";\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
Cloud = getResourceOrNull(graph, URIs.Cloud);\r
Configuration = getResourceOrNull(graph, URIs.Configuration);\r
Dependency = getResourceOrNull(graph, URIs.Dependency);\r
+ Expression = getResourceOrNull(graph, URIs.Expression);\r
Flow = getResourceOrNull(graph, URIs.Flow);\r
HasAngle = getResourceOrNull(graph, URIs.HasAngle);\r
+ HasExpression = getResourceOrNull(graph, URIs.HasExpression);\r
HasHead = getResourceOrNull(graph, URIs.HasHead);\r
HasTail = getResourceOrNull(graph, URIs.HasTail);\r
- HasValue = getResourceOrNull(graph, URIs.HasValue);\r
+ HasText = getResourceOrNull(graph, URIs.HasText);\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
Stock = getResourceOrNull(graph, URIs.Stock);\r
SysdynProject = getResourceOrNull(graph, URIs.SysdynProject);\r
Valve = getResourceOrNull(graph, URIs.Valve);\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;\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(Stock stock) {\r
- b.append(" Real ").append(escape(stock.name)).append(";\n");\r
- } \r
public void visit(Valve valve) {\r
- // TODO\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.value).append(";\n");\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
+ \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
+ for(IElement element : conf.elements)\r
+ element.accept(initialEquationWriter);\r
b.append("equation\n");\r
for(IElement element : conf.elements)\r
element.accept(equationWriter);\r
}\r
\r
public String escape(String name) {\r
- return name;\r
+ return name.replace(' ', '_');\r
+ }\r
+ \r
+ @Override\r
+ public String toString() {\r
+ return b.toString();\r
}\r
\r
}\r
\r
@GraphType("http://www.simantics.org/Sysdyn#Auxiliary")\r
public class Auxiliary extends Variable {\r
- // TODO\r
- public String value;\r
-\r
@Override\r
public void accept(IElementVisitorVoid v) {\r
v.visit(this); \r
import java.util.ArrayList;\r
\r
import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedElements;\r
import org.simantics.objmap.annotations.RelatedValue;\r
import org.simantics.sysdyn.representation.visitors.ElementVisitorVoidAdapter;\r
import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
@RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName")\r
public String name = "Unnamed";\r
\r
- @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#ConsistsOf")\r
+ @RelatedElements("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#ConsistsOf")\r
public ArrayList<IElement> elements = new ArrayList<IElement>();\r
\r
IElementVisitorVoid clearVisitor = new ElementVisitorVoidAdapter() {\r
import org.simantics.objmap.annotations.RelatedElement;\r
import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
\r
-@GraphType("http://www.simantics.org/Sysdyn#Cloud")\r
+@GraphType("http://www.simantics.org/Sysdyn#Dependency")\r
public class Dependency implements IElement {\r
@RelatedElement("http://www.simantics.org/Sysdyn#HasTail")\r
public IElement tail;\r
import org.simantics.objmap.annotations.RelatedElement;\r
import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
\r
-@GraphType("http://www.simantics.org/Sysdyn#Cloud")\r
+@GraphType("http://www.simantics.org/Sysdyn#Flow")\r
public class Flow implements IElement {\r
@RelatedElement("http://www.simantics.org/Sysdyn#HasTail")\r
public IElement tail;\r
--- /dev/null
+package org.simantics.sysdyn.representation;\r
+\r
+public interface IExpression {\r
+ String getText();\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.sysdyn.representation.visitors.IElementVisitorVoid;\r
\r
@GraphType("http://www.simantics.org/Sysdyn#Valve")\r
-public class Valve implements IElement {\r
+public class Valve extends Variable {\r
@Override\r
public void accept(IElementVisitorVoid v) {\r
v.visit(this); \r
package org.simantics.sysdyn.representation;\r
\r
+import org.simantics.objmap.annotations.RelatedElement;\r
import org.simantics.objmap.annotations.RelatedValue;\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
+ //@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