1 package net.mograsim.logic.model.examples;
3 import java.io.IOException;
4 import java.io.UncheckedIOException;
6 import com.google.gson.JsonNull;
8 import net.mograsim.logic.model.SimpleLogicUIStandalone;
9 import net.mograsim.logic.model.model.ViewModelModifiable;
10 import net.mograsim.logic.model.model.components.atomic.GUIBitDisplay;
11 import net.mograsim.logic.model.model.components.atomic.GUIManualSwitch;
12 import net.mograsim.logic.model.model.components.mi.nandbased.GUI_rsLatch;
13 import net.mograsim.logic.model.model.components.mi.nandbased.GUIfulladder;
14 import net.mograsim.logic.model.model.components.submodels.SimpleRectangularSubmodelComponent;
15 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
16 import net.mograsim.logic.model.model.wires.GUIWire;
17 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
18 import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
19 import net.mograsim.logic.model.serializing.SubmodelComponentParams;
20 import net.mograsim.logic.model.util.JsonHandler;
22 public class JsonExample
24 public static void main(String[] args)
26 SimpleLogicUIStandalone.executeVisualisation(JsonExample::basicTest);
29 public static void mappingTest(ViewModelModifiable model)
31 IndirectGUIComponentCreator.createComponent(model, "Am2901", JsonNull.INSTANCE, "Am2901 instance");
34 private static class TestComponent extends SimpleRectangularSubmodelComponent
36 protected TestComponent(ViewModelModifiable model, String name)
38 super(model, 1, "Test", name);
40 setInputPins("Input pin #0");
43 SubmodelComponentSerializer.deserialize(submodelModifiable, "HalfAdder.json", "halfadder");
47 throw new UncheckedIOException(e);
52 @SuppressWarnings("unused") // GUIWires being created
53 private static void basicTest(ViewModelModifiable viewModel)
55 GUI_rsLatch comp = new GUI_rsLatch(viewModel, "Original RS latch");
57 SubmodelComponentParams params = SubmodelComponentSerializer.serialize(comp);
58 String jsonString = JsonHandler.toJson(params);
59 System.out.println(jsonString);
60 SubmodelComponentParams paramsD = JsonHandler.fromJson(jsonString, SubmodelComponentParams.class);
61 SubmodelComponent componentD = SubmodelComponentSerializer.deserialize(viewModel, paramsD, "Deserialized RS latch");
62 componentD.moveTo(30, 50);
64 for (String s : comp.getInputPinNames())
66 GUIManualSwitch sw = new GUIManualSwitch(viewModel);
68 new GUIWire(viewModel, sw.getOutputPin(), comp.getPin(s));
69 sw = new GUIManualSwitch(viewModel);
71 new GUIWire(viewModel, sw.getOutputPin(), componentD.getPin(s));
75 for (String s : comp.getOutputPinNames())
77 GUIBitDisplay bd = new GUIBitDisplay(viewModel);
79 new GUIWire(viewModel, bd.getInputPin(), comp.getPin(s));
80 bd = new GUIBitDisplay(viewModel);
81 bd.moveTo(80, h + 50);
82 new GUIWire(viewModel, bd.getInputPin(), componentD.getPin(s));
87 // Execute only after HalfAdder.json has been created
88 public static void refJsonFromJsonTest(ViewModelModifiable model)
90 TestComponent t = new TestComponent(model, "Original component");
91 SubmodelComponentSerializer.serialize(t).writeJson("Test.json");
95 c = SubmodelComponentSerializer.deserialize(model, "Test.json", "Deserialized component");
99 throw new UncheckedIOException(e);
104 @SuppressWarnings("unused") // for GUIWires being created
105 public static void createFromJsonExample(ViewModelModifiable model)
107 SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model, "Original full adder");
108 SubmodelComponentParams pC = SubmodelComponentSerializer.serialize(tmp);
109 tmp.moveTo(1000, 100);
112 pC.writeJson("FullAdder.json");
113 pC = SubmodelComponentParams.readJson("FullAdder.json");
115 catch (IOException e)
120 SimpleRectangularSubmodelComponent adder;
123 adder = (SimpleRectangularSubmodelComponent) SubmodelComponentSerializer.deserialize(model, "FullAdder.json",
124 "Deserialized full adder");
126 catch (IOException e)
128 throw new UncheckedIOException(e);
131 GUIManualSwitch swA = new GUIManualSwitch(model);
133 GUIManualSwitch swB = new GUIManualSwitch(model);
135 GUIManualSwitch swC = new GUIManualSwitch(model);
138 adder.moveTo(30, 10);
139 GUIBitDisplay bdY = new GUIBitDisplay(model);
140 bdY.moveTo(90, 12.5);
141 GUIBitDisplay bdZ = new GUIBitDisplay(model);
144 new GUIWire(model, swA.getOutputPin(), adder.getPin("A"));
145 new GUIWire(model, swB.getOutputPin(), adder.getPin("B"));
146 new GUIWire(model, swC.getOutputPin(), adder.getPin("C"));
148 new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
149 new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
151 SubmodelComponent adder2 = SubmodelComponentSerializer.deserialize(model, pC, "Full adder created from params instance");
153 swA = new GUIManualSwitch(model);
155 swB = new GUIManualSwitch(model);
157 swC = new GUIManualSwitch(model);
160 adder2.moveTo(30, 80);
161 bdY = new GUIBitDisplay(model);
163 bdZ = new GUIBitDisplay(model);
166 new GUIWire(model, swA.getOutputPin(), adder2.getPin("A"));
167 new GUIWire(model, swB.getOutputPin(), adder2.getPin("B"));
168 new GUIWire(model, swC.getOutputPin(), adder2.getPin("C"));
170 new GUIWire(model, adder2.getPin("Y"), bdY.getInputPin());
171 new GUIWire(model, adder2.getPin("Z"), bdZ.getInputPin());