From 74f82a58865d948fa700efbc185a8d057e4a0175 Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Mon, 15 Jul 2019 16:16:23 +0200 Subject: [PATCH] Implemented some high level state handlers; changed standard snippet IDs --- .../DeserializedSubmodelComponent.java | 4 +- .../SubmodelComponentSerializer.java | 2 +- .../HighLevelStateHandlerContext.java | 6 +- .../StandardHighLevelStateHandler.java | 69 +++++++++++--- ...HighLevelStateHandlerSnippetSuppliers.java | 1 + ...rSplittingAtomicHighLevelStateHandler.java | 90 +++++++++++++++++++ ...DelegatingAtomicHighLevelStateHandler.java | 70 +++++++++++++++ .../standard/standardSnippetIDMapping.json | 9 +- ...tingSubcomponentHighLevelStateHandler.java | 76 ++++++++++++++++ .../snippets/standardSnippetIDMapping.json | 4 +- 10 files changed, 308 insertions(+), 23 deletions(-) create mode 100644 net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/BitVectorSplittingAtomicHighLevelStateHandler.java create mode 100644 net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java create mode 100644 net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/DeserializedSubmodelComponent.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/DeserializedSubmodelComponent.java index 8db85a1d..2de33454 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/DeserializedSubmodelComponent.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/DeserializedSubmodelComponent.java @@ -11,6 +11,7 @@ import net.mograsim.logic.model.model.wires.Pin; import net.mograsim.logic.model.snippets.HighLevelStateHandler; import net.mograsim.logic.model.snippets.Renderer; +//TODO serialize handlers public class DeserializedSubmodelComponent extends SubmodelComponent { /** @@ -31,7 +32,8 @@ public class DeserializedSubmodelComponent extends SubmodelComponent private Renderer symbolRenderer; private HighLevelStateHandler highLevelStateHandler; - public DeserializedSubmodelComponent(ViewModelModifiable model, String name, String idForSerializingOverride, JsonElement paramsForSerializingOverride) + public DeserializedSubmodelComponent(ViewModelModifiable model, String name, String idForSerializingOverride, + JsonElement paramsForSerializingOverride) { super(model, name); this.idForSerializingOverride = idForSerializingOverride; 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..d98d4c98 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 @@ -296,7 +296,7 @@ public final class SubmodelComponentSerializer symbolRendererParams.pinLabelHeight = SimpleRectangularSubmodelComponent.pinNameFontHeight; symbolRendererParams.pinLabelMargin = SimpleRectangularSubmodelComponent.pinNameMargin; - params.symbolRendererSnippetID = "SimpleRectangularLikeSymbolRenderer"; + params.symbolRendererSnippetID = "simpleRectangularLike"; params.symbolRendererParams = new Gson().toJsonTree(symbolRendererParams); } diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/HighLevelStateHandlerContext.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/HighLevelStateHandlerContext.java index c5f8f13f..ba3ae198 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/HighLevelStateHandlerContext.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/HighLevelStateHandlerContext.java @@ -1,13 +1,13 @@ package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard; -import net.mograsim.logic.model.model.components.GUIComponent; +import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; public class HighLevelStateHandlerContext { - public final GUIComponent component; + public final SubmodelComponent component; public final String stateID; - public HighLevelStateHandlerContext(GUIComponent component, String stateID) + public HighLevelStateHandlerContext(SubmodelComponent component, String stateID) { this.component = component; this.stateID = stateID; diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/StandardHighLevelStateHandler.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/StandardHighLevelStateHandler.java index ec3b04d9..1c3ac62c 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/StandardHighLevelStateHandler.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/StandardHighLevelStateHandler.java @@ -2,8 +2,10 @@ package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard; import java.util.HashMap; import java.util.Map; +import java.util.function.BiFunction; +import java.util.function.Function; -import net.mograsim.logic.model.model.components.GUIComponent; +import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; import net.mograsim.logic.model.snippets.HighLevelStateHandler; import net.mograsim.logic.model.snippets.SnippetDefinintion; import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers; @@ -14,24 +16,48 @@ import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomp public class StandardHighLevelStateHandler implements HighLevelStateHandler { - private final GUIComponent component; + private final SubmodelComponent component; private final Map subcomponentHighLevelStateHandlers; private final Map atomicHighLevelStateHandlers; - public StandardHighLevelStateHandler(GUIComponent component, SimpleHighLevelStateHandlerParams params) + public StandardHighLevelStateHandler(SubmodelComponent component) + { + this(component, null); + } + + public StandardHighLevelStateHandler(SubmodelComponent component, StandardHighLevelStateHandlerParams params) { this.component = component; this.subcomponentHighLevelStateHandlers = new HashMap<>(); this.atomicHighLevelStateHandlers = new HashMap<>(); - params.subcomponentHighLevelStates.forEach(this::addSubcomponentHighLevelState); - params.atomicHighLevelStates.forEach(this::addAtomicHighLevelState); + if (params != null) + { + params.subcomponentHighLevelStates.forEach(this::addSubcomponentHighLevelState); + params.atomicHighLevelStates.forEach(this::addAtomicHighLevelState); + } + } + + public SubcomponentHighLevelStateHandler addSubcomponentHighLevelState(String subcomponentStateID, + SubcomponentHighLevelStateHandlerParams handlerParams) + { + return addSubcomponentHighLevelState(subcomponentStateID, + StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier.getSnippetSupplier(handlerParams.id)::create, + handlerParams.params); + } + + public H addSubcomponentHighLevelState(String subcomponentStateID, + BiFunction handlerConstructor, P handlerParams) + { + return addSubcomponentHighLevelState(subcomponentStateID, c -> handlerConstructor.apply(c, handlerParams)); } - public void addSubcomponentHighLevelState(String subcomponentStateID, SubcomponentHighLevelStateHandlerParams handlerParams) + public H addSubcomponentHighLevelState(String subcomponentStateID, + Function handlerConstructor) { HighLevelStateHandlerContext context = new HighLevelStateHandlerContext(component, subcomponentStateID); - addSubcomponentHighLevelState(subcomponentStateID, StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier - .getSnippetSupplier(handlerParams.id).create(context, handlerParams.params)); + H handler = handlerConstructor.apply(context); + addSubcomponentHighLevelState(subcomponentStateID, handler); + return handler; } public void addSubcomponentHighLevelState(String subcomponentStateID, SubcomponentHighLevelStateHandler handler) @@ -40,11 +66,26 @@ public class StandardHighLevelStateHandler implements HighLevelStateHandler subcomponentHighLevelStateHandlers.put(subcomponentStateID, handler); } - public void addAtomicHighLevelState(String atomicStateID, AtomicHighLevelStateHandlerParams handlerParams) + public AtomicHighLevelStateHandler addAtomicHighLevelState(String atomicStateID, AtomicHighLevelStateHandlerParams handlerParams) + { + return addAtomicHighLevelState(atomicStateID, + StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier.getSnippetSupplier(handlerParams.id)::create, + handlerParams.params); + } + + public H addAtomicHighLevelState(String subcomponentStateID, + BiFunction handlerConstructor, P handlerParams) { - HighLevelStateHandlerContext context = new HighLevelStateHandlerContext(component, atomicStateID); - addSubcomponentHighLevelState(atomicStateID, StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier - .getSnippetSupplier(handlerParams.id).create(context, handlerParams.params)); + return addAtomicHighLevelState(subcomponentStateID, c -> handlerConstructor.apply(c, handlerParams)); + } + + public H addAtomicHighLevelState(String subcomponentStateID, + Function handlerConstructor) + { + HighLevelStateHandlerContext context = new HighLevelStateHandlerContext(component, subcomponentStateID); + H handler = handlerConstructor.apply(context); + addAtomicHighLevelState(subcomponentStateID, handler); + return handler; } public void addAtomicHighLevelState(String atomicStateID, AtomicHighLevelStateHandler handler) @@ -98,7 +139,7 @@ public class StandardHighLevelStateHandler implements HighLevelStateHandler } } - public static class SimpleHighLevelStateHandlerParams + public static class StandardHighLevelStateHandlerParams { public Map subcomponentHighLevelStates; public Map atomicHighLevelStates; @@ -108,6 +149,6 @@ public class StandardHighLevelStateHandler implements HighLevelStateHandler { SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier.setSnippetSupplier( StandardHighLevelStateHandler.class.getCanonicalName(), - SnippetDefinintion.create(SimpleHighLevelStateHandlerParams.class, StandardHighLevelStateHandler::new)); + SnippetDefinintion.create(StandardHighLevelStateHandlerParams.class, StandardHighLevelStateHandler::new)); } } \ No newline at end of file diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/StandardHighLevelStateHandlerSnippetSuppliers.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/StandardHighLevelStateHandlerSnippetSuppliers.java index e81607de..51629207 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/StandardHighLevelStateHandlerSnippetSuppliers.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/StandardHighLevelStateHandlerSnippetSuppliers.java @@ -22,6 +22,7 @@ public class StandardHighLevelStateHandlerSnippetSuppliers static { + // TODO update standardSnippetIDMapping.json try (InputStream s = StandardHighLevelStateHandlerSnippetSuppliers.class.getResourceAsStream("./standardSnippetIDMapping.json")) { if (s == null) diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/BitVectorSplittingAtomicHighLevelStateHandler.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/BitVectorSplittingAtomicHighLevelStateHandler.java new file mode 100644 index 00000000..4b1139be --- /dev/null +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/BitVectorSplittingAtomicHighLevelStateHandler.java @@ -0,0 +1,90 @@ +package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic; + +import java.util.ArrayList; +import java.util.List; + +import net.mograsim.logic.core.types.BitVector; +import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; +import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.HighLevelStateHandlerContext; + +public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler +{ + private SubmodelComponent component; + private final List vectorPartTargets; + private final List vectorPartLengthes; + + public BitVectorSplittingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context) + { + this(context, null); + } + + public BitVectorSplittingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context, + BitVectorSplittingAtomicHighLevelStateHandlerParams params) + { + this.component = context.component; + this.vectorPartTargets = new ArrayList<>(); + this.vectorPartLengthes = new ArrayList<>(); + if (params != null) + setVectorParts(params.vectorPartTargets, params.vectorPartLengthes); + } + + public void set(List targets, List lengthes) + { + setVectorParts(targets, lengthes); + } + + public void addVectorPart(String target, int length) + { + vectorPartTargets.add(target); + vectorPartLengthes.add(length); + } + + public void clearVectorParts() + { + vectorPartTargets.clear(); + vectorPartLengthes.clear(); + } + + private void setVectorParts(List targets, List lengthes) + { + clearVectorParts(); + if (targets.size() != lengthes.size()) + throw new IllegalArgumentException("Targets list and lenghtes list have different sizes"); + vectorPartTargets.addAll(targets); + vectorPartLengthes.addAll(lengthes); + } + + @Override + public Object getHighLevelState() + { + BitVector result = BitVector.of(); + for (int partIndex = 0; partIndex < vectorPartTargets.size(); partIndex++) + { + BitVector vectorPart = (BitVector) component.getHighLevelState(vectorPartTargets.get(partIndex)); + if (vectorPart.length() != vectorPartLengthes.get(partIndex)) + throw new IllegalArgumentException( + "Illegal vector part length: " + vectorPart.length() + "; expected " + vectorPartLengthes.get(partIndex)); + result = result.concat(vectorPart);// TODO is the bit order correct? + } + return result; + } + + @Override + public void setHighLevelState(Object newState) + { + BitVector newStateCasted = (BitVector) newState; + for (int partIndex = 0, bitIndex = 0; partIndex < vectorPartTargets.size(); partIndex++) + { + int vectorPartLength = vectorPartLengthes.get(partIndex); + BitVector vectorPart = newStateCasted.subVector(bitIndex, vectorPartLength);// TODO is the bit order correct? + component.setHighLevelState(vectorPartTargets.get(partIndex), vectorPart); + bitIndex += vectorPartLength; + } + } + + public static class BitVectorSplittingAtomicHighLevelStateHandlerParams + { + public List vectorPartTargets; + public List vectorPartLengthes; + } +} \ No newline at end of file diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java new file mode 100644 index 00000000..0ed207f4 --- /dev/null +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java @@ -0,0 +1,70 @@ +package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic; + +import net.mograsim.logic.model.model.components.GUIComponent; +import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; +import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.HighLevelStateHandlerContext; + +public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler +{ + private final SubmodelComponent parentComponent; + private GUIComponent delegateTarget; + private String subStateID; + + public DelegatingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context) + { + this(context, null); + } + + public DelegatingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context, DelegatingAtomicHighLevelStateHandlerParams params) + { + this.parentComponent = context.component; + if (params != null) + { + // TODO document this + if (params.delegateTarget == null) + setDelegateTarget(parentComponent); + else + setDelegateTarget(parentComponent.submodel.getComponentsByName().get(params.delegateTarget)); + setSubStateID(params.subStateID); + } + } + + public void set(GUIComponent delegateTarget, String subStateID) + { + setDelegateTarget(delegateTarget); + setSubStateID(subStateID); + } + + public void setDelegateTarget(GUIComponent delegateTarget) + { + if (delegateTarget == null) + this.delegateTarget = parentComponent; + else if (!parentComponent.submodel.getComponentsByName().containsValue(delegateTarget)) + throw new IllegalArgumentException( + "Can only set components belonging to the submodel of the parent component of this handler as the delegate target"); + this.delegateTarget = delegateTarget; + } + + public void setSubStateID(String subStateID) + { + this.subStateID = subStateID; + } + + @Override + public Object getHighLevelState() + { + return delegateTarget.getHighLevelState(subStateID); + } + + @Override + public void setHighLevelState(Object newState) + { + delegateTarget.setHighLevelState(subStateID, newState); + } + + public static class DelegatingAtomicHighLevelStateHandlerParams + { + public String delegateTarget; + public String subStateID; + } +} \ No newline at end of file diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/standardSnippetIDMapping.json b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/standardSnippetIDMapping.json index 1f3b909b..de32e706 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/standardSnippetIDMapping.json +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/standardSnippetIDMapping.json @@ -1,5 +1,10 @@ mograsim version: 0.1.3 { - "standardSubcomponentHandlerSuppliers": {}, - "standardAtomicHandlerSuppliers": {} + "standardSubcomponentHandlerSuppliers": { + "delegating": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent.DelegatingSubcomponentHighLevelStateHandler" + }, + "standardAtomicHandlerSuppliers": { + "delegating": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.DelegatingAtomicHighLevelStateHandler" + "bitVectorSplitting": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.BitVectorSplittingAtomicHighLevelStateHandler" + } } \ No newline at end of file diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java new file mode 100644 index 00000000..79ec244d --- /dev/null +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java @@ -0,0 +1,76 @@ +package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent; + +import net.mograsim.logic.model.model.components.GUIComponent; +import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; +import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.HighLevelStateHandlerContext; + +public class DelegatingSubcomponentHighLevelStateHandler implements SubcomponentHighLevelStateHandler +{ + private final SubmodelComponent parentComponent; + private GUIComponent delegateTarget; + private String prefix; + + public DelegatingSubcomponentHighLevelStateHandler(HighLevelStateHandlerContext context) + { + this(context, null); + } + + public DelegatingSubcomponentHighLevelStateHandler(HighLevelStateHandlerContext context, + DelegatingSubcomponentHighLevelStateHandlerParams params) + { + this.parentComponent = context.component; + if (params != null) + { + // TODO document this + if (params.delegateTarget == null) + setDelegateTarget(parentComponent); + else + this.delegateTarget = parentComponent.submodel.getComponentsByName().get(params.delegateTarget); + setPrefix(prefix); + } + } + + public void set(GUIComponent delegateTarget, String prefix) + { + setDelegateTarget(delegateTarget); + setPrefix(prefix); + } + + public void setDelegateTarget(GUIComponent delegateTarget) + { + if (delegateTarget == null) + this.delegateTarget = parentComponent; + else if (!parentComponent.submodel.getComponentsByName().containsValue(delegateTarget)) + throw new IllegalArgumentException( + "Can only set components belonging to the submodel of the parent component of this handler as the delegate target"); + this.delegateTarget = delegateTarget; + } + + public void setPrefix(String prefix) + { + this.prefix = prefix; + } + + @Override + public Object getHighLevelState(String subStateID) + { + return delegateTarget.getHighLevelState(getDelegateTargetHighLevelStateID(subStateID)); + } + + @Override + public void setHighLevelState(String subStateID, Object newState) + { + delegateTarget.setHighLevelState(getDelegateTargetHighLevelStateID(subStateID), newState); + } + + private String getDelegateTargetHighLevelStateID(String subStateID) + { + return prefix == null ? subStateID : prefix + '.' + subStateID; + } + + public static class DelegatingSubcomponentHighLevelStateHandlerParams + { + public String delegateTarget; + public String prefix; + } +} \ No newline at end of file diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/standardSnippetIDMapping.json b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/standardSnippetIDMapping.json index b6a4562d..68757796 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/standardSnippetIDMapping.json +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/standardSnippetIDMapping.json @@ -2,8 +2,8 @@ mograsim version: 0.1.3 { "standardOutlineRendererSuppliers": {}, "standardSymbolRendererSuppliers": { - "CenteredTextSymbolRenderer": "net.mograsim.logic.model.snippets.symbolrenderers.CenteredTextSymbolRenderer", - "SimpleRectangularLikeSymbolRenderer": "net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer" + "centeredText": "net.mograsim.logic.model.snippets.symbolrenderers.CenteredTextSymbolRenderer", + "simpleRectangularLike": "net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer" }, "standardHighLevelStateHandlerSuppliers": { "standard": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandler" -- 2.17.1