1 package net.mograsim.logic.model.serializing;
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.Comparator;
7 import com.google.gson.JsonElement;
9 import net.mograsim.logic.model.model.LogicModelModifiable;
10 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
11 import net.mograsim.logic.model.model.wires.MovablePin;
12 import net.mograsim.logic.model.model.wires.Pin;
13 import net.mograsim.logic.model.model.wires.PinUsage;
14 import net.mograsim.logic.model.serializing.SubmodelComponentParams.InterfacePinParams;
15 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
16 import net.mograsim.logic.model.snippets.Renderer;
17 import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
18 import net.mograsim.logic.model.util.JsonHandler;
19 import net.mograsim.logic.model.util.Version;
22 * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams}
24 * @author Fabian Stemmler
25 * @author Daniel Kirschten
27 public final class SubmodelComponentSerializer
29 public static final Version JSON_VERSION_CURRENT_SERIALIZING = Version.parseSemver("0.1.5");
30 public static final Version JSON_VERSION_LATEST_SUPPORTED_DESERIALIZING = Version.parseSemver("0.1.5");
31 public static final Version JSON_VERSION_EARLIEST_WITH_USAGE_SERIALIZED = Version.parseSemver("0.1.5");
32 // convenience methods
35 * Like {@link #deserialize(LogicModelModifiable, SubmodelComponentParams)}, but first reading the {@link SubmodelComponentParams} from
36 * the given file path.
38 * @author Daniel Kirschten
40 public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath) throws IOException
42 return deserialize(model, JsonHandler.readJson(sourcePath, SubmodelComponentParams.class));
46 * Like {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String, JsonElement)}, but first reading the
47 * {@link SubmodelComponentParams} from the given file path.
49 * @author Daniel Kirschten
51 public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath, String idForSerializingOverride,
52 JsonElement paramsForSerializingOverride) throws IOException
54 return deserialize(model, JsonHandler.readJson(sourcePath, SubmodelComponentParams.class), idForSerializingOverride,
55 paramsForSerializingOverride);
59 * Like {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String)}, but first reading the
60 * {@link SubmodelComponentParams} from the given file path.
62 * @author Daniel Kirschten
64 public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath, String name) throws IOException
66 return deserialize(model, JsonHandler.readJson(sourcePath, SubmodelComponentParams.class), name);
70 * Like {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)}, but first reading the
71 * {@link SubmodelComponentParams} from the given file path.
73 * @author Daniel Kirschten
75 public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath, String name, String idForSerializingOverride,
76 JsonElement paramsForSerializingOverride) throws IOException
78 return deserialize(model, JsonHandler.readJson(sourcePath, SubmodelComponentParams.class), name, idForSerializingOverride,
79 paramsForSerializingOverride);
83 * {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)} with no
84 * <code>idForSerializingOverride</code> set and using the default name.
86 * @author Daniel Kirschten
88 public static SubmodelComponent deserialize(LogicModelModifiable model, SubmodelComponentParams params)
90 return deserialize(model, params, null, null, null);
94 * {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)} using the default name.
96 * @author Daniel Kirschten
98 public static SubmodelComponent deserialize(LogicModelModifiable model, SubmodelComponentParams params, String idForSerializingOverride,
99 JsonElement paramsForSerializingOverride)
101 return deserialize(model, params, null, idForSerializingOverride, paramsForSerializingOverride);
105 * {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)} with no
106 * <code>idForSerializingOverride</code> set.
108 * @author Daniel Kirschten
110 public static SubmodelComponent deserialize(LogicModelModifiable model, SubmodelComponentParams params, String name)
112 return deserialize(model, params, name, null, null);
116 * Like {@link #serialize(SubmodelComponent)}, but instead of returning the generated {@link SubmodelComponentParams} they are written
117 * to a file at the given path.
119 * @author Daniel Kirschten
121 public static void serialize(SubmodelComponent comp, String targetPath) throws IOException
123 JsonHandler.writeJson(serialize(comp), targetPath);
127 * Like {@link #serialize(SubmodelComponent, IdentifyParams)}, but instead of returning the generated {@link SubmodelComponentParams}
128 * they are written to a file at the given path.
130 * @author Daniel Kirschten
132 public static void serialize(SubmodelComponent comp, IdentifyParams idParams, String targetPath) throws IOException
134 JsonHandler.writeJson(serialize(comp, idParams), targetPath);
138 * {@link #serialize(SubmodelComponent, IdentifyParams)} using the default {@link IdentifyParams} (see <code>IdentifyParams</code>'s
139 * {@link IdentifyParams#IdentifyParams() default constructor})
141 * @author Daniel Kirschten
143 public static SubmodelComponentParams serialize(SubmodelComponent comp)
145 return serialize(comp, new IdentifyParams());
150 * Creates a {@link SubmodelComponent} from the specified {@link SubmodelComponentParams} with the given name.
152 * When serializing a <code>SubmodelComponent</code>, it is undesired for every subcomponent to be serialized with its complete inner
153 * structure. Instead, these sub-<code>SubmodelComponent</code>s should be serialized with the ID and params which were used to
154 * determine the <code>SubmodelComponentParams</code> defining the sub-<code>SubmodelComponent</code>. Because of this, it is possible
155 * to override the ID and params used in {@link #serialize(SubmodelComponent, IdentifyParams) serialize(...)} to describe this
156 * subcomponent. See there for details.
158 * @author Fabian Stemmler
159 * @author Daniel Kirschten
161 @SuppressWarnings("unused") // for ModelWire being created
162 public static SubmodelComponent deserialize(LogicModelModifiable model, SubmodelComponentParams params, String name,
163 String idForSerializingOverride, JsonElement paramsForSerializingOverride)
165 Version version = params.version;
166 if (version.compareTo(JSON_VERSION_LATEST_SUPPORTED_DESERIALIZING) > 0)
167 throw new IllegalArgumentException("JSON version " + version + " not supported yet");
168 boolean hasUsageSerialized = version.compareTo(JSON_VERSION_EARLIEST_WITH_USAGE_SERIALIZED) >= 0;
169 DeserializedSubmodelComponent comp = new DeserializedSubmodelComponent(model, name, idForSerializingOverride,
170 paramsForSerializingOverride);
171 comp.setSubmodelScale(params.innerScale);
172 comp.setSize(params.width, params.height);
173 for (InterfacePinParams iPinParams : params.interfacePins)
174 // TRISTATE because we don't have a better choice
175 comp.addSubmodelInterface(new MovablePin(model, comp, iPinParams.name, iPinParams.logicWidth,
176 hasUsageSerialized ? iPinParams.usage : PinUsage.TRISTATE, iPinParams.location.x, iPinParams.location.y));
177 LogicModelModifiable submodelModifiable = comp.getSubmodelModifiable();
178 LogicModelSerializer.deserialize(comp.getSubmodelModifiable(), params.submodel);
179 comp.setSymbolRenderer(SubmodelComponentSnippetSuppliers.symbolRendererSupplier.getSnippetSupplier(params.symbolRendererSnippetID)
180 .create(comp, params.symbolRendererParams));
181 comp.setOutlineRenderer(SubmodelComponentSnippetSuppliers.outlineRendererSupplier
182 .getSnippetSupplier(params.outlineRendererSnippetID).create(comp, params.outlineRendererParams));
183 comp.setHighLevelStateHandler(SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier
184 .getSnippetSupplier(params.highLevelStateHandlerSnippetID).create(comp, params.highLevelStateHandlerParams));
189 * Returns {@link SubmodelComponentParams}, which describe this {@link SubmodelComponent}. <br>
190 * See {@link LogicModelSerializer#serialize(net.mograsim.logic.model.model.LogicModel, IdentifierGetter)
191 * LogicModelSerializer.serialize(...)} for how subcomponents are serialized.<br>
192 * CodeSnippets are serialized using the ID defined by <code>idGetter</code> and the params obtained by the respective
193 * <coce>getParamsForSerializing</code> methods ({@link Renderer#getParamsForSerializing()}).
195 * @author Fabian Stemmler
196 * @author Daniel Kirschten
198 public static SubmodelComponentParams serialize(SubmodelComponent comp, IdentifyParams idParams)
200 SubmodelComponentParams params = new SubmodelComponentParams(JSON_VERSION_CURRENT_SERIALIZING);
201 params.innerScale = comp.getSubmodelScale();
202 params.submodel = LogicModelSerializer.serialize(comp.submodel, idParams);
204 params.width = comp.getWidth();
205 params.height = comp.getHeight();
207 InterfacePinParams[] iPins = new InterfacePinParams[comp.getPins().size()];
209 for (Pin p : comp.getPins().values())
211 InterfacePinParams iPinParams = new InterfacePinParams();
212 iPins[i] = iPinParams;
213 iPinParams.location = p.getRelPos();
214 iPinParams.name = p.name;
215 iPinParams.logicWidth = p.logicWidth;
216 iPinParams.usage = p.usage;
219 params.interfacePins = iPins;
220 Arrays.sort(params.interfacePins, Comparator.comparing(p -> p.name));
222 Renderer symbolRenderer = comp.getSymbolRenderer();
223 if (symbolRenderer != null)
225 params.symbolRendererSnippetID = symbolRenderer.getIDForSerializing(idParams);
226 params.symbolRendererParams = symbolRenderer.getParamsForSerializingJSON(idParams);
229 Renderer outlineRenderer = comp.getOutlineRenderer();
230 if (outlineRenderer != null)
232 params.outlineRendererSnippetID = outlineRenderer.getIDForSerializing(idParams);
233 params.outlineRendererParams = outlineRenderer.getParamsForSerializingJSON(idParams);
236 HighLevelStateHandler highLevelStateHandler = comp.getHighLevelStateHandler();
237 if (highLevelStateHandler != null)
239 params.highLevelStateHandlerSnippetID = highLevelStateHandler.getIDForSerializing(idParams);
240 params.highLevelStateHandlerParams = highLevelStateHandler.getParamsForSerializingJSON(idParams);