From a4cb19c39112c67352f58f60c1fc7fab6d147dc5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jani=20M=C3=A4kinen?= Date: Wed, 19 Aug 2020 14:47:31 +0300 Subject: [PATCH] Make sure nested modules are in correct order when writing modelica code gitlab #59 Change-Id: Id469cdf6cc79f3320398dad8e713643241fb83c1 --- .../sysdyn/modelica/ModelicaWriter.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) 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)); } }); -- 2.47.1