1 package net.mograsim.logic.ui.examples;
3 import java.io.IOException;
5 import com.google.gson.Gson;
6 import com.google.gson.GsonBuilder;
7 import com.google.gson.JsonNull;
9 import net.mograsim.logic.ui.SimpleLogicUIStandalone;
10 import net.mograsim.logic.ui.model.ViewModelModifiable;
11 import net.mograsim.logic.ui.model.components.atomic.GUIBitDisplay;
12 import net.mograsim.logic.ui.model.components.atomic.GUIManualSwitch;
13 import net.mograsim.logic.ui.model.components.mi.nandbased.GUI_rsLatch;
14 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
15 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder;
16 import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
17 import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
18 import net.mograsim.logic.ui.model.wires.GUIWire;
19 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
20 import net.mograsim.logic.ui.serializing.SubmodelComponentDeserializer;
21 import net.mograsim.logic.ui.serializing.SubmodelComponentParams;
23 public class JsonExample
25 public static void main(String[] args)
27 SimpleLogicUIStandalone.executeVisualisation(JsonExample::basicTest);
30 public static void mappingTest(ViewModelModifiable model)
32 IndirectGUIComponentCreator.createComponent(model, "Am2901", JsonNull.INSTANCE);
35 private static class TestComponent extends SimpleRectangularSubmodelComponent
37 protected TestComponent(ViewModelModifiable model)
39 super(model, 1, "Test");
41 setInputPins("Input pin #0");
42 SubmodelComponentDeserializer.create(submodelModifiable, "HalfAdder.json");
46 @SuppressWarnings("unused") // GUIWires being created
47 private static void basicTest(ViewModelModifiable viewModel)
49 GUI_rsLatch comp = new GUI_rsLatch(viewModel);
51 SubmodelComponentParams params = comp.calculateParams();
52 String jsonString = new GsonBuilder().setPrettyPrinting().create().toJson(params);
53 System.out.println(jsonString);
54 SubmodelComponent deserialized = SubmodelComponentDeserializer.create(viewModel,
55 new Gson().fromJson(jsonString, SubmodelComponentParams.class));
56 deserialized.moveTo(30, 50);
58 for (String s : comp.getInputPinNames())
60 GUIManualSwitch sw = new GUIManualSwitch(viewModel);
62 new GUIWire(viewModel, sw.getOutputPin(), comp.getPin(s));
63 sw = new GUIManualSwitch(viewModel);
65 new GUIWire(viewModel, sw.getOutputPin(), deserialized.getPin(s));
69 for (String s : comp.getOutputPinNames())
71 GUIBitDisplay bd = new GUIBitDisplay(viewModel);
73 new GUIWire(viewModel, bd.getInputPin(), comp.getPin(s));
74 bd = new GUIBitDisplay(viewModel);
75 bd.moveTo(80, h + 50);
76 new GUIWire(viewModel, bd.getInputPin(), deserialized.getPin(s));
81 // Execute only after HalfAdder.json has been created
82 public static void refJsonFromJsonTest(ViewModelModifiable model)
84 TestComponent t = new TestComponent(model);
85 t.calculateParams().writeJson("Test.json");
86 SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json");
90 public static void createHalfAdderExample(ViewModelModifiable model)
92 GUIhalfadder tmp = new GUIhalfadder(model);
94 SubmodelComponentParams p = tmp.calculateParams();
97 p.writeJson("HalfAdder.json");
98 p = SubmodelComponentParams.readJson("HalfAdder.json");
100 catch (IOException e)
105 SubmodelComponentDeserializer.create(model, p);
108 @SuppressWarnings("unused") // for GUIWires being created
109 public static void createFromJsonExample(ViewModelModifiable model)
111 SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model);
112 SubmodelComponentParams pC = tmp.calculateParams();
113 tmp.moveTo(1000, 100);
116 pC.writeJson("FullAdder.json");
117 pC = SubmodelComponentParams.readJson("FullAdder.json");
119 catch (IOException e)
124 SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) SubmodelComponentDeserializer.create(model,
127 GUIManualSwitch swA = new GUIManualSwitch(model);
129 GUIManualSwitch swB = new GUIManualSwitch(model);
131 GUIManualSwitch swC = new GUIManualSwitch(model);
134 adder.moveTo(30, 10);
135 GUIBitDisplay bdY = new GUIBitDisplay(model);
136 bdY.moveTo(90, 12.5);
137 GUIBitDisplay bdZ = new GUIBitDisplay(model);
140 new GUIWire(model, swA.getOutputPin(), adder.getPin("A"));
141 new GUIWire(model, swB.getOutputPin(), adder.getPin("B"));
142 new GUIWire(model, swC.getOutputPin(), adder.getPin("C"));
144 new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
145 new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
147 SubmodelComponent adder2 = SubmodelComponentDeserializer.create(model, pC);
149 swA = new GUIManualSwitch(model);
151 swB = new GUIManualSwitch(model);
153 swC = new GUIManualSwitch(model);
156 adder2.moveTo(30, 80);
157 bdY = new GUIBitDisplay(model);
159 bdZ = new GUIBitDisplay(model);
162 new GUIWire(model, swA.getOutputPin(), adder2.getPin("A"));
163 new GUIWire(model, swB.getOutputPin(), adder2.getPin("B"));
164 new GUIWire(model, swC.getOutputPin(), adder2.getPin("C"));
166 new GUIWire(model, adder2.getPin("Y"), bdY.getInputPin());
167 new GUIWire(model, adder2.getPin("Z"), bdZ.getInputPin());