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