Renamed ViewModel to LogicModel
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / serializing / LogicModelSerializer.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.LogicModel;
16 import net.mograsim.logic.model.model.LogicModelModifiable;
17 import net.mograsim.logic.model.model.components.ModelComponent;
18 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
19 import net.mograsim.logic.model.model.wires.ModelWire;
20 import net.mograsim.logic.model.serializing.LogicModelParams.ComponentParams;
21 import net.mograsim.logic.model.serializing.LogicModelParams.WireParams;
22 import net.mograsim.logic.model.serializing.LogicModelParams.WireParams.PinParams;
23 import net.mograsim.logic.model.util.JsonHandler;
24 import net.mograsim.logic.model.util.Version;
25
26 public class LogicModelSerializer
27 {
28         public static final Version CURRENT_JSON_VERSION = Version.parseSemver("0.1.1");
29
30         // convenience methods
31         /**
32          * Like {@link #deserialize(LogicModelParams)}, but first reading the {@link LogicModelParams} from the given file path.
33          * 
34          * @author Daniel Kirschten
35          */
36         public static LogicModelModifiable deserialize(String sourcePath) throws IOException
37         {
38                 return deserialize(JsonHandler.readJson(sourcePath, LogicModelParams.class));
39         }
40
41         /**
42          * Like {@link #deserialize(LogicModelModifiable, LogicModelParams)}, but first reading the {@link LogicModelParams} from the given file
43          * path.
44          * 
45          * @author Daniel Kirschten
46          */
47         public static void deserialize(LogicModelModifiable model, String sourcePath) throws IOException
48         {
49                 deserialize(model, JsonHandler.readJson(sourcePath, LogicModelParams.class));
50         }
51
52         /**
53          * Like {@link #deserialize(LogicModelModifiable, LogicModelParams)}, but using a newly created {@link LogicModelModifiable}.
54          * 
55          * @author Daniel Kirschten
56          */
57         public static LogicModelModifiable deserialize(LogicModelParams params)
58         {
59                 LogicModelModifiable model = new LogicModelModifiable();
60                 deserialize(model, params);
61                 return model;
62         }
63
64         /**
65          * Like {@link #serialize(LogicModel)}, but instead of returning the generated {@link LogicModelParams} they are written to a file at the
66          * given path.
67          * 
68          * @author Daniel Kirschten
69          */
70         public static void serialize(LogicModel model, String targetPath) throws IOException
71         {
72                 JsonHandler.writeJson(serialize(model), targetPath);
73         }
74
75         /**
76          * Like {@link #serialize(LogicModel, IdentifierGetter)}, but instead of returning the generated {@link LogicModelParams} they are written
77          * to a file at the given path.
78          * 
79          * @author Daniel Kirschten
80          */
81         public static void serialize(LogicModel model, IdentifyParams idParams, String targetPath) throws IOException
82         {
83                 JsonHandler.writeJson(serialize(model, idParams), targetPath);
84         }
85
86         /**
87          * {@link #serialize(LogicModel, 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 LogicModelParams serialize(LogicModel model)
93         {
94                 return serialize(model, new IdentifyParams());
95         }
96
97         // "core" methods
98         /**
99          * Deserializes components and wires from the specified {@link LogicModelParams} and adds them to the given {@link LogicModelModifiable}.
100          * 
101          * @author Fabian Stemmler
102          * @author Daniel Kirschten
103          */
104         @SuppressWarnings("unused") // for ModelWire being created
105         public static void deserialize(LogicModelModifiable model, LogicModelParams params)
106         {
107                 Map<String, ModelComponent> componentsByName = model.getComponentsByName();
108                 ModelComponent[] components = new ModelComponent[params.components.length];
109                 for (int i = 0; i < components.length; i++)
110                 {
111                         ComponentParams compParams = params.components[i];
112                         components[i] = IndirectModelComponentCreator.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 ModelWire(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 LogicModelModifiable}, which describe the components and wires in the given {@link LogicModel}. <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(LogicModelModifiable, 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 ModelComponent#getParamsForSerializing() getParams()}
135          * are written.
136          * 
137          * @author Fabian Stemmler
138          * @author Daniel Kirschten
139          */
140         public static LogicModelParams serialize(LogicModel model, IdentifyParams idParams)
141         {
142                 LogicModelParams modelParams = new LogicModelParams(CURRENT_JSON_VERSION);
143
144                 Map<String, ModelComponent> components = new HashMap<>(model.getComponentsByName());
145                 components.remove(SubmodelComponent.SUBMODEL_INTERFACE_NAME);
146                 Set<ComponentParams> componentsParams = new HashSet<>();
147                 for (ModelComponent 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 = component.getIDForSerializing(idParams);
161                                 compParams.params = component.getParamsForSerializingJSON(idParams);
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<ModelWire> wires = model.getWiresByName().values();
169                 Set<WireParams> wiresParams = new HashSet<>();
170                 for (ModelWire 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 }