Restructured serializing classes
[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 import java.util.HashMap;
5
6 import net.mograsim.logic.ui.SimpleLogicUIStandalone;
7 import net.mograsim.logic.ui.model.ViewModelModifiable;
8 import net.mograsim.logic.ui.model.components.atomic.GUIBitDisplay;
9 import net.mograsim.logic.ui.model.components.atomic.GUIManualSwitch;
10 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
11 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder;
12 import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
13 import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
14 import net.mograsim.logic.ui.model.wires.GUIWire;
15 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
16 import net.mograsim.logic.ui.serializing.SubmodelComponentDeserializer;
17 import net.mograsim.logic.ui.serializing.SubmodelComponentParams;
18
19 public class JsonExample
20 {
21         public static void main(String[] args)
22         {
23                 SimpleLogicUIStandalone.executeVisualisation(JsonExample::mappingTest);
24         }
25
26         public static void mappingTest(ViewModelModifiable model)
27         {
28                 IndirectGUIComponentCreator.create(model, "GUIAm2901", new HashMap<String, Object>());
29         }
30
31         private static class TestComponent extends SimpleRectangularSubmodelComponent
32         {
33                 protected TestComponent(ViewModelModifiable model)
34                 {
35                         super(model, 1, "Test");
36                         setSubmodelScale(.4);
37                         setInputPins("Input pin #0");
38                         SubmodelComponentDeserializer.create(submodelModifiable, "HalfAdder.json");
39                 }
40         }
41
42         // Execute only after HalfAdder.json has been created
43         public static void refJsonFromJsonTest(ViewModelModifiable model)
44         {
45                 TestComponent t = new TestComponent(model);
46                 t.calculateParams().writeJson("Test.json");
47                 SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json");
48                 c.moveTo(0, 50);
49         }
50
51         public static void createHalfAdderExample(ViewModelModifiable model)
52         {
53                 GUIhalfadder tmp = new GUIhalfadder(model);
54                 tmp.moveTo(1000, 50);
55                 SubmodelComponentParams p = tmp.calculateParams();
56                 try
57                 {
58                         p.writeJson("HalfAdder.json");
59                         p = SubmodelComponentParams.readJson("HalfAdder.json");
60                 }
61                 catch (IOException e)
62                 {
63                         e.printStackTrace();
64                 }
65
66                 SubmodelComponentDeserializer.create(model, p);
67         }
68
69         @SuppressWarnings("unused") // for GUIWires being created
70         public static void createFromJsonExample(ViewModelModifiable model)
71         {
72                 SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model);
73                 SubmodelComponentParams pC = tmp.calculateParams();
74                 tmp.moveTo(1000, 100);
75                 try
76                 {
77                         pC.writeJson("FullAdder.json");
78                         pC = SubmodelComponentParams.readJson("FullAdder.json");
79                 }
80                 catch (IOException e)
81                 {
82                         e.printStackTrace();
83                 }
84
85                 SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) SubmodelComponentDeserializer.create(model,
86                                 "FullAdder.json");
87
88                 GUIManualSwitch swA = new GUIManualSwitch(model);
89                 swA.moveTo(0, 0);
90                 GUIManualSwitch swB = new GUIManualSwitch(model);
91                 swB.moveTo(0, 25);
92                 GUIManualSwitch swC = new GUIManualSwitch(model);
93                 swC.moveTo(0, 50);
94
95                 adder.moveTo(30, 10);
96                 GUIBitDisplay bdY = new GUIBitDisplay(model);
97                 bdY.moveTo(90, 12.5);
98                 GUIBitDisplay bdZ = new GUIBitDisplay(model);
99                 bdZ.moveTo(90, 30);
100
101                 new GUIWire(model, swA.getOutputPin(), adder.getPin("A"));
102                 new GUIWire(model, swB.getOutputPin(), adder.getPin("B"));
103                 new GUIWire(model, swC.getOutputPin(), adder.getPin("C"));
104
105                 new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
106                 new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
107
108                 SubmodelComponent adder2 = SubmodelComponentDeserializer.create(model, pC);
109
110                 swA = new GUIManualSwitch(model);
111                 swA.moveTo(0, 70);
112                 swB = new GUIManualSwitch(model);
113                 swB.moveTo(0, 85);
114                 swC = new GUIManualSwitch(model);
115                 swC.moveTo(0, 100);
116
117                 adder2.moveTo(30, 80);
118                 bdY = new GUIBitDisplay(model);
119                 bdY.moveTo(90, 70);
120                 bdZ = new GUIBitDisplay(model);
121                 bdZ.moveTo(90, 85);
122
123                 new GUIWire(model, swA.getOutputPin(), adder2.getPin("A"));
124                 new GUIWire(model, swB.getOutputPin(), adder2.getPin("B"));
125                 new GUIWire(model, swC.getOutputPin(), adder2.getPin("C"));
126
127                 new GUIWire(model, adder2.getPin("Y"), bdY.getInputPin());
128                 new GUIWire(model, adder2.getPin("Z"), bdZ.getInputPin());
129         }
130 }