Restructured JSON (de)serializing: ViewModels can be (de)serialized too
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / serializing / ViewModelSerializer.java
1 package net.mograsim.logic.model.serializing;
2
3 import java.io.IOException;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.Map;
8 import java.util.Set;
9
10 import com.google.gson.JsonElement;
11
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
13 import net.mograsim.logic.model.model.ViewModel;
14 import net.mograsim.logic.model.model.ViewModelModifiable;
15 import net.mograsim.logic.model.model.components.GUIComponent;
16 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
17 import net.mograsim.logic.model.model.wires.GUIWire;
18 import net.mograsim.logic.model.serializing.ViewModelParams.ComponentParams;
19 import net.mograsim.logic.model.serializing.ViewModelParams.WireParams;
20 import net.mograsim.logic.model.serializing.ViewModelParams.WireParams.PinParams;
21 import net.mograsim.logic.model.util.JsonHandler;
22 import net.mograsim.logic.model.util.Version;
23
24 public class ViewModelSerializer
25 {
26         public static final Version CURRENT_JSON_VERSION = Version.parseSemver("0.1.1");
27
28         // convenience methods
29         /**
30          * Like {@link #deserialize(ViewModelParams)}, but first reading the {@link ViewModelParams} from the given file path.
31          * 
32          * @author Daniel Kirschten
33          */
34         public static ViewModelModifiable deserialize(String sourcePath) throws IOException
35         {
36                 return deserialize(JsonHandler.readJson(sourcePath, ViewModelParams.class));
37         }
38
39         /**
40          * Like {@link #deserialize(ViewModelModifiable, ViewModelParams)}, but first reading the {@link ViewModelParams} from the given file
41          * path.
42          * 
43          * @author Daniel Kirschten
44          */
45         public static void deserialize(ViewModelModifiable model, String sourcePath) throws IOException
46         {
47                 deserialize(model, JsonHandler.readJson(sourcePath, ViewModelParams.class));
48         }
49
50         /**
51          * Like {@link #deserialize(ViewModelModifiable, ViewModelParams)}, but using a newly created {@link ViewModelModifiable}.
52          * 
53          * @author Daniel Kirschten
54          */
55         public static ViewModelModifiable deserialize(ViewModelParams params)
56         {
57                 ViewModelModifiable model = new ViewModelModifiable();
58                 deserialize(model, params);
59                 return model;
60         }
61
62         /**
63          * Like {@link #serialize(ViewModel)}, but instead of returning the generated {@link ViewModelParams} they are written to a file at the
64          * given path.
65          * 
66          * @author Daniel Kirschten
67          */
68         public static void serialize(ViewModel model, String targetPath) throws IOException
69         {
70                 JsonHandler.writeJson(serialize(model), targetPath);
71         }
72
73         /**
74          * Like {@link #serialize(ViewModel, IdentifierGetter)}, but instead of returning the generated {@link ViewModelParams} they are written
75          * to a file at the given path.
76          * 
77          * @author Daniel Kirschten
78          */
79         public static void serialize(ViewModel model, IdentifierGetter idGetter, String targetPath) throws IOException
80         {
81                 JsonHandler.writeJson(serialize(model, idGetter), targetPath);
82         }
83
84         /**
85          * {@link #serialize(ViewModel, IdentifierGetter)} using a default {@link IdentifierGetter} (see <code>IdentifierGetter</code>'s
86          * {@link IdentifierGetter#IdentifierGetter() default constructor})
87          * 
88          * @author Daniel Kirschten
89          */
90         public static ViewModelParams serialize(ViewModel model)
91         {
92                 return serialize(model, new IdentifierGetter());
93         }
94
95         // "core" methods
96         /**
97          * Deserializes components and wires from the specified {@link ViewModelParams} and adds them to the given {@link ViewModelModifiable}.
98          * 
99          * @author Fabian Stemmler
100          * @author Daniel Kirschten
101          */
102         @SuppressWarnings("unused") // for GUIWire being created
103         public static void deserialize(ViewModelModifiable model, ViewModelParams params)
104         {
105                 Map<String, GUIComponent> componentsByName = model.getComponentsByName();
106                 GUIComponent[] components = new GUIComponent[params.components.length];
107                 for (int i = 0; i < components.length; i++)
108                 {
109                         ComponentParams compParams = params.components[i];
110                         components[i] = IndirectGUIComponentCreator.createComponent(model, compParams.id, compParams.params, compParams.name);
111                         components[i].moveTo(compParams.pos.x, compParams.pos.y);
112                 }
113
114                 for (int i = 0; i < params.wires.length; i++)
115                 {
116                         WireParams wire = params.wires[i];
117                         new GUIWire(model, wire.name, componentsByName.get(wire.pin1.compName).getPin(wire.pin1.pinName),
118                                         componentsByName.get(wire.pin2.compName).getPin(wire.pin2.pinName), wire.path);
119                 }
120         }
121
122         /**
123          * Returns {@link ViewModelModifiable}, which describe the components and wires in the given {@link ViewModel}. <br>
124          * Components are serialized in the following way: <br>
125          * If a component is a <code>SubmodelComponent</code> which has been deserialized, and it has an
126          * {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride} set (e.g. non-null; see
127          * {@link SubmodelComponentSerializer#deserialize(ViewModelModifiable, SubmodelComponentParams, String, String, JsonElement)
128          * SubmodelComponentSerializer.deserialize(...)}), this ID and the component's
129          * {@link DeserializedSubmodelComponent#paramsForSerializingOverride paramsForSerializingOverride} are written.<br>
130          * If this case doesn't apply (e.g. if the component is not a <code>SubmodelComponent</code>; or it is a <code>SubmodelComponent</code>,
131          * but hasn't been deserialized; or it has no {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride}
132          * set), the ID defined by <code>idGetter</code> and the params obtained by {@link GUIComponent#getParamsForSerializing() getParams()}
133          * are written.
134          * 
135          * @author Fabian Stemmler
136          * @author Daniel Kirschten
137          */
138         public static ViewModelParams serialize(ViewModel model, IdentifierGetter idGetter)
139         {
140                 ViewModelParams modelParams = new ViewModelParams(CURRENT_JSON_VERSION);
141
142                 Map<String, GUIComponent> components = new HashMap<>(model.getComponentsByName());
143                 components.remove(SubmodelComponent.SUBMODEL_INTERFACE_NAME);
144                 Set<ComponentParams> componentsParams = new HashSet<>();
145                 for (GUIComponent component : components.values())
146                 {
147                         ComponentParams compParams = new ComponentParams();
148                         componentsParams.add(compParams);
149                         compParams.pos = new Point(component.getPosX(), component.getPosY());
150                         DeserializedSubmodelComponent innerCompCasted;
151                         if (component instanceof DeserializedSubmodelComponent
152                                         && (innerCompCasted = (DeserializedSubmodelComponent) component).idForSerializingOverride != null)
153                         {
154                                 compParams.id = innerCompCasted.idForSerializingOverride;
155                                 compParams.params = innerCompCasted.paramsForSerializingOverride;
156                         } else
157                         {
158                                 compParams.id = idGetter.componentIDs.apply(component);
159                                 compParams.params = component.getParamsForSerializing(idGetter);
160                         }
161                         compParams.name = component.name;
162                 }
163                 modelParams.components = componentsParams.toArray(ComponentParams[]::new);
164
165                 Collection<GUIWire> wires = model.getWiresByName().values();
166                 Set<WireParams> wiresParams = new HashSet<>();
167                 for (GUIWire innerWire : wires)
168                 {
169                         WireParams innerWireParams = new WireParams();
170                         wiresParams.add(innerWireParams);
171                         PinParams pin1Params = new PinParams(), pin2Params = new PinParams();
172
173                         pin1Params.pinName = innerWire.getPin1().name;
174                         pin1Params.compName = innerWire.getPin1().component.name;
175                         pin2Params.pinName = innerWire.getPin2().name;
176                         pin2Params.compName = innerWire.getPin2().component.name;
177                         innerWireParams.name = innerWire.name;
178                         innerWireParams.pin1 = pin1Params;
179                         innerWireParams.pin2 = pin2Params;
180                         innerWireParams.path = innerWire.getPath();
181                 }
182                 modelParams.wires = wiresParams.toArray(WireParams[]::new);
183
184                 return modelParams;
185         }
186 }