Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / serializing / LogicModelSerializer.java
1 package net.mograsim.logic.model.serializing;
2
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;
9 import java.util.Map;
10 import java.util.Set;
11
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;
23
24 public class LogicModelSerializer
25 {
26         public static final Version CURRENT_JSON_VERSION = Version.parseSemver("0.1.1");
27
28         // convenience methods
29         /**
30          * Like {@link #deserialize(LogicModelParams)}, but first reading the {@link LogicModelParams} from the given file path.
31          * 
32          * @author Daniel Kirschten
33          */
34         public static LogicModelModifiable deserialize(String sourcePath) throws IOException
35         {
36                 return deserialize(JsonHandler.readJson(sourcePath, LogicModelParams.class));
37         }
38
39         /**
40          * Like {@link #deserialize(LogicModelModifiable, LogicModelParams)}, but first reading the {@link LogicModelParams} from the given file
41          * path.
42          * 
43          * @author Daniel Kirschten
44          */
45         public static void deserialize(LogicModelModifiable model, String sourcePath) throws IOException
46         {
47                 deserialize(model, JsonHandler.readJson(sourcePath, LogicModelParams.class));
48         }
49
50         /**
51          * Like {@link #deserialize(LogicModelModifiable, LogicModelParams)}, but using a newly created {@link LogicModelModifiable}.
52          * 
53          * @author Daniel Kirschten
54          */
55         public static LogicModelModifiable deserialize(LogicModelParams params)
56         {
57                 LogicModelModifiable model = new LogicModelModifiable();
58                 deserialize(model, params);
59                 return model;
60         }
61
62         /**
63          * Like {@link #serialize(LogicModel)}, but instead of returning the generated {@link LogicModelParams} they are written to a file at
64          * the given path.
65          * 
66          * @author Daniel Kirschten
67          */
68         public static void serialize(LogicModel model, String targetPath) throws IOException
69         {
70                 JsonHandler.writeJson(serialize(model), targetPath);
71         }
72
73         /**
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.
76          * 
77          * @author Daniel Kirschten
78          */
79         public static void serialize(LogicModel model, IdentifyParams idParams, String targetPath) throws IOException
80         {
81                 JsonHandler.writeJson(serialize(model, idParams), targetPath);
82         }
83
84         /**
85          * {@link #serialize(LogicModel, IdentifyParams)} using the default {@link IdentifyParams} (see <code>IdentifyParams</code>'s
86          * {@link IdentifyParams#IdentifyParams() default constructor})
87          * 
88          * @author Daniel Kirschten
89          */
90         public static LogicModelParams serialize(LogicModel model)
91         {
92                 return serialize(model, new IdentifyParams());
93         }
94
95         // "core" methods
96         /**
97          * Deserializes components and wires from the specified {@link LogicModelParams} and adds them to the given
98          * {@link LogicModelModifiable}.
99          * 
100          * @author Fabian Stemmler
101          * @author Daniel Kirschten
102          */
103         @SuppressWarnings("unused") // for ModelWire being created
104         public static void deserialize(LogicModelModifiable model, LogicModelParams params)
105         {
106                 Map<String, ModelComponent> componentsByName = model.getComponentsByName();
107                 ModelComponent[] components = new ModelComponent[params.components.length];
108                 for (int i = 0; i < components.length; i++)
109                 {
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);
113                 }
114
115                 for (int i = 0; i < params.wires.length; i++)
116                 {
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);
120                 }
121         }
122
123         /**
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>
127          * 
128          * @author Fabian Stemmler
129          * @author Daniel Kirschten
130          */
131         public static LogicModelParams serialize(LogicModel model, IdentifyParams idParams)
132         {
133                 LogicModelParams modelParams = new LogicModelParams(CURRENT_JSON_VERSION);
134
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())
139                 {
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();
146                 }
147                 modelParams.components = componentsParams.toArray(ComponentParams[]::new);
148                 Arrays.sort(modelParams.components, Comparator.comparing(c -> c.name));
149
150                 Collection<ModelWire> wires = model.getWiresByName().values();
151                 Set<WireParams> wiresParams = new HashSet<>();
152                 for (ModelWire innerWire : wires)
153                 {
154                         WireParams innerWireParams = new WireParams();
155                         wiresParams.add(innerWireParams);
156                         PinParams pin1Params = new PinParams(), pin2Params = new PinParams();
157
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();
166                 }
167                 modelParams.wires = wiresParams.toArray(WireParams[]::new);
168                 Arrays.sort(modelParams.wires, Comparator.comparing(c -> c.name));
169
170                 return modelParams;
171         }
172 }