Fixed ReserializeAndVerifyJSONs.changePinUsages
[Mograsim.git] / plugins / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / examples / ReserializeAndVerifyJSONs.java
index cf12111..def5d9a 100644 (file)
@@ -1,5 +1,6 @@
 package net.mograsim.logic.model.examples;
 
+import java.io.FileWriter;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -46,8 +47,8 @@ public class ReserializeAndVerifyJSONs
 {
        public static double GRIDSIZE = 2.5;
        public static boolean changePinUsages = false;
-       public static boolean changeComponentNames = false;
-       public static boolean forceDefaultComponentNames = false;
+       public static boolean changeComponentNames = true;
+       public static boolean forceDefaultComponentNames = true;
        public static boolean changeWireNames = true;
        public static boolean forceDefaultWireNames = true;
        public static boolean snapWCPs = true;
@@ -114,7 +115,12 @@ public class ReserializeAndVerifyJSONs
                                changeWireNames_AfterSerialization(newComponentJSON, wireNameRemapping);
                        sortAllJSONArrays(newComponentJSON);
 
-                       JsonHandler.writeJson(newComponentJSON, componentPath.toString());
+                       try (FileWriter writer = new FileWriter(componentPath.toString()))
+                       {
+                               String json = JsonHandler.toJson(newComponentJSON);
+                               json = json.replace("\u00b5", "\\u00b5");
+                               writer.write(json);
+                       }
                }
                catch (Exception e)
                {
@@ -146,15 +152,16 @@ public class ReserializeAndVerifyJSONs
 
        private static void changePinUsages(Scanner sysin, DeserializedSubmodelComponent comp)
        {
-               comp.getSupermodelPins().entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).map(Entry::getValue).forEach(pin ->
+               comp.getSubmodelPins().entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).map(Entry::getValue).forEach(pin ->
                {
+                       PinUsage oldUsage = comp.getSupermodelPin(pin.name).usage;
                        PinUsage usage = null;
-                       while (usage == null)
+                       do
                                try
                                {
-                                       System.out.print("  Usage for interface pin " + pin.name + " (empty: " + pin.usage + ") >");
+                                       System.out.print("  Usage for interface pin " + pin.name + " (empty: " + oldUsage + ") >");
                                        String usageStr = sysin.nextLine().toUpperCase();
-                                       usage = usageStr.equals("") ? pin.usage
+                                       usage = usageStr.equals("") ? oldUsage
                                                        : usageStr.equals("I") ? PinUsage.INPUT
                                                                        : usageStr.equals("O") ? PinUsage.OUTPUT
                                                                                        : usageStr.equals("T") ? PinUsage.TRISTATE : PinUsage.valueOf(usageStr);
@@ -163,6 +170,7 @@ public class ReserializeAndVerifyJSONs
                                {
                                        System.err.println("  Illegal usage");
                                }
+                       while (usage == null);
                        setInterfacePinUsage(comp, pin, usage);
                });
        }
@@ -427,13 +435,19 @@ public class ReserializeAndVerifyJSONs
 
        private static void setInterfacePinUsage(DeserializedSubmodelComponent comp, Pin interfacePin, PinUsage usage)
        {
-               Set<ModelWire> wiresConnectedToPin = comp.submodel.getWiresByName().values().stream()
-                               .filter(w -> w.getPin1() == interfacePin || w.getPin2() == interfacePin).collect(Collectors.toSet());
+               Set<ModelWire> wiresConnectedToPin1 = comp.submodel.getWiresByName().values().stream().filter(w -> w.getPin1() == interfacePin)
+                               .collect(Collectors.toSet());
+               Set<ModelWire> wiresConnectedToPin2 = comp.submodel.getWiresByName().values().stream().filter(w -> w.getPin2() == interfacePin)
+                               .collect(Collectors.toSet());
                LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
-               wiresConnectedToPin.forEach(submodelModifiable::destroyWire);
+               wiresConnectedToPin1.forEach(submodelModifiable::destroyWire);
+               wiresConnectedToPin2.forEach(submodelModifiable::destroyWire);
+               Pin supermodelPin = comp.getSupermodelPin(interfacePin.name);
                comp.removeSubmodelInterface(interfacePin.name);
                comp.addSubmodelInterface(new MovablePin(submodelModifiable, comp, interfacePin.name, interfacePin.logicWidth, usage,
-                               interfacePin.getRelX(), interfacePin.getRelY()));
-               wiresConnectedToPin.forEach(w -> new ModelWire(submodelModifiable, w.getPin1(), w.getPin2()));
+                               supermodelPin.getRelX(), supermodelPin.getRelY()));
+               Pin interfacePinNew = comp.getSubmodelPin(interfacePin.name);
+               wiresConnectedToPin1.forEach(w -> new ModelWire(submodelModifiable, interfacePinNew, w.getPin2(), w.getPath()));
+               wiresConnectedToPin2.forEach(w -> new ModelWire(submodelModifiable, w.getPin1(), interfacePinNew, w.getPath()));
        }
 }
\ No newline at end of file