Browse Source

First compiling version of PRCTL parser

tempestpy_adaptions
Lanchid 12 years ago
parent
commit
d23b3dbee5
  1. 19
      src/formula/BoundOperator.h
  2. 2
      src/formula/Formulas.h
  3. 1
      src/formula/PctlPathFormula.h
  4. 1
      src/formula/PctlStateFormula.h
  5. 25
      src/formula/ProbabilisticBoundOperator.h
  6. 24
      src/formula/RewardBoundOperator.h
  7. 4
      src/models/GraphTransitions.h
  8. 80
      src/parser/PrctlParser.cpp

19
src/formula/BoundOperator.h

@ -46,8 +46,8 @@ public:
/*! /*!
* Constructor * Constructor
* *
* @param lowerBound The lower bound for the probability
* @param upperBound The upper bound for the probability
* @param comparisonOperator The relation for the bound.
* @param bound The bound for the probability
* @param pathFormula The child node * @param pathFormula The child node
*/ */
BoundOperator(ComparisonType comparisonOperator, T bound, PctlPathFormula<T>* pathFormula) BoundOperator(ComparisonType comparisonOperator, T bound, PctlPathFormula<T>* pathFormula)
@ -83,6 +83,17 @@ public:
this->pathFormula = pathFormula; this->pathFormula = pathFormula;
} }
/*!
* @returns the comparison relation
*/
const ComparisonType getComparisonOperator() const {
return comparisonOperator;
}
void setComparisonOperator(ComparisonType comparisonOperator) {
this->comparisonOperator = comparisonOperator;
}
/*! /*!
* @returns the bound for the measure * @returns the bound for the measure
*/ */
@ -103,7 +114,7 @@ public:
* @returns a string representation of the formula * @returns a string representation of the formula
*/ */
virtual std::string toString() const { virtual std::string toString() const {
std::string result = "P ";
std::string result = "";
switch (comparisonOperator) { switch (comparisonOperator) {
case LESS: result += "<"; break; case LESS: result += "<"; break;
case LESS_EQUAL: result += "<="; break; case LESS_EQUAL: result += "<="; break;
@ -117,7 +128,7 @@ public:
return result; return result;
} }
bool meetsBound(T value) {
bool meetsBound(T value) const {
switch (comparisonOperator) { switch (comparisonOperator) {
case LESS: return value < bound; break; case LESS: return value < bound; break;
case LESS_EQUAL: return value <= bound; break; case LESS_EQUAL: return value <= bound; break;

2
src/formula/Formulas.h

@ -8,8 +8,6 @@
#ifndef STORM_FORMULA_FORMULAS_H_ #ifndef STORM_FORMULA_FORMULAS_H_
#define STORM_FORMULA_FORMULAS_H_ #define STORM_FORMULA_FORMULAS_H_
#include "modelChecker/ForwardDeclarations.h"
#include "And.h" #include "And.h"
#include "Ap.h" #include "Ap.h"
#include "BoundedUntil.h" #include "BoundedUntil.h"

1
src/formula/PctlPathFormula.h

@ -8,6 +8,7 @@
#ifndef STORM_FORMULA_PCTLPATHFORMULA_H_ #ifndef STORM_FORMULA_PCTLPATHFORMULA_H_
#define STORM_FORMULA_PCTLPATHFORMULA_H_ #define STORM_FORMULA_PCTLPATHFORMULA_H_
#include "modelChecker/ForwardDeclarations.h"
#include "PctlFormula.h" #include "PctlFormula.h"
#include <vector> #include <vector>

1
src/formula/PctlStateFormula.h

@ -8,6 +8,7 @@
#ifndef STORM_FORMULA_PCTLSTATEFORMULA_H_ #ifndef STORM_FORMULA_PCTLSTATEFORMULA_H_
#define STORM_FORMULA_PCTLSTATEFORMULA_H_ #define STORM_FORMULA_PCTLSTATEFORMULA_H_
#include "modelChecker/ForwardDeclarations.h"
#include "PctlFormula.h" #include "PctlFormula.h"
#include "storage/BitVector.h" #include "storage/BitVector.h"

25
src/formula/ProbabilisticBoundOperator.h

@ -45,18 +45,21 @@ public:
/*! /*!
* Empty constructor * Empty constructor
*/ */
ProbabilisticBoundOperator() : BoundOperator<T>(storm::utility::constGetZero<T>(), storm::utility::constGetZero<T>(), nullptr) {
ProbabilisticBoundOperator() : BoundOperator<T>
(BoundOperator<T>::LESS_EQUAL, storm::utility::constGetZero<T>(), nullptr) {
// Intentionally left empty // Intentionally left empty
} }
/*! /*!
* Constructor * Constructor
* *
* @param lowerBound The lower bound for the probability
* @param upperBound The upper bound for the probability
* @param comparisonRelation The relation to compare the actual value and the bound
* @param bound The bound for the probability
* @param pathFormula The child node * @param pathFormula The child node
*/ */
ProbabilisticBoundOperator(T lowerBound, T upperBound, PctlPathFormula<T>& pathFormula) : BoundOperator<T>(lowerBound, upperBound, pathFormula) {
ProbabilisticBoundOperator(
typename BoundOperator<T>::ComparisonType comparisonRelation, T bound, PctlPathFormula<T>* pathFormula) :
BoundOperator<T>(BoundOperator<T>::LESS, bound, pathFormula) {
// Intentionally left empty // Intentionally left empty
} }
@ -64,13 +67,8 @@ public:
* @returns a string representation of the formula * @returns a string representation of the formula
*/ */
virtual std::string toString() const { virtual std::string toString() const {
std::string result = "P [";
result += std::to_string(this->getLowerBound());
result += ",";
result += std::to_string(this->getUpperBound());
result += "] (";
result += this->getPathFormula()->toString();
result += ")";
std::string result = "P";
result += BoundOperator<T>::toString();
return result; return result;
} }
@ -83,8 +81,9 @@ public:
*/ */
virtual PctlStateFormula<T>* clone() const { virtual PctlStateFormula<T>* clone() const {
ProbabilisticBoundOperator<T>* result = new ProbabilisticBoundOperator<T>(); ProbabilisticBoundOperator<T>* result = new ProbabilisticBoundOperator<T>();
result->setInterval(this->getLowerBound(), this->getUpperBound());
result->setPathFormula(this->getPathFormula()->clone());
result->setComparisonOperator(this->getComparisonOperator());
result->setBound(this->getBound());
result->setPathFormula(this->getPathFormula().clone());
return result; return result;
} }
}; };

24
src/formula/RewardBoundOperator.h

@ -44,18 +44,20 @@ public:
/*! /*!
* Empty constructor * Empty constructor
*/ */
RewardBoundOperator() : BoundOperator<T>(storm::utility::constGetZero<T>(), storm::utility::constGetZero<T>(), nullptr) {
RewardBoundOperator() : BoundOperator<T>(BoundOperator<T>::LESS_EQUAL, storm::utility::constGetZero<T>(), nullptr) {
// Intentionally left empty // Intentionally left empty
} }
/*! /*!
* Constructor * Constructor
* *
* @param lowerBound The lower bound for the probability
* @param upperBound The upper bound for the probability
* @param comparisonRelation The relation to compare the actual value and the bound
* @param bound The bound for the probability
* @param pathFormula The child node * @param pathFormula The child node
*/ */
RewardBoundOperator(T lowerBound, T upperBound, PctlPathFormula<T>& pathFormula) : BoundOperator<T>(lowerBound, upperBound, pathFormula) {
RewardBoundOperator(
typename BoundOperator<T>::ComparisonType comparisonRelation, T bound, PctlPathFormula<T>* pathFormula) :
BoundOperator<T>(BoundOperator<T>::LESS, bound, pathFormula) {
// Intentionally left empty // Intentionally left empty
} }
@ -63,13 +65,8 @@ public:
* @returns a string representation of the formula * @returns a string representation of the formula
*/ */
virtual std::string toString() const { virtual std::string toString() const {
std::string result = "R [";
result += std::to_string(this->getLowerBound());
result += ", ";
result += std::to_string(this->getUpperBound());
result += "] [";
result += this->getPathFormula()->toString();
result += "]";
std::string result = "R ";
result += BoundOperator<T>::toString();
return result; return result;
} }
@ -82,8 +79,9 @@ public:
*/ */
virtual PctlStateFormula<T>* clone() const { virtual PctlStateFormula<T>* clone() const {
RewardBoundOperator<T>* result = new RewardBoundOperator<T>(); RewardBoundOperator<T>* result = new RewardBoundOperator<T>();
result->setBound(this->getLowerBound(), this->getUpperBound());
result->setPathFormula(this->getPathFormula()->clone());
result->setComparisonOperator(this->getComparisonOperator());
result->setBound(this->getBound());
result->setPathFormula(this->getPathFormula().clone());
return result; return result;
} }
}; };

4
src/models/GraphTransitions.h

@ -110,8 +110,8 @@ private:
* matrix. * matrix.
*/ */
void initializeBackward(std::shared_ptr<storm::storage::SparseMatrix<T>> transitionMatrix) { void initializeBackward(std::shared_ptr<storm::storage::SparseMatrix<T>> transitionMatrix) {
this->successorList = new uint_fast64_t[numberOfNonZeroTransitions]();
this->stateIndications = new uint_fast64_t[numberOfStates + 1]();
this->successorList = new uint_fast64_t[numberOfNonZeroTransitions];
this->stateIndications = new uint_fast64_t[numberOfStates + 1];
// First, we need to count how many backward transitions each state has. // First, we need to count how many backward transitions each state has.
// NOTE: We disregard the diagonal here, as we only consider "true" // NOTE: We disregard the diagonal here, as we only consider "true"

80
src/parser/PrctlParser.cpp

@ -20,9 +20,8 @@
#include <iomanip> #include <iomanip>
// Some typedefs and namespace definitions to reduce code size. // Some typedefs and namespace definitions to reduce code size.
typedef std::istreambuf_iterator<char> base_iterator_type;
typedef boost::spirit::multi_pass<base_iterator_type> forward_iterator_type;
typedef boost::spirit::classic::position_iterator2<forward_iterator_type> pos_iterator_type;
typedef std::string::const_iterator BaseIteratorType;
typedef boost::spirit::classic::position_iterator2<BaseIteratorType> PositionIteratorType;
namespace qi = boost::spirit::qi; namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix; namespace phoenix = boost::phoenix;
@ -36,54 +35,47 @@ struct PrctlParser::PrctlGrammar : qi::grammar<Iterator, storm::formula::PctlFor
freeIdentifierName = qi::lexeme[(qi::alpha | qi::char_('_'))]; freeIdentifierName = qi::lexeme[(qi::alpha | qi::char_('_'))];
//This block defines rules for parsing state formulas //This block defines rules for parsing state formulas
stateFormula = (andFormula | atomicProposition | orFormula | notFormula | probabilisticBoundOperator | rewardBoundOperator);
andFormula = ("(" << stateFormula << "&" << stateFormula << ")")[qi::_val =
phoenix::new_<storm::formula::And<double>>(qi::_1, qi::_2)];
orFormula = ('(' << stateFormula << '|' << stateFormula << ')')[qi::_val =
stateFormula %= (andFormula | atomicProposition | orFormula | notFormula | probabilisticBoundOperator | rewardBoundOperator);
andFormula = (qi::lit("(") >> stateFormula >> qi::lit("&") >> stateFormula >> qi::lit(")"))[
qi::_val = phoenix::new_<storm::formula::And<double>>(qi::_1, qi::_2)];
orFormula = (qi::lit("(") >> stateFormula >> '|' >> stateFormula >> ')')[qi::_val =
phoenix::new_<storm::formula::Or<double>>(qi::_1, qi::_2)]; phoenix::new_<storm::formula::Or<double>>(qi::_1, qi::_2)];
notFormula = ('!' << stateFormula)[qi::_val =
notFormula = ('!' >> stateFormula)[qi::_val =
phoenix::new_<storm::formula::Not<double>>(qi::_1)]; phoenix::new_<storm::formula::Not<double>>(qi::_1)];
atomicProposition = (freeIdentifierName)[qi::_val = atomicProposition = (freeIdentifierName)[qi::_val =
phoenix::new_<storm::formula::Ap<double>>(qi::_1)]; phoenix::new_<storm::formula::Ap<double>>(qi::_1)];
probabilisticBoundOperator = ( probabilisticBoundOperator = (
("P=" << qi::double_ << '[' << pathFormula << ']') [qi::_val =
phoenix::new_<storm::formula::ProbabilisticBoundOperator<double>>(qi::_1, qi::_1, qi::_2)] |
("P[" << qi::double_ << qi::double_ << ']' << '[' << pathFormula << ']')[qi::_val =
phoenix::new_<storm::formula::ProbabilisticBoundOperator<double> >(qi::_1, qi::_2, qi::_3)] |
("P>=" << qi::double_ << '[' << pathFormula << ']')[qi::_val =
phoenix::new_<storm::formula::ProbabilisticBoundOperator<double> >(qi::_1, 1, qi::_2)] |
("P<=" << qi::double_ << '[' << pathFormula << ']')[qi::_val =
phoenix::new_<storm::formula::PctlStateFormula<double> >(0, qi::_1, qi::_2)]
("P>=" >> qi::double_ >> '[' >> pathFormula >> ']')[qi::_val =
phoenix::new_<storm::formula::ProbabilisticBoundOperator<double> >(storm::formula::BoundOperator<double>::GREATER_EQUAL, qi::_1, qi::_2)] |
("P<=" >> qi::double_ >> '[' >> pathFormula >> ']')[qi::_val =
phoenix::new_<storm::formula::ProbabilisticBoundOperator<double> >(storm::formula::BoundOperator<double>::LESS_EQUAL, qi::_1, qi::_2)]
); );
rewardBoundOperator = ( rewardBoundOperator = (
("R=" << qi::double_ << '[' << pathFormula << ']') [qi::_val =
phoenix::new_<storm::formula::RewardBoundOperator<double> >(qi::_1, qi::_1, qi::_2)] |
("R[" << qi::double_ << qi::double_ << ']' << '[' << pathFormula << ']')[qi::_val =
phoenix::new_<storm::formula::RewardBoundOperator<double> >(qi::_1, qi::_2, qi::_3)] |
("R>=" << qi::double_ << '[' << pathFormula << ']')[qi::_val =
phoenix::new_<storm::formula::RewardBoundOperator<double> >(qi::_1, storm::utility::constGetInfinity<double>(), qi::_2)] |
("R<=" << qi::double_ << '[' << pathFormula << ']')[qi::_val =
phoenix::new_<storm::formula::RewardBoundOperator<double> >(0, qi::_1, qi::_2)]
("R>=" >> qi::double_ >> '[' >> pathFormula >> ']')[qi::_val =
phoenix::new_<storm::formula::RewardBoundOperator<double> >(storm::formula::BoundOperator<double>::GREATER_EQUAL, qi::_1, qi::_2)] |
("R<=" >> qi::double_ >> '[' >> pathFormula >> ']')[qi::_val =
phoenix::new_<storm::formula::RewardBoundOperator<double> >(storm::formula::BoundOperator<double>::LESS_EQUAL, qi::_1, qi::_2)]
); );
//This block defines rules for parsing formulas with noBoundOperators //This block defines rules for parsing formulas with noBoundOperators
noBoundOperator %= (probabilisticNoBoundOperator | rewardNoBoundOperator); noBoundOperator %= (probabilisticNoBoundOperator | rewardNoBoundOperator);
probabilisticNoBoundOperator = ("P=?[" << pathFormula << ']')[qi::_val =
probabilisticNoBoundOperator = ("P=?[" >> pathFormula >> ']')[qi::_val =
phoenix::new_<storm::formula::ProbabilisticNoBoundOperator<double> >(qi::_1)]; phoenix::new_<storm::formula::ProbabilisticNoBoundOperator<double> >(qi::_1)];
rewardNoBoundOperator = ("R=?[" << pathFormula << ']')[qi::_val =
rewardNoBoundOperator = ("R=?[" >> pathFormula >> ']')[qi::_val =
phoenix::new_<storm::formula::RewardNoBoundOperator<double> >(qi::_1)]; phoenix::new_<storm::formula::RewardNoBoundOperator<double> >(qi::_1)];
//This block defines rules for parsing path formulas //This block defines rules for parsing path formulas
pathFormula %= (eventually | boundedEventually | globally | boundedUntil | until); pathFormula %= (eventually | boundedEventually | globally | boundedUntil | until);
eventually = ('F' << pathFormula)[qi::_val =
eventually = ('F' >> stateFormula)[qi::_val =
phoenix::new_<storm::formula::Eventually<double> >(qi::_1)]; phoenix::new_<storm::formula::Eventually<double> >(qi::_1)];
boundedEventually = ("F<=" << qi::double_ << pathFormula)[qi::_val =
boundedEventually = ("F<=" >> qi::int_ >> stateFormula)[qi::_val =
phoenix::new_<storm::formula::BoundedEventually<double>>(qi::_2, qi::_1)]; phoenix::new_<storm::formula::BoundedEventually<double>>(qi::_2, qi::_1)];
globally = ('G' << pathFormula)[qi::_val =
globally = ('G' >> stateFormula)[qi::_val =
phoenix::new_<storm::formula::Globally<double> >(qi::_1)]; phoenix::new_<storm::formula::Globally<double> >(qi::_1)];
until = (stateFormula << 'U' << stateFormula)[qi::_val =
until = (stateFormula >> 'U' >> stateFormula)[qi::_val =
phoenix::new_<storm::formula::Until<double>>(qi::_1, qi::_2)]; phoenix::new_<storm::formula::Until<double>>(qi::_1, qi::_2)];
boundedUntil = (stateFormula << "U<=" << qi::double_ << stateFormula)[qi::_val =
boundedUntil = (stateFormula >> "U<=" >> qi::int_ >> stateFormula)[qi::_val =
phoenix::new_<storm::formula::BoundedUntil<double>>(qi::_1, qi::_3, qi::_2)]; phoenix::new_<storm::formula::BoundedUntil<double>>(qi::_1, qi::_3, qi::_2)];
start %= (stateFormula | noBoundOperator); start %= (stateFormula | noBoundOperator);
@ -93,7 +85,7 @@ struct PrctlParser::PrctlGrammar : qi::grammar<Iterator, storm::formula::PctlFor
qi::rule<Iterator, storm::formula::PctlStateFormula<double>*(), Skipper> stateFormula; qi::rule<Iterator, storm::formula::PctlStateFormula<double>*(), Skipper> stateFormula;
qi::rule<Iterator, storm::formula::And<double>*(), Skipper> andFormula; qi::rule<Iterator, storm::formula::And<double>*(), Skipper> andFormula;
qi::rule<Iterator, storm::formula::PctlStateFormula<double>*(), Skipper> atomicProposition;
qi::rule<Iterator, storm::formula::Ap<double>*(), Skipper> atomicProposition;
qi::rule<Iterator, storm::formula::PctlStateFormula<double>*(), Skipper> orFormula; qi::rule<Iterator, storm::formula::PctlStateFormula<double>*(), Skipper> orFormula;
qi::rule<Iterator, storm::formula::PctlStateFormula<double>*(), Skipper> notFormula; qi::rule<Iterator, storm::formula::PctlStateFormula<double>*(), Skipper> notFormula;
qi::rule<Iterator, storm::formula::PctlStateFormula<double>*(), Skipper> probabilisticBoundOperator; qi::rule<Iterator, storm::formula::PctlStateFormula<double>*(), Skipper> probabilisticBoundOperator;
@ -117,24 +109,28 @@ struct PrctlParser::PrctlGrammar : qi::grammar<Iterator, storm::formula::PctlFor
} //namespace storm } //namespace storm
} //namespace parser } //namespace parser
storm::parser::PrctlParser::PrctlParser(std::string filename)
{
storm::parser::PrctlParser::PrctlParser(std::string filename) {
// Open file and initialize result. // Open file and initialize result.
std::ifstream inputFileStream(filename, std::ios::in); std::ifstream inputFileStream(filename, std::ios::in);
// Prepare iterators to input. // Prepare iterators to input.
base_iterator_type in_begin(inputFileStream);
forward_iterator_type fwd_begin = boost::spirit::make_default_multi_pass(in_begin);
forward_iterator_type fwd_end;
pos_iterator_type position_begin(fwd_begin, fwd_end, filename);
pos_iterator_type position_end;
// TODO: Right now, this parses the whole contents of the file into a string first.
// While this is usually not necessary, because there exist adapters that make an input stream
// iterable in both directions without storing it into a string, using the corresponding
// Boost classes gives an awful output under valgrind and is thus disabled for the time being.
std::string fileContent((std::istreambuf_iterator<char>(inputFileStream)), (std::istreambuf_iterator<char>()));
BaseIteratorType stringIteratorBegin = fileContent.begin();
BaseIteratorType stringIteratorEnd = fileContent.end();
PositionIteratorType positionIteratorBegin(stringIteratorBegin, stringIteratorEnd, filename);
PositionIteratorType positionIteratorEnd;
// Prepare resulting intermediate representation of input. // Prepare resulting intermediate representation of input.
storm::formula::PctlFormula<double>* result_pointer;
storm::formula::PctlFormula<double>* result_pointer = nullptr;
PrctlGrammar<pos_iterator_type, BOOST_TYPEOF(boost::spirit::ascii::space)> grammar;
PrctlGrammar<PositionIteratorType, BOOST_TYPEOF(boost::spirit::ascii::space)> grammar;
qi::phrase_parse(position_begin, position_end, grammar, boost::spirit::ascii::space, result_pointer);
qi::phrase_parse(positionIteratorBegin, positionIteratorEnd, grammar, boost::spirit::ascii::space, result_pointer);
} }
Loading…
Cancel
Save