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