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