Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ItemLabeling.cpp
Go to the documentation of this file.
2
8
9namespace storm {
10namespace models {
11namespace sparse {
12ItemLabeling::ItemLabeling(uint_fast64_t itemCount) : itemCount(itemCount), nameToLabelingIndexMap(), labelings() {
13 // Intentionally left empty.
14}
15
16bool ItemLabeling::isStateLabeling() const {
17 return false;
18}
19bool ItemLabeling::isChoiceLabeling() const {
20 return false;
21}
22
23StateLabeling const& ItemLabeling::asStateLabeling() const {
24 return dynamic_cast<StateLabeling const&>(*this);
25}
26StateLabeling& ItemLabeling::asStateLabeling() {
27 return dynamic_cast<StateLabeling&>(*this);
28}
29ChoiceLabeling const& ItemLabeling::asChoiceLabeling() const {
30 return dynamic_cast<ChoiceLabeling const&>(*this);
31}
32ChoiceLabeling& ItemLabeling::asChoiceLabeling() {
33 return dynamic_cast<ChoiceLabeling&>(*this);
34}
35
36bool ItemLabeling::operator==(ItemLabeling const& other) const {
37 if (itemCount != other.itemCount) {
38 return false;
39 }
40 if (this->getNumberOfLabels() != other.getNumberOfLabels()) {
41 return false;
42 }
43 for (auto const& labelIndexPair : this->nameToLabelingIndexMap) {
44 if (!other.containsLabel(labelIndexPair.first)) {
45 return false;
46 }
47 if (labelings[labelIndexPair.second] != other.getItems(labelIndexPair.first)) {
48 return false;
49 }
50 }
51 return true;
52}
53
54ItemLabeling ItemLabeling::getSubLabeling(storm::storage::BitVector const& items) const {
55 ItemLabeling result(items.getNumberOfSetBits());
56 for (auto const& labelIndexPair : nameToLabelingIndexMap) {
57 result.addLabel(labelIndexPair.first, labelings[labelIndexPair.second] % items);
58 }
59 return result;
60}
61
62void ItemLabeling::addLabel(std::string const& label) {
63 addLabel(label, storage::BitVector(itemCount));
64}
65
66void ItemLabeling::removeLabel(std::string const& label) {
67 auto labelIt = nameToLabelingIndexMap.find(label);
68 STORM_LOG_THROW(labelIt != nameToLabelingIndexMap.end(), storm::exceptions::InvalidArgumentException, "Label '" << label << "' does not exist.");
69 uint64_t labelIndex = labelIt->second;
70 // Erase entry in map
71 nameToLabelingIndexMap.erase(labelIt);
72 // Erase label by 'swap and pop'
73 std::iter_swap(labelings.begin() + labelIndex, labelings.end() - 1);
74 labelings.pop_back();
75
76 // Update index of labeling we swapped from the end
77 for (auto& it : nameToLabelingIndexMap) {
78 if (it.second == labelings.size()) {
79 it.second = labelIndex;
80 break;
81 }
82 }
83}
84
85void ItemLabeling::join(ItemLabeling const& other) {
86 STORM_LOG_THROW(this->itemCount == other.itemCount, storm::exceptions::InvalidArgumentException,
87 "The item count of the two labelings does not match: " << this->itemCount << " vs. " << other.itemCount << ".");
88 for (auto const& label : other.getLabels()) {
89 if (this->containsLabel(label)) {
90 this->setItems(label, this->getItems(label) | other.getItems(label));
91 } else {
92 this->addLabel(label, other.getItems(label));
93 }
94 }
95}
96
97std::set<std::string> ItemLabeling::getLabels() const {
98 std::set<std::string> result;
99 for (auto const& labelIndexPair : this->nameToLabelingIndexMap) {
100 result.insert(labelIndexPair.first);
101 }
102 return result;
103}
104
105std::set<std::string> ItemLabeling::getLabelsOfItem(uint64_t item) const {
106 std::set<std::string> result;
107 for (auto const& labelIndexPair : this->nameToLabelingIndexMap) {
108 if (this->getItemHasLabel(labelIndexPair.first, item)) {
109 result.insert(labelIndexPair.first);
110 }
111 }
112 return result;
113}
114
115void ItemLabeling::permuteItems(std::vector<uint64_t> const& inversePermutation) {
116 STORM_LOG_THROW(inversePermutation.size() == itemCount, storm::exceptions::InvalidArgumentException, "Permutation does not match number of items");
117 std::vector<storm::storage::BitVector> newLabelings;
118 for (storm::storage::BitVector const& source : this->labelings) {
119 newLabelings.push_back(source.permute(inversePermutation));
120 }
121
122 this->labelings = newLabelings;
123}
124
125void ItemLabeling::addLabel(std::string const& label, storage::BitVector const& labeling) {
126 STORM_LOG_THROW(!this->containsLabel(label), storm::exceptions::InvalidArgumentException, "Label '" << label << "' already exists.");
127 STORM_LOG_THROW(labeling.size() == itemCount, storm::exceptions::InvalidArgumentException,
128 "Labeling vector has invalid size. Expected: " << itemCount << " Actual: " << labeling.size());
129 nameToLabelingIndexMap.emplace(label, labelings.size());
130 labelings.push_back(labeling);
131}
132
133void ItemLabeling::addLabel(std::string const& label, storage::BitVector&& labeling) {
134 STORM_LOG_THROW(!this->containsLabel(label), storm::exceptions::InvalidArgumentException, "Label '" << label << "' already exists.");
135 STORM_LOG_THROW(labeling.size() == itemCount, storm::exceptions::InvalidArgumentException,
136 "Labeling vector has invalid size. Expected: " << itemCount << " Actual: " << labeling.size());
137 nameToLabelingIndexMap.emplace(label, labelings.size());
138 labelings.emplace_back(std::move(labeling));
139}
140
141std::string ItemLabeling::addUniqueLabel(std::string const& prefix, storage::BitVector const& labeling) {
142 std::string label = generateUniqueLabel(prefix);
143 addLabel(label, labeling);
144 return label;
145}
146
147std::string ItemLabeling::addUniqueLabel(std::string const& prefix, storage::BitVector const&& labeling) {
148 std::string label = generateUniqueLabel(prefix);
149 addLabel(label, labeling);
150 return label;
151}
152
153bool ItemLabeling::containsLabel(std::string const& label) const {
154 return nameToLabelingIndexMap.find(label) != nameToLabelingIndexMap.end();
155}
156
157void ItemLabeling::addLabelToItem(std::string const& label, uint64_t item) {
158 STORM_LOG_THROW(this->containsLabel(label), storm::exceptions::InvalidArgumentException, "Label '" << label << "' unknown.");
159 STORM_LOG_THROW(item < itemCount, storm::exceptions::OutOfRangeException, "Item index out of range.");
160 this->labelings[nameToLabelingIndexMap.at(label)].set(item, true);
161}
162
163void ItemLabeling::removeLabelFromItem(std::string const& label, uint64_t item) {
164 STORM_LOG_THROW(item < itemCount, storm::exceptions::OutOfRangeException, "Item index out of range.");
165 STORM_LOG_THROW(this->getItemHasLabel(label, item), storm::exceptions::InvalidArgumentException,
166 "Item " << item << " does not have label '" << label << "'.");
167 this->labelings[nameToLabelingIndexMap.at(label)].set(item, false);
168}
169
170bool ItemLabeling::getItemHasLabel(std::string const& label, uint64_t item) const {
171 STORM_LOG_THROW(this->containsLabel(label), storm::exceptions::InvalidArgumentException,
172 "The label '" << label << "' is invalid for the labeling of the model.");
173 return this->labelings[nameToLabelingIndexMap.at(label)].get(item);
174}
175
176std::size_t ItemLabeling::getNumberOfLabels() const {
177 return labelings.size();
178}
179
180std::size_t ItemLabeling::getNumberOfItems() const {
181 return itemCount;
182}
183
184storm::storage::BitVector const& ItemLabeling::getItems(std::string const& label) const {
185 STORM_LOG_THROW(this->containsLabel(label), storm::exceptions::InvalidArgumentException,
186 "The label " << label << " is invalid for the labeling of the model.");
187 return this->labelings[nameToLabelingIndexMap.at(label)];
188}
189
190void ItemLabeling::setItems(std::string const& label, storage::BitVector const& labeling) {
191 STORM_LOG_THROW(this->containsLabel(label), storm::exceptions::InvalidArgumentException,
192 "The label " << label << " is invalid for the labeling of the model.");
193 STORM_LOG_THROW(labeling.size() == itemCount, storm::exceptions::InvalidArgumentException, "Labeling vector has invalid size.");
194 this->labelings[nameToLabelingIndexMap.at(label)] = labeling;
195}
196
197void ItemLabeling::setItems(std::string const& label, storage::BitVector&& labeling) {
198 STORM_LOG_THROW(this->containsLabel(label), storm::exceptions::InvalidArgumentException,
199 "The label " << label << " is invalid for the labeling of the model.");
200 STORM_LOG_THROW(labeling.size() == itemCount, storm::exceptions::InvalidArgumentException, "Labeling vector has invalid size.");
201 this->labelings[nameToLabelingIndexMap.at(label)] = labeling;
202}
203
204void ItemLabeling::printLabelingInformationToStream(std::ostream& out) const {
205 out << this->getNumberOfLabels() << " labels\n";
206 for (auto const& labelIndexPair : this->nameToLabelingIndexMap) {
207 out << " * " << labelIndexPair.first << " -> " << this->labelings[labelIndexPair.second].getNumberOfSetBits() << " item(s)\n";
208 }
209}
210
211void ItemLabeling::printCompleteLabelingInformationToStream(std::ostream& out) const {
212 out << "Labels: \t" << this->getNumberOfLabels() << '\n';
213 for (auto label : nameToLabelingIndexMap) {
214 out << "Label '" << label.first << "': ";
215 for (auto index : this->labelings[label.second]) {
216 out << index << " ";
217 }
218 out << '\n';
219 }
220}
221
222std::size_t ItemLabeling::hash() const {
223 return 0;
224}
225
226std::ostream& operator<<(std::ostream& out, ItemLabeling const& labeling) {
228 return out;
229}
230
231std::string ItemLabeling::generateUniqueLabel(const std::string& prefix) const {
232 if (!containsLabel(prefix)) {
233 return prefix;
234 }
235 unsigned int i = 0;
236 std::string label;
237 do {
238 label = prefix + "_" + std::to_string(i);
239 } while (containsLabel(label));
240 return label;
241}
242
243} // namespace sparse
244} // namespace models
245} // namespace storm
This class manages the labeling of the choice space with a number of (atomic) labels.
A base class managing the labeling of items with a number of (atomic) labels.
std::set< std::string > getLabels() const
Retrieves the set of labels contained in this labeling.
ItemLabeling(uint64_t itemCount=0)
Constructs an empty labeling for the given number of items.
void addLabel(std::string const &label)
Adds a new label to the labelings.
bool containsLabel(std::string const &label) const
Checks whether a label is registered within this labeling.
virtual storm::storage::BitVector const & getItems(std::string const &label) const
Returns the labeling of items associated with the given label.
void printLabelingInformationToStream(std::ostream &out=std::cout) const
Prints information about the labeling to the specified stream.
std::size_t getNumberOfLabels() const
Returns the number of labels managed by this object.
This class manages the labeling of the state space with a number of (atomic) labels.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
uint64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
size_t size() const
Retrieves the number of bits this bit vector can store.
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30