package org.simantics.sysdyn.representation;\r
\r
import java.util.ArrayList;\r
+import java.util.HashSet;\r
import java.util.Iterator;\r
import java.util.List;\r
+import java.util.Set;\r
\r
import org.simantics.layer0.Layer0;\r
import org.simantics.objmap.annotations.GraphType;\r
import org.simantics.objmap.annotations.RelatedElements;\r
import org.simantics.objmap.annotations.RelatedValue;\r
import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.representation.utils.RepresentationUtils;\r
import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid;\r
import org.simantics.utils.datastructures.Pair;\r
\r
@GraphType(SysdynResource.URIs.Module)\r
public class Module implements IElement {\r
\r
- @RelatedValue(Layer0.URIs.HasName)\r
- private String name;\r
-\r
- @RelatedElement(Layer0.URIs.PartOf)\r
- private Configuration parentConfiguration;\r
-\r
- @RelatedElement(Layer0.URIs.InstanceOf)\r
- private ModuleType type;\r
-\r
- @RelatedElements(SysdynResource.URIs.Module_redeclaration)\r
- private List<Redeclaration> redeclarations;\r
-\r
- @RelatedElements(Layer0.URIs.ConsistsOf)\r
- private List<Entity> consistsOf;\r
-\r
- @Override\r
- public void accept(IElementVisitorVoid v) {\r
- v.visit(this);\r
- }\r
-\r
- public String getName() {\r
- return name;\r
- }\r
-\r
- public ModuleType getType() {\r
- return type;\r
- }\r
-\r
- /**\r
- * Returns the declaration of a module with possible redeclared enumerations and parameter assignments\r
- *\r
- * <p>\r
- * Enumeration redeclaration:\r
- * parameter Integer Enum__size = Enum.size; <br>\r
- * parameter Integer Enum__elements[:] = Enum.elements; <br>\r
- * ModuleType ModuleType1(Enum.size = Enum__size, Enum.elements = Enum__elements); <br>\r
- * \r
- * <p>\r
- * Temporary parameter variables are needed to avoid name conflicts when redeclaring an \r
- * enumeration with the same name.\r
- * \r
- * @return Declaration of a module instance\r
- */\r
- public String getDeclaration() {\r
-\r
-\r
- String enumerationRedeclarations = getEnumerationRedeclarations();\r
- Pair<String, String> inputRedeclarations = getInputRedeclarations();\r
- String parameterOverrides = getParameterOverrides();\r
-\r
- StringBuilder redeclarations = new StringBuilder();\r
-\r
- if(!enumerationRedeclarations.isEmpty() || !inputRedeclarations.first.isEmpty() || !parameterOverrides.isEmpty()) {\r
- redeclarations.append("(");\r
- redeclarations.append(enumerationRedeclarations);\r
-\r
- String ir = inputRedeclarations.first;\r
- if(enumerationRedeclarations.isEmpty() && !inputRedeclarations.first.isEmpty())\r
- // remove first ", " if no enumeration redeclarations\r
- ir = inputRedeclarations.first.substring(2); \r
- redeclarations.append(ir);\r
-\r
- if(!parameterOverrides.isEmpty()) {\r
- if(redeclarations.length() > 1)\r
- redeclarations.append(", ");\r
- redeclarations.append(parameterOverrides);\r
- }\r
-\r
- redeclarations.append(")");\r
- }\r
-\r
- StringBuilder sb = new StringBuilder();\r
-\r
- // Possible reference variables\r
- if(inputRedeclarations.second != null && !inputRedeclarations.second.isEmpty()) {\r
- sb.append(inputRedeclarations.second);\r
- }\r
-\r
- sb.append(" ");\r
- sb.append(getType().getName());\r
- sb.append(" ");\r
- sb.append(getName());\r
- sb.append(redeclarations.toString()); // possible redeclarations\r
- sb.append(";\n");\r
- return sb.toString();\r
- }\r
-\r
- /**\r
- * Get possible enumeration redeclarations.\r
- * @return enumeration redeclarations or empty string\r
- */\r
- private String getEnumerationRedeclarations() {\r
- StringBuilder redeclarations = new StringBuilder();\r
- if(!getRedeclarations().isEmpty()) {\r
-\r
- Iterator<Redeclaration> i = getRedeclarations().iterator();\r
- while(i.hasNext()) {\r
- Redeclaration rd = i.next();\r
- redeclarations.append(rd.getRedeclaration());\r
- if(i.hasNext())\r
- redeclarations.append(",");\r
- }\r
- }\r
- return redeclarations.toString();\r
- }\r
-\r
- /**\r
- * Get possible input redeclarations\r
- * @return input redeclarations or empty string\r
- */\r
- private Pair<String, String> getInputRedeclarations() {\r
- StringBuilder declarations = new StringBuilder();\r
- StringBuilder references = new StringBuilder();\r
-\r
- for(IElement element : parentConfiguration.getElements()) {\r
- if (element instanceof Dependency) {\r
- Dependency dependency = (Dependency)element;\r
- if(dependency.getHead().equals(this)){\r
- Input reference = (Input)dependency.refersTo();\r
- Variable outputVariable = (Variable)dependency.getTail();\r
- String name = outputVariable.getName();\r
-\r
- Module module = (Module)dependency.getHead();\r
-\r
- if(reference != null && reference.getName() != null) {\r
- if(reference.getVariability() == null || reference.getVariability().isEmpty())\r
- continue; // Only parameters and constants are redeclared\r
-\r
- for(IElement e : module.getType().getConfiguration().getElements()) {\r
- if(e instanceof Variable) {\r
- Variable v = (Variable)e;\r
-\r
- if(v.getName() != null && outputVariable.getName().equals(v.getName())) {\r
- /*\r
- * The target module contains a variable with the same name. Need to\r
- * avoid Module(variable = variable) situations \r
- */\r
- String declaration;\r
- if(outputVariable instanceof Input)\r
- declaration = ((Input)outputVariable).getDeclaration();\r
- else\r
- declaration = ((IndependentVariable)outputVariable).getDeclaration();\r
-\r
- if(declaration.contains("="))\r
- declaration = declaration.substring(0, declaration.indexOf("=") - 1); // Break to "="\r
- else\r
- declaration = declaration.substring(0, declaration.length() - 2); // Remove ending ";\n" \r
-\r
- name = outputVariable.getName() + "_reference";\r
- declaration = declaration.replace(outputVariable.getName(), name);\r
- declaration += " = " + outputVariable.getName() + " /* Reference value to avoid name conflicts in module instantiation */ ;\n";\r
- references.append(declaration);\r
- break;\r
- }\r
- }\r
- }\r
-\r
- declarations.append(", " + reference.getName() + " = " + name);\r
- }\r
- }\r
- }\r
- }\r
- return new Pair<String, String>(declarations.toString(), references.toString());\r
- }\r
-\r
- public Configuration getParentConfiguration() {\r
- return this.parentConfiguration;\r
- }\r
-\r
- /**\r
- * Return the list of the defined redeclarations for this module instance.\r
- * \r
- * @return List of the defined redeclarations or an empty list if redeclarations have not been set \r
- */\r
- public List<Redeclaration> getRedeclarations() {\r
- if(redeclarations == null) {\r
- redeclarations = new ArrayList<Redeclaration>();\r
- }\r
- return redeclarations;\r
- }\r
-\r
-\r
- /**\r
- * Get all parameter override elements for this module instance\r
- * @return parameter overrides\r
- */\r
- public String getParameterOverrides() {\r
- String result = "";\r
-\r
- if(consistsOf != null) {\r
- for(Entity e : consistsOf) {\r
- if(e instanceof ParameterOverride) {\r
- ParameterOverride po = (ParameterOverride) e;\r
- IndependentVariable var = po.getVariable();\r
- if(!Variability.CONTINUOUS.equals(Variability.getVariability(var, false, parentConfiguration))) {\r
- if(!result.isEmpty())\r
- result += ", ";\r
-\r
- result += po.getOverride();\r
- }\r
-\r
- }\r
- }\r
- }\r
-\r
- return result;\r
- }\r
+ @RelatedValue(Layer0.URIs.HasName)\r
+ private String name;\r
+\r
+ @RelatedElement(Layer0.URIs.PartOf)\r
+ private Configuration parentConfiguration;\r
+\r
+ @RelatedElement(Layer0.URIs.InstanceOf)\r
+ private ModuleType type;\r
+\r
+ @RelatedElements(SysdynResource.URIs.Module_redeclaration)\r
+ private List<Redeclaration> redeclarations;\r
+\r
+ @RelatedElements(Layer0.URIs.ConsistsOf)\r
+ private List<Entity> consistsOf;\r
+\r
+ @Override\r
+ public void accept(IElementVisitorVoid v) {\r
+ v.visit(this);\r
+ }\r
+\r
+ public String getName() {\r
+ return name;\r
+ }\r
+\r
+ public ModuleType getType() {\r
+ return type;\r
+ }\r
+\r
+ /**\r
+ * Returns the declaration of a module with possible redeclared enumerations and parameter assignments\r
+ *\r
+ * <p>\r
+ * Enumeration redeclaration:\r
+ * parameter Integer Enum__size = Enum.size; <br>\r
+ * parameter Integer Enum__elements[:] = Enum.elements; <br>\r
+ * ModuleType ModuleType1(Enum.size = Enum__size, Enum.elements = Enum__elements); <br>\r
+ * \r
+ * <p>\r
+ * Temporary parameter variables are needed to avoid name conflicts when redeclaring an \r
+ * enumeration with the same name.\r
+ * \r
+ * @return Declaration of a module instance\r
+ */\r
+ public String getDeclaration() {\r
+\r
+\r
+ String enumerationRedeclarations = getEnumerationRedeclarations();\r
+ Pair<String, String> inputRedeclarations = getInputRedeclarations();\r
+ String parameterOverrides = getParameterOverrideString();\r
+\r
+ StringBuilder redeclarations = new StringBuilder();\r
+\r
+ if(!enumerationRedeclarations.isEmpty() || !inputRedeclarations.first.isEmpty() || !parameterOverrides.isEmpty()) {\r
+ redeclarations.append("(");\r
+ redeclarations.append(enumerationRedeclarations);\r
+\r
+ String ir = inputRedeclarations.first;\r
+ if(enumerationRedeclarations.isEmpty() && !inputRedeclarations.first.isEmpty())\r
+ // remove first ", " if no enumeration redeclarations\r
+ ir = inputRedeclarations.first.substring(2); \r
+ redeclarations.append(ir);\r
+\r
+ if(!parameterOverrides.isEmpty()) {\r
+ if(redeclarations.length() > 1)\r
+ redeclarations.append(", ");\r
+ redeclarations.append(parameterOverrides);\r
+ }\r
+\r
+ redeclarations.append(")");\r
+ }\r
+\r
+ StringBuilder sb = new StringBuilder();\r
+\r
+ // Possible reference variables\r
+ if(inputRedeclarations.second != null && !inputRedeclarations.second.isEmpty()) {\r
+ sb.append(inputRedeclarations.second);\r
+ }\r
+\r
+ sb.append(" ");\r
+ sb.append(getType().getName());\r
+ sb.append(" ");\r
+ sb.append(getName());\r
+ sb.append(redeclarations.toString()); // possible redeclarations\r
+ sb.append(";\n");\r
+ return sb.toString();\r
+ }\r
+\r
+ /**\r
+ * Get possible enumeration redeclarations.\r
+ * @return enumeration redeclarations or empty string\r
+ */\r
+ private String getEnumerationRedeclarations() {\r
+ StringBuilder redeclarations = new StringBuilder();\r
+ if(!getRedeclarations().isEmpty()) {\r
+\r
+ Iterator<Redeclaration> i = getRedeclarations().iterator();\r
+ while(i.hasNext()) {\r
+ Redeclaration rd = i.next();\r
+ redeclarations.append(rd.getRedeclaration());\r
+ if(i.hasNext())\r
+ redeclarations.append(",");\r
+ }\r
+ }\r
+ return redeclarations.toString();\r
+ }\r
+\r
+ /**\r
+ * Get possible input redeclarations\r
+ * @return input redeclarations or empty string\r
+ */\r
+ private Pair<String, String> getInputRedeclarations() {\r
+ StringBuilder declarations = new StringBuilder();\r
+ StringBuilder references = new StringBuilder();\r
+\r
+ for(IElement element : parentConfiguration.getElements()) {\r
+ if (element instanceof Dependency) {\r
+ Dependency dependency = (Dependency)element;\r
+ if(dependency.getHead().equals(this)){\r
+ Input reference = (Input)dependency.refersTo();\r
+ Variable outputVariable = (Variable)dependency.getTail();\r
+ String name = outputVariable.getName();\r
+\r
+ Module module = (Module)dependency.getHead();\r
+\r
+ if(reference != null && reference.getName() != null) {\r
+ if(reference.getVariability() == null || reference.getVariability().isEmpty())\r
+ continue; // Only parameters and constants are redeclared\r
+\r
+ for(IElement e : module.getType().getConfiguration().getElements()) {\r
+ if(e instanceof Variable) {\r
+ Variable v = (Variable)e;\r
+\r
+ if(v.getName() != null && outputVariable.getName().equals(v.getName())) {\r
+ /*\r
+ * The target module contains a variable with the same name. Need to\r
+ * avoid Module(variable = variable) situations \r
+ */\r
+ String declaration;\r
+ if(outputVariable instanceof Input)\r
+ declaration = ((Input)outputVariable).getDeclaration();\r
+ else\r
+ declaration = ((IndependentVariable)outputVariable).getDeclaration();\r
+\r
+ if(declaration.contains("="))\r
+ declaration = declaration.substring(0, declaration.indexOf("=") - 1); // Break to "="\r
+ else\r
+ declaration = declaration.substring(0, declaration.length() - 2); // Remove ending ";\n" \r
+\r
+ name = outputVariable.getName() + "_reference";\r
+ declaration = declaration.replace(outputVariable.getName(), name);\r
+ declaration += " = " + outputVariable.getName() + " /* Reference value to avoid name conflicts in module instantiation */ ;\n";\r
+ references.append(declaration);\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ declarations.append(", " + reference.getName() + " = " + name);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return new Pair<String, String>(declarations.toString(), references.toString());\r
+ }\r
+\r
+ public Configuration getParentConfiguration() {\r
+ return this.parentConfiguration;\r
+ }\r
+\r
+ /**\r
+ * Return the list of the defined redeclarations for this module instance.\r
+ * \r
+ * @return List of the defined redeclarations or an empty list if redeclarations have not been set \r
+ */\r
+ public List<Redeclaration> getRedeclarations() {\r
+ if(redeclarations == null) {\r
+ redeclarations = new ArrayList<Redeclaration>();\r
+ }\r
+ return redeclarations;\r
+ }\r
+\r
+\r
+ /**\r
+ * Get all parameter override elements for this module instance as a string "param = value, param2 = value2, ..."\r
+ * <p>\r
+ * Only used with game experiments. Other experiments use inits-file.\r
+ * @return parameter overrides\r
+ */\r
+ public String getParameterOverrideString() {\r
+ String result = "";\r
+\r
+ if(RepresentationUtils.isGameExperimentActive()) {\r
+ for(ParameterOverride po : getParameterOverrides()) {\r
+ IndependentVariable var = po.getVariable();\r
+ if(!Variability.CONTINUOUS.equals(Variability.getVariability(var, false, parentConfiguration))) {\r
+ if(!result.isEmpty())\r
+ result += ", ";\r
+\r
+ result += po.getOverride();\r
+ }\r
+ }\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+\r
+ /**\r
+ * Get all parameter overrides of this module instance\r
+ * @return\r
+ */\r
+ public Set<ParameterOverride> getParameterOverrides() {\r
+ HashSet<ParameterOverride> result = new HashSet<ParameterOverride>();\r
+ if(consistsOf != null) {\r
+ for(Entity e : consistsOf) {\r
+ if(e instanceof ParameterOverride) {\r
+ result.add( (ParameterOverride) e);\r
+ }\r
+ }\r
+ }\r
+ return result; \r
+ }\r
+\r
\r
}\r