1 package net.mograsim.logic.model.examples;
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7 import java.util.Comparator;
8 import java.util.HashMap;
10 import java.util.Map.Entry;
11 import java.util.Optional;
12 import java.util.Scanner;
14 import java.util.function.Function;
15 import java.util.stream.Collectors;
16 import java.util.stream.Stream;
18 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
19 import net.mograsim.logic.model.am2900.Am2900Loader;
20 import net.mograsim.logic.model.model.LogicModelModifiable;
21 import net.mograsim.logic.model.model.components.ModelComponent;
22 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
23 import net.mograsim.logic.model.model.components.submodels.SubmodelInterface;
24 import net.mograsim.logic.model.model.wires.ModelWire;
25 import net.mograsim.logic.model.model.wires.ModelWireCrossPoint;
26 import net.mograsim.logic.model.model.wires.MovablePin;
27 import net.mograsim.logic.model.model.wires.Pin;
28 import net.mograsim.logic.model.model.wires.PinUsage;
29 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
30 import net.mograsim.logic.model.serializing.IdentifyParams;
31 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
32 import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
33 import net.mograsim.logic.model.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler;
35 public class ReserializeJSONs
37 public static double GRIDSIZE = 2.5;
38 public static boolean changePinUsages = false;
39 public static boolean changeComponentNames = false;
40 public static boolean snapWCPs = true;
41 public static boolean warnNonSnappedPoints = true;
43 public static void main(String[] args) throws IOException
46 try (Scanner sysin = new Scanner(System.in))
48 System.out.print("Directory to search for JSONs in / JSON file to reserialize >");
49 Path root = Paths.get(sysin.nextLine());
50 if (!Files.exists(root))
51 throw new IllegalArgumentException("Path doesn't exist");
52 if (Files.isRegularFile(root))
53 reserializeJSON(root, sysin);
56 System.out.print("Recursive? >");
57 boolean recursive = Boolean.valueOf(sysin.nextLine());
58 try (Stream<Path> jsons = recursive ? Files.walk(root) : Files.list(root))
60 jsons.filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(".json"))
61 .forEach(j -> reserializeJSON(j, sysin));
67 public static void reserializeJSON(Path componentPath, Scanner sysin)
71 DeserializedSubmodelComponent comp = (DeserializedSubmodelComponent) IndirectModelComponentCreator
72 .createComponent(new LogicModelModifiable(), "jsonfile:" + componentPath.toString());
73 System.out.println("Reserializing " + componentPath);
75 comp.getSupermodelPins().entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).map(Entry::getValue).forEach(pin ->
77 PinUsage usage = null;
81 System.out.print(" Usage for interface pin " + pin.name + " (empty: " + pin.usage + ") >");
82 String usageStr = sysin.nextLine().toUpperCase();
83 usage = usageStr.equals("") ? pin.usage
84 : usageStr.equals("I") ? PinUsage.INPUT
85 : usageStr.equals("O") ? PinUsage.OUTPUT
86 : usageStr.equals("T") ? PinUsage.TRISTATE : PinUsage.valueOf(usageStr);
88 catch (@SuppressWarnings("unused") IllegalArgumentException e)
90 System.err.println(" Illegal usage");
92 setInterfacePinUsage(comp, pin, usage);
94 LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
95 Map<String, String> componentNameRemapping = new HashMap<>();
96 if (changeComponentNames)
98 componentNameRemapping.put(SubmodelComponent.SUBMODEL_INTERFACE_NAME, SubmodelComponent.SUBMODEL_INTERFACE_NAME);
99 LogicModelModifiable tempModel = new LogicModelModifiable();
100 IdentifyParams iP = new IdentifyParams();
101 submodelModifiable.getComponentsByName().entrySet().stream()
102 .filter(e -> !e.getKey().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME))
103 .sorted(Comparator.comparing(Entry::getKey, ReserializeJSONs::compareStringsWithIntegers)).forEach(e ->
105 String oldName = e.getKey();
106 ModelComponent subcomp = e.getValue();
107 String defaultName = tempModel.getDefaultComponentName(subcomp);
108 String newName = null;
109 while (newName == null)
111 System.out.print(" New name for component " + oldName + " of type " + subcomp.getIDForSerializing(iP)
112 + " (empty: " + defaultName + ") >");
113 newName = sysin.nextLine();
114 if (newName.equals(""))
115 newName = defaultName;
116 if (tempModel.getComponentsByName().containsKey(newName))
118 System.err.println(" There already is a component with that name");
122 componentNameRemapping.put(oldName, newName);
123 IndirectModelComponentCreator.createComponent(tempModel, subcomp.getIDForSerializing(iP),
124 subcomp.getParamsForSerializingJSON(iP), newName).moveTo(subcomp.getPosX(), subcomp.getPosY());
126 SubmodelInterface tempSubmodelInterface = new SubmodelInterface(tempModel);
127 for (Pin p : submodelModifiable.getComponentsByName().get(SubmodelComponent.SUBMODEL_INTERFACE_NAME).getPins().values())
128 tempSubmodelInterface
129 .addPin(new Pin(tempModel, tempSubmodelInterface, p.name, p.logicWidth, p.usage, p.getRelX(), p.getRelY()));
130 for (ModelWire w : submodelModifiable.getWiresByName().values())
131 createWire(componentNameRemapping::get, tempModel, w);
133 Optional<ModelComponent> o;
134 while ((o = submodelModifiable.getComponentsByName().values().stream()
135 .filter(c -> !c.getName().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)).findAny()).isPresent())
136 submodelModifiable.destroyComponent(o.get());
138 tempModel.getComponentsByName().values().stream()
139 .filter(c -> !c.getName().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME))
140 .forEach(c -> IndirectModelComponentCreator.createComponent(submodelModifiable, c.getIDForSerializing(iP),
141 c.getParamsForSerializingJSON(iP), c.getName()).moveTo(c.getPosX(), c.getPosY()));
142 for (ModelWire w : tempModel.getWiresByName().values())
143 createWire(Function.identity(), submodelModifiable, w);
146 submodelModifiable.getComponentsByName().values().stream().filter(c -> c instanceof ModelWireCrossPoint).forEach(c ->
148 double x = c.getPosX();
149 double y = c.getPosY();
150 c.moveTo(x % GRIDSIZE == 0 ? x - 1 : x, y % GRIDSIZE == 0 ? y - 1 : y);
152 if (warnNonSnappedPoints)
154 submodelModifiable.getComponentsByName().values().forEach(c ->
156 double x = c.getPosX();
157 double y = c.getPosY();
158 if (c instanceof ModelWireCrossPoint)
163 if (x % GRIDSIZE != 0 || y % GRIDSIZE != 0)
164 System.out.println(" Component " + c.getName() + " (type " + c.getIDForSerializing(new IdentifyParams())
165 + ") is not snapped to grid: " + x + "," + y);
167 submodelModifiable.getWiresByName().values().stream().forEach(w ->
169 Point[] p = w.getPath();
171 for (int i = 0; i < p.length; i++)
172 if (p[i].x % GRIDSIZE != 0 || p[i].y % GRIDSIZE != 0)
174 " Wire " + w.name + " path point #" + i + " is not snapped to grid: " + p[i].x + "," + p[i].y);
176 comp.getPins().values().forEach(p ->
178 if (p.getRelX() % GRIDSIZE != 0 || p.getRelY() % GRIDSIZE != 0)
179 System.out.println(" Interface point " + p.name + " is not snapped to grid: " + p.getRelX() + "," + p.getRelY());
182 SubmodelComponentSerializer.serialize(comp, componentPath.toString());
183 if (changeComponentNames && (comp.getHighLevelStateHandler() == null
184 || !(comp.getHighLevelStateHandler() instanceof DefaultHighLevelStateHandler)))
186 System.out.println(" A non-default HighLevelStateHandler was detected. Check for changes there manually.");
187 System.out.print(" Empty line to continue to next component, old component name to get new component name >");
188 for (String line = sysin.nextLine(); !line.equals(""); line = sysin.nextLine())
189 System.out.println(" " + line + "->" + componentNameRemapping.get(line) + " >");
194 System.err.println("An error occurred visiting " + componentPath + ":");
199 private static ModelWire createWire(Function<String, String> componentNameRemapping, LogicModelModifiable tempModelForDefaultNames,
202 return new ModelWire(tempModelForDefaultNames, w.name,
203 getRemappedPin(componentNameRemapping, tempModelForDefaultNames, w.getPin1()),
204 getRemappedPin(componentNameRemapping, tempModelForDefaultNames, w.getPin2()), w.getPath());
207 private static Pin getRemappedPin(Function<String, String> componentNameRemapping, LogicModelModifiable tempModelForDefaultNames,
210 return tempModelForDefaultNames.getComponentsByName().get(componentNameRemapping.apply(pin.component.getName())).getPin(pin.name);
213 private static int compareStringsWithIntegers(String a, String b)
219 if (aLoc == a.length())
221 if (bLoc == b.length())
225 if (bLoc == b.length())
232 nextCharA = a.charAt(aLoc++);
233 if (nextCharA < '0' || nextCharA > '9')
236 aInt = aInt * 10 + nextCharA - '0';
237 if (aLoc == a.length())
245 nextCharB = b.charAt(bLoc++);
246 if (nextCharB < '0' || nextCharB > '9')
249 bInt = bInt * 10 + nextCharB - '0';
250 if (bLoc == b.length())
257 int comp = Integer.compare(aInt, bInt);
264 int comp = Character.compare(nextCharA, nextCharB);
271 private static void setInterfacePinUsage(DeserializedSubmodelComponent comp, Pin interfacePin, PinUsage usage)
273 Set<ModelWire> wiresConnectedToPin = comp.submodel.getWiresByName().values().stream()
274 .filter(w -> w.getPin1() == interfacePin || w.getPin2() == interfacePin).collect(Collectors.toSet());
275 LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
276 wiresConnectedToPin.forEach(submodelModifiable::destroyWire);
277 comp.removeSubmodelInterface(interfacePin.name);
278 comp.addSubmodelInterface(new MovablePin(submodelModifiable, comp, interfacePin.name, interfacePin.logicWidth, usage,
279 interfacePin.getRelX(), interfacePin.getRelY()));
280 wiresConnectedToPin.forEach(w -> new ModelWire(submodelModifiable, w.getPin1(), w.getPin2()));