Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / serializing / LegacySubmodelComponentSerializer.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.Map;
7 import java.util.function.Function;
8
9 import com.google.gson.JsonElement;
10
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12 import net.mograsim.logic.model.model.LogicModelModifiable;
13 import net.mograsim.logic.model.model.components.ModelComponent;
14 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
15 import net.mograsim.logic.model.model.wires.ModelWire;
16 import net.mograsim.logic.model.model.wires.MovablePin;
17 import net.mograsim.logic.model.model.wires.Pin;
18 import net.mograsim.logic.model.model.wires.PinUsage;
19 import net.mograsim.logic.model.serializing.LegacySubmodelComponentParams.LegacyInterfacePinParams;
20 import net.mograsim.logic.model.serializing.LegacySubmodelComponentParams.LegacySubmodelParameters;
21 import net.mograsim.logic.model.serializing.LegacySubmodelComponentParams.LegacySubmodelParameters.LegacyInnerComponentParams;
22 import net.mograsim.logic.model.serializing.LegacySubmodelComponentParams.LegacySubmodelParameters.LegacyInnerWireParams;
23 import net.mograsim.logic.model.serializing.LegacySubmodelComponentParams.LegacySubmodelParameters.LegacyInnerWireParams.LegacyInnerPinParams;
24 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
25 import net.mograsim.logic.model.snippets.Renderer;
26 import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
27 import net.mograsim.logic.model.util.JsonHandler;
28
29 /**
30  * Creates {@link SubmodelComponent}s from {@link LegacySubmodelComponentParams}
31  * 
32  * @author Fabian Stemmler
33  * @author Daniel Kirschten
34  */
35 public final class LegacySubmodelComponentSerializer
36 {
37         // convenience methods
38
39         /**
40          * Like {@link #deserialize(LogicModelModifiable, LegacySubmodelComponentParams)}, but first reading the
41          * {@link LegacySubmodelComponentParams} from the given file path.
42          * 
43          * @author Daniel Kirschten
44          */
45         public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath) throws IOException
46         {
47                 return deserialize(model, JsonHandler.readJson(sourcePath, LegacySubmodelComponentParams.class));
48         }
49
50         /**
51          * Like {@link #deserialize(LogicModelModifiable, LegacySubmodelComponentParams, String, JsonElement)}, but first reading the
52          * {@link LegacySubmodelComponentParams} from the given file path.
53          * 
54          * @author Daniel Kirschten
55          */
56         public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath, String idForSerializingOverride,
57                         JsonElement paramsForSerializingOverride) throws IOException
58         {
59                 return deserialize(model, JsonHandler.readJson(sourcePath, LegacySubmodelComponentParams.class), idForSerializingOverride,
60                                 paramsForSerializingOverride);
61         }
62
63         /**
64          * Like {@link #deserialize(LogicModelModifiable, LegacySubmodelComponentParams, String)}, but first reading the
65          * {@link LegacySubmodelComponentParams} from the given file path.
66          * 
67          * @author Daniel Kirschten
68          */
69         public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath, String name) throws IOException
70         {
71                 return deserialize(model, JsonHandler.readJson(sourcePath, LegacySubmodelComponentParams.class), name);
72         }
73
74         /**
75          * Like {@link #deserialize(LogicModelModifiable, LegacySubmodelComponentParams, String, String, JsonElement)}, but first reading the
76          * {@link LegacySubmodelComponentParams} from the given file path.
77          * 
78          * @author Daniel Kirschten
79          */
80         public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath, String name, String idForSerializingOverride,
81                         JsonElement paramsForSerializingOverride) throws IOException
82         {
83                 return deserialize(model, JsonHandler.readJson(sourcePath, LegacySubmodelComponentParams.class), name, idForSerializingOverride,
84                                 paramsForSerializingOverride);
85         }
86
87         /**
88          * {@link #deserialize(LogicModelModifiable, LegacySubmodelComponentParams, String, String, JsonElement)} with no
89          * <code>idForSerializingOverride</code> set and using the default name.
90          * 
91          * @author Daniel Kirschten
92          */
93         public static SubmodelComponent deserialize(LogicModelModifiable model, LegacySubmodelComponentParams params)
94         {
95                 return deserialize(model, params, null, null, null);
96         }
97
98         /**
99          * {@link #deserialize(LogicModelModifiable, LegacySubmodelComponentParams, String, String, JsonElement)} using the default name.
100          * 
101          * @author Daniel Kirschten
102          */
103         public static SubmodelComponent deserialize(LogicModelModifiable model, LegacySubmodelComponentParams params,
104                         String idForSerializingOverride, JsonElement paramsForSerializingOverride)
105         {
106                 return deserialize(model, params, null, idForSerializingOverride, paramsForSerializingOverride);
107         }
108
109         /**
110          * {@link #deserialize(LogicModelModifiable, LegacySubmodelComponentParams, String, String, JsonElement)} with no
111          * <code>idForSerializingOverride</code> set.
112          * 
113          * @author Daniel Kirschten
114          */
115         public static SubmodelComponent deserialize(LogicModelModifiable model, LegacySubmodelComponentParams params, String name)
116         {
117                 return deserialize(model, params, name, null, null);
118         }
119
120         /**
121          * Like {@link #serialize(SubmodelComponent)}, but instead of returning the generated {@link LegacySubmodelComponentParams} they are
122          * written to a file at the given path.
123          * 
124          * @author Daniel Kirschten
125          */
126         public static void serialize(SubmodelComponent comp, String targetPath) throws IOException
127         {
128                 JsonHandler.writeJson(serialize(comp), targetPath);
129         }
130
131         /**
132          * Like {@link #serialize(SubmodelComponent, Function)}, but instead of returning the generated {@link LegacySubmodelComponentParams}
133          * they are written to a file at the given path.
134          * 
135          * @author Daniel Kirschten
136          */
137         public static void serialize(SubmodelComponent comp, IdentifyParams idParams, String targetPath) throws IOException
138         {
139                 JsonHandler.writeJson(serialize(comp, idParams), targetPath);
140         }
141
142         /**
143          * {@link #serialize(SubmodelComponent, Function)} using the default {@link IdentifyParams} (see <code>IdentifyParams</code>'s
144          * {@link IdentifyParams#IdentifyParams() default constructor})
145          * 
146          * @author Daniel Kirschten
147          */
148         public static LegacySubmodelComponentParams serialize(SubmodelComponent comp)
149         {
150                 return serialize(comp, new IdentifyParams());
151         }
152
153         // "core" methods
154         /**
155          * Creates a {@link SubmodelComponent} from the specified {@link LegacySubmodelComponentParams} with the given name.
156          * <p>
157          * When serializing a <code>SubmodelComponent</code>, it is undesired for every subcomponent to be serialized with its complete inner
158          * structure. Instead, these sub-<code>SubmodelComponent</code>s should be serialized with the ID and params which were used to
159          * determine the <code>SubmodelComponentParams</code> defining the sub-<code>SubmodelComponent</code>. Because of this, it is possible
160          * to override the ID and params used in {@link #serialize(SubmodelComponent, Function) serialize(...)} to describe this subcomponent.
161          * See there for details.
162          * 
163          * @author Fabian Stemmler
164          * @author Daniel Kirschten
165          */
166         @SuppressWarnings("unused") // for ModelWire being created
167         public static SubmodelComponent deserialize(LogicModelModifiable model, LegacySubmodelComponentParams params, String name,
168                         String idForSerializingOverride, JsonElement paramsForSerializingOverride)
169         {
170                 DeserializedSubmodelComponent comp = new DeserializedSubmodelComponent(model, name, idForSerializingOverride,
171                                 paramsForSerializingOverride);
172                 comp.setSubmodelScale(params.submodel.innerScale);
173                 comp.setSize(params.width, params.height);
174                 for (LegacyInterfacePinParams iPinParams : params.interfacePins)
175                         // TRISTATE because we don't have a better choice
176                         comp.addSubmodelInterface(new MovablePin(model, comp, iPinParams.name, iPinParams.logicWidth, PinUsage.TRISTATE,
177                                         iPinParams.location.x, iPinParams.location.y));
178                 LegacySubmodelParameters submodelParams = params.submodel;
179                 LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
180                 Map<String, ModelComponent> componentsByName = submodelModifiable.getComponentsByName();
181                 ModelComponent[] components = new ModelComponent[submodelParams.subComps.length];
182                 for (int i = 0; i < components.length; i++)
183                 {
184                         LegacyInnerComponentParams cParams = submodelParams.subComps[i];
185                         components[i] = IndirectModelComponentCreator.createComponent(submodelModifiable, cParams.id, cParams.params, cParams.name);
186                         components[i].moveTo(cParams.pos.x, cParams.pos.y);
187                 }
188
189                 for (int i = 0; i < submodelParams.innerWires.length; i++)
190                 {
191                         LegacyInnerWireParams innerWire = submodelParams.innerWires[i];
192                         new ModelWire(submodelModifiable, innerWire.name, componentsByName.get(innerWire.pin1.compName).getPin(innerWire.pin1.pinName),
193                                         componentsByName.get(innerWire.pin2.compName).getPin(innerWire.pin2.pinName), innerWire.path);
194                 }
195                 comp.setSymbolRenderer(SubmodelComponentSnippetSuppliers.symbolRendererSupplier.getSnippetSupplier(params.symbolRendererSnippetID)
196                                 .create(comp, params.symbolRendererParams));
197                 comp.setOutlineRenderer(SubmodelComponentSnippetSuppliers.outlineRendererSupplier
198                                 .getSnippetSupplier(params.outlineRendererSnippetID).create(comp, params.outlineRendererParams));
199                 comp.setHighLevelStateHandler(SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier
200                                 .getSnippetSupplier(params.highLevelStateHandlerSnippetID).create(comp, params.highLevelStateHandlerParams));
201                 return comp;
202         }
203
204         /**
205          * Returns {@link LegacySubmodelComponentParams}, which describe this {@link SubmodelComponent}. <br>
206          * Subcomponents are serialized in the following way: <br>
207          * If a subcomponent is a <code>SubmodelComponent</code> which has been deserialized, and it has an
208          * {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride} set (e.g. non-null; see
209          * {@link #deserialize(LogicModelModifiable, LegacySubmodelComponentParams, String, String, JsonElement) deserialize(...)}), this ID and
210          * the component's {@link DeserializedSubmodelComponent#paramsForSerializingOverride paramsForSerializingOverride} are written.<br>
211          * If this case doesn't apply (e.g. if the subcomponent is not a <code>SubmodelComponent</code>; or it is a
212          * <code>SubmodelComponent</code>, but hasn't been deserialized; or it has no
213          * {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride} set), the ID defined by <code>idGetter</code>
214          * and the params obtained by {@link ModelComponent#getParamsForSerializing() getParams()} are written.<br>
215          * CodeSnippets are serialized using the ID defined by <code>idGetter</code> and the params obtained by the respective
216          * <coce>getParamsForSerializing</code> methods ({@link Renderer#getParamsForSerializing()}).
217          * 
218          * @author Fabian Stemmler
219          * @author Daniel Kirschten
220          */
221         public static LegacySubmodelComponentParams serialize(SubmodelComponent comp, IdentifyParams idParams)
222         {
223                 LegacySubmodelParameters submodelParams = new LegacySubmodelParameters();
224                 submodelParams.innerScale = comp.getSubmodelScale();
225
226                 Map<String, ModelComponent> components = new HashMap<>(comp.submodel.getComponentsByName());
227                 components.remove(SubmodelComponent.SUBMODEL_INTERFACE_NAME);
228                 LegacyInnerComponentParams[] componentParams = new LegacyInnerComponentParams[components.size()];
229                 int i1 = 0;
230                 for (ModelComponent innerComponent : components.values())
231                 {
232                         LegacyInnerComponentParams innerComponentParams = new LegacyInnerComponentParams();
233                         componentParams[i1] = innerComponentParams;
234                         innerComponentParams.pos = new Point(innerComponent.getPosX(), innerComponent.getPosY());
235                         innerComponentParams.id = innerComponent.getIDForSerializing(idParams);
236                         innerComponentParams.params = innerComponent.getParamsForSerializingJSON(idParams);
237                         innerComponentParams.name = innerComponent.getName();
238                         i1++;
239                 }
240                 submodelParams.subComps = componentParams;
241
242                 Collection<ModelWire> wires = comp.submodel.getWiresByName().values();
243                 LegacyInnerWireParams wireParams[] = new LegacyInnerWireParams[wires.size()];
244                 i1 = 0;
245                 for (ModelWire innerWire : wires)
246                 {
247                         LegacyInnerWireParams innerWireParams = new LegacyInnerWireParams();
248                         wireParams[i1] = innerWireParams;
249                         LegacyInnerPinParams pin1Params = new LegacyInnerPinParams(), pin2Params = new LegacyInnerPinParams();
250
251                         pin1Params.pinName = innerWire.getPin1().name;
252                         pin1Params.compName = innerWire.getPin1().component.getName();
253                         pin2Params.pinName = innerWire.getPin2().name;
254                         pin2Params.compName = innerWire.getPin2().component.getName();
255                         innerWireParams.name = innerWire.name;
256                         innerWireParams.pin1 = pin1Params;
257                         innerWireParams.pin2 = pin2Params;
258                         innerWireParams.path = innerWire.getPath();
259                         i1++;
260                 }
261                 submodelParams.innerWires = wireParams;
262
263                 LegacySubmodelComponentParams params = new LegacySubmodelComponentParams();
264                 params.submodel = submodelParams;
265
266                 params.width = comp.getWidth();
267                 params.height = comp.getHeight();
268
269                 LegacyInterfacePinParams[] iPins = new LegacyInterfacePinParams[comp.getPins().size()];
270                 int i = 0;
271                 for (Pin p : comp.getPins().values())
272                 {
273                         LegacyInterfacePinParams iPinParams = new LegacyInterfacePinParams();
274                         iPins[i] = iPinParams;
275                         iPinParams.location = p.getRelPos();
276                         iPinParams.name = p.name;
277                         iPinParams.logicWidth = p.logicWidth;
278                         i++;
279                 }
280                 params.interfacePins = iPins;
281
282                 Renderer symbolRenderer = comp.getSymbolRenderer();
283                 if (symbolRenderer != null)
284                 {
285                         params.symbolRendererSnippetID = symbolRenderer.getIDForSerializing(idParams);
286                         params.symbolRendererParams = symbolRenderer.getParamsForSerializingJSON(idParams);
287                 }
288
289                 Renderer outlineRenderer = comp.getOutlineRenderer();
290                 if (outlineRenderer != null)
291                 {
292                         params.outlineRendererSnippetID = outlineRenderer.getIDForSerializing(idParams);
293                         params.outlineRendererParams = outlineRenderer.getParamsForSerializingJSON(idParams);
294                 }
295
296                 HighLevelStateHandler highLevelStateHandler = comp.getHighLevelStateHandler();
297                 if (highLevelStateHandler != null)
298                 {
299                         params.highLevelStateHandlerSnippetID = highLevelStateHandler.getIDForSerializing(idParams);
300                         params.highLevelStateHandlerParams = highLevelStateHandler.getParamsForSerializingJSON(idParams);
301                 }
302
303                 return params;
304         }
305 }