Updated version; added string-based methods in JsonHandler
[Mograsim.git] / net.mograsim.logic.ui.am2900 / src / net / mograsim / logic / ui / examples / JsonExample.java
1 package net.mograsim.logic.ui.examples;
2
3 import java.io.IOException;
4
5 import com.google.gson.JsonNull;
6
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;
21
22 public class JsonExample
23 {
24         public static void main(String[] args)
25         {
26                 SimpleLogicUIStandalone.executeVisualisation(JsonExample::basicTest);
27         }
28
29         public static void mappingTest(ViewModelModifiable model)
30         {
31                 IndirectGUIComponentCreator.createComponent(model, "Am2901", JsonNull.INSTANCE);
32         }
33
34         private static class TestComponent extends SimpleRectangularSubmodelComponent
35         {
36                 protected TestComponent(ViewModelModifiable model)
37                 {
38                         super(model, 1, "Test");
39                         setSubmodelScale(.4);
40                         setInputPins("Input pin #0");
41                         SubmodelComponentDeserializer.create(submodelModifiable, "HalfAdder.json");
42                 }
43         }
44
45         @SuppressWarnings("unused") // GUIWires being created
46         private static void basicTest(ViewModelModifiable viewModel)
47         {
48                 GUI_rsLatch comp = new GUI_rsLatch(viewModel);
49                 comp.moveTo(30, 0);
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);
56                 double h = 0;
57                 for (String s : comp.getInputPinNames())
58                 {
59                         GUIManualSwitch sw = new GUIManualSwitch(viewModel);
60                         sw.moveTo(0, h);
61                         new GUIWire(viewModel, sw.getOutputPin(), comp.getPin(s));
62                         sw = new GUIManualSwitch(viewModel);
63                         sw.moveTo(0, h + 50);
64                         new GUIWire(viewModel, sw.getOutputPin(), componentD.getPin(s));
65                         h += 20;
66                 }
67                 h = 0;
68                 for (String s : comp.getOutputPinNames())
69                 {
70                         GUIBitDisplay bd = new GUIBitDisplay(viewModel);
71                         bd.moveTo(80, h);
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));
76                         h += 20;
77                 }
78         }
79
80         // Execute only after HalfAdder.json has been created
81         public static void refJsonFromJsonTest(ViewModelModifiable model)
82         {
83                 TestComponent t = new TestComponent(model);
84                 t.calculateParams().writeJson("Test.json");
85                 SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json");
86                 c.moveTo(0, 50);
87         }
88
89         public static void createHalfAdderExample(ViewModelModifiable model)
90         {
91                 GUIhalfadder tmp = new GUIhalfadder(model);
92                 tmp.moveTo(1000, 50);
93                 SubmodelComponentParams p = tmp.calculateParams();
94                 try
95                 {
96                         p.writeJson("HalfAdder.json");
97                         p = SubmodelComponentParams.readJson("HalfAdder.json");
98                 }
99                 catch (IOException e)
100                 {
101                         e.printStackTrace();
102                 }
103
104                 SubmodelComponentDeserializer.create(model, p);
105         }
106
107         @SuppressWarnings("unused") // for GUIWires being created
108         public static void createFromJsonExample(ViewModelModifiable model)
109         {
110                 SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model);
111                 SubmodelComponentParams pC = tmp.calculateParams();
112                 tmp.moveTo(1000, 100);
113                 try
114                 {
115                         pC.writeJson("FullAdder.json");
116                         pC = SubmodelComponentParams.readJson("FullAdder.json");
117                 }
118                 catch (IOException e)
119                 {
120                         e.printStackTrace();
121                 }
122
123                 SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) SubmodelComponentDeserializer.create(model,
124                                 "FullAdder.json");
125
126                 GUIManualSwitch swA = new GUIManualSwitch(model);
127                 swA.moveTo(0, 0);
128                 GUIManualSwitch swB = new GUIManualSwitch(model);
129                 swB.moveTo(0, 25);
130                 GUIManualSwitch swC = new GUIManualSwitch(model);
131                 swC.moveTo(0, 50);
132
133                 adder.moveTo(30, 10);
134                 GUIBitDisplay bdY = new GUIBitDisplay(model);
135                 bdY.moveTo(90, 12.5);
136                 GUIBitDisplay bdZ = new GUIBitDisplay(model);
137                 bdZ.moveTo(90, 30);
138
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"));
142
143                 new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
144                 new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
145
146                 SubmodelComponent adder2 = SubmodelComponentDeserializer.create(model, pC);
147
148                 swA = new GUIManualSwitch(model);
149                 swA.moveTo(0, 70);
150                 swB = new GUIManualSwitch(model);
151                 swB.moveTo(0, 85);
152                 swC = new GUIManualSwitch(model);
153                 swC.moveTo(0, 100);
154
155                 adder2.moveTo(30, 80);
156                 bdY = new GUIBitDisplay(model);
157                 bdY.moveTo(90, 70);
158                 bdZ = new GUIBitDisplay(model);
159                 bdZ.moveTo(90, 85);
160
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"));
164
165                 new GUIWire(model, adder2.getPin("Y"), bdY.getInputPin());
166                 new GUIWire(model, adder2.getPin("Z"), bdZ.getInputPin());
167         }
168 }