cf139885360b3445394b71e822fdf912583f0ed6
[Mograsim.git] /
1 package net.mograsim.logic.model.serializing;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.Comparator;
6
7 import com.google.gson.JsonElement;
8
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;
20
21 /**
22  * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams}
23  * 
24  * @author Fabian Stemmler
25  * @author Daniel Kirschten
26  */
27 public final class SubmodelComponentSerializer
28 {
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
33
34         /**
35          * Like {@link #deserialize(LogicModelModifiable, SubmodelComponentParams)}, but first reading the {@link SubmodelComponentParams} from
36          * the given file path.
37          * 
38          * @author Daniel Kirschten
39          */
40         public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath) throws IOException
41         {
42                 return deserialize(model, JsonHandler.readJson(sourcePath, SubmodelComponentParams.class));
43         }
44
45         /**
46          * Like {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String, JsonElement)}, but first reading the
47          * {@link SubmodelComponentParams} from the given file path.
48          * 
49          * @author Daniel Kirschten
50          */
51         public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath, String idForSerializingOverride,
52                         JsonElement paramsForSerializingOverride) throws IOException
53         {
54                 return deserialize(model, JsonHandler.readJson(sourcePath, SubmodelComponentParams.class), idForSerializingOverride,
55                                 paramsForSerializingOverride);
56         }
57
58         /**
59          * Like {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String)}, but first reading the
60          * {@link SubmodelComponentParams} from the given file path.
61          * 
62          * @author Daniel Kirschten
63          */
64         public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath, String name) throws IOException
65         {
66                 return deserialize(model, JsonHandler.readJson(sourcePath, SubmodelComponentParams.class), name);
67         }
68
69         /**
70          * Like {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)}, but first reading the
71          * {@link SubmodelComponentParams} from the given file path.
72          * 
73          * @author Daniel Kirschten
74          */
75         public static SubmodelComponent deserialize(LogicModelModifiable model, String sourcePath, String name, String idForSerializingOverride,
76                         JsonElement paramsForSerializingOverride) throws IOException
77         {
78                 return deserialize(model, JsonHandler.readJson(sourcePath, SubmodelComponentParams.class), name, idForSerializingOverride,
79                                 paramsForSerializingOverride);
80         }
81
82         /**
83          * {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)} with no
84          * <code>idForSerializingOverride</code> set and using the default name.
85          * 
86          * @author Daniel Kirschten
87          */
88         public static SubmodelComponent deserialize(LogicModelModifiable model, SubmodelComponentParams params)
89         {
90                 return deserialize(model, params, null, null, null);
91         }
92
93         /**
94          * {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)} using the default name.
95          * 
96          * @author Daniel Kirschten
97          */
98         public static SubmodelComponent deserialize(LogicModelModifiable model, SubmodelComponentParams params, String idForSerializingOverride,
99                         JsonElement paramsForSerializingOverride)
100         {
101                 return deserialize(model, params, null, idForSerializingOverride, paramsForSerializingOverride);
102         }
103
104         /**
105          * {@link #deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)} with no
106          * <code>idForSerializingOverride</code> set.
107          * 
108          * @author Daniel Kirschten
109          */
110         public static SubmodelComponent deserialize(LogicModelModifiable model, SubmodelComponentParams params, String name)
111         {
112                 return deserialize(model, params, name, null, null);
113         }
114
115         /**
116          * Like {@link #serialize(SubmodelComponent)}, but instead of returning the generated {@link SubmodelComponentParams} they are written
117          * to a file at the given path.
118          * 
119          * @author Daniel Kirschten
120          */
121         public static void serialize(SubmodelComponent comp, String targetPath) throws IOException
122         {
123                 JsonHandler.writeJson(serialize(comp), targetPath);
124         }
125
126         /**
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.
129          * 
130          * @author Daniel Kirschten
131          */
132         public static void serialize(SubmodelComponent comp, IdentifyParams idParams, String targetPath) throws IOException
133         {
134                 JsonHandler.writeJson(serialize(comp, idParams), targetPath);
135         }
136
137         /**
138          * {@link #serialize(SubmodelComponent, IdentifyParams)} using the default {@link IdentifyParams} (see <code>IdentifyParams</code>'s
139          * {@link IdentifyParams#IdentifyParams() default constructor})
140          * 
141          * @author Daniel Kirschten
142          */
143         public static SubmodelComponentParams serialize(SubmodelComponent comp)
144         {
145                 return serialize(comp, new IdentifyParams());
146         }
147
148         // "core" methods
149         /**
150          * Creates a {@link SubmodelComponent} from the specified {@link SubmodelComponentParams} with the given name.
151          * <p>
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.
157          * 
158          * @author Fabian Stemmler
159          * @author Daniel Kirschten
160          */
161         @SuppressWarnings("unused") // for ModelWire being created
162         public static SubmodelComponent deserialize(LogicModelModifiable model, SubmodelComponentParams params, String name,
163                         String idForSerializingOverride, JsonElement paramsForSerializingOverride)
164         {
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));
185                 return comp;
186         }
187
188         /**
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()}).
194          * 
195          * @author Fabian Stemmler
196          * @author Daniel Kirschten
197          */
198         public static SubmodelComponentParams serialize(SubmodelComponent comp, IdentifyParams idParams)
199         {
200                 SubmodelComponentParams params = new SubmodelComponentParams(JSON_VERSION_CURRENT_SERIALIZING);
201                 params.innerScale = comp.getSubmodelScale();
202                 params.submodel = LogicModelSerializer.serialize(comp.submodel, idParams);
203
204                 params.width = comp.getWidth();
205                 params.height = comp.getHeight();
206
207                 InterfacePinParams[] iPins = new InterfacePinParams[comp.getPins().size()];
208                 int i = 0;
209                 for (Pin p : comp.getPins().values())
210                 {
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;
217                         i++;
218                 }
219                 params.interfacePins = iPins;
220                 Arrays.sort(params.interfacePins, Comparator.comparing(p -> p.name));
221
222                 Renderer symbolRenderer = comp.getSymbolRenderer();
223                 if (symbolRenderer != null)
224                 {
225                         params.symbolRendererSnippetID = symbolRenderer.getIDForSerializing(idParams);
226                         params.symbolRendererParams = symbolRenderer.getParamsForSerializingJSON(idParams);
227                 }
228
229                 Renderer outlineRenderer = comp.getOutlineRenderer();
230                 if (outlineRenderer != null)
231                 {
232                         params.outlineRendererSnippetID = outlineRenderer.getIDForSerializing(idParams);
233                         params.outlineRendererParams = outlineRenderer.getParamsForSerializingJSON(idParams);
234                 }
235
236                 HighLevelStateHandler highLevelStateHandler = comp.getHighLevelStateHandler();
237                 if (highLevelStateHandler != null)
238                 {
239                         params.highLevelStateHandlerSnippetID = highLevelStateHandler.getIDForSerializing(idParams);
240                         params.highLevelStateHandlerParams = highLevelStateHandler.getParamsForSerializingJSON(idParams);
241                 }
242
243                 return params;
244         }
245 }