\r
@Override\r
public String getDeclaration() {\r
-\r
- String each = "";\r
+ // is it really guaranteed that variability is the only necessary \r
+ // parameter to consider when deciding whether to use start=value \r
+ // notation or initial equations?\r
+ String each = (getArrayIndexes() != null && !getArrayIndexes().isEmpty()) ? "each " : "";\r
// each is required when a single value is used for all dimensions e.g. Stock[30](each start = 0) \r
if (Variability.getVariability(this) == Variability.CONTINUOUS) {\r
// start parameter is not used, everything needs to be fixed=false\r
- if(getArrayIndexes() != null && !getArrayIndexes().isEmpty())\r
- each = "each ";\r
return " " + getType() + " " + getModelicaName() + getRange() + "(" + each + "fixed=false);\n";\r
} else {\r
// Start parameter is used. Parameter guarantees that there is only one expression.\r
String initialEquation = e.getModelicaExpression();\r
initialEquation = SheetFormatUtils.reformatSheetReferences(this, initialEquation);\r
\r
- if(getArrayIndexes() != null && !getArrayIndexes().isEmpty())\r
- each = "each ";\r
-\r
- // TODO: there is probably a bug with startvalue here\r
return " " + \r
getType() + \r
" " + \r
getModelicaName() + \r
getRange() + \r
"(" + \r
- (e.getStartValue() != null ? each : "") + \r
"start=" + \r
initialEquation + \r
", " + \r
*******************************************************************************/\r
package org.simantics.sysdyn.representation.expressions;\r
\r
-import java.io.StringReader;\r
import java.util.ArrayList;\r
-import java.util.Set;\r
\r
import org.simantics.db.ReadGraph;\r
import org.simantics.objmap.annotations.GraphType;\r
import org.simantics.objmap.annotations.RelatedValue;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
-import org.simantics.sysdyn.expressionParser.ParseException;\r
import org.simantics.sysdyn.manager.SysdynModel;\r
-import org.simantics.sysdyn.representation.Book;\r
import org.simantics.sysdyn.representation.Enumeration;\r
-import org.simantics.sysdyn.representation.IElement;\r
-import org.simantics.sysdyn.representation.Sheet;\r
import org.simantics.sysdyn.representation.Stock;\r
import org.simantics.sysdyn.representation.Valve;\r
import org.simantics.sysdyn.representation.utils.FormatUtils;\r
}\r
return b.toString();\r
}\r
- \r
- /** \r
- * Check whether to use fixed=true and start=... in Modelica code\r
- * @return\r
- */\r
- private boolean useStartValue() {\r
- // If no variables are used in the equation, start value is used\r
- \r
- // First the equation is formatted and parsed\r
- String equation = FormatUtils.formatExpressionForModelica(parent, initialEquation);\r
- ExpressionParser parser = new ExpressionParser(new StringReader(equation));\r
- try {\r
- parser.expr();\r
- if(parser.getReferences().isEmpty()) {\r
- // if equation did not contain any references, start value is used\r
- return true;\r
- } else {\r
- // Check if references are references to sheets. \r
- // Sheet references are allowed, since sheets contain only constants\r
- boolean found = false;\r
- Set<String> references = parser.getReferences().keySet();\r
- \r
- // Go through each reference\r
- for(String reference : references) {\r
- // We only need the first element to know that it is a Sheet (SheetName.CellOrRange)\r
- reference = reference.split("\\.")[0]; \r
- found = false;\r
- for(IElement element : parent.getParentConfiguration().getElements()) {\r
- if(element instanceof Book) {\r
- for(Sheet sheet : ((Book)element).getSheets()) {\r
- if(reference.equals(sheet.getName())) {\r
- found = true;\r
- break;\r
- }\r
- }\r
- }\r
- if(found)\r
- break;\r
- }\r
- \r
- // If there was no sheet for this reference name, return false\r
- if(!found)\r
- return false;\r
- }\r
- }\r
- } catch (ParseException e) {\r
- e.printStackTrace();\r
- }\r
- return true;\r
- }\r
-\r
@Override\r
public String getInitialEquation() {\r
String initialEquation = FormatUtils.replaceWhitespace(this.initialEquation);\r
- // if start value is used, no initial equation is returned\r
- if(useStartValue())\r
- return null;\r
// format the initial equation for modelica execution \r
String equation = FormatUtils.formatExpressionForModelica(parent, initialEquation, false);\r
String range = IndexUtils.rangeToIndexes(parent, this.getArrayRange());\r
- if(range == null)\r
- range = "";\r
- return " " + parent.getModelicaName() + range + " = " + equation + ";\n";\r
+ return " " + parent.getModelicaName() + (range != null ? range : "") + " = " + equation + ";\n";\r
}\r
\r
\r