f1e90aa017ec4f6ee3e25b8c7858640575aa4651
[Mograsim.git] /
1 package net.mograsim.logic.model.examples;
2
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;
9 import java.util.Map;
10 import java.util.Map.Entry;
11 import java.util.Optional;
12 import java.util.Scanner;
13 import java.util.Set;
14 import java.util.function.Function;
15 import java.util.stream.Collectors;
16 import java.util.stream.Stream;
17
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;
31
32 public class ReserializeJSONsSettingUsages
33 {
34         public static boolean changePinUsages = false;
35         public static boolean changeComponentNames = true;
36
37         public static void main(String[] args) throws IOException
38         {
39                 Am2900Loader.setup();
40                 try (Scanner sysin = new Scanner(System.in))
41                 {
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);
48                         else
49                         {
50                                 System.out.print("Recursive? >");
51                                 boolean recursive = Boolean.valueOf(sysin.nextLine());
52                                 try (Stream<Path> jsons = recursive ? Files.walk(root) : Files.list(root))
53                                 {
54                                         jsons.filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(".json"))
55                                                         .forEach(j -> reserializeJSON(j, sysin));
56                                 }
57                         }
58                 }
59         }
60
61         public static void reserializeJSON(Path json, Scanner sysin)
62         {
63                 try
64                 {
65                         DeserializedSubmodelComponent comp = (DeserializedSubmodelComponent) IndirectModelComponentCreator
66                                         .createComponent(new LogicModelModifiable(), "jsonfile:" + json.toString());
67                         System.out.println("Reserializing " + json);
68                         if (changePinUsages)
69                                 comp.getSupermodelPins().entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).map(Entry::getValue).forEach(pin ->
70                                 {
71                                         PinUsage usage = null;
72                                         while (usage == null)
73                                                 try
74                                                 {
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);
81                                                 }
82                                                 catch (@SuppressWarnings("unused") IllegalArgumentException e)
83                                                 {
84                                                         System.err.println("  Illegal usage");
85                                                 }
86                                         setInterfacePinUsage(comp, pin, usage);
87                                 });
88                         LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
89                         if (changeComponentNames)
90                         {
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 ->
98                                                 {
99                                                         String oldName = e.getKey();
100                                                         ModelComponent subcomp = e.getValue();
101                                                         String defaultName = tempModel.getDefaultComponentName(subcomp);
102                                                         String newName = null;
103                                                         while (newName == null)
104                                                         {
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))
111                                                                 {
112                                                                         System.err.println("  There already is a component with that name");
113                                                                         newName = null;
114                                                                 }
115                                                         }
116                                                         componentNameRemapping.put(oldName, newName);
117                                                         IndirectModelComponentCreator.createComponent(tempModel, subcomp.getIDForSerializing(iP),
118                                                                         subcomp.getParamsForSerializingJSON(iP), newName).moveTo(subcomp.getPosX(), subcomp.getPosY());
119                                                 });
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);
126
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());
131
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);
138                         }
139                         SubmodelComponentSerializer.serialize(comp, json.toString());
140                 }
141                 catch (Exception e)
142                 {
143                         System.err.println("An error occurred visiting " + json + ":");
144                         e.printStackTrace();
145                 }
146         }
147
148         private static ModelWire createWire(Function<String, String> componentNameRemapping, LogicModelModifiable tempModelForDefaultNames,
149                         ModelWire w)
150         {
151                 return new ModelWire(tempModelForDefaultNames, w.name,
152                                 getRemappedPin(componentNameRemapping, tempModelForDefaultNames, w.getPin1()),
153                                 getRemappedPin(componentNameRemapping, tempModelForDefaultNames, w.getPin2()), w.getPath());
154         }
155
156         private static Pin getRemappedPin(Function<String, String> componentNameRemapping, LogicModelModifiable tempModelForDefaultNames,
157                         Pin pin)
158         {
159                 return tempModelForDefaultNames.getComponentsByName().get(componentNameRemapping.apply(pin.component.name)).getPin(pin.name);
160         }
161
162         private static int compareStringsWithIntegers(String a, String b)
163         {
164                 int aLoc = 0;
165                 int bLoc = 0;
166                 for (;;)
167                 {
168                         if (aLoc == a.length())
169                         {
170                                 if (bLoc == b.length())
171                                         return 0;
172                                 return -1;
173                         }
174                         if (bLoc == b.length())
175                                 return 1;
176                         int aInt = 0;
177                         int aIntLen = 0;
178                         char nextCharA;
179                         for (;;)
180                         {
181                                 nextCharA = a.charAt(aLoc++);
182                                 if (nextCharA < '0' || nextCharA > '9')
183                                         break;
184                                 aIntLen++;
185                                 aInt = aInt * 10 + nextCharA - '0';
186                                 if (aLoc == a.length())
187                                         break;
188                         }
189                         int bInt = 0;
190                         int bIntLen = 0;
191                         char nextCharB;
192                         for (;;)
193                         {
194                                 nextCharB = b.charAt(bLoc++);
195                                 if (nextCharB < '0' || nextCharB > '9')
196                                         break;
197                                 bIntLen++;
198                                 bInt = bInt * 10 + nextCharB - '0';
199                                 if (bLoc == b.length())
200                                         break;
201                         }
202                         if (aIntLen != 0)
203                         {
204                                 if (bIntLen == 0)
205                                         return -1;
206                                 int comp = Integer.compare(aInt, bInt);
207                                 if (comp != 0)
208                                         return comp;
209                         } else
210                         {
211                                 if (bIntLen != 0)
212                                         return 1;
213                                 int comp = Character.compare(nextCharA, nextCharB);
214                                 if (comp != 0)
215                                         return comp;
216                         }
217                 }
218         }
219
220         private static void setInterfacePinUsage(DeserializedSubmodelComponent comp, Pin interfacePin, PinUsage usage)
221         {
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()));
230         }
231 }