From 4e78f21805a214b7a367a7a1efc45f2d4fe0a426 Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 2 May 2012 07:18:12 +0000 Subject: [PATCH] Preparing for 1.6 release: Input/Output tab tweaks, diagram chart size tweak, better module parameter handling (refs #3414) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@24847 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/editor/SysdynEditorNamingService.java | 7 +- .../sysdyn/ui/properties/ModuleInputTab.java | 2 +- .../sysdyn/ui/properties/ModuleOutputTab.java | 2 +- .../widgets/modules/ParameterChildRule.java | 3 +- .../chart/element/ChartElementFactory.java | 2 +- .../sysdyn/manager/FunctionUtils.java | 4 +- .../simantics/sysdyn/manager/SysdynModel.java | 7 +- .../sysdyn/manager/SysdynResult.java | 4 +- .../sysdyn/modelica/ModelicaWriter.java | 2 +- .../sysdyn/representation/Module.java | 433 +++++++++--------- 10 files changed, 244 insertions(+), 222 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynEditorNamingService.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynEditorNamingService.java index ca386d6b..abb9f0b8 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynEditorNamingService.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynEditorNamingService.java @@ -37,9 +37,9 @@ public class SysdynEditorNamingService extends EditorNamingService2 { if(in instanceof IResourceEditorInput2) { IResourceEditorInput2 input = (IResourceEditorInput2) in; - Resource model = g.getPossibleResource(input.getModelURI()); if(!input.getRVI().isEmpty()) { + Resource model = g.getPossibleResource(input.getModelURI()); if(model != null) { Resource configuration = g.getPossibleObject(model, SimulationResource.getInstance(g).HasConfiguration); String configurationName = NameUtils.getSafeName(g, configuration); @@ -54,10 +54,7 @@ public class SysdynEditorNamingService extends EditorNamingService2 { return name + " : " + instanceOf; } } - - } else { - return NameUtils.getSafeLabel(g, model); - } + } } return super.getName(g, editorId, in); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleInputTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleInputTab.java index b29c0357..c4c92ac2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleInputTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleInputTab.java @@ -43,7 +43,7 @@ public class ModuleInputTab extends LabelPropertyTabContributor { ReferenceTable referenceTable = new ReferenceTable(body, support, SWT.NONE); String[] titles = { FIRSTCOLUMN, SECONDCOLUMN}; - int[] bounds = { 100, 100, 100, 100 }; + int[] bounds = { 200, 200 }; for (int i = 0; i < titles.length; i++) { TableViewerColumn column = new TableViewerColumn(referenceTable.getTableViewer(), SWT.NONE); column.getColumn().setText(titles[i]); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleOutputTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleOutputTab.java index 47a114e7..ef0e5cc4 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleOutputTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleOutputTab.java @@ -43,7 +43,7 @@ public class ModuleOutputTab extends LabelPropertyTabContributor { ReferenceTable referenceTable = new ReferenceTable(body, support, SWT.NONE); String[] titles = { FIRSTCOLUMN, SECONDCOLUMN}; - int[] bounds = { 100, 100, 100, 100 }; + int[] bounds = { 200, 200 }; for (int i = 0; i < titles.length; i++) { TableViewerColumn column = new TableViewerColumn(referenceTable.getTableViewer(), SWT.NONE); column.getColumn().setText(titles[i]); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/modules/ParameterChildRule.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/modules/ParameterChildRule.java index 3b270819..5d88453e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/modules/ParameterChildRule.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/modules/ParameterChildRule.java @@ -27,6 +27,7 @@ import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.IElement; import org.simantics.sysdyn.representation.IndependentVariable; +import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Variability; public class ParameterChildRule implements ChildRule { @@ -58,7 +59,7 @@ public class ParameterChildRule implements ChildRule { for(Resource variable : graph.syncRequest(new ObjectsWithType(configuration, L0.ConsistsOf, sr.IndependentVariable))) { IElement element = model.getElement(variable); - if(element instanceof IndependentVariable) + if(element instanceof IndependentVariable && !(element instanceof Stock)) if(!Variability.CONTINUOUS.equals(Variability.getVariability((IndependentVariable)element, false, varConf))) result.add(new ParameterNode(moduleInstance, varConf, variable, (IndependentVariable)element)); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java index 0a153c93..142774e7 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java @@ -98,7 +98,7 @@ public class ChartElementFactory extends SyncElementFactory { DefaultTransform.INSTANCE, StaticSymbolImageInitializer.INSTANCE, new StaticSymbolImpl(STATIC_IMAGE), - new ChartBoundsOutline(new Rectangle2D.Double(-40, -40, 80, 80)), + new ChartBoundsOutline(new Rectangle2D.Double(-20, -20, 60, 40)), new ElementPropertySetter(ChartSceneGraph.KEY_SG_NODE) ).setId(CLASS_ID); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java index 91fb1cd9..2a12c789 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java @@ -22,6 +22,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.simantics.Simantics; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -29,7 +30,6 @@ import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.util.Simantics; import org.simantics.layer0.Layer0; import org.simantics.simulation.ontology.SimulationResource; import org.simantics.sysdyn.Activator; @@ -58,7 +58,7 @@ public class FunctionUtils { } } else { String libraryname = NameUtils.getSafeName(graph, parent); - paths.add("..\\\\..\\\\libraries\\\\functions\\\\" + libraryname + ".mo"); + paths.add("..\\\\\\\\..\\\\\\\\libraries\\\\\\\\functions\\\\\\\\" + libraryname + ".mo"); } } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index eeff4a6b..740ae5a2 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -53,6 +53,7 @@ import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.IElement; import org.simantics.sysdyn.representation.IndependentVariable; import org.simantics.sysdyn.representation.Module; +import org.simantics.sysdyn.representation.ParameterOverride; import org.simantics.sysdyn.representation.SysdynSchema; import org.simantics.sysdyn.representation.expressions.IExpression; import org.simantics.sysdyn.representation.expressions.ParameterExpression; @@ -470,7 +471,11 @@ public class SysdynModel implements IMappingListener, IModel { if (element instanceof Module) { Module module = (Module) element; Configuration conf = module.getType().getConfiguration(); - inits.putAll(getInits(conf, prefix + module.getName() + ".")); + String prfx = prefix + module.getName() + "."; + inits.putAll(getInits(conf, prfx)); + for(ParameterOverride po : module.getParameterOverrides()) { + inits.put(prfx + po.getVariable().getName(), po.getExpression()); + } } else if (element instanceof IndependentVariable) { IndependentVariable variable = (IndependentVariable) element; //FIXME: more general solution for finding out if the variable is a parameter diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java index 1bfe6621..f081908e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java @@ -238,8 +238,8 @@ public class SysdynResult { double[] times_ = new double[size]; double[] values_ = new double[size]; for (int i=0; i redeclarations; - - @RelatedElements(Layer0.URIs.ConsistsOf) - private List consistsOf; - - @Override - public void accept(IElementVisitorVoid v) { - v.visit(this); - } - - public String getName() { - return name; - } - - public ModuleType getType() { - return type; - } - - /** - * Returns the declaration of a module with possible redeclared enumerations and parameter assignments - * - *

- * Enumeration redeclaration: - * parameter Integer Enum__size = Enum.size;
- * parameter Integer Enum__elements[:] = Enum.elements;
- * ModuleType ModuleType1(Enum.size = Enum__size, Enum.elements = Enum__elements);
- * - *

- * Temporary parameter variables are needed to avoid name conflicts when redeclaring an - * enumeration with the same name. - * - * @return Declaration of a module instance - */ - public String getDeclaration() { - - - String enumerationRedeclarations = getEnumerationRedeclarations(); - Pair inputRedeclarations = getInputRedeclarations(); - String parameterOverrides = getParameterOverrides(); - - StringBuilder redeclarations = new StringBuilder(); - - if(!enumerationRedeclarations.isEmpty() || !inputRedeclarations.first.isEmpty() || !parameterOverrides.isEmpty()) { - redeclarations.append("("); - redeclarations.append(enumerationRedeclarations); - - String ir = inputRedeclarations.first; - if(enumerationRedeclarations.isEmpty() && !inputRedeclarations.first.isEmpty()) - // remove first ", " if no enumeration redeclarations - ir = inputRedeclarations.first.substring(2); - redeclarations.append(ir); - - if(!parameterOverrides.isEmpty()) { - if(redeclarations.length() > 1) - redeclarations.append(", "); - redeclarations.append(parameterOverrides); - } - - redeclarations.append(")"); - } - - StringBuilder sb = new StringBuilder(); - - // Possible reference variables - if(inputRedeclarations.second != null && !inputRedeclarations.second.isEmpty()) { - sb.append(inputRedeclarations.second); - } - - sb.append(" "); - sb.append(getType().getName()); - sb.append(" "); - sb.append(getName()); - sb.append(redeclarations.toString()); // possible redeclarations - sb.append(";\n"); - return sb.toString(); - } - - /** - * Get possible enumeration redeclarations. - * @return enumeration redeclarations or empty string - */ - private String getEnumerationRedeclarations() { - StringBuilder redeclarations = new StringBuilder(); - if(!getRedeclarations().isEmpty()) { - - Iterator i = getRedeclarations().iterator(); - while(i.hasNext()) { - Redeclaration rd = i.next(); - redeclarations.append(rd.getRedeclaration()); - if(i.hasNext()) - redeclarations.append(","); - } - } - return redeclarations.toString(); - } - - /** - * Get possible input redeclarations - * @return input redeclarations or empty string - */ - private Pair getInputRedeclarations() { - StringBuilder declarations = new StringBuilder(); - StringBuilder references = new StringBuilder(); - - for(IElement element : parentConfiguration.getElements()) { - if (element instanceof Dependency) { - Dependency dependency = (Dependency)element; - if(dependency.getHead().equals(this)){ - Input reference = (Input)dependency.refersTo(); - Variable outputVariable = (Variable)dependency.getTail(); - String name = outputVariable.getName(); - - Module module = (Module)dependency.getHead(); - - if(reference != null && reference.getName() != null) { - if(reference.getVariability() == null || reference.getVariability().isEmpty()) - continue; // Only parameters and constants are redeclared - - for(IElement e : module.getType().getConfiguration().getElements()) { - if(e instanceof Variable) { - Variable v = (Variable)e; - - if(v.getName() != null && outputVariable.getName().equals(v.getName())) { - /* - * The target module contains a variable with the same name. Need to - * avoid Module(variable = variable) situations - */ - String declaration; - if(outputVariable instanceof Input) - declaration = ((Input)outputVariable).getDeclaration(); - else - declaration = ((IndependentVariable)outputVariable).getDeclaration(); - - if(declaration.contains("=")) - declaration = declaration.substring(0, declaration.indexOf("=") - 1); // Break to "=" - else - declaration = declaration.substring(0, declaration.length() - 2); // Remove ending ";\n" - - name = outputVariable.getName() + "_reference"; - declaration = declaration.replace(outputVariable.getName(), name); - declaration += " = " + outputVariable.getName() + " /* Reference value to avoid name conflicts in module instantiation */ ;\n"; - references.append(declaration); - break; - } - } - } - - declarations.append(", " + reference.getName() + " = " + name); - } - } - } - } - return new Pair(declarations.toString(), references.toString()); - } - - public Configuration getParentConfiguration() { - return this.parentConfiguration; - } - - /** - * Return the list of the defined redeclarations for this module instance. - * - * @return List of the defined redeclarations or an empty list if redeclarations have not been set - */ - public List getRedeclarations() { - if(redeclarations == null) { - redeclarations = new ArrayList(); - } - return redeclarations; - } - - - /** - * Get all parameter override elements for this module instance - * @return parameter overrides - */ - public String getParameterOverrides() { - String result = ""; - - if(consistsOf != null) { - for(Entity e : consistsOf) { - if(e instanceof ParameterOverride) { - ParameterOverride po = (ParameterOverride) e; - IndependentVariable var = po.getVariable(); - if(!Variability.CONTINUOUS.equals(Variability.getVariability(var, false, parentConfiguration))) { - if(!result.isEmpty()) - result += ", "; - - result += po.getOverride(); - } - - } - } - } - - return result; - } + @RelatedValue(Layer0.URIs.HasName) + private String name; + + @RelatedElement(Layer0.URIs.PartOf) + private Configuration parentConfiguration; + + @RelatedElement(Layer0.URIs.InstanceOf) + private ModuleType type; + + @RelatedElements(SysdynResource.URIs.Module_redeclaration) + private List redeclarations; + + @RelatedElements(Layer0.URIs.ConsistsOf) + private List consistsOf; + + @Override + public void accept(IElementVisitorVoid v) { + v.visit(this); + } + + public String getName() { + return name; + } + + public ModuleType getType() { + return type; + } + + /** + * Returns the declaration of a module with possible redeclared enumerations and parameter assignments + * + *

+ * Enumeration redeclaration: + * parameter Integer Enum__size = Enum.size;
+ * parameter Integer Enum__elements[:] = Enum.elements;
+ * ModuleType ModuleType1(Enum.size = Enum__size, Enum.elements = Enum__elements);
+ * + *

+ * Temporary parameter variables are needed to avoid name conflicts when redeclaring an + * enumeration with the same name. + * + * @return Declaration of a module instance + */ + public String getDeclaration() { + + + String enumerationRedeclarations = getEnumerationRedeclarations(); + Pair inputRedeclarations = getInputRedeclarations(); + String parameterOverrides = getParameterOverrideString(); + + StringBuilder redeclarations = new StringBuilder(); + + if(!enumerationRedeclarations.isEmpty() || !inputRedeclarations.first.isEmpty() || !parameterOverrides.isEmpty()) { + redeclarations.append("("); + redeclarations.append(enumerationRedeclarations); + + String ir = inputRedeclarations.first; + if(enumerationRedeclarations.isEmpty() && !inputRedeclarations.first.isEmpty()) + // remove first ", " if no enumeration redeclarations + ir = inputRedeclarations.first.substring(2); + redeclarations.append(ir); + + if(!parameterOverrides.isEmpty()) { + if(redeclarations.length() > 1) + redeclarations.append(", "); + redeclarations.append(parameterOverrides); + } + + redeclarations.append(")"); + } + + StringBuilder sb = new StringBuilder(); + + // Possible reference variables + if(inputRedeclarations.second != null && !inputRedeclarations.second.isEmpty()) { + sb.append(inputRedeclarations.second); + } + + sb.append(" "); + sb.append(getType().getName()); + sb.append(" "); + sb.append(getName()); + sb.append(redeclarations.toString()); // possible redeclarations + sb.append(";\n"); + return sb.toString(); + } + + /** + * Get possible enumeration redeclarations. + * @return enumeration redeclarations or empty string + */ + private String getEnumerationRedeclarations() { + StringBuilder redeclarations = new StringBuilder(); + if(!getRedeclarations().isEmpty()) { + + Iterator i = getRedeclarations().iterator(); + while(i.hasNext()) { + Redeclaration rd = i.next(); + redeclarations.append(rd.getRedeclaration()); + if(i.hasNext()) + redeclarations.append(","); + } + } + return redeclarations.toString(); + } + + /** + * Get possible input redeclarations + * @return input redeclarations or empty string + */ + private Pair getInputRedeclarations() { + StringBuilder declarations = new StringBuilder(); + StringBuilder references = new StringBuilder(); + + for(IElement element : parentConfiguration.getElements()) { + if (element instanceof Dependency) { + Dependency dependency = (Dependency)element; + if(dependency.getHead().equals(this)){ + Input reference = (Input)dependency.refersTo(); + Variable outputVariable = (Variable)dependency.getTail(); + String name = outputVariable.getName(); + + Module module = (Module)dependency.getHead(); + + if(reference != null && reference.getName() != null) { + if(reference.getVariability() == null || reference.getVariability().isEmpty()) + continue; // Only parameters and constants are redeclared + + for(IElement e : module.getType().getConfiguration().getElements()) { + if(e instanceof Variable) { + Variable v = (Variable)e; + + if(v.getName() != null && outputVariable.getName().equals(v.getName())) { + /* + * The target module contains a variable with the same name. Need to + * avoid Module(variable = variable) situations + */ + String declaration; + if(outputVariable instanceof Input) + declaration = ((Input)outputVariable).getDeclaration(); + else + declaration = ((IndependentVariable)outputVariable).getDeclaration(); + + if(declaration.contains("=")) + declaration = declaration.substring(0, declaration.indexOf("=") - 1); // Break to "=" + else + declaration = declaration.substring(0, declaration.length() - 2); // Remove ending ";\n" + + name = outputVariable.getName() + "_reference"; + declaration = declaration.replace(outputVariable.getName(), name); + declaration += " = " + outputVariable.getName() + " /* Reference value to avoid name conflicts in module instantiation */ ;\n"; + references.append(declaration); + break; + } + } + } + + declarations.append(", " + reference.getName() + " = " + name); + } + } + } + } + return new Pair(declarations.toString(), references.toString()); + } + + public Configuration getParentConfiguration() { + return this.parentConfiguration; + } + + /** + * Return the list of the defined redeclarations for this module instance. + * + * @return List of the defined redeclarations or an empty list if redeclarations have not been set + */ + public List getRedeclarations() { + if(redeclarations == null) { + redeclarations = new ArrayList(); + } + return redeclarations; + } + + + /** + * Get all parameter override elements for this module instance as a string "param = value, param2 = value2, ..." + *

+ * Only used with game experiments. Other experiments use inits-file. + * @return parameter overrides + */ + public String getParameterOverrideString() { + String result = ""; + + if(RepresentationUtils.isGameExperimentActive()) { + for(ParameterOverride po : getParameterOverrides()) { + IndependentVariable var = po.getVariable(); + if(!Variability.CONTINUOUS.equals(Variability.getVariability(var, false, parentConfiguration))) { + if(!result.isEmpty()) + result += ", "; + + result += po.getOverride(); + } + } + } + + return result; + } + + + /** + * Get all parameter overrides of this module instance + * @return + */ + public Set getParameterOverrides() { + HashSet result = new HashSet(); + if(consistsOf != null) { + for(Entity e : consistsOf) { + if(e instanceof ParameterOverride) { + result.add( (ParameterOverride) e); + } + } + } + return result; + } + } -- 2.47.1