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.Map.Entry;
9 import java.util.Scanner;
11 import java.util.stream.Collectors;
12 import java.util.stream.Stream;
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;
24 public class ReserializeJSONsSettingUsages
26 public static void main(String[] args) throws IOException
29 try (Scanner sysin = new Scanner(System.in))
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);
39 System.out.print("Recursive? >");
40 boolean recursive = Boolean.valueOf(sysin.nextLine());
41 try (Stream<Path> jsons = recursive ? Files.walk(root) : Files.list(root))
43 jsons.filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(".json"))
44 .forEach(j -> reserializeJSON(j, sysin));
50 public static void reserializeJSON(Path json, Scanner sysin)
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 ->
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);
67 SubmodelComponentSerializer.serialize(comp, json.toString());
71 System.err.println("An error occurred visiting " + json + ":");
76 private static void setInterfacePinUsage(DeserializedSubmodelComponent comp, Pin interfacePin, PinUsage usage)
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()));