1 package net.mograsim.logic.model.serializing;
3 import java.io.IOException;
4 import java.util.Collection;
5 import java.util.HashMap;
7 import java.util.function.Function;
9 import com.google.gson.JsonElement;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12 import net.mograsim.logic.model.model.ViewModelModifiable;
13 import net.mograsim.logic.model.model.components.GUIComponent;
14 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
15 import net.mograsim.logic.model.model.wires.GUIWire;
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;
30 * Creates {@link SubmodelComponent}s from {@link LegacySubmodelComponentParams}
32 * @author Fabian Stemmler
33 * @author Daniel Kirschten
35 public final class LegacySubmodelComponentSerializer
37 // convenience methods
40 * Like {@link #deserialize(ViewModelModifiable, LegacySubmodelComponentParams)}, but first reading the
41 * {@link LegacySubmodelComponentParams} from the given file path.
43 * @author Daniel Kirschten
45 public static SubmodelComponent deserialize(ViewModelModifiable model, String sourcePath) throws IOException
47 return deserialize(model, JsonHandler.readJson(sourcePath, LegacySubmodelComponentParams.class));
51 * Like {@link #deserialize(ViewModelModifiable, LegacySubmodelComponentParams, String, JsonElement)}, but first reading the
52 * {@link LegacySubmodelComponentParams} from the given file path.
54 * @author Daniel Kirschten
56 public static SubmodelComponent deserialize(ViewModelModifiable model, String sourcePath, String idForSerializingOverride,
57 JsonElement paramsForSerializingOverride) throws IOException
59 return deserialize(model, JsonHandler.readJson(sourcePath, LegacySubmodelComponentParams.class), idForSerializingOverride,
60 paramsForSerializingOverride);
64 * Like {@link #deserialize(ViewModelModifiable, LegacySubmodelComponentParams, String)}, but first reading the
65 * {@link LegacySubmodelComponentParams} from the given file path.
67 * @author Daniel Kirschten
69 public static SubmodelComponent deserialize(ViewModelModifiable model, String sourcePath, String name) throws IOException
71 return deserialize(model, JsonHandler.readJson(sourcePath, LegacySubmodelComponentParams.class), name);
75 * Like {@link #deserialize(ViewModelModifiable, LegacySubmodelComponentParams, String, String, JsonElement)}, but first reading the
76 * {@link LegacySubmodelComponentParams} from the given file path.
78 * @author Daniel Kirschten
80 public static SubmodelComponent deserialize(ViewModelModifiable model, String sourcePath, String name, String idForSerializingOverride,
81 JsonElement paramsForSerializingOverride) throws IOException
83 return deserialize(model, JsonHandler.readJson(sourcePath, LegacySubmodelComponentParams.class), name, idForSerializingOverride,
84 paramsForSerializingOverride);
88 * {@link #deserialize(ViewModelModifiable, LegacySubmodelComponentParams, String, String, JsonElement)} with no
89 * <code>idForSerializingOverride</code> set and using the default name.
91 * @author Daniel Kirschten
93 public static SubmodelComponent deserialize(ViewModelModifiable model, LegacySubmodelComponentParams params)
95 return deserialize(model, params, null, null, null);
99 * {@link #deserialize(ViewModelModifiable, LegacySubmodelComponentParams, String, String, JsonElement)} using the default name.
101 * @author Daniel Kirschten
103 public static SubmodelComponent deserialize(ViewModelModifiable model, LegacySubmodelComponentParams params,
104 String idForSerializingOverride, JsonElement paramsForSerializingOverride)
106 return deserialize(model, params, null, idForSerializingOverride, paramsForSerializingOverride);
110 * {@link #deserialize(ViewModelModifiable, LegacySubmodelComponentParams, String, String, JsonElement)} with no
111 * <code>idForSerializingOverride</code> set.
113 * @author Daniel Kirschten
115 public static SubmodelComponent deserialize(ViewModelModifiable model, LegacySubmodelComponentParams params, String name)
117 return deserialize(model, params, name, null, null);
121 * Like {@link #serialize(SubmodelComponent)}, but instead of returning the generated {@link LegacySubmodelComponentParams} they are
122 * written to a file at the given path.
124 * @author Daniel Kirschten
126 public static void serialize(SubmodelComponent comp, String targetPath) throws IOException
128 JsonHandler.writeJson(serialize(comp), targetPath);
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.
135 * @author Daniel Kirschten
137 public static void serialize(SubmodelComponent comp, IdentifyParams idParams, String targetPath) throws IOException
139 JsonHandler.writeJson(serialize(comp, idParams), targetPath);
143 * {@link #serialize(SubmodelComponent, Function)} using a default {@link IdentifierGetter} (see <code>IdentifierGetter</code>'s
144 * {@link IdentifierGetter#IdentifierGetter() default constructor})
146 * @author Daniel Kirschten
148 public static LegacySubmodelComponentParams serialize(SubmodelComponent comp)
150 return serialize(comp, new IdentifyParams());
155 * Creates a {@link SubmodelComponent} from the specified {@link LegacySubmodelComponentParams} with the given name.
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.
163 * @author Fabian Stemmler
164 * @author Daniel Kirschten
166 @SuppressWarnings("unused") // for GUIWire being created
167 public static SubmodelComponent deserialize(ViewModelModifiable model, LegacySubmodelComponentParams params, String name,
168 String idForSerializingOverride, JsonElement paramsForSerializingOverride)
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(comp, iPinParams.name, iPinParams.logicWidth, PinUsage.TRISTATE, iPinParams.location.x,
177 iPinParams.location.y));
178 LegacySubmodelParameters submodelParams = params.submodel;
179 ViewModelModifiable submodelModifiable = comp.getSubmodelModifiable();
180 Map<String, GUIComponent> componentsByName = submodelModifiable.getComponentsByName();
181 GUIComponent[] components = new GUIComponent[submodelParams.subComps.length];
182 for (int i = 0; i < components.length; i++)
184 LegacyInnerComponentParams cParams = submodelParams.subComps[i];
185 components[i] = IndirectGUIComponentCreator.createComponent(submodelModifiable, cParams.id, cParams.params, cParams.name);
186 components[i].moveTo(cParams.pos.x, cParams.pos.y);
189 for (int i = 0; i < submodelParams.innerWires.length; i++)
191 LegacyInnerWireParams innerWire = submodelParams.innerWires[i];
192 new GUIWire(submodelModifiable, innerWire.name, componentsByName.get(innerWire.pin1.compName).getPin(innerWire.pin1.pinName),
193 componentsByName.get(innerWire.pin2.compName).getPin(innerWire.pin2.pinName), innerWire.path);
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));
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(ViewModelModifiable, 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 GUIComponent#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()}).
218 * @author Fabian Stemmler
219 * @author Daniel Kirschten
221 public static LegacySubmodelComponentParams serialize(SubmodelComponent comp, IdentifyParams idParams)
223 LegacySubmodelParameters submodelParams = new LegacySubmodelParameters();
224 submodelParams.innerScale = comp.getSubmodelScale();
226 Map<String, GUIComponent> components = new HashMap<>(comp.submodel.getComponentsByName());
227 components.remove(SubmodelComponent.SUBMODEL_INTERFACE_NAME);
228 LegacyInnerComponentParams[] componentParams = new LegacyInnerComponentParams[components.size()];
230 for (GUIComponent innerComponent : components.values())
232 LegacyInnerComponentParams innerComponentParams = new LegacyInnerComponentParams();
233 componentParams[i1] = innerComponentParams;
234 innerComponentParams.pos = new Point(innerComponent.getPosX(), innerComponent.getPosY());
235 DeserializedSubmodelComponent innerCompCasted;
236 if (innerComponent instanceof DeserializedSubmodelComponent
237 && (innerCompCasted = (DeserializedSubmodelComponent) innerComponent).idForSerializingOverride != null)
239 innerComponentParams.id = innerCompCasted.idForSerializingOverride;
240 innerComponentParams.params = innerCompCasted.paramsForSerializingOverride;
243 innerComponentParams.id = innerComponent.getIDForSerializing(idParams);
244 innerComponentParams.params = innerComponent.getParamsForSerializingJSON(idParams);
246 innerComponentParams.name = innerComponent.name;
249 submodelParams.subComps = componentParams;
251 Collection<GUIWire> wires = comp.submodel.getWiresByName().values();
252 LegacyInnerWireParams wireParams[] = new LegacyInnerWireParams[wires.size()];
254 for (GUIWire innerWire : wires)
256 LegacyInnerWireParams innerWireParams = new LegacyInnerWireParams();
257 wireParams[i1] = innerWireParams;
258 LegacyInnerPinParams pin1Params = new LegacyInnerPinParams(), pin2Params = new LegacyInnerPinParams();
260 pin1Params.pinName = innerWire.getPin1().name;
261 pin1Params.compName = innerWire.getPin1().component.name;
262 pin2Params.pinName = innerWire.getPin2().name;
263 pin2Params.compName = innerWire.getPin2().component.name;
264 innerWireParams.name = innerWire.name;
265 innerWireParams.pin1 = pin1Params;
266 innerWireParams.pin2 = pin2Params;
267 innerWireParams.path = innerWire.getPath();
270 submodelParams.innerWires = wireParams;
272 LegacySubmodelComponentParams params = new LegacySubmodelComponentParams();
273 params.submodel = submodelParams;
275 params.width = comp.getWidth();
276 params.height = comp.getHeight();
278 LegacyInterfacePinParams[] iPins = new LegacyInterfacePinParams[comp.getPins().size()];
280 for (Pin p : comp.getPins().values())
282 LegacyInterfacePinParams iPinParams = new LegacyInterfacePinParams();
283 iPins[i] = iPinParams;
284 iPinParams.location = p.getRelPos();
285 iPinParams.name = p.name;
286 iPinParams.logicWidth = p.logicWidth;
289 params.interfacePins = iPins;
291 Renderer symbolRenderer = comp.getSymbolRenderer();
292 if (symbolRenderer != null)
294 params.symbolRendererSnippetID = symbolRenderer.getIDForSerializing(idParams);
295 params.symbolRendererParams = symbolRenderer.getParamsForSerializingJSON(idParams);
298 Renderer outlineRenderer = comp.getOutlineRenderer();
299 if (outlineRenderer != null)
301 params.outlineRendererSnippetID = outlineRenderer.getIDForSerializing(idParams);
302 params.outlineRendererParams = outlineRenderer.getParamsForSerializingJSON(idParams);
305 HighLevelStateHandler highLevelStateHandler = comp.getHighLevelStateHandler();
306 if (highLevelStateHandler != null)
308 params.highLevelStateHandlerSnippetID = highLevelStateHandler.getIDForSerializing(idParams);
309 params.highLevelStateHandlerParams = highLevelStateHandler.getParamsForSerializingJSON(idParams);