1 package net.mograsim.logic.model.serializing;
3 import java.io.IOException;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.HashSet;
10 import com.google.gson.JsonElement;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
13 import net.mograsim.logic.model.model.ViewModel;
14 import net.mograsim.logic.model.model.ViewModelModifiable;
15 import net.mograsim.logic.model.model.components.GUIComponent;
16 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
17 import net.mograsim.logic.model.model.wires.GUIWire;
18 import net.mograsim.logic.model.serializing.ViewModelParams.ComponentParams;
19 import net.mograsim.logic.model.serializing.ViewModelParams.WireParams;
20 import net.mograsim.logic.model.serializing.ViewModelParams.WireParams.PinParams;
21 import net.mograsim.logic.model.util.JsonHandler;
22 import net.mograsim.logic.model.util.Version;
24 public class ViewModelSerializer
26 public static final Version CURRENT_JSON_VERSION = Version.parseSemver("0.1.1");
28 // convenience methods
30 * Like {@link #deserialize(ViewModelParams)}, but first reading the {@link ViewModelParams} from the given file path.
32 * @author Daniel Kirschten
34 public static ViewModelModifiable deserialize(String sourcePath) throws IOException
36 return deserialize(JsonHandler.readJson(sourcePath, ViewModelParams.class));
40 * Like {@link #deserialize(ViewModelModifiable, ViewModelParams)}, but first reading the {@link ViewModelParams} from the given file
43 * @author Daniel Kirschten
45 public static void deserialize(ViewModelModifiable model, String sourcePath) throws IOException
47 deserialize(model, JsonHandler.readJson(sourcePath, ViewModelParams.class));
51 * Like {@link #deserialize(ViewModelModifiable, ViewModelParams)}, but using a newly created {@link ViewModelModifiable}.
53 * @author Daniel Kirschten
55 public static ViewModelModifiable deserialize(ViewModelParams params)
57 ViewModelModifiable model = new ViewModelModifiable();
58 deserialize(model, params);
63 * Like {@link #serialize(ViewModel)}, but instead of returning the generated {@link ViewModelParams} they are written to a file at the
66 * @author Daniel Kirschten
68 public static void serialize(ViewModel model, String targetPath) throws IOException
70 JsonHandler.writeJson(serialize(model), targetPath);
74 * Like {@link #serialize(ViewModel, IdentifierGetter)}, but instead of returning the generated {@link ViewModelParams} they are written
75 * to a file at the given path.
77 * @author Daniel Kirschten
79 public static void serialize(ViewModel model, IdentifierGetter idGetter, String targetPath) throws IOException
81 JsonHandler.writeJson(serialize(model, idGetter), targetPath);
85 * {@link #serialize(ViewModel, IdentifierGetter)} using a default {@link IdentifierGetter} (see <code>IdentifierGetter</code>'s
86 * {@link IdentifierGetter#IdentifierGetter() default constructor})
88 * @author Daniel Kirschten
90 public static ViewModelParams serialize(ViewModel model)
92 return serialize(model, new IdentifierGetter());
97 * Deserializes components and wires from the specified {@link ViewModelParams} and adds them to the given {@link ViewModelModifiable}.
99 * @author Fabian Stemmler
100 * @author Daniel Kirschten
102 @SuppressWarnings("unused") // for GUIWire being created
103 public static void deserialize(ViewModelModifiable model, ViewModelParams params)
105 Map<String, GUIComponent> componentsByName = model.getComponentsByName();
106 GUIComponent[] components = new GUIComponent[params.components.length];
107 for (int i = 0; i < components.length; i++)
109 ComponentParams compParams = params.components[i];
110 components[i] = IndirectGUIComponentCreator.createComponent(model, compParams.id, compParams.params, compParams.name);
111 components[i].moveTo(compParams.pos.x, compParams.pos.y);
114 for (int i = 0; i < params.wires.length; i++)
116 WireParams wire = params.wires[i];
117 new GUIWire(model, wire.name, componentsByName.get(wire.pin1.compName).getPin(wire.pin1.pinName),
118 componentsByName.get(wire.pin2.compName).getPin(wire.pin2.pinName), wire.path);
123 * Returns {@link ViewModelModifiable}, which describe the components and wires in the given {@link ViewModel}. <br>
124 * Components are serialized in the following way: <br>
125 * If a component is a <code>SubmodelComponent</code> which has been deserialized, and it has an
126 * {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride} set (e.g. non-null; see
127 * {@link SubmodelComponentSerializer#deserialize(ViewModelModifiable, SubmodelComponentParams, String, String, JsonElement)
128 * SubmodelComponentSerializer.deserialize(...)}), this ID and the component's
129 * {@link DeserializedSubmodelComponent#paramsForSerializingOverride paramsForSerializingOverride} are written.<br>
130 * If this case doesn't apply (e.g. if the component is not a <code>SubmodelComponent</code>; or it is a <code>SubmodelComponent</code>,
131 * but hasn't been deserialized; or it has no {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride}
132 * set), the ID defined by <code>idGetter</code> and the params obtained by {@link GUIComponent#getParamsForSerializing() getParams()}
135 * @author Fabian Stemmler
136 * @author Daniel Kirschten
138 public static ViewModelParams serialize(ViewModel model, IdentifierGetter idGetter)
140 ViewModelParams modelParams = new ViewModelParams(CURRENT_JSON_VERSION);
142 Map<String, GUIComponent> components = new HashMap<>(model.getComponentsByName());
143 components.remove(SubmodelComponent.SUBMODEL_INTERFACE_NAME);
144 Set<ComponentParams> componentsParams = new HashSet<>();
145 for (GUIComponent component : components.values())
147 ComponentParams compParams = new ComponentParams();
148 componentsParams.add(compParams);
149 compParams.pos = new Point(component.getPosX(), component.getPosY());
150 DeserializedSubmodelComponent innerCompCasted;
151 if (component instanceof DeserializedSubmodelComponent
152 && (innerCompCasted = (DeserializedSubmodelComponent) component).idForSerializingOverride != null)
154 compParams.id = innerCompCasted.idForSerializingOverride;
155 compParams.params = innerCompCasted.paramsForSerializingOverride;
158 compParams.id = idGetter.componentIDs.apply(component);
159 compParams.params = component.getParamsForSerializing(idGetter);
161 compParams.name = component.name;
163 modelParams.components = componentsParams.toArray(ComponentParams[]::new);
165 Collection<GUIWire> wires = model.getWiresByName().values();
166 Set<WireParams> wiresParams = new HashSet<>();
167 for (GUIWire innerWire : wires)
169 WireParams innerWireParams = new WireParams();
170 wiresParams.add(innerWireParams);
171 PinParams pin1Params = new PinParams(), pin2Params = new PinParams();
173 pin1Params.pinName = innerWire.getPin1().name;
174 pin1Params.compName = innerWire.getPin1().component.name;
175 pin2Params.pinName = innerWire.getPin2().name;
176 pin2Params.compName = innerWire.getPin2().component.name;
177 innerWireParams.name = innerWire.name;
178 innerWireParams.pin1 = pin1Params;
179 innerWireParams.pin2 = pin2Params;
180 innerWireParams.path = innerWire.getPath();
182 modelParams.wires = wiresParams.toArray(WireParams[]::new);