import org.simantics.sysdyn.representation.Enumeration;\r
import org.simantics.sysdyn.representation.EnumerationIndex;\r
import org.simantics.sysdyn.representation.IElement;\r
+import org.simantics.sysdyn.representation.Model;\r
import org.simantics.sysdyn.representation.Module;\r
import org.simantics.sysdyn.representation.Sheet;\r
import org.simantics.sysdyn.representation.Variable;\r
for(int i = 0; i < parts.length && current != null; i++) {\r
current = getElement(current, parts[i]);\r
}\r
+ \r
+ if(current == null) {\r
+ // Sheets are currently located in the model root. Try to find the sheet.\r
+ current = conf.getModuleType().getParent(); // Get module type parent (should be a model)\r
+ if(current instanceof Model)\r
+ current = getElement(((Model)current).getModelConfiguration(), parts[0]); // Try to get the sheet\r
+ }\r
+ \r
if(current != null && current instanceof Sheet) {\r
Sheet sheet = (Sheet) current;\r
String e = ef.getExpression();\r
* @throws DatabaseException\r
*/\r
private static void traverseIndexes(ReadGraph g, String rvi, HashMap<String, String> rvis, List<Resource> arrayIndexes) throws DatabaseException {\r
- traverseIndexes(g, rvi, rvis, arrayIndexes, arrayIndexes.get(0), "", "");\r
+ traverseIndexes(g, rvi, rvis, arrayIndexes, 0, "", "");\r
}\r
\r
/**\r
* @param indexNamesSoFar String representation of the indexes so far in name format\r
* @throws DatabaseException\r
*/\r
- private static void traverseIndexes(ReadGraph g, String rvi, HashMap<String, String> rvis, List<Resource> arrayIndexes, Resource currentEnumeration, String indexesSoFar, String indexNamesSoFar) throws DatabaseException {\r
+ private static void traverseIndexes(ReadGraph g, String rvi, HashMap<String, String> rvis, List<Resource> arrayIndexes, int currentIndex, String indexesSoFar, String indexNamesSoFar) throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(g);\r
// Enumeration indexes of the current enumeration (e.g. the first EnumIndexes in Var[EnumIndexes, EnumIndexes, EnumIndexes])\r
- Resource enumerationIndexes = g.getPossibleObject(currentEnumeration, sr.Enumeration_enumerationIndexes);\r
+ Resource enumerationIndexes = g.getPossibleObject(arrayIndexes.get(currentIndex), sr.Enumeration_enumerationIndexes);\r
if(enumerationIndexes == null)\r
return;\r
List<Resource> indexes = OrderedSetUtils.toList(g, enumerationIndexes);\r
Boolean b = g.getPossibleRelatedValue(indexes.get(i), sr.EnumerationIndex_showEnumerationIndexInCharts, Bindings.BOOLEAN);\r
// If this index is not wanted to be shown in charts, the recursion does not go any further and rvis.put() is not called for this enumeration\r
if(Boolean.TRUE.equals(b)) {\r
- int arrayIndex = arrayIndexes.indexOf(currentEnumeration);\r
// Get the name of the index\r
String name = g.getRelatedValue(indexes.get(i), Layer0.getInstance(g).HasName);\r
- if(arrayIndex < arrayIndexes.size() - 1)\r
+ if(currentIndex < arrayIndexes.size() - 1)\r
// If there are still more EnumIndexes, recursively call the function and add current index to indexesSoFar and indexNamesSoFar\r
- traverseIndexes(g, rvi, rvis, arrayIndexes, arrayIndexes.get(arrayIndex + 1), \r
+ traverseIndexes(g, rvi, rvis, arrayIndexes, currentIndex + 1, \r
indexesSoFar + (i + 1) +",", indexNamesSoFar + (name) +",");\r
else {\r
// The last enumeration. Add [rvi[1, 1, 1] = rvi[index1, index1, index1]}and so on to the rvis map\r
// Find all redeclarations\r
for(Resource redeclaration : graph.syncRequest(new ObjectsWithType(module, sr.Module_redeclaration, sr.Redeclaration))) {\r
Resource replaced = graph.getSingleObject(redeclaration, sr.Redeclaration_replacedEnumeration);\r
- if(enumerations.contains(replaced)) {\r
+ while(enumerations.contains(replaced)) {\r
// Replace the redelcared enumeration in enumerations -list with the replacing enumeration\r
enumerations.add(enumerations.indexOf(replaced), graph.getSingleObject(redeclaration, sr.Redeclaration_replacingEnumeration));\r
enumerations.remove(replaced);\r
b.append(" parameter Integer size;\n");\r
b.append(" parameter Integer elements[:];\n");\r
b.append("end Enumeration_class;\n\n");\r
-\r
+ \r
for(Configuration conf : configurations)\r
- writeConfiguration(conf, b);\r
+ writeConfiguration(conf, "SpreadSheetBook", b);\r
+ \r
+ b.append(getGlobalSpreadSheets(configurations));\r
\r
return b.toString();\r
}\r
+ \r
+ /**\r
+ * \r
+ */\r
+ private static String getGlobalSpreadSheets(Collection<Configuration> configurations) {\r
+ StringBuilder sheets = new StringBuilder();\r
+ for(Configuration conf : configurations) {\r
+ if(conf.getModel() != null) {\r
+ for(IElement e : conf.getElements()) {\r
+ if(e instanceof Book) {\r
+ return ((Book)e).getBook();\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ return sheets.toString();\r
+ }\r
\r
/**\r
* Write a single configuration to a given string builder\r
* @param configuration Model or module configuration\r
* @param b String builder\r
*/\r
- private static void writeConfiguration(Configuration configuration, StringBuilder b) {\r
+ private static void writeConfiguration(Configuration configuration, String spreadSheetClass, StringBuilder b) {\r
String app;\r
\r
boolean game = RepresentationUtils.isGameExperimentActive();\r
ArrayList<Dependency> inputDependencies = new ArrayList<Dependency>();\r
ArrayList<Dependency> outputDependencies = new ArrayList<Dependency>();\r
HashMap<String, ArrayList<Input>> moduleInputs = new HashMap<String, ArrayList<Input>>();\r
- Book book = null;\r
\r
// Initialize lists\r
for(IElement element : configuration.getElements()) {\r
// References from child modules\r
inputDependencies.add(dependency);\r
}\r
- } else if (element instanceof Book) {\r
- // Spreadsheet book\r
- book = (Book)element;\r
}\r
}\r
\r
// If the configuration is model configuration, use model name. Otherwise, use configuration name.\r
ModuleType mt = configuration.getModuleType();\r
String className = mt != null ? (mt.getName().replace(" ", "")) : (configuration.getName().replace(" ", ""));\r
- b.append("class ").append(className).append('\n');\r
+ \r
+ b.append("class ").append(className);\r
+ \r
+ // Extend spreadsheetclass to get spreadsheet cell info\r
+ if(spreadSheetClass != null && !spreadSheetClass.isEmpty())\r
+ b.append("\n extends " + spreadSheetClass + ";");\r
+ \r
+ b.append("\n");\r
\r
b.append("// Variable definitions\n");\r
for(IndependentVariable variable : variables) {\r
}\r
}\r
\r
- if(book != null) {\r
- b.append("// Spreadsheet definition\n");\r
- b.append(book.markBook());\r
- }\r
-\r
boolean initialEquations = false;\r
for(Stock stock : stocks) {\r
app = stock.getInitialEquation();\r
\r
b.append("end ").append(className).append(";\n\n");\r
\r
-\r
- // Update sheet definitions to contain the elements that were used.\r
- if(book != null) {\r
- int s = b.indexOf(book.markBook());\r
- b.replace(s, s + book.markBook().length(), book.getBook());\r
- }\r
}\r
\r
/**\r
\r
public String getBook() {\r
StringBuilder book = new StringBuilder();\r
+ \r
+ book.append("partial class SpreadSheetBook\n");\r
+ \r
for(Sheet sheet : sheets)\r
book.append(sheet.getStringRepresentation());\r
+ \r
+ book.append("end SpreadSheetBook;");\r
+ \r
return book.toString();\r
+ \r
}\r
\r
}\r
\r
import org.simantics.layer0.Layer0;\r
import org.simantics.objmap.annotations.GraphType;\r
+import org.simantics.objmap.annotations.RelatedElement;\r
import org.simantics.objmap.annotations.RelatedElements;\r
import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.representation.annotations.BuiltinFunctions;\r
\r
\r
@RelatedValue(SysdynResource.URIs.SysdynModel_variableFilter)\r
private String variableFilter;\r
+ \r
+ @RelatedElement(SimulationResource.URIs.HasConfiguration)\r
+ private Configuration configuration;\r
\r
@RelatedElements(\r
value = Layer0.URIs.ConsistsOf,\r
public FunctionLibrary getBuiltins() {\r
return builtins;\r
}\r
+ \r
+ \r
+ /**\r
+ * \r
+ * @return Configuration of this model\r
+ */\r
+ public Configuration getModelConfiguration() {\r
+ return configuration;\r
+ }\r
}\r
Variant cell = matrix.get(i, j);\r
if(cell.getBinding().equals(Bindings.DOUBLE)) {\r
array[i][j] = (Double)cell.getValue();\r
- } else if (cell.getBinding().equals(Bindings.MUTABLE_STRING)) {\r
+ } else if (cell.getBinding().equals(Bindings.MUTABLE_STRING) || (cell.getBinding().equals(Bindings.STRING))) {\r
try {\r
array[i][j] = Double.parseDouble(cell.getValue().toString());\r
} catch (NumberFormatException e) {\r
private static boolean isParameter(IndependentVariable variable, Configuration configuration, String reference, boolean allowVariables) {\r
// Check if references are references to sheets or enumerations. \r
// Sheet and Enumeration references are allowed, since sheets contain only constants / parameters\r
-\r
String r = reference.split("\\.")[0]; \r
for(IElement element : configuration.getElements()) {\r
if(element instanceof Module) {\r
}\r
}\r
}\r
+ \r
+ // Try to find sheet in another way: this might be a module type configuration. Find the model configuration and its sheet\r
+ if(configuration.getModuleType() != null) {\r
+ Object parent = configuration.getModuleType().getParent();\r
+ if(parent != null && parent instanceof Model) {\r
+ configuration = ((Model)parent).getModelConfiguration();\r
+ for(IElement element : configuration.getElements()) {\r
+ if(element instanceof Book) {\r
+ for(Sheet sheet : ((Book)element).getSheets()) {\r
+ if(r.equals(sheet.getName())) {\r
+ return true;\r
+ }\r
+ }\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ \r
+ \r
// If there was no sheet for this reference name, or there was some other problem, return false\r
return false;\r
}\r
import org.simantics.sysdyn.expressionParser.Token;\r
import org.simantics.sysdyn.representation.Configuration;\r
import org.simantics.sysdyn.representation.IElement;\r
+import org.simantics.sysdyn.representation.Model;\r
+import org.simantics.sysdyn.representation.ModuleType;\r
import org.simantics.sysdyn.representation.Sheet;\r
import org.simantics.sysdyn.representation.Variable;\r
\r
for(String key : functionCalls.keySet()) {\r
String[] parts = key.split("\\.");\r
Object current = v.getParentConfiguration();\r
+ \r
+ // Hack. Sheets can currently exist only in models, not in module types.\r
+ ModuleType moduleType= ((Configuration)current).getModuleType();\r
+ if(moduleType != null) {\r
+ if(moduleType.getParent() != null && moduleType.getParent() instanceof Model)\r
+ current = ((Model)moduleType.getParent()).getModelConfiguration();\r
+ }\r
+ // end Hack.\r
+ \r
Object found = null;\r
for(int i = 0; i < parts.length && current != null; i++) {\r
found = null;\r