import net.mograsim.logic.model.snippets.HighLevelStateHandler;
import net.mograsim.logic.model.snippets.Renderer;
+//TODO serialize handlers
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;
symbolRendererParams.pinLabelHeight = SimpleRectangularSubmodelComponent.pinNameFontHeight;
symbolRendererParams.pinLabelMargin = SimpleRectangularSubmodelComponent.pinNameMargin;
- params.symbolRendererSnippetID = "SimpleRectangularLikeSymbolRenderer";
+ params.symbolRendererSnippetID = "simpleRectangularLike";
params.symbolRendererParams = new Gson().toJsonTree(symbolRendererParams);
}
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;
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;
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)
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)
}
}
- public static class SimpleHighLevelStateHandlerParams
+ public static class StandardHighLevelStateHandlerParams
{
public Map<String, SubcomponentHighLevelStateHandlerParams> subcomponentHighLevelStates;
public Map<String, AtomicHighLevelStateHandlerParams> atomicHighLevelStates;
{
SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier.setSnippetSupplier(
StandardHighLevelStateHandler.class.getCanonicalName(),
- SnippetDefinintion.create(SimpleHighLevelStateHandlerParams.class, StandardHighLevelStateHandler::new));
+ SnippetDefinintion.create(StandardHighLevelStateHandlerParams.class, StandardHighLevelStateHandler::new));
}
}
\ No newline at end of file
static
{
+ // TODO update standardSnippetIDMapping.json
try (InputStream s = StandardHighLevelStateHandlerSnippetSuppliers.class.getResourceAsStream("./standardSnippetIDMapping.json"))
{
if (s == null)
--- /dev/null
+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
--- /dev/null
+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
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
--- /dev/null
+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
{
"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"