3#ifdef STORM_HAVE_XERCES
13bool isOnlyWhitespace(std::string
const& in) {
14 return std::all_of(in.begin(), in.end(), [](
char c) { return std::isspace(static_cast<unsigned char>(c)); });
20storm::gspn::GSPN* PnmlParser::parse(xercesc::DOMElement
const* elementRoot) {
21 if (storm::adapters::getName(elementRoot) ==
"pnml") {
22 traversePnmlElement(elementRoot);
25 STORM_LOG_THROW(
false, storm::exceptions::UnexpectedException,
"Failed to identify the root element.");
27 return builder.buildGspn();
30void PnmlParser::traversePnmlElement(xercesc::DOMElement
const*
const element) {
32 for (uint_fast64_t i = 0;
i < element->getAttributes()->getLength(); ++
i) {
33 auto attr = element->getAttributes()->item(i);
34 auto name = storm::adapters::getName(attr);
42 for (uint_fast64_t i = 0;
i < element->getChildNodes()->getLength(); ++
i) {
43 auto child = element->getChildNodes()->item(i);
44 auto name = storm::adapters::getName(child);
47 traverseNetOrPage(child);
48 }
else if (isOnlyWhitespace(name)) {
58void PnmlParser::traverseNetOrPage(xercesc::DOMNode
const*
const node) {
60 for (uint_fast64_t i = 0;
i < node->getAttributes()->getLength(); ++
i) {
61 auto attr = node->getAttributes()->item(i);
62 auto name = storm::adapters::getName(attr);
65 builder.setGspnName(storm::adapters::XMLtoString(attr->getNodeValue()));
69 STORM_LOG_WARN(
"unknown attribute (node=" + storm::adapters::XMLtoString(node->getNodeName()) +
"): " + name +
"\n");
74 for (uint_fast64_t i = 0;
i < node->getChildNodes()->getLength(); ++
i) {
75 auto child = node->getChildNodes()->item(i);
76 auto name = storm::adapters::getName(child);
78 if (name ==
"place") {
80 }
else if (name ==
"transition") {
81 traverseTransition(child);
82 }
else if (name ==
"arc") {
84 }
else if (name ==
"page") {
87 traverseNetOrPage(child);
88 }
else if (isOnlyWhitespace(name)) {
93 STORM_LOG_WARN(
"unknown child (node=" + storm::adapters::XMLtoString(node->getNodeName()) +
"): " + name +
"\n");
98void PnmlParser::traversePlace(xercesc::DOMNode
const*
const node) {
99 std::string placeName;
101 std::pair<bool, uint_fast64_t> numberOfInitialTokens(
false, defaultNumberOfInitialTokens);
102 std::pair<bool, boost::optional<uint64_t>> capacity(
false, boost::none);
105 for (uint_fast64_t i = 0;
i < node->getAttributes()->getLength(); ++
i) {
106 auto attr = node->getAttributes()->item(i);
107 auto name = storm::adapters::getName(attr);
110 placeName = storm::adapters::XMLtoString(attr->getNodeValue());
119 for (uint_fast64_t i = 0;
i < node->getChildNodes()->getLength(); ++
i) {
120 auto child = node->getChildNodes()->item(i);
121 auto name = storm::adapters::getName(child);
123 if (name ==
"initialMarking") {
124 numberOfInitialTokens.first =
true;
125 numberOfInitialTokens.second = traverseInitialMarking(child);
126 }
else if (name ==
"capacity") {
127 capacity.first =
true;
128 capacity.second = traverseCapacity(child);
129 }
else if (isOnlyWhitespace(name)) {
131 }
else if (name ==
"name" || name ==
"graphics") {
140 if (!numberOfInitialTokens.first) {
143 STORM_LOG_WARN(
"unknown numberOfInitialTokens (place=" + placeName +
")\n");
145 if (!capacity.first) {
149 builder.addPlace(capacity.second, numberOfInitialTokens.first ? numberOfInitialTokens.second : 0, placeName);
152void PnmlParser::traverseTransition(xercesc::DOMNode
const*
const node) {
154 std::pair<bool, bool> timed(
false, defaultTransitionType);
155 std::pair<bool, std::string> value(
false,
"");
157 uint_fast64_t priority = defaultPriority;
160 for (uint_fast64_t i = 0;
i < node->getAttributes()->getLength(); ++
i) {
161 auto attr = node->getAttributes()->item(i);
162 auto name = storm::adapters::getName(attr);
165 id = storm::adapters::XMLtoString(attr->getNodeValue());
169 STORM_LOG_WARN(
"unknown attribute (node=transition): " + name +
"\n");
174 for (uint_fast64_t i = 0;
i < node->getChildNodes()->getLength(); ++
i) {
175 auto child = node->getChildNodes()->item(i);
176 auto name = storm::adapters::getName(child);
178 if (name ==
"rate") {
180 value.second = traverseTransitionValue(child);
181 }
else if (name ==
"timed") {
183 timed.second = traverseTransitionType(child);
184 }
else if (name ==
"priority") {
185 priority = traversePriority(child);
186 }
else if (isOnlyWhitespace(name)) {
188 }
else if (name ==
"graphics" || name ==
"name" || name ==
"orientation") {
193 STORM_LOG_WARN(
"unknown child (node=transition): " + name +
"\n");
198 STORM_LOG_THROW(timed.first, storm::exceptions::UnexpectedException,
"Unknown transition type (transition=" +
id +
").");
202 STORM_LOG_THROW(value.first, storm::exceptions::UnexpectedException,
"Unknown transition rate (transition=" +
id +
").");
203 builder.addTimedTransition(priority, std::stod(value.second),
id);
208 STORM_LOG_WARN(
"unknown transition weight (transition=" +
id +
")\n");
210 builder.addImmediateTransition(priority, std::stod(value.second),
id);
214void PnmlParser::traverseArc(xercesc::DOMNode
const*
const node) {
216 std::pair<bool, std::string> source(
false,
"");
217 std::pair<bool, std::string> target(
false,
"");
218 std::pair<bool, std::string> type(
false, defaultArcType);
219 std::pair<bool, uint_fast64_t> multiplicity(
false, defaultMultiplicity);
223 for (uint_fast64_t i = 0;
i < node->getAttributes()->getLength(); ++
i) {
224 auto attr = node->getAttributes()->item(i);
225 auto name = storm::adapters::getName(attr);
227 if (name ==
"source") {
229 source.second = storm::adapters::XMLtoString(attr->getNodeValue());
230 }
else if (name ==
"target") {
232 target.second = storm::adapters::XMLtoString(attr->getNodeValue());
233 }
else if (name ==
"id") {
234 id = storm::adapters::XMLtoString(attr->getNodeValue());
243 for (uint_fast64_t i = 0;
i < node->getChildNodes()->getLength(); ++
i) {
244 auto child = node->getChildNodes()->item(i);
245 auto name = storm::adapters::getName(child);
246 if (name ==
"type") {
248 type.second = traverseArcType(child);
249 }
else if (name ==
"inscription") {
250 multiplicity.first =
true;
251 multiplicity.second = traverseMultiplicity(child);
252 }
else if (isOnlyWhitespace(name)) {
254 }
else if (name ==
"graphics" || name ==
"arcpath" || name ==
"tagged") {
265 STORM_LOG_THROW(source.first, storm::exceptions::UnexpectedException,
"Unknown arc source (arc=" +
id +
").");
267 STORM_LOG_THROW(target.first, storm::exceptions::UnexpectedException,
"Unknown arc target (arc=" +
id +
").");
268 if (!multiplicity.first) {
274 if (type.second ==
"normal") {
275 builder.addNormalArc(source.second, target.second, multiplicity.second);
276 }
else if (type.second ==
"inhibition") {
277 builder.addInhibitionArc(source.second, target.second, multiplicity.second);
279 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Arc type '" << type.second <<
"' in arc '" <<
id <<
"' is unknown.");
283uint_fast64_t PnmlParser::traverseInitialMarking(xercesc::DOMNode
const*
const node) {
284 uint_fast64_t result = defaultNumberOfInitialTokens;
285 for (uint_fast64_t i = 0;
i < node->getChildNodes()->getLength(); ++
i) {
286 auto child = node->getChildNodes()->item(i);
287 auto name = storm::adapters::getName(child);
288 if (name ==
"text") {
289 result = std::stoull(storm::adapters::getName(child->getFirstChild()));
290 }
else if (name ==
"value") {
291 auto value = storm::adapters::getName(child->getFirstChild());
292 value = value.substr(std::string(
"Default,").length());
293 result = std::stoull(value);
294 }
else if (isOnlyWhitespace(name)) {
296 }
else if (name ==
"graphics") {
301 STORM_LOG_WARN(
"unknown child (node=initialMarking): " + name +
"\n");
307int_fast64_t PnmlParser::traverseCapacity(xercesc::DOMNode
const*
const node) {
308 int_fast64_t result = defaultCapacity;
309 for (uint_fast64_t i = 0;
i < node->getChildNodes()->getLength(); ++
i) {
310 auto child = node->getChildNodes()->item(i);
311 auto name = storm::adapters::getName(child);
312 if (name ==
"value") {
313 auto value = storm::adapters::getName(child->getFirstChild());
314 if (value.find(
"Default,") == 0) {
315 value = value.substr(std::string(
"Default,").length());
317 result = std::stoull(value);
318 }
else if (name ==
"graphics") {
320 }
else if (isOnlyWhitespace(name)) {
331uint_fast64_t PnmlParser::traverseMultiplicity(xercesc::DOMNode
const*
const node) {
332 uint_fast64_t result = defaultMultiplicity;
333 for (uint_fast64_t i = 0;
i < node->getChildNodes()->getLength(); ++
i) {
334 auto child = node->getChildNodes()->item(i);
335 auto name = storm::adapters::getName(child);
336 if (name ==
"value") {
337 auto value = storm::adapters::getName(child->getFirstChild());
338 if (value.find(
"Default,") == 0) {
339 value = value.substr(std::string(
"Default,").length());
341 result = std::stoull(value);
342 }
else if (name ==
"graphics") {
344 }
else if (isOnlyWhitespace(name)) {
349 STORM_LOG_WARN(
"unknown child (node=inscription): " + name +
"\n");
355std::string PnmlParser::traverseTransitionValue(xercesc::DOMNode
const*
const node) {
357 for (uint_fast64_t i = 0;
i < node->getChildNodes()->getLength(); ++
i) {
358 auto child = node->getChildNodes()->item(i);
359 auto name = storm::adapters::getName(child);
360 if (name ==
"value") {
361 result = storm::adapters::getName(child->getFirstChild());
362 }
else if (isOnlyWhitespace(name)) {
373bool PnmlParser::traverseTransitionType(xercesc::DOMNode
const*
const node) {
375 for (uint_fast64_t i = 0;
i < node->getChildNodes()->getLength(); ++
i) {
376 auto child = node->getChildNodes()->item(i);
377 auto name = storm::adapters::getName(child);
378 if (name ==
"value") {
379 result = storm::adapters::getName(child->getFirstChild()) ==
"true";
380 }
else if (isOnlyWhitespace(name)) {
391std::string PnmlParser::traverseArcType(xercesc::DOMNode
const*
const node) {
392 for (uint_fast64_t i = 0;
i < node->getAttributes()->getLength(); ++
i) {
393 auto attr = node->getAttributes()->item(i);
394 auto name = storm::adapters::getName(attr);
395 if (name ==
"value") {
396 return storm::adapters::XMLtoString(attr->getNodeValue());
403 return defaultArcType;
406uint_fast64_t PnmlParser::traversePriority(xercesc::DOMNode
const*
const node) {
407 uint_fast64_t result = defaultPriority;
408 for (uint_fast64_t i = 0;
i < node->getChildNodes()->getLength(); ++
i) {
409 auto child = node->getChildNodes()->item(i);
410 auto name = storm::adapters::getName(child);
411 if (name ==
"text") {
412 result = std::stoull(storm::adapters::getName(child->getFirstChild()));
413 }
else if (name ==
"value") {
414 auto value = storm::adapters::getName(child->getFirstChild());
415 value = value.substr(std::string(
"Default,").length());
416 result = std::stoull(value);
417 }
else if (isOnlyWhitespace(name)) {
419 }
else if (name ==
"graphics") {
#define STORM_LOG_WARN(message)
#define STORM_LOG_THROW(cond, exception, message)
Contains all file parsers and helper classes.