From: Jani Mäkinen Date: Wed, 19 Aug 2020 11:47:31 +0000 (+0300) Subject: Make sure nested modules are in correct order when writing modelica code X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F86%2F4386%2F2;p=simantics%2Fsysdyn.git Make sure nested modules are in correct order when writing modelica code gitlab #59 Change-Id: Id469cdf6cc79f3320398dad8e713643241fb83c1 --- diff --git a/bundles/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java b/bundles/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java index 97d84943..9c0287d6 100644 --- a/bundles/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/bundles/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -48,6 +48,15 @@ public class ModelicaWriter { public static final String VAR_STOP = "stopTime"; public static final String VAR_STEP = "timeStep"; + static String configurationName(Configuration c) { + ModuleType moduleType = c.getModuleType(); + if(moduleType == null) { + return "Model"; + } else { + return moduleType.getName(); + } + } + /** * Write a collection of configurations into a single Modelica code * @param isGame @@ -57,9 +66,19 @@ public class ModelicaWriter { */ public static String write(Collection _configurations, double startTime, double stopTime, double timeStep, boolean isGame, String omVersion) { + ArrayList configurations = new ArrayList(_configurations); Collections.sort(configurations, new Comparator() { - + + Configuration findConfiguration(ModuleType type) { + for(Configuration c : _configurations) { + if(type.equals(c.getModuleType())) { + return c; + } + } + return null; + } + boolean uses(Configuration o1, Configuration o2) { ModuleType type = o2.getModuleType(); if(type == null) return false; @@ -68,6 +87,11 @@ public class ModelicaWriter { Module m = (Module)e; if(m.getType().equals(type)) { return true; + } else { + Configuration c = findConfiguration(m.getType()); + if(c != null) { + return uses(c, o2); + } } } } @@ -78,7 +102,7 @@ public class ModelicaWriter { public int compare(Configuration o1, Configuration o2) { if(uses(o1, o2)) return 1; else if(uses(o2, o1)) return -1; - else return 0; + else return configurationName(o1).compareTo(configurationName(o2)); } });