1 package net.mograsim.logic.model.examples;
3 import java.io.IOException;
5 import com.google.gson.JsonNull;
7 import net.mograsim.logic.model.SimpleLogicUIStandalone;
8 import net.mograsim.logic.model.model.ViewModelModifiable;
9 import net.mograsim.logic.model.model.components.atomic.GUIBitDisplay;
10 import net.mograsim.logic.model.model.components.atomic.GUIManualSwitch;
11 import net.mograsim.logic.model.model.components.mi.nandbased.GUI_rsLatch;
12 import net.mograsim.logic.model.model.components.mi.nandbased.GUIfulladder;
13 import net.mograsim.logic.model.model.components.submodels.SimpleRectangularSubmodelComponent;
14 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
15 import net.mograsim.logic.model.model.wires.GUIWire;
16 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
17 import net.mograsim.logic.model.serializing.SubmodelComponentDeserializer;
18 import net.mograsim.logic.model.serializing.SubmodelComponentParams;
19 import net.mograsim.logic.model.util.JsonHandler;
21 public class JsonExample
23 public static void main(String[] args)
25 SimpleLogicUIStandalone.executeVisualisation(JsonExample::basicTest);
28 public static void mappingTest(ViewModelModifiable model)
30 IndirectGUIComponentCreator.createComponent(model, "Am2901", JsonNull.INSTANCE, "Am2901 instance");
33 private static class TestComponent extends SimpleRectangularSubmodelComponent
35 protected TestComponent(ViewModelModifiable model, String name)
37 super(model, 1, "Test", name);
39 setInputPins("Input pin #0");
40 SubmodelComponentDeserializer.create(submodelModifiable, "HalfAdder.json", "halfadder");
44 @SuppressWarnings("unused") // GUIWires being created
45 private static void basicTest(ViewModelModifiable viewModel)
47 GUI_rsLatch comp = new GUI_rsLatch(viewModel, "Original RS latch");
49 SubmodelComponentParams params = comp.calculateParams();
50 String jsonString = JsonHandler.toJson(params);
51 System.out.println(jsonString);
52 SubmodelComponentParams paramsD = JsonHandler.fromJson(jsonString, SubmodelComponentParams.class);
53 SubmodelComponent componentD = SubmodelComponentDeserializer.create(viewModel, paramsD, "Deserialized RS latch");
54 componentD.moveTo(30, 50);
56 for (String s : comp.getInputPinNames())
58 GUIManualSwitch sw = new GUIManualSwitch(viewModel);
60 new GUIWire(viewModel, sw.getOutputPin(), comp.getPin(s));
61 sw = new GUIManualSwitch(viewModel);
63 new GUIWire(viewModel, sw.getOutputPin(), componentD.getPin(s));
67 for (String s : comp.getOutputPinNames())
69 GUIBitDisplay bd = new GUIBitDisplay(viewModel);
71 new GUIWire(viewModel, bd.getInputPin(), comp.getPin(s));
72 bd = new GUIBitDisplay(viewModel);
73 bd.moveTo(80, h + 50);
74 new GUIWire(viewModel, bd.getInputPin(), componentD.getPin(s));
79 // Execute only after HalfAdder.json has been created
80 public static void refJsonFromJsonTest(ViewModelModifiable model)
82 TestComponent t = new TestComponent(model, "Original component");
83 t.calculateParams().writeJson("Test.json");
84 SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json", "Deserialized component");
88 @SuppressWarnings("unused") // for GUIWires being created
89 public static void createFromJsonExample(ViewModelModifiable model)
91 SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model, "Original full adder");
92 SubmodelComponentParams pC = tmp.calculateParams();
93 tmp.moveTo(1000, 100);
96 pC.writeJson("FullAdder.json");
97 pC = SubmodelComponentParams.readJson("FullAdder.json");
104 SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) SubmodelComponentDeserializer.create(model,
105 "FullAdder.json", "Deserialized full adder");
107 GUIManualSwitch swA = new GUIManualSwitch(model);
109 GUIManualSwitch swB = new GUIManualSwitch(model);
111 GUIManualSwitch swC = new GUIManualSwitch(model);
114 adder.moveTo(30, 10);
115 GUIBitDisplay bdY = new GUIBitDisplay(model);
116 bdY.moveTo(90, 12.5);
117 GUIBitDisplay bdZ = new GUIBitDisplay(model);
120 new GUIWire(model, swA.getOutputPin(), adder.getPin("A"));
121 new GUIWire(model, swB.getOutputPin(), adder.getPin("B"));
122 new GUIWire(model, swC.getOutputPin(), adder.getPin("C"));
124 new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
125 new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
127 SubmodelComponent adder2 = SubmodelComponentDeserializer.create(model, pC, "Full adder created from params instance");
129 swA = new GUIManualSwitch(model);
131 swB = new GUIManualSwitch(model);
133 swC = new GUIManualSwitch(model);
136 adder2.moveTo(30, 80);
137 bdY = new GUIBitDisplay(model);
139 bdZ = new GUIBitDisplay(model);
142 new GUIWire(model, swA.getOutputPin(), adder2.getPin("A"));
143 new GUIWire(model, swB.getOutputPin(), adder2.getPin("B"));
144 new GUIWire(model, swC.getOutputPin(), adder2.getPin("C"));
146 new GUIWire(model, adder2.getPin("Y"), bdY.getInputPin());
147 new GUIWire(model, adder2.getPin("Z"), bdZ.getInputPin());