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.mograsim.logic.model.am2900.Am2900Loader;
19 import net.mograsim.logic.model.model.LogicModelModifiable;
20 import net.mograsim.logic.model.model.components.ModelComponent;
21 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
22 import net.mograsim.logic.model.model.components.submodels.SubmodelInterface;
23 import net.mograsim.logic.model.model.wires.ModelWire;
24 import net.mograsim.logic.model.model.wires.MovablePin;
25 import net.mograsim.logic.model.model.wires.Pin;
26 import net.mograsim.logic.model.model.wires.PinUsage;
27 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
28 import net.mograsim.logic.model.serializing.IdentifyParams;
29 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
30 import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
32 public class ReserializeJSONsSettingUsages
34 public static boolean changePinUsages = false;
35 public static boolean changeComponentNames = true;
37 public static void main(String[] args) throws IOException
40 try (Scanner sysin = new Scanner(System.in))
42 System.out.print("Directory to search for JSONs in / JSON file to reserialize >");
43 Path root = Paths.get(sysin.nextLine());
44 if (!Files.exists(root))
45 throw new IllegalArgumentException("Path doesn't exist");
46 if (Files.isRegularFile(root))
47 reserializeJSON(root, sysin);
50 System.out.print("Recursive? >");
51 boolean recursive = Boolean.valueOf(sysin.nextLine());
52 try (Stream<Path> jsons = recursive ? Files.walk(root) : Files.list(root))
54 jsons.filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(".json"))
55 .forEach(j -> reserializeJSON(j, sysin));
61 public static void reserializeJSON(Path json, Scanner sysin)
65 DeserializedSubmodelComponent comp = (DeserializedSubmodelComponent) IndirectModelComponentCreator
66 .createComponent(new LogicModelModifiable(), "jsonfile:" + json.toString());
67 System.out.println("Reserializing " + json);
69 comp.getSupermodelPins().entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).map(Entry::getValue).forEach(pin ->
71 PinUsage usage = null;
75 System.out.print(" Usage for interface pin " + pin.name + " (empty: " + pin.usage + ") >");
76 String usageStr = sysin.nextLine().toUpperCase();
77 usage = usageStr.equals("") ? pin.usage
78 : usageStr.equals("I") ? PinUsage.INPUT
79 : usageStr.equals("O") ? PinUsage.OUTPUT
80 : usageStr.equals("T") ? PinUsage.TRISTATE : PinUsage.valueOf(usageStr);
82 catch (@SuppressWarnings("unused") IllegalArgumentException e)
84 System.err.println(" Illegal usage");
86 setInterfacePinUsage(comp, pin, usage);
88 LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
89 if (changeComponentNames)
91 Map<String, String> componentNameRemapping = new HashMap<>();
92 componentNameRemapping.put(SubmodelComponent.SUBMODEL_INTERFACE_NAME, SubmodelComponent.SUBMODEL_INTERFACE_NAME);
93 LogicModelModifiable tempModel = new LogicModelModifiable();
94 IdentifyParams iP = new IdentifyParams();
95 submodelModifiable.getComponentsByName().entrySet().stream()
96 .filter(e -> !e.getKey().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME))
97 .sorted(Comparator.comparing(Entry::getKey, ReserializeJSONsSettingUsages::compareStringsWithIntegers)).forEach(e ->
99 String oldName = e.getKey();
100 ModelComponent subcomp = e.getValue();
101 String defaultName = tempModel.getDefaultComponentName(subcomp);
102 String newName = null;
103 while (newName == null)
105 System.out.print(" New name for component " + oldName + " of type " + subcomp.getIDForSerializing(iP)
106 + " (empty: " + defaultName + ") >");
107 newName = sysin.nextLine();
108 if (newName.equals(""))
109 newName = defaultName;
110 if (tempModel.getComponentsByName().containsKey(newName))
112 System.err.println(" There already is a component with that name");
116 componentNameRemapping.put(oldName, newName);
117 IndirectModelComponentCreator.createComponent(tempModel, subcomp.getIDForSerializing(iP),
118 subcomp.getParamsForSerializingJSON(iP), newName).moveTo(subcomp.getPosX(), subcomp.getPosY());
120 SubmodelInterface tempSubmodelInterface = new SubmodelInterface(tempModel);
121 for (Pin p : submodelModifiable.getComponentsByName().get(SubmodelComponent.SUBMODEL_INTERFACE_NAME).getPins().values())
122 tempSubmodelInterface
123 .addPin(new Pin(tempModel, tempSubmodelInterface, p.name, p.logicWidth, p.usage, p.getRelX(), p.getRelY()));
124 for (ModelWire w : submodelModifiable.getWiresByName().values())
125 createWire(componentNameRemapping::get, tempModel, w);
127 Optional<ModelComponent> o;
128 while ((o = submodelModifiable.getComponentsByName().values().stream()
129 .filter(c -> !c.name.equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)).findAny()).isPresent())
130 submodelModifiable.destroyComponent(o.get());
132 tempModel.getComponentsByName().values().stream().filter(c -> !c.name.equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME))
133 .forEach(c -> IndirectModelComponentCreator
134 .createComponent(submodelModifiable, c.getIDForSerializing(iP), c.getParamsForSerializingJSON(iP), c.name)
135 .moveTo(c.getPosX(), c.getPosY()));
136 for (ModelWire w : tempModel.getWiresByName().values())
137 createWire(Function.identity(), submodelModifiable, w);
139 SubmodelComponentSerializer.serialize(comp, json.toString());
143 System.err.println("An error occurred visiting " + json + ":");
148 private static ModelWire createWire(Function<String, String> componentNameRemapping, LogicModelModifiable tempModelForDefaultNames,
151 return new ModelWire(tempModelForDefaultNames, w.name,
152 getRemappedPin(componentNameRemapping, tempModelForDefaultNames, w.getPin1()),
153 getRemappedPin(componentNameRemapping, tempModelForDefaultNames, w.getPin2()), w.getPath());
156 private static Pin getRemappedPin(Function<String, String> componentNameRemapping, LogicModelModifiable tempModelForDefaultNames,
159 return tempModelForDefaultNames.getComponentsByName().get(componentNameRemapping.apply(pin.component.name)).getPin(pin.name);
162 private static int compareStringsWithIntegers(String a, String b)
168 if (aLoc == a.length())
170 if (bLoc == b.length())
174 if (bLoc == b.length())
181 nextCharA = a.charAt(aLoc++);
182 if (nextCharA < '0' || nextCharA > '9')
185 aInt = aInt * 10 + nextCharA - '0';
186 if (aLoc == a.length())
194 nextCharB = b.charAt(bLoc++);
195 if (nextCharB < '0' || nextCharB > '9')
198 bInt = bInt * 10 + nextCharB - '0';
199 if (bLoc == b.length())
206 int comp = Integer.compare(aInt, bInt);
213 int comp = Character.compare(nextCharA, nextCharB);
220 private static void setInterfacePinUsage(DeserializedSubmodelComponent comp, Pin interfacePin, PinUsage usage)
222 Set<ModelWire> wiresConnectedToPin = comp.submodel.getWiresByName().values().stream()
223 .filter(w -> w.getPin1() == interfacePin || w.getPin2() == interfacePin).collect(Collectors.toSet());
224 LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
225 wiresConnectedToPin.forEach(submodelModifiable::destroyWire);
226 comp.removeSubmodelInterface(interfacePin.name);
227 comp.addSubmodelInterface(new MovablePin(submodelModifiable, comp, interfacePin.name, interfacePin.logicWidth, usage,
228 interfacePin.getRelX(), interfacePin.getRelY()));
229 wiresConnectedToPin.forEach(w -> new ModelWire(submodelModifiable, w.getPin1(), w.getPin2()));