X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fserializing%2FSubmodelComponentSerializer.java;h=a062eaaaba0268dc0ee7bf3090e795e139770db7;hb=8e8d5776ccc95a08885789834abd55cc50e18a24;hp=7c002a091b9abee7777b46e28d2fc8223090b95a;hpb=a3f31d6bf39eb747172a6db329de72c803903c2e;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/SubmodelComponentSerializer.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/SubmodelComponentSerializer.java index 7c002a09..a062eaaa 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/SubmodelComponentSerializer.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/SubmodelComponentSerializer.java @@ -1,30 +1,22 @@ package net.mograsim.logic.model.serializing; import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; +import java.util.Arrays; +import java.util.Comparator; -import com.google.gson.Gson; import com.google.gson.JsonElement; -import net.haspamelodica.swt.helper.swtobjectwrappers.Point; import net.mograsim.logic.model.model.ViewModelModifiable; -import net.mograsim.logic.model.model.components.GUIComponent; -import net.mograsim.logic.model.model.components.submodels.SimpleRectangularSubmodelComponent; import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; -import net.mograsim.logic.model.model.wires.GUIWire; import net.mograsim.logic.model.model.wires.MovablePin; import net.mograsim.logic.model.model.wires.Pin; +import net.mograsim.logic.model.model.wires.PinUsage; import net.mograsim.logic.model.serializing.SubmodelComponentParams.InterfacePinParams; -import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters; -import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters.InnerComponentParams; -import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters.InnerWireParams; -import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters.InnerWireParams.InnerPinParams; +import net.mograsim.logic.model.snippets.HighLevelStateHandler; +import net.mograsim.logic.model.snippets.Renderer; import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers; -import net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer.SimpleRectangularLikeParams; import net.mograsim.logic.model.util.JsonHandler; +import net.mograsim.logic.model.util.Version; /** * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams} @@ -34,6 +26,9 @@ import net.mograsim.logic.model.util.JsonHandler; */ public final class SubmodelComponentSerializer { + public static final Version JSON_VERSION_CURRENT_SERIALIZING = Version.parseSemver("0.1.5"); + public static final Version JSON_VERSION_LATEST_SUPPORTED_DESERIALIZING = Version.parseSemver("0.1.5"); + public static final Version JSON_VERSION_EARLIEST_WITH_USAGE_SERIALIZED = Version.parseSemver("0.1.5"); // convenience methods /** @@ -129,25 +124,25 @@ public final class SubmodelComponentSerializer } /** - * Like {@link #serialize(SubmodelComponent, Function)}, but instead of returning the generated {@link SubmodelComponentParams} they are - * written to a file at the given path. + * Like {@link #serialize(SubmodelComponent, IdentifierGetter)}, but instead of returning the generated {@link SubmodelComponentParams} + * they are written to a file at the given path. * * @author Daniel Kirschten */ - public static void serialize(SubmodelComponent comp, Function getIdentifier, String targetPath) throws IOException + public static void serialize(SubmodelComponent comp, IdentifierGetter idGetter, String targetPath) throws IOException { - JsonHandler.writeJson(serialize(comp, getIdentifier), targetPath); + JsonHandler.writeJson(serialize(comp, idGetter), targetPath); } /** - * {@link #serialize(SubmodelComponent, Function)} using "class:" concatenated with a component's complete (canonical) - * class name for the ID of a component. + * {@link #serialize(SubmodelComponent, IdentifierGetter)} using a default {@link IdentifierGetter} (see IdentifierGetter's + * {@link IdentifierGetter#IdentifierGetter() default constructor}) * * @author Daniel Kirschten */ public static SubmodelComponentParams serialize(SubmodelComponent comp) { - return serialize(comp, c -> "class:" + c.getClass().getCanonicalName()); + return serialize(comp, new IdentifierGetter()); } // "core" methods @@ -157,8 +152,8 @@ public final class SubmodelComponentSerializer * When serializing a SubmodelComponent, it is undesired for every subcomponent to be serialized with its complete inner * structure. Instead, these sub-SubmodelComponents should be serialized with the ID and params which were used to * determine the SubmodelComponentParams defining the sub-SubmodelComponent. Because of this, it is possible - * to override the ID and params used in {@link #serialize(SubmodelComponent, Function) serialize(...)} to describe this subcomponent. - * See there for details. + * to override the ID and params used in {@link #serialize(SubmodelComponent, IdentifierGetter) serialize(...)} to describe this + * subcomponent. See there for details. * * @author Fabian Stemmler * @author Daniel Kirschten @@ -167,106 +162,44 @@ public final class SubmodelComponentSerializer public static SubmodelComponent deserialize(ViewModelModifiable model, SubmodelComponentParams params, String name, String idForSerializingOverride, JsonElement paramsForSerializingOverride) { + Version version = params.version; + if (version.compareTo(JSON_VERSION_LATEST_SUPPORTED_DESERIALIZING) > 0) + throw new IllegalArgumentException("JSON version " + version + " not supported yet"); + boolean hasUsageSerialized = version.compareTo(JSON_VERSION_EARLIEST_WITH_USAGE_SERIALIZED) >= 0; DeserializedSubmodelComponent comp = new DeserializedSubmodelComponent(model, name, idForSerializingOverride, paramsForSerializingOverride); - comp.setSubmodelScale(params.submodel.innerScale); - comp.setOutlineRenderer(SubmodelComponentSnippetSuppliers.outlineRendererSupplier - .getSnippetSupplier(params.outlineRendererSnippetID).create(comp, params.outlineRendererParams)); + comp.setSubmodelScale(params.innerScale); + comp.setSize(params.width, params.height); + for (InterfacePinParams iPinParams : params.interfacePins) + // TRISTATE because we don't have a better choice + comp.addSubmodelInterface(new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, + hasUsageSerialized ? iPinParams.usage : PinUsage.TRISTATE, iPinParams.location.x, iPinParams.location.y)); + ViewModelModifiable submodelModifiable = comp.getSubmodelModifiable(); + ViewModelSerializer.deserialize(comp.getSubmodelModifiable(), params.submodel); comp.setSymbolRenderer(SubmodelComponentSnippetSuppliers.symbolRendererSupplier.getSnippetSupplier(params.symbolRendererSnippetID) .create(comp, params.symbolRendererParams)); + comp.setOutlineRenderer(SubmodelComponentSnippetSuppliers.outlineRendererSupplier + .getSnippetSupplier(params.outlineRendererSnippetID).create(comp, params.outlineRendererParams)); comp.setHighLevelStateHandler(SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier .getSnippetSupplier(params.highLevelStateHandlerSnippetID).create(comp, params.highLevelStateHandlerParams)); - comp.setSize(params.width, params.height); - for (InterfacePinParams iPinParams : params.interfacePins) - comp.addSubmodelInterface( - new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y)); - SubmodelParameters submodelParams = params.submodel; - ViewModelModifiable submodelModifiable = comp.getSubmodelModifiable(); - Map componentsByName = submodelModifiable.getComponentsByName(); - GUIComponent[] components = new GUIComponent[submodelParams.subComps.length]; - for (int i = 0; i < components.length; i++) - { - InnerComponentParams cParams = submodelParams.subComps[i]; - components[i] = IndirectGUIComponentCreator.createComponent(submodelModifiable, cParams.id, cParams.params, cParams.name); - components[i].moveTo(cParams.pos.x, cParams.pos.y); - } - - for (int i = 0; i < submodelParams.innerWires.length; i++) - { - InnerWireParams innerWire = submodelParams.innerWires[i]; - new GUIWire(submodelModifiable, componentsByName.get(innerWire.pin1.compName).getPin(innerWire.pin1.pinName), - componentsByName.get(innerWire.pin2.compName).getPin(innerWire.pin2.pinName), innerWire.path); - } return comp; } /** * Returns {@link SubmodelComponentParams}, which describe this {@link SubmodelComponent}.
- * Subcomponents are serialized in the following way:
- * If a subcomponent is a SubmodelComponent which has been deserialized, and it has an - * {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride} set (e.g. non-null; see - * {@link #deserialize(ViewModelModifiable, SubmodelComponentParams, String, String, JsonElement) deserialize(...)}), this ID and the - * component's {@link DeserializedSubmodelComponent#paramsForSerializingOverride paramsForSerializingOverride} are written.
- * If this case doesn't apply (e.g. if the subcomponent is not a SubmodelComponent; or it is a - * SubmodelComponent, but hasn't been deserialized; or it has no - * {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride} set), the ID returned by - * getIdentifier and the params obtained by {@link GUIComponent#getParamsForSerializing() getParams()} are written. + * See {@link ViewModelSerializer#serialize(net.mograsim.logic.model.model.ViewModel, IdentifierGetter) + * ViewModelSerializer.serialize(...)} for how subcomponents are serialized.
+ * CodeSnippets are serialized using the ID defined by idGetter and the params obtained by the respective + * getParamsForSerializing methods ({@link Renderer#getParamsForSerializing()}). * * @author Fabian Stemmler * @author Daniel Kirschten */ - public static SubmodelComponentParams serialize(SubmodelComponent comp, Function getIdentifier) + public static SubmodelComponentParams serialize(SubmodelComponent comp, IdentifierGetter idGetter) { - SubmodelParameters submodelParams = new SubmodelParameters(); - submodelParams.innerScale = comp.getSubmodelScale(); - - Map components = new HashMap<>(comp.submodel.getComponentsByName()); - components.remove(SubmodelComponent.SUBMODEL_INTERFACE_NAME); - InnerComponentParams[] comps = new InnerComponentParams[components.size()]; - int i1 = 0; - for (GUIComponent innerComp : components.values()) - { - InnerComponentParams innerParams = new InnerComponentParams(); - comps[i1] = innerParams; - innerParams.pos = new Point(innerComp.getPosX(), innerComp.getPosY()); - DeserializedSubmodelComponent innerCompCasted; - if (innerComp instanceof DeserializedSubmodelComponent - && (innerCompCasted = (DeserializedSubmodelComponent) innerComp).idForSerializingOverride != null) - { - innerParams.id = innerCompCasted.idForSerializingOverride; - innerParams.params = innerCompCasted.paramsForSerializingOverride; - } else - { - innerParams.id = getIdentifier.apply(innerComp); - innerParams.params = innerComp.getParamsForSerializing(); - } - innerParams.name = innerComp.name; - i1++; - } - submodelParams.subComps = comps; - - List wireList = comp.submodel.getWires(); - InnerWireParams wires[] = new InnerWireParams[wireList.size()]; - i1 = 0; - for (GUIWire wire : wireList) - { - InnerWireParams inner = new InnerWireParams(); - wires[i1] = inner; - InnerPinParams pin1Params = new InnerPinParams(), pin2Params = new InnerPinParams(); - - pin1Params.pinName = wire.getPin1().name; - pin1Params.compName = wire.getPin1().component.name; - pin2Params.pinName = wire.getPin2().name; - pin2Params.compName = wire.getPin2().component.name; - inner.pin1 = pin1Params; - inner.pin2 = pin2Params; - inner.path = wire.getPath(); - i1++; - } - submodelParams.innerWires = wires; - - SubmodelComponentParams params = new SubmodelComponentParams(); - params.submodel = submodelParams; + SubmodelComponentParams params = new SubmodelComponentParams(JSON_VERSION_CURRENT_SERIALIZING); + params.innerScale = comp.getSubmodelScale(); + params.submodel = ViewModelSerializer.serialize(comp.submodel, idGetter); params.width = comp.getWidth(); params.height = comp.getHeight(); @@ -280,24 +213,31 @@ public final class SubmodelComponentSerializer iPinParams.location = p.getRelPos(); iPinParams.name = p.name; iPinParams.logicWidth = p.logicWidth; + iPinParams.usage = p.usage; i++; } params.interfacePins = iPins; + Arrays.sort(params.interfacePins, Comparator.comparing(p -> p.name)); - // TODO does this code belong here? - if (comp instanceof SimpleRectangularSubmodelComponent) + Renderer symbolRenderer = comp.getSymbolRenderer(); + if (symbolRenderer != null) { - SimpleRectangularSubmodelComponent compCasted = (SimpleRectangularSubmodelComponent) comp; + params.symbolRendererSnippetID = idGetter.symbolRendererIDs.apply(symbolRenderer); + params.symbolRendererParams = symbolRenderer.getParamsForSerializingJSON(idGetter); + } - SimpleRectangularLikeParams symbolRendererParams = new SimpleRectangularLikeParams(); - symbolRendererParams.centerText = compCasted.label; - symbolRendererParams.centerTextHeight = SimpleRectangularSubmodelComponent.labelFontHeight; - symbolRendererParams.horizontalComponentCenter = compCasted.getWidth() / 2; - symbolRendererParams.pinLabelHeight = SimpleRectangularSubmodelComponent.pinNameFontHeight; - symbolRendererParams.pinLabelMargin = SimpleRectangularSubmodelComponent.pinNameMargin; + Renderer outlineRenderer = comp.getOutlineRenderer(); + if (outlineRenderer != null) + { + params.outlineRendererSnippetID = idGetter.outlineRendererIDs.apply(outlineRenderer); + params.outlineRendererParams = outlineRenderer.getParamsForSerializingJSON(idGetter); + } - params.symbolRendererSnippetID = "SimpleRectangularLikeSymbolRenderer"; - params.symbolRendererParams = new Gson().toJsonTree(symbolRendererParams); + HighLevelStateHandler highLevelStateHandler = comp.getHighLevelStateHandler(); + if (highLevelStateHandler != null) + { + params.highLevelStateHandlerSnippetID = idGetter.highLevelStateHandlerIDs.apply(highLevelStateHandler); + params.highLevelStateHandlerParams = highLevelStateHandler.getParamsForSerializingJSON(idGetter); } return params;