Merge branch 'development' of https://gitlab.lrz.de/lrr-tum/students/eragp-misim...
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / examples / ReserializeJSONsSettingUsages.java
index f1e90aa..0788589 100644 (file)
@@ -15,12 +15,14 @@ import java.util.function.Function;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.mograsim.logic.model.am2900.Am2900Loader;
 import net.mograsim.logic.model.model.LogicModelModifiable;
 import net.mograsim.logic.model.model.components.ModelComponent;
 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
 import net.mograsim.logic.model.model.components.submodels.SubmodelInterface;
 import net.mograsim.logic.model.model.wires.ModelWire;
+import net.mograsim.logic.model.model.wires.ModelWireCrossPoint;
 import net.mograsim.logic.model.model.wires.MovablePin;
 import net.mograsim.logic.model.model.wires.Pin;
 import net.mograsim.logic.model.model.wires.PinUsage;
@@ -28,11 +30,15 @@ import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
 import net.mograsim.logic.model.serializing.IdentifyParams;
 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
 import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
+import net.mograsim.logic.model.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler;
 
 public class ReserializeJSONsSettingUsages
 {
+       public static double GRIDSIZE = 2.5;
        public static boolean changePinUsages = false;
-       public static boolean changeComponentNames = true;
+       public static boolean changeComponentNames = false;
+       public static boolean snapWCPs = true;
+       public static boolean warnNonSnappedPoints = true;
 
        public static void main(String[] args) throws IOException
        {
@@ -58,13 +64,13 @@ public class ReserializeJSONsSettingUsages
                }
        }
 
-       public static void reserializeJSON(Path json, Scanner sysin)
+       public static void reserializeJSON(Path componentPath, Scanner sysin)
        {
                try
                {
                        DeserializedSubmodelComponent comp = (DeserializedSubmodelComponent) IndirectModelComponentCreator
-                                       .createComponent(new LogicModelModifiable(), "jsonfile:" + json.toString());
-                       System.out.println("Reserializing " + json);
+                                       .createComponent(new LogicModelModifiable(), "jsonfile:" + componentPath.toString());
+                       System.out.println("Reserializing " + componentPath);
                        if (changePinUsages)
                                comp.getSupermodelPins().entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).map(Entry::getValue).forEach(pin ->
                                {
@@ -86,9 +92,9 @@ public class ReserializeJSONsSettingUsages
                                        setInterfacePinUsage(comp, pin, usage);
                                });
                        LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
+                       Map<String, String> componentNameRemapping = new HashMap<>();
                        if (changeComponentNames)
                        {
-                               Map<String, String> componentNameRemapping = new HashMap<>();
                                componentNameRemapping.put(SubmodelComponent.SUBMODEL_INTERFACE_NAME, SubmodelComponent.SUBMODEL_INTERFACE_NAME);
                                LogicModelModifiable tempModel = new LogicModelModifiable();
                                IdentifyParams iP = new IdentifyParams();
@@ -126,21 +132,66 @@ public class ReserializeJSONsSettingUsages
 
                                Optional<ModelComponent> o;
                                while ((o = submodelModifiable.getComponentsByName().values().stream()
-                                               .filter(c -> !c.name.equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)).findAny()).isPresent())
+                                               .filter(c -> !c.getName().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)).findAny()).isPresent())
                                        submodelModifiable.destroyComponent(o.get());
 
-                               tempModel.getComponentsByName().values().stream().filter(c -> !c.name.equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME))
-                                               .forEach(c -> IndirectModelComponentCreator
-                                                               .createComponent(submodelModifiable, c.getIDForSerializing(iP), c.getParamsForSerializingJSON(iP), c.name)
-                                                               .moveTo(c.getPosX(), c.getPosY()));
+                               tempModel.getComponentsByName().values().stream()
+                                               .filter(c -> !c.getName().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME))
+                                               .forEach(c -> IndirectModelComponentCreator.createComponent(submodelModifiable, c.getIDForSerializing(iP),
+                                                               c.getParamsForSerializingJSON(iP), c.getName()).moveTo(c.getPosX(), c.getPosY()));
                                for (ModelWire w : tempModel.getWiresByName().values())
                                        createWire(Function.identity(), submodelModifiable, w);
                        }
-                       SubmodelComponentSerializer.serialize(comp, json.toString());
+                       if (snapWCPs)
+                               submodelModifiable.getComponentsByName().values().stream().filter(c -> c instanceof ModelWireCrossPoint).forEach(c ->
+                               {
+                                       double x = c.getPosX();
+                                       double y = c.getPosY();
+                                       c.moveTo(x % GRIDSIZE == 0 ? x - 1 : x, y % GRIDSIZE == 0 ? y - 1 : y);
+                               });
+                       if (warnNonSnappedPoints)
+                       {
+                               submodelModifiable.getComponentsByName().values().forEach(c ->
+                               {
+                                       double x = c.getPosX();
+                                       double y = c.getPosY();
+                                       if (c instanceof ModelWireCrossPoint)
+                                       {
+                                               x++;
+                                               y++;
+                                       }
+                                       if (x % GRIDSIZE != 0 || y % GRIDSIZE != 0)
+                                               System.out.println("  Component " + c.getName() + " (type " + c.getIDForSerializing(new IdentifyParams())
+                                                               + ") is not snapped to grid: " + x + "," + y);
+                               });
+                               submodelModifiable.getWiresByName().values().stream().forEach(w ->
+                               {
+                                       Point[] p = w.getPath();
+                                       if (p != null)
+                                               for (int i = 0; i < p.length; i++)
+                                                       if (p[i].x % GRIDSIZE != 0 || p[i].y % GRIDSIZE != 0)
+                                                               System.out.println(
+                                                                               "  Wire " + w.name + " path point #" + i + " is not snapped to grid: " + p[i].x + "," + p[i].y);
+                               });
+                               comp.getPins().values().forEach(p ->
+                               {
+                                       if (p.getRelX() % GRIDSIZE != 0 || p.getRelY() % GRIDSIZE != 0)
+                                               System.out.println("  Interface point " + p.name + " is not snapped to grid: " + p.getRelX() + "," + p.getRelY());
+                               });
+                       }
+                       SubmodelComponentSerializer.serialize(comp, componentPath.toString());
+                       if (changeComponentNames && (comp.getHighLevelStateHandler() == null
+                                       || !(comp.getHighLevelStateHandler() instanceof DefaultHighLevelStateHandler)))
+                       {
+                               System.out.println("  A non-default HighLevelStateHandler was detected. Check for changes there manually.");
+                               System.out.print("  Empty line to continue to next component, old component name to get new component name >");
+                               for (String line = sysin.nextLine(); !line.equals(""); line = sysin.nextLine())
+                                       System.out.println("  " + line + "->" + componentNameRemapping.get(line) + " >");
+                       }
                }
                catch (Exception e)
                {
-                       System.err.println("An error occurred visiting " + json + ":");
+                       System.err.println("An error occurred visiting " + componentPath + ":");
                        e.printStackTrace();
                }
        }
@@ -156,7 +207,7 @@ public class ReserializeJSONsSettingUsages
        private static Pin getRemappedPin(Function<String, String> componentNameRemapping, LogicModelModifiable tempModelForDefaultNames,
                        Pin pin)
        {
-               return tempModelForDefaultNames.getComponentsByName().get(componentNameRemapping.apply(pin.component.name)).getPin(pin.name);
+               return tempModelForDefaultNames.getComponentsByName().get(componentNameRemapping.apply(pin.component.getName())).getPin(pin.name);
        }
 
        private static int compareStringsWithIntegers(String a, String b)