1 package net.mograsim.logic.ui.examples;
3 import java.io.IOException;
5 import com.google.gson.JsonNull;
7 import net.mograsim.logic.ui.SimpleLogicUIStandalone;
8 import net.mograsim.logic.ui.model.ViewModelModifiable;
9 import net.mograsim.logic.ui.model.components.atomic.GUIBitDisplay;
10 import net.mograsim.logic.ui.model.components.atomic.GUIManualSwitch;
11 import net.mograsim.logic.ui.model.components.mi.nandbased.GUI_rsLatch;
12 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
13 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder;
14 import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
15 import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
16 import net.mograsim.logic.ui.model.wires.GUIWire;
17 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
18 import net.mograsim.logic.ui.serializing.SubmodelComponentDeserializer;
19 import net.mograsim.logic.ui.serializing.SubmodelComponentParams;
20 import net.mograsim.logic.ui.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);
34 private static class TestComponent extends SimpleRectangularSubmodelComponent
36 protected TestComponent(ViewModelModifiable model)
38 super(model, 1, "Test");
40 setInputPins("Input pin #0");
41 SubmodelComponentDeserializer.create(submodelModifiable, "HalfAdder.json");
45 @SuppressWarnings("unused") // GUIWires being created
46 private static void basicTest(ViewModelModifiable viewModel)
48 GUI_rsLatch comp = new GUI_rsLatch(viewModel);
50 SubmodelComponentParams params = comp.calculateParams();
51 String jsonString = JsonHandler.toJson(params);
52 System.out.println(jsonString);
53 SubmodelComponentParams paramsD = JsonHandler.fromJson(jsonString, SubmodelComponentParams.class);
54 SubmodelComponent componentD = SubmodelComponentDeserializer.create(viewModel, paramsD);
55 componentD.moveTo(30, 50);
57 for (String s : comp.getInputPinNames())
59 GUIManualSwitch sw = new GUIManualSwitch(viewModel);
61 new GUIWire(viewModel, sw.getOutputPin(), comp.getPin(s));
62 sw = new GUIManualSwitch(viewModel);
64 new GUIWire(viewModel, sw.getOutputPin(), componentD.getPin(s));
68 for (String s : comp.getOutputPinNames())
70 GUIBitDisplay bd = new GUIBitDisplay(viewModel);
72 new GUIWire(viewModel, bd.getInputPin(), comp.getPin(s));
73 bd = new GUIBitDisplay(viewModel);
74 bd.moveTo(80, h + 50);
75 new GUIWire(viewModel, bd.getInputPin(), componentD.getPin(s));
80 // Execute only after HalfAdder.json has been created
81 public static void refJsonFromJsonTest(ViewModelModifiable model)
83 TestComponent t = new TestComponent(model);
84 t.calculateParams().writeJson("Test.json");
85 SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json");
89 public static void createHalfAdderExample(ViewModelModifiable model)
91 GUIhalfadder tmp = new GUIhalfadder(model);
93 SubmodelComponentParams p = tmp.calculateParams();
96 p.writeJson("HalfAdder.json");
97 p = SubmodelComponentParams.readJson("HalfAdder.json");
104 SubmodelComponentDeserializer.create(model, p);
107 @SuppressWarnings("unused") // for GUIWires being created
108 public static void createFromJsonExample(ViewModelModifiable model)
110 SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model);
111 SubmodelComponentParams pC = tmp.calculateParams();
112 tmp.moveTo(1000, 100);
115 pC.writeJson("FullAdder.json");
116 pC = SubmodelComponentParams.readJson("FullAdder.json");
118 catch (IOException e)
123 SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) SubmodelComponentDeserializer.create(model,
126 GUIManualSwitch swA = new GUIManualSwitch(model);
128 GUIManualSwitch swB = new GUIManualSwitch(model);
130 GUIManualSwitch swC = new GUIManualSwitch(model);
133 adder.moveTo(30, 10);
134 GUIBitDisplay bdY = new GUIBitDisplay(model);
135 bdY.moveTo(90, 12.5);
136 GUIBitDisplay bdZ = new GUIBitDisplay(model);
139 new GUIWire(model, swA.getOutputPin(), adder.getPin("A"));
140 new GUIWire(model, swB.getOutputPin(), adder.getPin("B"));
141 new GUIWire(model, swC.getOutputPin(), adder.getPin("C"));
143 new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
144 new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
146 SubmodelComponent adder2 = SubmodelComponentDeserializer.create(model, pC);
148 swA = new GUIManualSwitch(model);
150 swB = new GUIManualSwitch(model);
152 swC = new GUIManualSwitch(model);
155 adder2.moveTo(30, 80);
156 bdY = new GUIBitDisplay(model);
158 bdZ = new GUIBitDisplay(model);
161 new GUIWire(model, swA.getOutputPin(), adder2.getPin("A"));
162 new GUIWire(model, swB.getOutputPin(), adder2.getPin("B"));
163 new GUIWire(model, swC.getOutputPin(), adder2.getPin("C"));
165 new GUIWire(model, adder2.getPin("Y"), bdY.getInputPin());
166 new GUIWire(model, adder2.getPin("Z"), bdZ.getInputPin());