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 net.haspamelodica.swt.helper.swtobjectwrappers.Point;
13 import net.mograsim.logic.model.model.LogicModel;
14 import net.mograsim.logic.model.model.LogicModelModifiable;
15 import net.mograsim.logic.model.model.components.ModelComponent;
16 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
17 import net.mograsim.logic.model.model.wires.ModelWire;
18 import net.mograsim.logic.model.serializing.LogicModelParams.ComponentParams;
19 import net.mograsim.logic.model.serializing.LogicModelParams.WireParams;
20 import net.mograsim.logic.model.serializing.LogicModelParams.WireParams.PinParams;
21 import net.mograsim.logic.model.util.JsonHandler;
22 import net.mograsim.logic.model.util.Version;
24 public class LogicModelSerializer
26 public static final Version CURRENT_JSON_VERSION = Version.parseSemver("0.1.1");
28 // convenience methods
30 * Like {@link #deserialize(LogicModelParams)}, but first reading the {@link LogicModelParams} from the given file path.
32 * @author Daniel Kirschten
34 public static LogicModelModifiable deserialize(String sourcePath) throws IOException
36 return deserialize(JsonHandler.readJson(sourcePath, LogicModelParams.class));
40 * Like {@link #deserialize(LogicModelModifiable, LogicModelParams)}, but first reading the {@link LogicModelParams} from the given file
43 * @author Daniel Kirschten
45 public static void deserialize(LogicModelModifiable model, String sourcePath) throws IOException
47 deserialize(model, JsonHandler.readJson(sourcePath, LogicModelParams.class));
51 * Like {@link #deserialize(LogicModelModifiable, LogicModelParams)}, but using a newly created {@link LogicModelModifiable}.
53 * @author Daniel Kirschten
55 public static LogicModelModifiable deserialize(LogicModelParams params)
57 LogicModelModifiable model = new LogicModelModifiable();
58 deserialize(model, params);
63 * Like {@link #serialize(LogicModel)}, but instead of returning the generated {@link LogicModelParams} they are written to a file at
66 * @author Daniel Kirschten
68 public static void serialize(LogicModel model, String targetPath) throws IOException
70 JsonHandler.writeJson(serialize(model), targetPath);
74 * Like {@link #serialize(LogicModel, IdentifyParams)}, but instead of returning the generated {@link LogicModelParams} they are written
75 * to a file at the given path.
77 * @author Daniel Kirschten
79 public static void serialize(LogicModel model, IdentifyParams idParams, String targetPath) throws IOException
81 JsonHandler.writeJson(serialize(model, idParams), targetPath);
85 * {@link #serialize(LogicModel, IdentifyParams)} using the default {@link IdentifyParams} (see <code>IdentifyParams</code>'s
86 * {@link IdentifyParams#IdentifyParams() default constructor})
88 * @author Daniel Kirschten
90 public static LogicModelParams serialize(LogicModel model)
92 return serialize(model, new IdentifyParams());
97 * Deserializes components and wires from the specified {@link LogicModelParams} and adds them to the given
98 * {@link LogicModelModifiable}.
100 * @author Fabian Stemmler
101 * @author Daniel Kirschten
103 @SuppressWarnings("unused") // for ModelWire being created
104 public static void deserialize(LogicModelModifiable model, LogicModelParams params)
106 Map<String, ModelComponent> componentsByName = model.getComponentsByName();
107 ModelComponent[] components = new ModelComponent[params.components.length];
108 for (int i = 0; i < components.length; i++)
110 ComponentParams compParams = params.components[i];
111 components[i] = IndirectModelComponentCreator.createComponent(model, compParams.id, compParams.params, compParams.name);
112 components[i].moveTo(compParams.pos.x, compParams.pos.y);
115 for (int i = 0; i < params.wires.length; i++)
117 WireParams wire = params.wires[i];
118 new ModelWire(model, wire.name, componentsByName.get(wire.pin1.compName).getPin(wire.pin1.pinName),
119 componentsByName.get(wire.pin2.compName).getPin(wire.pin2.pinName), wire.path);
124 * Returns {@link LogicModelModifiable}, which describe the components and wires in the given {@link LogicModel}. <br>
125 * Components are serialized using {@link ModelComponent#getIDForSerializing(IdentifyParams)} and
126 * {@link ModelComponent#getParamsForSerializingJSON(IdentifyParams)} <br>
128 * @author Fabian Stemmler
129 * @author Daniel Kirschten
131 public static LogicModelParams serialize(LogicModel model, IdentifyParams idParams)
133 LogicModelParams modelParams = new LogicModelParams(CURRENT_JSON_VERSION);
135 Map<String, ModelComponent> components = new HashMap<>(model.getComponentsByName());
136 components.remove(SubmodelComponent.SUBMODEL_INTERFACE_NAME);
137 Set<ComponentParams> componentsParams = new HashSet<>();
138 for (ModelComponent component : components.values())
140 ComponentParams compParams = new ComponentParams();
141 componentsParams.add(compParams);
142 compParams.pos = new Point(component.getPosX(), component.getPosY());
143 compParams.id = component.getIDForSerializing(idParams);
144 compParams.params = component.getParamsForSerializingJSON(idParams);
145 compParams.name = component.getName();
147 modelParams.components = componentsParams.toArray(ComponentParams[]::new);
148 Arrays.sort(modelParams.components, Comparator.comparing(c -> c.name));
150 Collection<ModelWire> wires = model.getWiresByName().values();
151 Set<WireParams> wiresParams = new HashSet<>();
152 for (ModelWire innerWire : wires)
154 WireParams innerWireParams = new WireParams();
155 wiresParams.add(innerWireParams);
156 PinParams pin1Params = new PinParams(), pin2Params = new PinParams();
158 pin1Params.pinName = innerWire.getPin1().name;
159 pin1Params.compName = innerWire.getPin1().component.getName();
160 pin2Params.pinName = innerWire.getPin2().name;
161 pin2Params.compName = innerWire.getPin2().component.getName();
162 innerWireParams.name = innerWire.name;
163 innerWireParams.pin1 = pin1Params;
164 innerWireParams.pin2 = pin2Params;
165 innerWireParams.path = innerWire.getPath();
167 modelParams.wires = wiresParams.toArray(WireParams[]::new);
168 Arrays.sort(modelParams.wires, Comparator.comparing(c -> c.name));