1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import net.mograsim.logic.core.types.Bit;
8 import net.mograsim.logic.core.types.BitVector;
9 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
10 import net.mograsim.logic.model.serializing.IdentifyParams;
11 import net.mograsim.logic.model.snippets.SnippetDefinintion;
12 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers;
14 public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler
16 private final SubmodelComponent component;
17 private final List<String> vectorPartTargets;
18 private final List<String> vectorPartTargetsUnmodifiable;
19 private final List<Integer> vectorPartLengthes;
20 private final List<Integer> vectorPartLengthesUnmodifiable;
23 public BitVectorSplittingAtomicHighLevelStateHandler(SubmodelComponent component)
25 this(component, null);
28 public BitVectorSplittingAtomicHighLevelStateHandler(SubmodelComponent component,
29 BitVectorSplittingAtomicHighLevelStateHandlerParams params)
31 this.component = component;
32 this.vectorPartTargets = new ArrayList<>();
33 this.vectorPartTargetsUnmodifiable = Collections.unmodifiableList(vectorPartTargets);
34 this.vectorPartLengthes = new ArrayList<>();
35 this.vectorPartLengthesUnmodifiable = Collections.unmodifiableList(vectorPartLengthes);
37 setVectorParts(params.vectorPartTargets, params.vectorPartLengthes);
40 public void set(List<String> targets, List<Integer> lengthes)
42 setVectorParts(targets, lengthes);
45 public void addVectorPart(String target, int length)
47 vectorPartTargets.add(target);
48 vectorPartLengthes.add(length);
49 this.length += length;
52 public void clearVectorParts()
54 vectorPartTargets.clear();
55 vectorPartLengthes.clear();
59 private void setVectorParts(List<String> targets, List<Integer> lengthes)
62 if (targets.size() != lengthes.size())
63 throw new IllegalArgumentException("Targets list and lengthes list have different sizes");
64 vectorPartTargets.addAll(targets);
65 vectorPartLengthes.addAll(lengthes);
66 length += lengthes.stream().mapToInt(Integer::intValue).sum();
69 public List<String> getVectorPartTargets()
71 return vectorPartTargetsUnmodifiable;
74 public List<Integer> getVectorPartLenghtes()
76 return vectorPartLengthesUnmodifiable;
80 public Object getHighLevelState()
82 BitVector result = BitVector.of();
83 for (int partIndex = 0; partIndex < vectorPartTargets.size(); partIndex++)
85 Object subStateUncasted = component.getHighLevelState(vectorPartTargets.get(partIndex));
87 if (subStateUncasted instanceof Bit)
88 vectorPart = BitVector.of((Bit) subStateUncasted);
90 vectorPart = (BitVector) subStateUncasted;
91 if (vectorPart.length() != vectorPartLengthes.get(partIndex))
92 throw new IllegalArgumentException(
93 "Incorrect vector part length: " + vectorPart.length() + "; expected " + vectorPartLengthes.get(partIndex));
94 result = result.concat(vectorPart);
100 public void setHighLevelState(Object newState)
102 BitVector newStateCasted = (BitVector) newState;
103 if (newStateCasted.length() != length)
104 throw new IllegalArgumentException("Incorrect vector length: " + newStateCasted.length() + "; expected " + length);
105 for (int partIndex = 0, bitIndex = 0; partIndex < vectorPartTargets.size(); partIndex++)
107 int vectorPartLength = vectorPartLengthes.get(partIndex);
108 BitVector vectorPart = newStateCasted.subVector(bitIndex, bitIndex + vectorPartLength);
109 component.setHighLevelState(vectorPartTargets.get(partIndex), vectorPart);
110 bitIndex += vectorPartLength;
115 public String getIDForSerializing(IdentifyParams idParams)
117 return "bitVectorSplitting";
121 public BitVectorSplittingAtomicHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams)
123 BitVectorSplittingAtomicHighLevelStateHandlerParams params = new BitVectorSplittingAtomicHighLevelStateHandlerParams();
124 params.vectorPartTargets = new ArrayList<>(vectorPartTargets);
125 params.vectorPartLengthes = new ArrayList<>(vectorPartLengthes);
129 public static class BitVectorSplittingAtomicHighLevelStateHandlerParams
131 public List<String> vectorPartTargets;
132 public List<Integer> vectorPartLengthes;
137 StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier
138 .setSnippetSupplier(BitVectorSplittingAtomicHighLevelStateHandler.class.getCanonicalName(), SnippetDefinintion.create(
139 BitVectorSplittingAtomicHighLevelStateHandlerParams.class, BitVectorSplittingAtomicHighLevelStateHandler::new));