From: lempinen Date: Thu, 6 Jun 2013 06:16:02 +0000 (+0000) Subject: Unit validation for stock integrals (fixes #4306) X-Git-Tag: 1.8.1~302 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=499ec0c23e97259656b0aa2bc7149517a614be77;p=simantics%2Fsysdyn.git Unit validation for stock integrals (fixes #4306) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27508 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Model.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Model.java index 5403623e..76f28a47 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Model.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Model.java @@ -55,7 +55,10 @@ public class Model { @RelatedElement(SimulationResource.URIs.HasConfiguration) private Configuration configuration; - + + @RelatedValue(SysdynResource.URIs.SysdynModel_timeUnit) + private String timeUnit; + @RelatedElements( value = Layer0.URIs.ConsistsOf, composition = true) @@ -243,4 +246,9 @@ public class Model { public Configuration getModelConfiguration() { return configuration; } + + + public String getTimeUnit() { + return timeUnit; + } } 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 index 17e1a78e..9bd2b3db 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java @@ -15,11 +15,13 @@ import java.io.StringReader; import java.util.ArrayList; import java.util.Set; +import org.simantics.db.ReadGraph; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.expressionParser.ExpressionParser; import org.simantics.sysdyn.expressionParser.ParseException; +import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.representation.Book; import org.simantics.sysdyn.representation.Enumeration; import org.simantics.sysdyn.representation.IElement; @@ -28,6 +30,7 @@ import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Valve; import org.simantics.sysdyn.representation.utils.FormatUtils; import org.simantics.sysdyn.representation.utils.IndexUtils; +import org.simantics.sysdyn.representation.utils.UnitUtils; /** * Class representing a stock expression in a variable @@ -174,5 +177,21 @@ public class StockExpression extends Expression { return value; } - + + @Override + public String validateUnits(ReadGraph graph, SysdynModel model) { + String result = UnitUtils.matchUnits(graph, model, parent.getParentConfiguration(), parent.getUnit(), initialEquation); + if(result == null) { + String integralEquation = getEquation(); + if(integralEquation.contains("=")) + result = UnitUtils.matchUnits( + graph, + model, + parent.getParentConfiguration(), + parent.getUnit(), + "(" + integralEquation.substring(integralEquation.indexOf("=") + 1, integralEquation.lastIndexOf(";")) +") * time"); + } + + return result; + } }