| LOOKAHEAD( name() "(" ) name() function_call_args()\r
| component_reference()\r
// | "(" output_expression_list() ")"\r
- // | "[" expression_list() { ";" expression_list() } "]"\r
+ | "[" expression_list() ( ";" expression_list() )* "]"\r
//| "{" function_arguments() "}"\r
| "end"\r
}\r
void named_argument() : {\r
} { \r
<IDENT> "=" expression()\r
-}
\ No newline at end of file
+}\r
+\r
+void expression_list() : {\r
+} {\r
+ expression() ( "," expression() )*\r
+}\r
import org.simantics.sysdyn.representation.Stock;\r
import org.simantics.sysdyn.representation.Variable;\r
import org.simantics.sysdyn.representation.expressions.IExpression;\r
+import org.simantics.sysdyn.representation.expressions.WithLookupExpression;\r
\r
public class ModelicaWriter {\r
\r
StringBuilder b = new StringBuilder();\r
String app;\r
boolean initialEquations = false;\r
- \r
+ boolean insertInterpolate = false;\r
+\r
public void write(Configuration conf) {\r
conf.update();\r
b.append("model ").append(conf.name).append('\n');\r
if(expr != null) {\r
app = expr.getDeclaration((Variable)element);\r
if (app != null) b.append(app);\r
+ if (expr instanceof WithLookupExpression) insertInterpolate = true;\r
}\r
}\r
- \r
+\r
for(IElement element : conf.elements)\r
if(element instanceof Stock) {\r
IExpression expr = ((Stock)element).expression;\r
}\r
}\r
b.append("end ").append(conf.name).append(";\n");\r
+\r
+ if(insertInterpolate)\r
+ b.append(\r
+ "\nfunction Interpolate\n" +\r
+ " input Real table[:, 2];\n" +\r
+ " input Real xp;\n" +\r
+ " output Real yp;\n" +\r
+ "protected\n" +\r
+ " Integer i, sz;\n" +\r
+ "algorithm\n" +\r
+ " sz := size(table,1);\n" +\r
+ " for i in 1:(sz-1) loop // assume monotonically increasing\n" +\r
+ " if ((i == 1 and xp <= table[i+1,1]) or\n" +\r
+ " (i == sz - 1) or\n" +\r
+ " (xp > table[i,1] and xp <= table[i+1,1])) then // found appropriate segment\n" +\r
+ " yp := table[i,2] + (xp - table[i,1]) * (table[i+1,2] - table[i,2]) / (table[i+1,1] - table[i,1]);\n" +\r
+ " end if;\n" +\r
+ " end for;\n" +\r
+ " end Interpolate;\n");\r
+ \r
+\r
}\r
- \r
+\r
public String escape(String name) {\r
return name.replace(' ', '_');\r
}\r
- \r
+\r
@Override\r
public String toString() {\r
return b.toString();\r
}\r
- \r
+\r
}\r
package org.simantics.sysdyn.representation.expressions;\r
\r
import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.sysdyn.representation.Variable;\r
\r
\r
@GraphType("http://www.simantics.org/Sysdyn#WithLookupExpression")\r
public class WithLookupExpression extends Expression {\r
\r
+ @RelatedValue("http://www.simantics.org/Sysdyn#HasLookup")\r
+ private String lookupTable;\r
+ @RelatedValue("http://www.simantics.org/Sysdyn#HasEquation")\r
+ private String equation;\r
+\r
+ @Override\r
+ public String getDeclaration(Variable variable) {\r
+ return " " + variable.getType() + " " + variable.getName() + ";\n";\r
+ }\r
+\r
+ @Override\r
+ public String getEquation(Variable variable) {\r
+ return \r
+ " " + variable.getName() + " = Interpolate(table = " + lookupTable + ", xp = " + equation + ");\n";\r
+ }\r
+\r
}\r