bd4d45883d1ebdf53666ba19ee91a0b82c9e698e
[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.Map.Entry;
9 import java.util.Scanner;
10 import java.util.Set;
11 import java.util.stream.Collectors;
12 import java.util.stream.Stream;
13
14 import net.mograsim.logic.model.am2900.Am2900Loader;
15 import net.mograsim.logic.model.model.LogicModelModifiable;
16 import net.mograsim.logic.model.model.wires.ModelWire;
17 import net.mograsim.logic.model.model.wires.MovablePin;
18 import net.mograsim.logic.model.model.wires.Pin;
19 import net.mograsim.logic.model.model.wires.PinUsage;
20 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
21 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
22 import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
23
24 public class ReserializeJSONsSettingUsages
25 {
26         public static void main(String[] args) throws IOException
27         {
28                 Am2900Loader.setup();
29                 try (Scanner sysin = new Scanner(System.in))
30                 {
31                         System.out.print("Directory to search for JSONs in / JSON file to reserialize >");
32                         Path root = Paths.get(sysin.nextLine());
33                         if (!Files.exists(root))
34                                 throw new IllegalArgumentException("Path doesn't exist");
35                         if (Files.isRegularFile(root))
36                                 reserializeJSON(root, sysin);
37                         else
38                         {
39                                 System.out.print("Recursive? >");
40                                 boolean recursive = Boolean.valueOf(sysin.nextLine());
41                                 try (Stream<Path> jsons = recursive ? Files.walk(root) : Files.list(root))
42                                 {
43                                         jsons.filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(".json"))
44                                                         .forEach(j -> reserializeJSON(j, sysin));
45                                 }
46                         }
47                 }
48         }
49
50         public static void reserializeJSON(Path json, Scanner sysin)
51         {
52                 try
53                 {
54                         DeserializedSubmodelComponent comp = (DeserializedSubmodelComponent) IndirectModelComponentCreator
55                                         .createComponent(new LogicModelModifiable(), "jsonfile:" + json.toString());
56                         System.out.println("Reserializing " + json);
57                         comp.getSupermodelPins().entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).map(Entry::getValue).forEach(pin ->
58                         {
59                                 System.out.print("  Usage for interface pin " + pin.name + " (empty: " + pin.usage + ") >");
60                                 String usageStr = sysin.nextLine().toUpperCase();
61                                 PinUsage usage = usageStr.equals("") ? pin.usage
62                                                 : usageStr.equals("I") ? PinUsage.INPUT
63                                                                 : usageStr.equals("O") ? PinUsage.OUTPUT
64                                                                                 : usageStr.equals("T") ? PinUsage.TRISTATE : PinUsage.valueOf(usageStr);
65                                 setInterfacePinUsage(comp, pin, usage);
66                         });
67                         SubmodelComponentSerializer.serialize(comp, json.toString());
68                 }
69                 catch (Exception e)
70                 {
71                         System.err.println("An error occurred visiting " + json + ":");
72                         e.printStackTrace();
73                 }
74         }
75
76         private static void setInterfacePinUsage(DeserializedSubmodelComponent comp, Pin interfacePin, PinUsage usage)
77         {
78                 Set<ModelWire> wiresConnectedToPin = comp.submodel.getWiresByName().values().stream()
79                                 .filter(w -> w.getPin1() == interfacePin || w.getPin2() == interfacePin).collect(Collectors.toSet());
80                 LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
81                 wiresConnectedToPin.forEach(submodelModifiable::destroyWire);
82                 comp.removeSubmodelInterface(interfacePin.name);
83                 comp.addSubmodelInterface(new MovablePin(submodelModifiable, comp, interfacePin.name, interfacePin.logicWidth, usage,
84                                 interfacePin.getRelX(), interfacePin.getRelY()));
85                 wiresConnectedToPin.forEach(w -> new ModelWire(submodelModifiable, w.getPin1(), w.getPin2()));
86         }
87 }