From: lempinen Date: Wed, 30 Jun 2010 07:12:21 +0000 (+0000) Subject: First model with modules that can be simulated! X-Git-Tag: simantics-1.2.0~191 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=e86088549d9846eac3daaff49a8c648c88ab750d;p=simantics%2Fsysdyn.git First model with modules that can be simulated! git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16436 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ConfigurationLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ConfigurationLabeler.java index 56f4e97e..379e3a56 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ConfigurationLabeler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ConfigurationLabeler.java @@ -20,7 +20,7 @@ public class ConfigurationLabeler extends LabelerContributor{ @Override public String getLabel(ReadGraph graph, ConfigurationNode conf) throws DatabaseException { - String name = graph.getPossibleRelatedValue(conf.resource, graph.getBuiltins().HasName); + String name = graph.getPossibleRelatedValue(conf.resource, graph.getBuiltins().HasLabel); return name == null ? "Project (no name)" : name; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java index 78e02ffe..fdc289c6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java @@ -25,6 +25,9 @@ public class ModuleLabeler extends LabelerContributor{ Builtins b = graph.getBuiltins(); Resource resource = module.resource; StringBuilder sb = new StringBuilder(); + for(Resource r : graph.getObjects(resource, b.HasLabel)) + sb.append(graph.getValue(r)); + sb.append(" : "); for(Resource r : graph.getObjects(resource, b.HasName)) sb.append(graph.getValue(r)); sb.append(" : "); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java index e847edb4..c59bc4bd 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -58,14 +58,13 @@ public class SysdynResource { public final Resource HasStartTime; public final Resource HasStopTime; public final Resource HasTail; - public final Resource HasTerminal; public final Resource HasUnit; public final Resource HasX; public final Resource HasY; public final Resource IndependentVariable; - public final Resource Input; public final Resource IsHeadOf; public final Resource IsHeadOfTerminal; + public final Resource IsReferredBy; public final Resource IsTailOf; public final Resource IsTailOfTerminal; public final Resource LookupExpression; @@ -83,7 +82,6 @@ public class SysdynResource { public final Resource SysdynModelManager; public final Resource SysdynProject; public final Resource SysdynTerminal; - public final Resource Terminal; public final Resource Valve; public final Resource ValveSymbol; public final Resource Variable; @@ -128,14 +126,13 @@ public class SysdynResource { public static final String HasStartTime = "http://www.simantics.org/Sysdyn-1.0/HasStartTime"; public static final String HasStopTime = "http://www.simantics.org/Sysdyn-1.0/HasStopTime"; public static final String HasTail = "http://www.simantics.org/Sysdyn-1.0/HasTail"; - public static final String HasTerminal = "http://www.simantics.org/Sysdyn-1.0/HasTerminal"; public static final String HasUnit = "http://www.simantics.org/Sysdyn-1.0/HasUnit"; public static final String HasX = "http://www.simantics.org/Sysdyn-1.0/HasX"; public static final String HasY = "http://www.simantics.org/Sysdyn-1.0/HasY"; public static final String IndependentVariable = "http://www.simantics.org/Sysdyn-1.0/IndependentVariable"; - public static final String Input = "http://www.simantics.org/Sysdyn-1.0/Input"; public static final String IsHeadOf = "http://www.simantics.org/Sysdyn-1.0/IsHeadOf"; public static final String IsHeadOfTerminal = "http://www.simantics.org/Sysdyn-1.0/IsHeadOfTerminal"; + public static final String IsReferredBy = "http://www.simantics.org/Sysdyn-1.0/IsReferredBy"; public static final String IsTailOf = "http://www.simantics.org/Sysdyn-1.0/IsTailOf"; public static final String IsTailOfTerminal = "http://www.simantics.org/Sysdyn-1.0/IsTailOfTerminal"; public static final String LookupExpression = "http://www.simantics.org/Sysdyn-1.0/LookupExpression"; @@ -153,7 +150,6 @@ public class SysdynResource { public static final String SysdynModelManager = "http://www.simantics.org/Sysdyn-1.0/SysdynModelManager"; public static final String SysdynProject = "http://www.simantics.org/Sysdyn-1.0/SysdynProject"; public static final String SysdynTerminal = "http://www.simantics.org/Sysdyn-1.0/SysdynTerminal"; - public static final String Terminal = "http://www.simantics.org/Sysdyn-1.0/Terminal"; public static final String Valve = "http://www.simantics.org/Sysdyn-1.0/Valve"; public static final String ValveSymbol = "http://www.simantics.org/Sysdyn-1.0/ValveSymbol"; public static final String Variable = "http://www.simantics.org/Sysdyn-1.0/Variable"; @@ -208,14 +204,13 @@ public class SysdynResource { HasStartTime = getResourceOrNull(graph, URIs.HasStartTime); HasStopTime = getResourceOrNull(graph, URIs.HasStopTime); HasTail = getResourceOrNull(graph, URIs.HasTail); - HasTerminal = getResourceOrNull(graph, URIs.HasTerminal); HasUnit = getResourceOrNull(graph, URIs.HasUnit); HasX = getResourceOrNull(graph, URIs.HasX); HasY = getResourceOrNull(graph, URIs.HasY); IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable); - Input = getResourceOrNull(graph, URIs.Input); IsHeadOf = getResourceOrNull(graph, URIs.IsHeadOf); IsHeadOfTerminal = getResourceOrNull(graph, URIs.IsHeadOfTerminal); + IsReferredBy = getResourceOrNull(graph, URIs.IsReferredBy); IsTailOf = getResourceOrNull(graph, URIs.IsTailOf); IsTailOfTerminal = getResourceOrNull(graph, URIs.IsTailOfTerminal); LookupExpression = getResourceOrNull(graph, URIs.LookupExpression); @@ -233,7 +228,6 @@ public class SysdynResource { SysdynModelManager = getResourceOrNull(graph, URIs.SysdynModelManager); SysdynProject = getResourceOrNull(graph, URIs.SysdynProject); SysdynTerminal = getResourceOrNull(graph, URIs.SysdynTerminal); - Terminal = getResourceOrNull(graph, URIs.Terminal); Valve = getResourceOrNull(graph, URIs.Valve); ValveSymbol = getResourceOrNull(graph, URIs.ValveSymbol); 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 b7072dcc..dee7a664 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -32,7 +32,7 @@ public class ModelicaWriter { public void write(Configuration conf) { writeModule(conf); - + if(insertInterpolate) b.append( "\nfunction Interpolate\n" + @@ -52,21 +52,21 @@ public class ModelicaWriter { " end for;\n" + " end Interpolate;\n"); } - + private void writeModule(IModule module) { - + module.update(); - - ArrayList variables = new ArrayList(); + + ArrayList variables = new ArrayList(); ArrayList modules = new ArrayList(); - ArrayList stocks = new ArrayList(); + ArrayList stocks = new ArrayList(); // Initialize lists for(IElement element : module.getElements()) { if(element instanceof Variable) { - variables.add(element); + variables.add((Variable)element); if(element instanceof Stock) - stocks.add(element); + stocks.add((Stock)element); } else if (element instanceof Module) { modules.add((Module)element); } @@ -74,45 +74,81 @@ public class ModelicaWriter { b.append("class ").append(module.getName().replace(" ", "")).append('\n'); - for(IElement element : variables) { - IExpression expr = ((Variable)element).expression; + + b.append("// Variable definitions\n"); + for(Variable variable : variables) { + IExpression expr = (variable).getExpression(); if(expr != null && insertInterpolate == false) { - app = expr.getDeclaration((Variable)element); + app = expr.getDeclaration(variable); if (app != null) b.append(app); if (expr instanceof WithLookupExpression) insertInterpolate = true; } } + + if(!modules.isEmpty()) { + b.append("// Module definitions\n"); + for(Module m : modules) { + b.append(" " + m.getName() + " " + m.getLabel() + ";\n"); + } + } - for(IElement element : stocks) { - IExpression expr = ((Stock)element).expression; + for(Stock stock : stocks) { + IExpression expr = stock.getExpression(); if(expr != null) { - app = expr.getInitialEquation((Stock)element); + app = expr.getInitialEquation(stock); if (app != null) { if(initialEquations == false) { initialEquations = true; + b.append("// Initial Equations\n"); b.append("initial equation\n"); } b.append(app); } } } - + b.append("equation\n"); - for(IElement element : variables) { - IExpression expr = ((Variable)element).expression; - if(expr != null) { - app = expr.getEquation((Variable)element); - if (app != null) b.append(app); + + b.append("// Equations\n"); + for(Variable variable : variables) { + if(variable.getRefersTo() != null) continue; + IExpression expr = variable.getExpression(); + if(expr != null) { + app = expr.getEquation(variable); + if (app != null) b.append(app); + } + } + + + b.append("// Inputs\n"); + for(Variable variable : variables) { + Variable reference = variable.getRefersTo(); + if(reference == null) continue; + if(variable.getParentModule().getElements().contains(reference.getParentModule())) { + b.append(" " + variable.getName() + + " = " + reference.getParentModule().getLabel() + "." + reference.getName() + ";\n"); + } + } + + b.append("// Outputs\n"); + for(Variable variable : variables) { + ArrayList isReferredBy = variable.getIsReferredBy(); + for(Variable var : isReferredBy) { + if(variable.getParentModule().getElements().contains(var.getParentModule())) { + b.append(" " + var.getParentModule().getLabel() + "." + var.getName() + + " = " + variable.getName() + ";\n"); } } + } + b.append("end ").append(module.getName().replace(" ", "")).append(";\n\n"); - - + + for(Module m : modules) { writeModule(m); } } - + public String escape(String name) { return name.replace(' ', '_'); } 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 2308eaa4..be8bcdf0 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java @@ -32,6 +32,9 @@ public class Configuration implements IModule { @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName") private String name; + @RelatedValue("http://www.simantics.org/Layer0-1.0/HasLabel") + private String label; + @RelatedElements( value = "http://www.simantics.org/Layer0-1.0/ConsistsOf", composition = true) @@ -71,6 +74,11 @@ public class Configuration implements IModule { public String getName() { return name; } + + @Override + public String getLabel() { + return label; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IModule.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IModule.java index af462ef3..814a30ca 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IModule.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IModule.java @@ -5,7 +5,8 @@ import java.util.ArrayList; public interface IModule { public ArrayList getElements(); - public String getName(); + public String getName(); + public String getLabel(); public void update(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java index b19e804f..bdb37afd 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java @@ -13,6 +13,9 @@ public class Module implements IElement, IModule { @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName") private String name; + + @RelatedValue("http://www.simantics.org/Layer0-1.0/HasLabel") + private String label; @RelatedElements( value = "http://www.simantics.org/Layer0-1.0/ConsistsOf", @@ -59,4 +62,10 @@ public class Module implements IElement, IModule { public String getName() { return name; } + + + @Override + public String getLabel() { + return label; + } } 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 6c0d58c5..6b9b50f0 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java @@ -11,7 +11,10 @@ *******************************************************************************/ package org.simantics.sysdyn.representation; +import java.util.ArrayList; + import org.simantics.objmap.annotations.RelatedElement; +import org.simantics.objmap.annotations.RelatedElements; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.representation.expressions.IExpression; @@ -21,7 +24,17 @@ public abstract class Variable implements IElement { @RelatedValue("http://www.simantics.org/Layer0-1.0/HasType") protected String type; @RelatedElement("http://www.simantics.org/Sysdyn-1.0/HasExpression") - public IExpression expression; + protected IExpression expression; + @RelatedElement("http://www.simantics.org/Sysdyn-1.0/RefersTo") + protected Variable refersTo; + @RelatedElements( + value = "http://www.simantics.org/Sysdyn-1.0/IsReferredBy", + composition = true) + private ArrayList isReferredBy = new ArrayList(); + @RelatedElement("http://www.simantics.org/Layer0-1.0/PartOf") + protected IModule module; + + public String getName() { return this.name; @@ -30,4 +43,20 @@ public abstract class Variable implements IElement { public String getType() { return this.type; } + + public IExpression getExpression() { + return this.expression; + } + + public Variable getRefersTo() { + return this.refersTo; + } + + public ArrayList getIsReferredBy() { + return this.isReferredBy; + } + + public IModule getParentModule() { + return this.module; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ConstantExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ConstantExpression.java index 9d5d64a2..b8f7bf53 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ConstantExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ConstantExpression.java @@ -23,7 +23,11 @@ public class ConstantExpression extends Expression { @Override public String getDeclaration(Variable variable) { - return " constant " + variable.getType() + " " + variable.getName() + " = " + equation + ";\n"; - } + StringBuilder b = new StringBuilder(); + b.append(" constant " + variable.getType() + " " + variable.getName()); + if(variable.getRefersTo() == null) + b.append(" = " + equation); + b.append(";\n"); + return b.toString(); } } 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 index d1a83a04..8e096b09 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java @@ -23,6 +23,11 @@ public class ParameterExpression extends Expression { @Override public String getDeclaration(Variable variable) { - return " parameter " + variable.getType() + " " + variable.getName() + " = " + equation + ";\n"; + StringBuilder b = new StringBuilder(); + b.append(" parameter " + variable.getType() + " " + variable.getName()); + if(variable.getRefersTo() == null) + b.append(" = " + equation); + b.append(";\n"); + return b.toString(); } } diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index a38e4853..6bead35c 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -118,9 +118,7 @@ HasY