From 5140b424c507b8f22f80b452e475d0b56a3a36ce Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Wed, 11 Sep 2019 17:22:51 +0200 Subject: [PATCH] Improved some snippets (HighLevelStateHandlers and PinNamesRenderer) --- .../StandardHighLevelStateHandler.java | 27 +++++++++++++++++++ ...rSplittingAtomicHighLevelStateHandler.java | 17 +++++++++++- ...DelegatingAtomicHighLevelStateHandler.java | 26 +++++++++++++++++- ...ireForcingAtomicHighLevelStateHandler.java | 24 ++++++++++++++++- ...tingSubcomponentHighLevelStateHandler.java | 26 +++++++++++++++++- .../PinNamesSymbolRenderer.java | 1 + 6 files changed, 117 insertions(+), 4 deletions(-) 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 af3ad22b..490b2676 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 @@ -1,5 +1,6 @@ package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -20,7 +21,9 @@ public class StandardHighLevelStateHandler implements HighLevelStateHandler { private final SubmodelComponent component; private final Map subcomponentHighLevelStateHandlers; + private final Map subcomponentHighLevelStateHandlersUnmodifiable; private final Map atomicHighLevelStateHandlers; + private final Map atomicHighLevelStateHandlersUnmodifiable; public StandardHighLevelStateHandler(SubmodelComponent component) { @@ -31,7 +34,9 @@ public class StandardHighLevelStateHandler implements HighLevelStateHandler { this.component = component; this.subcomponentHighLevelStateHandlers = new HashMap<>(); + this.subcomponentHighLevelStateHandlersUnmodifiable = Collections.unmodifiableMap(subcomponentHighLevelStateHandlers); this.atomicHighLevelStateHandlers = new HashMap<>(); + this.atomicHighLevelStateHandlersUnmodifiable = Collections.unmodifiableMap(atomicHighLevelStateHandlers); if (params != null) { params.subcomponentHighLevelStates.forEach(this::addSubcomponentHighLevelState); @@ -68,6 +73,17 @@ public class StandardHighLevelStateHandler implements HighLevelStateHandler subcomponentHighLevelStateHandlers.put(subcomponentStateID, handler); } + public void removeSubcomponentHighLevelState(String subcomponentStateID) + { + checkHighLevelStateIDPart(subcomponentStateID); + subcomponentHighLevelStateHandlers.remove(subcomponentStateID); + } + + public Map getSubcomponentHighLevelStates() + { + return subcomponentHighLevelStateHandlersUnmodifiable; + } + public AtomicHighLevelStateHandler addAtomicHighLevelState(String atomicStateID, AtomicHighLevelStateHandlerParams handlerParams) { return addAtomicHighLevelState(atomicStateID, @@ -96,6 +112,17 @@ public class StandardHighLevelStateHandler implements HighLevelStateHandler atomicHighLevelStateHandlers.put(atomicStateID, handler); } + public void removeAtomicHighLevelState(String atomicStateID) + { + checkHighLevelStateIDPart(atomicStateID); + atomicHighLevelStateHandlers.remove(atomicStateID); + } + + public Map getAtomicHighLevelStates() + { + return atomicHighLevelStateHandlersUnmodifiable; + } + private static void checkHighLevelStateIDPart(String stateIDPart) { if (stateIDPart.indexOf('.') != -1) 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 index b5630a52..4b9e2d62 100644 --- 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 @@ -1,6 +1,7 @@ package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import net.mograsim.logic.core.types.Bit; @@ -13,9 +14,11 @@ import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.Standar public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler { - private SubmodelComponent component; + private final SubmodelComponent component; private final List vectorPartTargets; + private final List vectorPartTargetsUnmodifiable; private final List vectorPartLengthes; + private final List vectorPartLengthesUnmodifiable; private int length; public BitVectorSplittingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context) @@ -28,7 +31,9 @@ public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHigh { this.component = context.component; this.vectorPartTargets = new ArrayList<>(); + this.vectorPartTargetsUnmodifiable = Collections.unmodifiableList(vectorPartTargets); this.vectorPartLengthes = new ArrayList<>(); + this.vectorPartLengthesUnmodifiable = Collections.unmodifiableList(vectorPartLengthes); if (params != null) setVectorParts(params.vectorPartTargets, params.vectorPartLengthes); } @@ -62,6 +67,16 @@ public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHigh length += lengthes.stream().mapToInt(Integer::intValue).sum(); } + public List getVectorPartTargets() + { + return vectorPartTargetsUnmodifiable; + } + + public List getVectorPartLenghtes() + { + return vectorPartLengthesUnmodifiable; + } + @Override public Object getHighLevelState() { 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 index 621dde31..1deabde8 100644 --- 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 @@ -27,9 +27,19 @@ public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelSta if (params.delegateTarget == null) setDelegateTarget(parentComponent); else - setDelegateTarget(parentComponent.submodel.getComponentsByName().get(params.delegateTarget)); + { + ModelComponent delegateTarget = parentComponent.submodel.getComponentsByName().get(params.delegateTarget); + if (delegateTarget == null) + throw new NullPointerException("No subcomponent with name " + params.delegateTarget); + setDelegateTarget(delegateTarget); + } setSubStateID(params.subStateID); } + parentComponent.submodel.addComponentRemovedListener(c -> + { + if (delegateTarget == c) + delegateTarget = null; + }); } public void set(ModelComponent delegateTarget, String subStateID) @@ -48,20 +58,34 @@ public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelSta this.delegateTarget = delegateTarget; } + public ModelComponent getDelegateTarget() + { + return delegateTarget; + } + public void setSubStateID(String subStateID) { this.subStateID = subStateID; } + public String getSubStateID() + { + return subStateID; + } + @Override public Object getHighLevelState() { + if (delegateTarget == null) + throw new IllegalStateException("Delegating to a component that was destroyed"); return delegateTarget.getHighLevelState(subStateID); } @Override public void setHighLevelState(Object newState) { + if (delegateTarget == null) + throw new IllegalStateException("Delegating to a component that was destroyed"); delegateTarget.setHighLevelState(subStateID, newState); } diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/WireForcingAtomicHighLevelStateHandler.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/WireForcingAtomicHighLevelStateHandler.java index ecf3a4d9..ac0f8a42 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/WireForcingAtomicHighLevelStateHandler.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/WireForcingAtomicHighLevelStateHandler.java @@ -1,6 +1,7 @@ package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -17,10 +18,12 @@ import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.Standar public class WireForcingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler { - private SubmodelComponent component; + private final SubmodelComponent component; private int logicWidth; private final List wiresToForce; + private final List wiresToForceUnmodifiable; private final List wiresToForceInverted; + private final List wiresToForceInvertedUnmodifiable; public WireForcingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context) { @@ -31,13 +34,20 @@ public class WireForcingAtomicHighLevelStateHandler implements AtomicHighLevelSt { this.component = context.component; this.wiresToForce = new ArrayList<>(); + this.wiresToForceUnmodifiable = Collections.unmodifiableList(wiresToForce); this.wiresToForceInverted = new ArrayList<>(); + this.wiresToForceInvertedUnmodifiable = Collections.unmodifiableList(wiresToForceInverted); if (params != null) { Map wiresByName = component.submodel.getWiresByName(); setWiresToForce(params.wiresToForce.stream().map((Function) wiresByName::get).collect(Collectors.toList()), params.wiresToForceInverted.stream().map((Function) wiresByName::get).collect(Collectors.toList())); } + component.submodel.addWireRemovedListener(w -> + { + wiresToForce.removeIf(w::equals); + wiresToForceInverted.removeIf(w::equals); + }); } public void set(List wiresToForce, List wiresToForceInverted) @@ -62,6 +72,8 @@ public class WireForcingAtomicHighLevelStateHandler implements AtomicHighLevelSt logicWidth = wire.logicWidth; else if (wire.logicWidth != logicWidth) throw new IllegalArgumentException("Can only force wires of the same logic width"); + // this can add the same wire multiple times, but maybe there is a weird configuration where it is neccessary, due to race + // conditions, to force the same wire twice. if (inverted) wiresToForceInverted.add(wire); else @@ -75,6 +87,16 @@ public class WireForcingAtomicHighLevelStateHandler implements AtomicHighLevelSt logicWidth = 0; } + public List getWiresToForce() + { + return wiresToForceUnmodifiable; + } + + public List getWiresToForceInverted() + { + return wiresToForceInvertedUnmodifiable; + } + @Override public Object getHighLevelState() { 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 index fc3c6bd1..069a6cb3 100644 --- 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 @@ -28,9 +28,19 @@ public class DelegatingSubcomponentHighLevelStateHandler implements Subcomponent if (params.delegateTarget == null) setDelegateTarget(parentComponent); else - this.delegateTarget = parentComponent.submodel.getComponentsByName().get(params.delegateTarget); + { + ModelComponent delegateTarget = parentComponent.submodel.getComponentsByName().get(params.delegateTarget); + if (delegateTarget == null) + throw new NullPointerException("No subcomponent with name " + params.delegateTarget); + setDelegateTarget(delegateTarget); + } setPrefix(params.prefix); } + parentComponent.submodel.addComponentRemovedListener(c -> + { + if (delegateTarget == c) + delegateTarget = null; + }); } public void set(ModelComponent delegateTarget, String prefix) @@ -49,20 +59,34 @@ public class DelegatingSubcomponentHighLevelStateHandler implements Subcomponent this.delegateTarget = delegateTarget; } + public ModelComponent getDelegateTarget() + { + return delegateTarget; + } + public void setPrefix(String prefix) { this.prefix = prefix; } + public String getPrefix() + { + return prefix; + } + @Override public Object getHighLevelState(String subStateID) { + if (delegateTarget == null) + throw new IllegalStateException("Delegating to a component that was destroyed"); return delegateTarget.getHighLevelState(getDelegateTargetHighLevelStateID(subStateID)); } @Override public void setHighLevelState(String subStateID, Object newState) { + if (delegateTarget == null) + throw new IllegalStateException("Delegating to a component that was destroyed"); delegateTarget.setHighLevelState(getDelegateTargetHighLevelStateID(subStateID), newState); } diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/symbolrenderers/PinNamesSymbolRenderer.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/symbolrenderers/PinNamesSymbolRenderer.java index a6380135..ede87105 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/symbolrenderers/PinNamesSymbolRenderer.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/symbolrenderers/PinNamesSymbolRenderer.java @@ -30,6 +30,7 @@ public class PinNamesSymbolRenderer implements Renderer this.pinLabelMargin = params.pinLabelMargin; if (params.pinNamePositions != null) params.pinNamePositions.forEach(this::setPinPosition); + component.addPinRemovedListener(p -> setPinPosition(p, null)); } public void setPinPosition(String pinName, Position position) -- 2.17.1