From d66aa6bc0592529d2f05991f90e2643863fd0315 Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 27 Oct 2011 07:36:23 +0000 Subject: [PATCH] Allow references to sheets in stock initial values (when fixed = true) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23048 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../expressions/StockExpression.java | 49 ++++++++++++++++--- 1 file changed, 41 insertions(+), 8 deletions(-) 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 f95cde94..31d975df 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in + * Copyright (c) 2010, 2011 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -14,14 +14,18 @@ package org.simantics.sysdyn.representation.expressions; import java.io.StringReader; import java.util.ArrayList; import java.util.Iterator; +import java.util.Set; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.expressionParser.ExpressionParser; import org.simantics.sysdyn.expressionParser.ParseException; import org.simantics.sysdyn.representation.ArrayIndexes; +import org.simantics.sysdyn.representation.Book; import org.simantics.sysdyn.representation.Enumeration; +import org.simantics.sysdyn.representation.IElement; import org.simantics.sysdyn.representation.IndependentVariable; +import org.simantics.sysdyn.representation.Sheet; import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Valve; import org.simantics.sysdyn.representation.utils.FormatUtils; @@ -37,8 +41,8 @@ public class StockExpression extends Expression { public String getDeclaration(IndependentVariable variable) { // Double value = getStartValue(variable); String value = null; - if(useStartValue()) - value = initialEquation; + if(useStartValue(variable)) + value = FormatUtils.formatExpressionForModelica(variable, initialEquation); ArrayIndexes ai = variable.getArrayIndexes(); ArrayList enumerations = null; @@ -110,14 +114,43 @@ public class StockExpression extends Expression { return b.toString(); } - private boolean useStartValue() { - ExpressionParser parser = new ExpressionParser(new StringReader(initialEquation)); + /** + * Check whether to use fixed=true and start=... in Modelica code + * @return + */ + private boolean useStartValue(IndependentVariable variable) { + String equation = FormatUtils.formatExpressionForModelica(variable, initialEquation); + ExpressionParser parser = new ExpressionParser(new StringReader(equation)); try { parser.expr(); - return parser.getReferences().isEmpty(); + if(parser.getReferences().isEmpty()) { + return true; + } else { + boolean found = false; + Set references = parser.getReferences().keySet(); + for(String reference : references) { + // We only need the first element to know that it is a Sheet + reference = reference.split("\\.")[0]; + found = false; + for(IElement element : variable.getParentConfiguration().getElements()) { + if(element instanceof Book) { + for(Sheet sheet : ((Book)element).getSheets()) { + if(reference.equals(sheet.getName())) { + found = true; + break; + } + } + } + if(found) + break; + } + if(!found) + return false; + } + } } catch (ParseException e) { } - return false; + return true; } @Override @@ -128,7 +161,7 @@ public class StockExpression extends Expression { // } catch (Exception e){ // // Has an initial equation // } - if(useStartValue()) + if(useStartValue(variable)) return null; String equation = FormatUtils.formatExpressionForModelica(variable, initialEquation); String range = IndexUtils.rangeToIndexes(variable, this.getArrayRange()); -- 2.47.1