Restructured serializing/deserializing
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / examples / JsonExample.java
1 package net.mograsim.logic.model.examples;
2
3 import java.io.IOException;
4 import java.io.UncheckedIOException;
5
6 import com.google.gson.JsonNull;
7
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;
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, "Am2901 instance");
32         }
33
34         private static class TestComponent extends SimpleRectangularSubmodelComponent
35         {
36                 protected TestComponent(ViewModelModifiable model, String name)
37                 {
38                         super(model, 1, "Test", name);
39                         setSubmodelScale(.4);
40                         setInputPins("Input pin #0");
41                         try
42                         {
43                                 SubmodelComponentSerializer.deserialize(submodelModifiable, "HalfAdder.json", "halfadder");
44                         }
45                         catch (IOException e)
46                         {
47                                 throw new UncheckedIOException(e);
48                         }
49                 }
50         }
51
52         @SuppressWarnings("unused") // GUIWires being created
53         private static void basicTest(ViewModelModifiable viewModel)
54         {
55                 GUI_rsLatch comp = new GUI_rsLatch(viewModel, "Original RS latch");
56                 comp.moveTo(30, 0);
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);
63                 double h = 0;
64                 for (String s : comp.getInputPinNames())
65                 {
66                         GUIManualSwitch sw = new GUIManualSwitch(viewModel);
67                         sw.moveTo(0, h);
68                         new GUIWire(viewModel, sw.getOutputPin(), comp.getPin(s));
69                         sw = new GUIManualSwitch(viewModel);
70                         sw.moveTo(0, h + 50);
71                         new GUIWire(viewModel, sw.getOutputPin(), componentD.getPin(s));
72                         h += 20;
73                 }
74                 h = 0;
75                 for (String s : comp.getOutputPinNames())
76                 {
77                         GUIBitDisplay bd = new GUIBitDisplay(viewModel);
78                         bd.moveTo(80, h);
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));
83                         h += 20;
84                 }
85         }
86
87         // Execute only after HalfAdder.json has been created
88         public static void refJsonFromJsonTest(ViewModelModifiable model)
89         {
90                 TestComponent t = new TestComponent(model, "Original component");
91                 SubmodelComponentSerializer.serialize(t).writeJson("Test.json");
92                 SubmodelComponent c;
93                 try
94                 {
95                         c = SubmodelComponentSerializer.deserialize(model, "Test.json", "Deserialized component");
96                 }
97                 catch (IOException e)
98                 {
99                         throw new UncheckedIOException(e);
100                 }
101                 c.moveTo(0, 50);
102         }
103
104         @SuppressWarnings("unused") // for GUIWires being created
105         public static void createFromJsonExample(ViewModelModifiable model)
106         {
107                 SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model, "Original full adder");
108                 SubmodelComponentParams pC = SubmodelComponentSerializer.serialize(tmp);
109                 tmp.moveTo(1000, 100);
110                 try
111                 {
112                         pC.writeJson("FullAdder.json");
113                         pC = SubmodelComponentParams.readJson("FullAdder.json");
114                 }
115                 catch (IOException e)
116                 {
117                         e.printStackTrace();
118                 }
119
120                 SimpleRectangularSubmodelComponent adder;
121                 try
122                 {
123                         adder = (SimpleRectangularSubmodelComponent) SubmodelComponentSerializer.deserialize(model, "FullAdder.json",
124                                         "Deserialized full adder");
125                 }
126                 catch (IOException e)
127                 {
128                         throw new UncheckedIOException(e);
129                 }
130
131                 GUIManualSwitch swA = new GUIManualSwitch(model);
132                 swA.moveTo(0, 0);
133                 GUIManualSwitch swB = new GUIManualSwitch(model);
134                 swB.moveTo(0, 25);
135                 GUIManualSwitch swC = new GUIManualSwitch(model);
136                 swC.moveTo(0, 50);
137
138                 adder.moveTo(30, 10);
139                 GUIBitDisplay bdY = new GUIBitDisplay(model);
140                 bdY.moveTo(90, 12.5);
141                 GUIBitDisplay bdZ = new GUIBitDisplay(model);
142                 bdZ.moveTo(90, 30);
143
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"));
147
148                 new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
149                 new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
150
151                 SubmodelComponent adder2 = SubmodelComponentSerializer.deserialize(model, pC, "Full adder created from params instance");
152
153                 swA = new GUIManualSwitch(model);
154                 swA.moveTo(0, 70);
155                 swB = new GUIManualSwitch(model);
156                 swB.moveTo(0, 85);
157                 swC = new GUIManualSwitch(model);
158                 swC.moveTo(0, 100);
159
160                 adder2.moveTo(30, 80);
161                 bdY = new GUIBitDisplay(model);
162                 bdY.moveTo(90, 70);
163                 bdZ = new GUIBitDisplay(model);
164                 bdZ.moveTo(90, 85);
165
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"));
169
170                 new GUIWire(model, adder2.getPin("Y"), bdY.getInputPin());
171                 new GUIWire(model, adder2.getPin("Z"), bdZ.getInputPin());
172         }
173 }