3#include <boost/algorithm/string.hpp>
20template<
typename ValueType>
26 const std::regex commentRegex(
"(/\\*([^*]|(\\*+[^*/]))*\\*+/)|(//.*)");
33 std::string toplevelId =
"";
41 size_t commentEnd = line.find(
"*/");
42 if (commentEnd == std::string::npos) {
46 line = line.substr(commentEnd + 2);
51 line = std::regex_replace(line, commentRegex,
"");
53 size_t commentStart = line.find(
"/*");
54 if (commentStart != std::string::npos) {
56 line = line.substr(0, commentStart);
67 STORM_LOG_THROW(line.back() ==
';', storm::exceptions::WrongFormatException,
"Semicolon expected at the end of line " << lineNo <<
".");
72 std::vector<std::string> tokens;
73 boost::split(tokens, line, boost::is_any_of(
" \t"), boost::token_compress_on);
76 if (tokens[0] ==
"toplevel") {
78 STORM_LOG_THROW(toplevelId.empty(), storm::exceptions::WrongFormatException,
"Toplevel element already defined.");
79 STORM_LOG_THROW(tokens.size() == 2, storm::exceptions::WrongFormatException,
"Expected unique element id after 'toplevel'.");
81 }
else if (tokens[0] ==
"param") {
83 STORM_LOG_THROW(tokens.size() == 2, storm::exceptions::WrongFormatException,
"Expected unique parameter name after 'param'.");
84 STORM_LOG_THROW((std::is_same<ValueType, storm::RationalFunction>::value), storm::exceptions::NotSupportedException,
85 "Parameters are only allowed when using rational functions.");
91 std::vector<std::string> childNames;
92 for (
size_t i = 2; i < tokens.size(); ++i) {
93 childNames.push_back(
parseName(tokens[i]));
97 std::string type = tokens[1];
99 builder.addAndGate(name, childNames);
100 }
else if (type ==
"or") {
101 builder.addOrGate(name, childNames);
102 }
else if (boost::starts_with(type,
"vot")) {
104 builder.addVotingGate(name, threshold, childNames);
105 }
else if (type.find(
"of") != std::string::npos) {
106 size_t pos = type.find(
"of");
109 STORM_LOG_THROW(count == childNames.size(), storm::exceptions::WrongFormatException,
110 "Voting gate number " << count <<
" does not correspond to number of children " << childNames.size() <<
".");
111 builder.addVotingGate(name, threshold, childNames);
112 }
else if (type ==
"pand") {
113 builder.addPandGate(name, childNames);
114 }
else if (type ==
"pand-incl" || type ==
"pand<=") {
115 builder.addPandGate(name, childNames,
true);
116 }
else if (type ==
"pand-excl" || type ==
"pand<") {
117 builder.addPandGate(name, childNames,
false);
118 }
else if (type ==
"por") {
119 builder.addPorGate(name, childNames);
120 }
else if (type ==
"por-incl" || type ==
"por<=") {
121 builder.addPorGate(name, childNames,
true);
122 }
else if (type ==
"por-excl" || type ==
"por<") {
123 builder.addPorGate(name, childNames,
false);
124 }
else if (type ==
"wsp" || type ==
"csp" || type ==
"hsp" || type ==
"spare") {
125 builder.addSpareGate(name, childNames);
126 }
else if (type ==
"seq") {
127 builder.addSequenceEnforcer(name, childNames);
128 }
else if (type ==
"mutex") {
129 builder.addMutex(name, childNames);
130 }
else if (type ==
"fdep") {
132 }
else if (boost::starts_with(type,
"pdep=")) {
133 ValueType probability = valueParser.
parseValue(type.substr(5));
134 builder.addPdep(name, childNames, probability);
135 }
else if (type.find(
"=") != std::string::npos) {
138 std::regex regexName(
"\"?" + tokens[0] +
"\"?");
139 std::string remaining_line = std::regex_replace(line, regexName,
"");
140 parseBasicElement(name, remaining_line,
builder, valueParser);
141 }
else if (type.find(
"insp") != std::string::npos) {
143 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"Inspections are not supported.");
145 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"Type name '" << type <<
"' not recognized.");
150 STORM_LOG_THROW(
false, storm::exceptions::FileIoException,
"A parsing exception occurred in line " << lineNo <<
": " << exception.
what() <<
".");
152 builder.setTopLevel(toplevelId);
159template<
typename ValueType>
161 size_t firstQuots = name.find(
"\"");
162 if (firstQuots != std::string::npos) {
164 size_t secondQuots = name.find(
"\"", firstQuots + 1);
165 STORM_LOG_THROW(secondQuots != std::string::npos, storm::exceptions::WrongFormatException,
"No ending quotation mark found in " << name <<
".");
166 return name.substr(firstQuots + 1, secondQuots - 1);
172template<
typename ValueType>
173std::string DFTGalileoParser<ValueType>::parseValue(std::string name, std::string& line) {
175 std::regex nameRegex(name +
"\\s*=\\s*([^\\s]*)");
177 if (std::regex_search(line, match, nameRegex)) {
179 std::string value = match.str(1);
180 line = std::regex_replace(line, nameRegex,
"");
188template<
typename ValueType>
195 std::optional<BEType> distribution;
196 std::optional<ValueType> prob;
197 std::optional<ValueType> lambda;
198 std::optional<size_t> phases;
199 std::optional<ValueType> shape;
200 std::optional<ValueType> rate;
201 std::optional<ValueType> mean;
202 std::optional<ValueType> stddev;
203 std::optional<ValueType> dorm;
207 std::string value = parseValue(
"prob", input);
208 if (!value.empty()) {
215 if (!value.empty()) {
217 !distribution.has_value(), storm::exceptions::WrongFormatException,
218 "Two distributions " <<
toString(distribution.value()) <<
" and " <<
toString(BEType::EXPONENTIAL) <<
" are defined for BE '" << name <<
"'.");
220 distribution = BEType::EXPONENTIAL;
226 if (!value.empty()) {
228 !distribution.has_value() || distribution.value() == BEType::EXPONENTIAL, storm::exceptions::WrongFormatException,
229 "Two distributions " <<
toString(distribution.value()) <<
" and " <<
toString(BEType::ERLANG) <<
" are defined for BE '" << name <<
"'.");
231 distribution = BEType::ERLANG;
236 if (!value.empty()) {
238 !distribution.has_value(), storm::exceptions::WrongFormatException,
239 "Two distributions " <<
toString(distribution.value()) <<
" and " <<
toString(BEType::WEIBULL) <<
" are defined for BE '" << name <<
"'.");
241 distribution = BEType::WEIBULL;
244 if (!value.empty()) {
246 !distribution.has_value() || distribution.value() == BEType::WEIBULL, storm::exceptions::WrongFormatException,
247 "Two distributions " <<
toString(distribution.value()) <<
" and " <<
toString(BEType::WEIBULL) <<
" are defined for BE '" << name <<
"'.");
253 if (!value.empty()) {
255 !distribution.has_value(), storm::exceptions::WrongFormatException,
256 "Two distributions " <<
toString(distribution.value()) <<
" and " <<
toString(BEType::LOGNORMAL) <<
" are defined for BE '" << name <<
"'.");
258 distribution = BEType::LOGNORMAL;
261 if (!value.empty()) {
263 !distribution.has_value() || distribution.value() == BEType::LOGNORMAL, storm::exceptions::WrongFormatException,
264 "Two distributions " <<
toString(distribution.value()) <<
" and " <<
toString(BEType::LOGNORMAL) <<
" are defined for BE '" << name <<
"'.");
270 if (!value.empty()) {
276 if (!value.empty()) {
277 STORM_LOG_WARN(
"Coverage is not supported and will be ignored for basic element '" << name <<
"'.");
280 if (!value.empty()) {
281 STORM_LOG_WARN(
"Restoration is not supported and will be ignored for basic element '" << name <<
"'.");
284 if (!value.empty()) {
286 STORM_LOG_THROW(replication == 1, storm::exceptions::NotSupportedException,
"Replication > 1 is not supported for basic element '" << name <<
"'.");
289 if (!value.empty()) {
290 STORM_LOG_WARN(
"Interval is not supported and will be ignored for basic element '" << name <<
"'.");
293 STORM_LOG_THROW(value.empty(), storm::exceptions::NotSupportedException,
294 "Repairs are not supported and will be ignored for basic element '" << name <<
"'.");
297 STORM_LOG_THROW(input ==
"", storm::exceptions::WrongFormatException,
"Unknown arguments for basic element '" << name <<
"': " << input <<
".");
300 STORM_LOG_THROW(distribution.has_value(), storm::exceptions::WrongFormatException,
"No failure distribution is defined for BE '" << name <<
"'.");
301 switch (distribution.value()) {
302 case BEType::PROBABILITY:
303 STORM_LOG_THROW(prob.has_value(), storm::exceptions::WrongFormatException,
304 "Distribution " <<
toString(BEType::PROBABILITY) <<
" requires parameter 'prob' for BE '" << name <<
"'.");
305 if (!dorm.has_value()) {
306 STORM_LOG_WARN(
"No dormancy factor was provided for basic element '" << name <<
"'. Assuming dormancy factor of 1.");
311 case BEType::EXPONENTIAL:
312 STORM_LOG_THROW(lambda.has_value(), storm::exceptions::WrongFormatException,
313 "Distribution " <<
toString(BEType::EXPONENTIAL) <<
" requires parameter 'lambda' for BE '" << name <<
"'.");
314 if (!dorm.has_value()) {
315 STORM_LOG_WARN(
"No dormancy factor was provided for basic element '" << name <<
"'. Assuming dormancy factor of 1.");
321 STORM_LOG_THROW(lambda.has_value(), storm::exceptions::WrongFormatException,
322 "Distribution " <<
toString(BEType::ERLANG) <<
" requires parameter 'lambda' for BE '" << name <<
"'.");
323 STORM_LOG_THROW(phases.has_value(), storm::exceptions::WrongFormatException,
324 "Distribution " <<
toString(BEType::ERLANG) <<
" requires parameter 'phases' for BE '" << name <<
"'.");
325 if (!dorm.has_value()) {
326 STORM_LOG_WARN(
"No dormancy factor was provided for basic element '" << name <<
"'. Assuming dormancy factor of 1.");
331 case BEType::WEIBULL:
332 STORM_LOG_THROW(shape.has_value(), storm::exceptions::WrongFormatException,
333 "Distribution " <<
toString(BEType::WEIBULL) <<
" requires parameter 'shape' for BE '" << name <<
"'.");
334 STORM_LOG_THROW(rate.has_value(), storm::exceptions::WrongFormatException,
335 "Distribution " <<
toString(BEType::WEIBULL) <<
" requires parameter 'rate' for BE '" << name <<
"'.");
338 case BEType::LOGNORMAL:
339 STORM_LOG_THROW(mean.has_value(), storm::exceptions::WrongFormatException,
340 "Distribution " <<
toString(BEType::LOGNORMAL) <<
" requires parameter 'mean' for BE '" << name <<
"'.");
341 STORM_LOG_THROW(stddev.has_value(), storm::exceptions::WrongFormatException,
342 "Distribution " <<
toString(BEType::WEIBULL) <<
" requires parameter 'stddev' for BE '" << name <<
"'.");
346 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"No distribution defined for basic element '" << name <<
"'.");
void addBasicElementErlang(std::string const &name, ValueType rate, unsigned phases, ValueType dormancyFactor)
Create BE with Erlang distribution and add it to DFT.
void addBasicElementProbability(std::string const &name, ValueType probability, ValueType dormancyFactor)
Create BE with constant (Bernoulli) distribution and add it to DFT.
void addBasicElementExponential(std::string const &name, ValueType rate, ValueType dormancyFactor, bool transient=false)
Create BE with exponential distribution and add it to DFT.
void addBasicElementWeibull(std::string const &name, ValueType shape, ValueType rate)
Create BE with Weibull distribution and add it to DFT.
void addBasicElementLogNormal(std::string const &name, ValueType mean, ValueType standardDeviation)
Create BE with log-normal distribution and add it to DFT.
Parser for DFT in the Galileo format.
static storm::dft::storage::DFT< ValueType > parseDFT(std::string const &filename)
Parse DFT in Galileo format and build DFT.
static std::string parseName(std::string const &name)
Parse element name (strip quotation marks, etc.).
Represents a Dynamic Fault Tree.
This class represents the base class of all exception classes.
virtual const char * what() const noexcept override
Retrieves the message associated with this exception.
Parser for values according to their ValueType.
void addParameter(std::string const ¶meter)
Add declaration of parameter.
ValueType parseValue(std::string const &value) const
Parse ValueType from string.
#define STORM_LOG_WARN(message)
#define STORM_LOG_THROW(cond, exception, message)
std::string toString(DFTElementType const &type)
std::basic_istream< CharT, Traits > & getline(std::basic_istream< CharT, Traits > &input, std::basic_string< CharT, Traits, Allocator > &str)
Overloaded getline function which handles different types of newline ( and \r).
void closeFile(std::ofstream &stream)
Close the given file after writing.
void openFile(std::string const &filepath, std::ofstream &filestream, bool append=false, bool silent=false)
Open the given file for writing.
ValueType parseValue(std::string const &valueStr, std::unordered_map< std::string, ValueType > const &placeholders, ValueParser< ValueType > const &valueParser)
NumberType parseNumber(std::string const &value)
Parse number from string.