Implemented some high level state handlers; changed standard snippet IDs
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 15 Jul 2019 14:16:23 +0000 (16:16 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 15 Jul 2019 14:16:36 +0000 (16:16 +0200)
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/DeserializedSubmodelComponent.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/SubmodelComponentSerializer.java
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/HighLevelStateHandlerContext.java
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/StandardHighLevelStateHandler.java
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/StandardHighLevelStateHandlerSnippetSuppliers.java
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/BitVectorSplittingAtomicHighLevelStateHandler.java [new file with mode: 0644]
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java [new file with mode: 0644]
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/standardSnippetIDMapping.json
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java [new file with mode: 0644]
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/standardSnippetIDMapping.json

index 8db85a1..2de3345 100644 (file)
@@ -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;
index 7c002a0..d98d4c9 100644 (file)
@@ -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);
                }
 
index c5f8f13..ba3ae19 100644 (file)
@@ -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;
index ec3b04d..1c3ac62 100644 (file)
@@ -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<String, SubcomponentHighLevelStateHandler> subcomponentHighLevelStateHandlers;
        private final Map<String, AtomicHighLevelStateHandler> 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 <P, H extends SubcomponentHighLevelStateHandler> H addSubcomponentHighLevelState(String subcomponentStateID,
+                       BiFunction<HighLevelStateHandlerContext, P, H> handlerConstructor, P handlerParams)
+       {
+               return addSubcomponentHighLevelState(subcomponentStateID, c -> handlerConstructor.apply(c, handlerParams));
        }
 
-       public void addSubcomponentHighLevelState(String subcomponentStateID, SubcomponentHighLevelStateHandlerParams handlerParams)
+       public <H extends SubcomponentHighLevelStateHandler> H addSubcomponentHighLevelState(String subcomponentStateID,
+                       Function<HighLevelStateHandlerContext, H> 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 <P, H extends AtomicHighLevelStateHandler> H addAtomicHighLevelState(String subcomponentStateID,
+                       BiFunction<HighLevelStateHandlerContext, P, H> 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 extends AtomicHighLevelStateHandler> H addAtomicHighLevelState(String subcomponentStateID,
+                       Function<HighLevelStateHandlerContext, H> 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<String, SubcomponentHighLevelStateHandlerParams> subcomponentHighLevelStates;
                public Map<String, AtomicHighLevelStateHandlerParams> 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
index e81607d..5162920 100644 (file)
@@ -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 (file)
index 0000000..4b1139b
--- /dev/null
@@ -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<String> vectorPartTargets;
+       private final List<Integer> 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<String> targets, List<Integer> 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<String> targets, List<Integer> 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<String> vectorPartTargets;
+               public List<Integer> 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 (file)
index 0000000..0ed207f
--- /dev/null
@@ -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
index 1f3b909..de32e70 100644 (file)
@@ -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 (file)
index 0000000..79ec244
--- /dev/null
@@ -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
index b6a4562..6875779 100644 (file)
@@ -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"