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