From b4ab3ce273adb37184833288b6b481d40742b4bc Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Sat, 14 Sep 2019 15:53:45 +0200 Subject: [PATCH] ReserializeAndVerifyJSONs now checks wire part orientations --- ...Ns.java => ReserializeAndVerifyJSONs.java} | 230 +++++++++++------- 1 file changed, 137 insertions(+), 93 deletions(-) rename net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/{ReserializeJSONs.java => ReserializeAndVerifyJSONs.java} (52%) diff --git a/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeJSONs.java b/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeAndVerifyJSONs.java similarity index 52% rename from net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeJSONs.java rename to net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeAndVerifyJSONs.java index c9486cc8..dfbb772a 100644 --- a/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeJSONs.java +++ b/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeAndVerifyJSONs.java @@ -32,13 +32,14 @@ import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; import net.mograsim.logic.model.serializing.SubmodelComponentSerializer; import net.mograsim.logic.model.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler; -public class ReserializeJSONs +public class ReserializeAndVerifyJSONs { public static double GRIDSIZE = 2.5; public static boolean changePinUsages = false; public static boolean changeComponentNames = false; public static boolean snapWCPs = true; public static boolean warnNonSnappedPoints = true; + public static boolean warnNonVertHorizLines = true; public static void main(String[] args) throws IOException { @@ -94,108 +95,151 @@ public class ReserializeJSONs LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable(); Map componentNameRemapping = new HashMap<>(); if (changeComponentNames) - { - componentNameRemapping.put(SubmodelComponent.SUBMODEL_INTERFACE_NAME, SubmodelComponent.SUBMODEL_INTERFACE_NAME); - LogicModelModifiable tempModel = new LogicModelModifiable(); - IdentifyParams iP = new IdentifyParams(); - submodelModifiable.getComponentsByName().entrySet().stream() - .filter(e -> !e.getKey().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)) - .sorted(Comparator.comparing(Entry::getKey, ReserializeJSONs::compareStringsWithIntegers)).forEach(e -> - { - String oldName = e.getKey(); - ModelComponent subcomp = e.getValue(); - String defaultName = tempModel.getDefaultComponentName(subcomp); - String newName = null; - while (newName == null) - { - System.out.print(" New name for component " + oldName + " of type " + subcomp.getIDForSerializing(iP) - + " (empty: " + defaultName + ") >"); - newName = sysin.nextLine(); - if (newName.equals("")) - newName = defaultName; - if (tempModel.getComponentsByName().containsKey(newName)) - { - System.err.println(" There already is a component with that name"); - newName = null; - } - } - componentNameRemapping.put(oldName, newName); - IndirectModelComponentCreator.createComponent(tempModel, subcomp.getIDForSerializing(iP), - subcomp.getParamsForSerializingJSON(iP), newName).moveTo(subcomp.getPosX(), subcomp.getPosY()); - }); - SubmodelInterface tempSubmodelInterface = new SubmodelInterface(tempModel); - for (Pin p : submodelModifiable.getComponentsByName().get(SubmodelComponent.SUBMODEL_INTERFACE_NAME).getPins().values()) - tempSubmodelInterface - .addPin(new Pin(tempModel, tempSubmodelInterface, p.name, p.logicWidth, p.usage, p.getRelX(), p.getRelY())); - for (ModelWire w : submodelModifiable.getWiresByName().values()) - createWire(componentNameRemapping::get, tempModel, w); - - Optional o; - while ((o = submodelModifiable.getComponentsByName().values().stream() - .filter(c -> !c.getName().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)).findAny()).isPresent()) - submodelModifiable.destroyComponent(o.get()); - - 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); - } + changeComponentNames(sysin, submodelModifiable, componentNameRemapping); 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); - }); + snapWCPs(submodelModifiable); if (warnNonSnappedPoints) - { - if (comp.getWidth() % GRIDSIZE != 0 || comp.getHeight() % GRIDSIZE != 0) - System.out.println(" Size is not snapped to grid: " + comp.getWidth() + "," + comp.getHeight()); - submodelModifiable.getComponentsByName().values().forEach(c -> + warnNonSnappedPoints(comp, submodelModifiable); + if (warnNonVertHorizLines) + warnNonVertHorizLines(submodelModifiable); + SubmodelComponentSerializer.serialize(comp, componentPath.toString()); + if (changeComponentNames) + changeComponentNames_AfterSerialization(sysin, comp, componentNameRemapping); + } + catch (Exception e) + { + System.err.println("An error occurred visiting " + componentPath + ":"); + e.printStackTrace(); + } + } + + private static void changeComponentNames_AfterSerialization(Scanner sysin, DeserializedSubmodelComponent comp, + Map componentNameRemapping) + { + if (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) + " >"); + } + } + + private static void changeComponentNames(Scanner sysin, LogicModelModifiable submodelModifiable, + Map componentNameRemapping) + { + componentNameRemapping.put(SubmodelComponent.SUBMODEL_INTERFACE_NAME, SubmodelComponent.SUBMODEL_INTERFACE_NAME); + LogicModelModifiable tempModel = new LogicModelModifiable(); + IdentifyParams iP = new IdentifyParams(); + submodelModifiable.getComponentsByName().entrySet().stream() + .filter(e -> !e.getKey().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)) + .sorted(Comparator.comparing(Entry::getKey, ReserializeAndVerifyJSONs::compareStringsWithIntegers)).forEach(e -> { - double x = c.getPosX(); - double y = c.getPosY(); - if (c instanceof ModelWireCrossPoint) + String oldName = e.getKey(); + ModelComponent subcomp = e.getValue(); + String defaultName = tempModel.getDefaultComponentName(subcomp); + String newName = null; + while (newName == null) { - x++; - y++; + System.out.print(" New name for component " + oldName + " of type " + subcomp.getIDForSerializing(iP) + " (empty: " + + defaultName + ") >"); + newName = sysin.nextLine(); + if (newName.equals("")) + newName = defaultName; + if (tempModel.getComponentsByName().containsKey(newName)) + { + System.err.println(" There already is a component with that name"); + newName = null; + } } - 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()); + componentNameRemapping.put(oldName, newName); + IndirectModelComponentCreator + .createComponent(tempModel, subcomp.getIDForSerializing(iP), subcomp.getParamsForSerializingJSON(iP), newName) + .moveTo(subcomp.getPosX(), subcomp.getPosY()); }); + SubmodelInterface tempSubmodelInterface = new SubmodelInterface(tempModel); + for (Pin p : submodelModifiable.getComponentsByName().get(SubmodelComponent.SUBMODEL_INTERFACE_NAME).getPins().values()) + tempSubmodelInterface + .addPin(new Pin(tempModel, tempSubmodelInterface, p.name, p.logicWidth, p.usage, p.getRelX(), p.getRelY())); + for (ModelWire w : submodelModifiable.getWiresByName().values()) + createWire(componentNameRemapping::get, tempModel, w); + + Optional o; + while ((o = submodelModifiable.getComponentsByName().values().stream() + .filter(c -> !c.getName().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)).findAny()).isPresent()) + submodelModifiable.destroyComponent(o.get()); + + 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); + } + + private static void snapWCPs(LogicModelModifiable submodelModifiable) + { + submodelModifiable.getComponentsByName().values().stream().filter(c -> c instanceof ModelWireCrossPoint).forEach(c -> + { + double x = c.getPosX(); + double y = c.getPosY(); + double newX = x % GRIDSIZE == 0 ? x - 1 : x; + double newY = y % GRIDSIZE == 0 ? y - 1 : y; + if (x != newX || y != newY) + { + c.moveTo(newX, newY); + System.out.println(" Snapping WCP " + c.getName()); } - SubmodelComponentSerializer.serialize(comp, componentPath.toString()); - if (changeComponentNames && (comp.getHighLevelStateHandler() == null - || !(comp.getHighLevelStateHandler() instanceof DefaultHighLevelStateHandler))) + }); + } + + private static void warnNonSnappedPoints(DeserializedSubmodelComponent comp, LogicModelModifiable submodelModifiable) + { + if (comp.getWidth() % GRIDSIZE != 0 || comp.getHeight() % GRIDSIZE != 0) + System.out.println(" Size is not snapped to grid: " + comp.getWidth() + "," + comp.getHeight()); + submodelModifiable.getComponentsByName().values().forEach(c -> + { + double x = c.getPosX(); + double y = c.getPosY(); + if (c instanceof ModelWireCrossPoint) { - 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) + " >"); + x++; + y++; } - } - catch (Exception e) + 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().forEach(w -> { - System.err.println("An error occurred visiting " + componentPath + ":"); - e.printStackTrace(); - } + 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()); + }); + } + + private static void warnNonVertHorizLines(LogicModelModifiable submodelModifiable) + { + submodelModifiable.getWiresByName().values().forEach(w -> + { + double[] p = w.getEffectivePath(); + for (int i = 1; i < p.length / 2; i++) + { + double x1 = p[2 * i - 2]; + double y1 = p[2 * i - 1]; + double x2 = p[2 * i + 0]; + double y2 = p[2 * i + 1]; + if (x1 != x2 && y1 != y2) + System.out.println(" Wire " + w.name + " part #" + (i - 1) + " is neither vertical nor horizontal"); + } + }); } private static ModelWire createWire(Function componentNameRemapping, LogicModelModifiable tempModelForDefaultNames, -- 2.17.1