diff --git a/src/ir/Module.cpp b/src/ir/Module.cpp index a8998a715..b1a4c975f 100644 --- a/src/ir/Module.cpp +++ b/src/ir/Module.cpp @@ -9,6 +9,7 @@ #include #include "Module.h" +#include "src/parser/prismparser/VariableState.h" #include "src/exceptions/OutOfRangeException.h" #include "src/exceptions/InvalidArgumentException.h" @@ -38,7 +39,7 @@ Module::Module(std::string const& moduleName, this->collectActions(); } -Module::Module(Module const& oldModule, std::string const& newModuleName, std::map const& renaming, std::map const& booleanVariableToIndexMap, std::map const& integerVariableToIndexMap, std::shared_ptr const& variableState) + Module::Module(Module const& oldModule, std::string const& newModuleName, std::map const& renaming, std::map const& booleanVariableToIndexMap, std::map const& integerVariableToIndexMap, parser::prismparser::VariableState const& variableState) : moduleName(newModuleName), booleanVariableToLocalIndexMap(oldModule.booleanVariableToLocalIndexMap), integerVariableToLocalIndexMap(oldModule.integerVariableToLocalIndexMap) { LOG4CPLUS_TRACE(logger, "Start renaming " << oldModule.getName() << " to " << moduleName << "."); @@ -52,7 +53,7 @@ Module::Module(Module const& oldModule, std::string const& newModuleName, std::m throw storm::exceptions::InvalidArgumentException() << "Boolean variable " << moduleName << "." << booleanVariable.getName() << " was not renamed."; } else { this->booleanVariables.emplace_back(booleanVariable, renamingPair->second, variableState->getNextGlobalBooleanVariableIndex(), renaming, booleanVariableToIndexMap, integerVariableToIndexMap); - variableState->addBooleanVariable(renamingPair->second); + variableState.addBooleanVariable(renamingPair->second); } } // Now do the same for the integer variables. @@ -64,7 +65,7 @@ Module::Module(Module const& oldModule, std::string const& newModuleName, std::m throw storm::exceptions::InvalidArgumentException() << "Integer variable " << moduleName << "." << integerVariable.getName() << " was not renamed."; } else { this->integerVariables.emplace_back(integerVariable, renamingPair->second, variableState->getNextGlobalIntegerVariableIndex(), renaming, booleanVariableToIndexMap, integerVariableToIndexMap); - variableState->addIntegerVariable(renamingPair->second); + variableState.addIntegerVariable(renamingPair->second); } } diff --git a/src/ir/Module.h b/src/ir/Module.h index b57b9e70d..89c409607 100644 --- a/src/ir/Module.h +++ b/src/ir/Module.h @@ -14,7 +14,6 @@ #include #include -#include "VariableStateInterface.h" #include "BooleanVariable.h" #include "IntegerVariable.h" #include "Command.h" @@ -22,8 +21,14 @@ namespace storm { +namespace parser { +namespace prismparser { + class VariableState; +} // namespace prismparser +} // namespace parser + namespace ir { - + /*! * A class representing a module. */ @@ -65,7 +70,7 @@ public: * @param adder An instance of the VariableAdder interface that keeps track of all the variables in the probabilistic * program. */ - Module(Module const& oldModule, std::string const& newModuleName, std::map const& renaming, std::map const& booleanVariableToIndexMap, std::map const& integerVariableToIndexMap, std::shared_ptr const& variableState); + Module(Module const& oldModule, std::string const& newModuleName, std::map const& renaming, std::map const& booleanVariableToIndexMap, std::map const& integerVariableToIndexMap, parser::prismparser::VariableState const& variableState); /*! * Retrieves the number of boolean variables in the module. diff --git a/src/ir/expressions/BaseExpression.cpp b/src/ir/expressions/BaseExpression.cpp new file mode 100644 index 000000000..7ffc6ee9f --- /dev/null +++ b/src/ir/expressions/BaseExpression.cpp @@ -0,0 +1,71 @@ +/* + * BaseExpression.cpp + * + * Created on: 10.06.2013 + * Author: Christian Dehnert + */ + +#include "BaseExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + BaseExpression::BaseExpression() : type(undefined) { + // Nothing to do here. + } + + BaseExpression::BaseExpression(ReturnType type) : type(type) { + // Nothing to do here. + } + + BaseExpression::~BaseExpression() { + // Nothing to do here. + } + + int_fast64_t BaseExpression::getValueAsInt(std::pair, std::vector> const* variableValues) const { + if (type != int_) { + throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression of type '" + << this->getTypeName() << "' as 'int'."; + } + throw storm::exceptions::NotImplementedException() << "Cannot evaluate expression of type '" + << this->getTypeName() << " because evaluation implementation is missing."; + } + + bool BaseExpression::getValueAsBool(std::pair, std::vector> const* variableValues) const { + if (type != bool_) { + throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression of type '" + << this->getTypeName() << "' as 'bool'."; + } + throw storm::exceptions::NotImplementedException() << "Cannot evaluate expression of type '" + << this->getTypeName() << " because evaluation implementation is missing."; + } + + double BaseExpression::getValueAsDouble(std::pair, std::vector> const* variableValues) const { + if (type != bool_) { + throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression of type '" + << this->getTypeName() << "' as 'double'."; + } + throw storm::exceptions::NotImplementedException() << "Cannot evaluate expression of type '" + << this->getTypeName() << " because evaluation implementation is missing."; + } + + void BaseExpression::accept(ExpressionVisitor* visitor) { + visitor->visit(this); + } + + std::string BaseExpression::getTypeName() const { + switch(type) { + case bool_: return std::string("bool"); + case int_: return std::string("int"); + case double_: return std::string("double"); + default: return std::string("undefined"); + } + } + + BaseExpression::ReturnType BaseExpression::getType() const { + return type; + } + + } // namespace expressions + } // namespace ir +} // namespace storm diff --git a/src/ir/expressions/BaseExpression.h b/src/ir/expressions/BaseExpression.h index 98f8f2ee2..ef5a0d44c 100644 --- a/src/ir/expressions/BaseExpression.h +++ b/src/ir/expressions/BaseExpression.h @@ -1,5 +1,5 @@ /* - * Expression.h + * BaseExpression.h * * Created on: 03.01.2013 * Author: Christian Dehnert @@ -8,98 +8,135 @@ #ifndef STORM_IR_EXPRESSIONS_BASEEXPRESSION_H_ #define STORM_IR_EXPRESSIONS_BASEEXPRESSION_H_ -#include "src/exceptions/ExpressionEvaluationException.h" -#include "src/exceptions/NotImplementedException.h" - -#include "ExpressionVisitor.h" - #include #include #include #include -namespace storm { -namespace ir { -namespace expressions { - -class BaseExpression { - -public: - enum ReturnType {undefined, bool_, int_, double_}; - - BaseExpression() : type(undefined) { - - } - - BaseExpression(ReturnType type) : type(type) { - - } - BaseExpression(const BaseExpression& be) - : type(be.type) { - } - - virtual ~BaseExpression() { - - } - - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) = 0; - - virtual int_fast64_t getValueAsInt(std::pair, std::vector> const* variableValues) const { - if (type != int_) { - throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression of type '" - << this->getTypeName() << "' as 'int'."; - } - throw storm::exceptions::NotImplementedException() << "Cannot evaluate expression of type '" - << this->getTypeName() << " because evaluation implementation is missing."; - } - - virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const { - if (type != bool_) { - throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression of type '" - << this->getTypeName() << "' as 'bool'."; - } - throw storm::exceptions::NotImplementedException() << "Cannot evaluate expression of type '" - << this->getTypeName() << " because evaluation implementation is missing."; - } - - virtual double getValueAsDouble(std::pair, std::vector> const* variableValues) const { - if (type != bool_) { - throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression of type '" - << this->getTypeName() << "' as 'double'."; - } - throw storm::exceptions::NotImplementedException() << "Cannot evaluate expression of type '" - << this->getTypeName() << " because evaluation implementation is missing."; - } - - virtual void accept(ExpressionVisitor* visitor) { - visitor->visit(this); - } - - virtual std::string toString() const = 0; - - std::string getTypeName() const { - switch(type) { - case bool_: return std::string("bool"); - case int_: return std::string("int"); - case double_: return std::string("double"); - default: return std::string("undefined"); - } - } - - ReturnType getType() const { - return type; - } - - virtual std::string dump(std::string prefix) const = 0; - -private: - ReturnType type; -}; - -} // namespace expressions - -} // namespace ir +#include "src/exceptions/ExpressionEvaluationException.h" +#include "src/exceptions/NotImplementedException.h" +#include "ExpressionVisitor.h" +namespace storm { + + // Forward-declare VariableState. + namespace parser { + namespace prismparser { + class VariableState; + } // namespace prismparser + } // namespace parser + + namespace ir { + namespace expressions { + + /*! + * The base class for all expressions. + */ + class BaseExpression { + + public: + /*! + * Each node in an expression tree has a uniquely defined type from this enum. + */ + enum ReturnType {undefined, bool_, int_, double_}; + + /*! + * Creates an expression with undefined type. + */ + BaseExpression(); + + /*! + * Creates an expression with the given type. + * + * @param type The type of the expression. + */ + BaseExpression(ReturnType type); + + /*! + * Copy-constructs from the given expression. + * + * @param baseExpression The expression to copy. + */ + BaseExpression(BaseExpression const& baseExpression); + + /*! + * Destructor. + */ + virtual ~BaseExpression(); + + /*! + * Copies the expression tree underneath (including) the current node and performs the provided renaming. + * + * @param renaming A mapping from identifier names to strings they are to be replaced with. + * @param variableState An object knowing about the global variable state. + */ + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const = 0; + + /*! + * Retrieves the value of the expression as an integer given the provided variable valuation. + * + * @param variableValues The variable valuation under which to evaluate the expression. If set to null, + * constant expressions can be evaluated without variable values. However, upon encountering a variable + * expression an expression is thrown, because evaluation is impossible without the variable values then. + * @return The value of the expression as an integer. + */ + virtual int_fast64_t getValueAsInt(std::pair, std::vector> const* variableValues) const; + + /*! + * Retrieves the value of the expression as a boolean given the provided variable valuation. + * + * @param variableValues The variable valuation under which to evaluate the expression. If set to null, + * constant expressions can be evaluated without variable values. However, upon encountering a variable + * expression an expression is thrown, because evaluation is impossible without the variable values then. + * @return The value of the expression as a boolean. + */ + virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const; + + /*! + * Retrieves the value of the expression as a double given the provided variable valuation. + * + * @param variableValues The variable valuation under which to evaluate the expression. If set to null, + * constant expressions can be evaluated without variable values. However, upon encountering a variable + * expression an expression is thrown, because evaluation is impossible without the variable values then. + * @return The value of the expression as a double. + */ + virtual double getValueAsDouble(std::pair, std::vector> const* variableValues) const; + + /*! + * Acceptor method for visitor pattern. + * + * @param visitor The visitor that is supposed to visit each node of the expression tree. + */ + virtual void accept(ExpressionVisitor* visitor); + + /*! + * Retrieves a string representation of the expression tree underneath the current node. + * + * @return A string representation of the expression tree underneath the current node. + */ + virtual std::string toString() const = 0; + + /*! + * Retrieves a string representation of the type to which this node evaluates. + * + * @return A string representation of the type to which this node evaluates. + */ + std::string getTypeName() const; + + /*! + * Retrieves the type to which the node evaluates. + * + * @return The type to which the node evaluates. + */ + ReturnType getType() const; + + private: + // The type to which this node evaluates. + ReturnType type; + }; + + } // namespace expressions + } // namespace ir } // namespace storm #endif /* STORM_IR_EXPRESSIONS_BASEEXPRESSION_H_ */ diff --git a/src/ir/expressions/BinaryBooleanFunctionExpression.cpp b/src/ir/expressions/BinaryBooleanFunctionExpression.cpp new file mode 100644 index 000000000..f122f0a95 --- /dev/null +++ b/src/ir/expressions/BinaryBooleanFunctionExpression.cpp @@ -0,0 +1,58 @@ +/* + * BinaryBooleanFunctionExpression.cpp + * + * Created on: 10.06.2013 + * Author: Christian Dehnert + */ + +#include + +#include "BinaryBooleanFunctionExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + BinaryBooleanFunctionExpression::BinaryBooleanFunctionExpression(std::shared_ptr const& left, std::shared_ptr const& right, FunctionType functionType) + : BinaryExpression(bool_, left, right), functionType(functionType) { + // Nothing to do here. + } + + std::shared_ptr BinaryBooleanFunctionExpression::clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { + return std::shared_ptr(new BinaryBooleanFunctionExpression(this->getLeft()->clone(renaming, variableState), this->getRight()->clone(renaming, variableState), this->functionType)); + } + + bool BinaryBooleanFunctionExpression::getValueAsBool(std::pair, std::vector> const* variableValues) const { + bool resultLeft = this->getLeft()->getValueAsBool(variableValues); + bool resultRight = this->getRight()->getValueAsBool(variableValues); + switch(functionType) { + case AND: return resultLeft & resultRight; break; + case OR: return resultLeft | resultRight; break; + default: throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " + << "Unknown boolean binary operator: '" << functionType << "'."; + } + } + + BinaryBooleanFunctionExpression::FunctionType BinaryBooleanFunctionExpression::getFunctionType() const { + return functionType; + } + + void BinaryBooleanFunctionExpression::accept(ExpressionVisitor* visitor) { + visitor->visit(this); + } + + std::string BinaryBooleanFunctionExpression::toString() const { + std::stringstream result; + result << this->getLeft()->toString(); + switch (functionType) { + case AND: result << " & "; break; + case OR: result << " | "; break; + } + result << this->getRight()->toString(); + + return result.str(); + } + + } // namespace expressions + } // namespace ir +} // namespace storm diff --git a/src/ir/expressions/BinaryBooleanFunctionExpression.h b/src/ir/expressions/BinaryBooleanFunctionExpression.h index 4882c0d92..e1b52968c 100644 --- a/src/ir/expressions/BinaryBooleanFunctionExpression.h +++ b/src/ir/expressions/BinaryBooleanFunctionExpression.h @@ -2,7 +2,7 @@ * BinaryBooleanFunctionExpression.h * * Created on: 03.01.2013 - * Author: chris + * Author: Christian Dehnert */ #ifndef STORM_IR_EXPRESSIONS_BINARYBOOLEANFUNCTIONEXPRESSION_H_ @@ -10,82 +10,51 @@ #include "src/ir/expressions/BinaryExpression.h" -#include -#include - namespace storm { - -namespace ir { - -namespace expressions { - -class BinaryBooleanFunctionExpression : public BinaryExpression { -public: - enum FunctionType {AND, OR}; - - BinaryBooleanFunctionExpression(std::shared_ptr left, std::shared_ptr right, FunctionType functionType) : BinaryExpression(bool_, left, right), functionType(functionType) { - - } - - virtual ~BinaryBooleanFunctionExpression() { - - } - - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { - return std::shared_ptr(new BinaryBooleanFunctionExpression(this->getLeft()->clone(renaming, bools, ints), this->getRight()->clone(renaming, bools, ints), this->functionType)); - } - - virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const { - bool resultLeft = this->getLeft()->getValueAsBool(variableValues); - bool resultRight = this->getRight()->getValueAsBool(variableValues); - switch(functionType) { - case AND: return resultLeft & resultRight; break; - case OR: return resultLeft | resultRight; break; - default: throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " - << "Unknown boolean binary operator: '" << functionType << "'."; - } - } - - FunctionType getFunctionType() const { - return functionType; - } - - virtual void accept(ExpressionVisitor* visitor) { - visitor->visit(this); - } - - virtual std::string toString() const { - std::stringstream result; - result << this->getLeft()->toString(); - switch (functionType) { - case AND: result << " & "; break; - case OR: result << " | "; break; - } - result << this->getRight()->toString(); - - return result.str(); - } - - virtual std::string dump(std::string prefix) const { - std::stringstream result; - result << prefix << "BinaryBooleanFunctionExpression" << std::endl; - result << this->getLeft()->dump(prefix + "\t"); - switch (functionType) { - case AND: result << prefix << "&" << std::endl; break; - case OR: result << prefix << "|" << std::endl; break; - } - result << this->getRight()->dump(prefix + "\t"); - return result.str(); - } - -private: - FunctionType functionType; -}; - -} // namespace expressions - -} // namespace ir - + namespace ir { + namespace expressions { + + /*! + * A class representing a binary function expression of boolean type. + */ + class BinaryBooleanFunctionExpression : public BinaryExpression { + public: + /*! + * An enum type specifying the different functions applicable. + */ + enum FunctionType {AND, OR}; + + /*! + * Creates a binary boolean function expression tree node with the given children and function type. + * + * @param left The left child of the node. + * @param right The right child of the node. + * @param functionType The operator that is to be applied to the two children. + */ + BinaryBooleanFunctionExpression(std::shared_ptr const& left, std::shared_ptr const& right, FunctionType functionType); + + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const override; + + virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const override; + + /*! + * Retrieves the operator that is associated with this node. + * + * @param The operator that is associated with this node. + */ + FunctionType getFunctionType() const; + + virtual void accept(ExpressionVisitor* visitor) override; + + virtual std::string toString() const override; + + private: + // The operator that is associated with this node. + FunctionType functionType; + }; + + } // namespace expressions + } // namespace ir } // namespace storm #endif /* STORM_IR_EXPRESSIONS_BINARYBOOLEANFUNCTIONEXPRESSION_H_ */ diff --git a/src/ir/expressions/BinaryExpression.cpp b/src/ir/expressions/BinaryExpression.cpp new file mode 100644 index 000000000..4008c9de8 --- /dev/null +++ b/src/ir/expressions/BinaryExpression.cpp @@ -0,0 +1,29 @@ +/* + * BinaryBooleanFunctionExpression.cpp + * + * Created on: 10.06.2013 + * Author: Christian Dehnert + */ + +#include "BinaryExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + BinaryExpression::BinaryExpression(ReturnType type, std::shared_ptr const& left, std::shared_ptr const& right) + : BaseExpression(type), left(left), right(right) { + // Nothing to do here. + } + + std::shared_ptr const& BinaryExpression::getLeft() const { + return left; + } + + std::shared_ptr const& BinaryExpression::getRight() const { + return right; + } + + } // namespace expressions + } // namespace ir +} // namespace storm \ No newline at end of file diff --git a/src/ir/expressions/BinaryExpression.h b/src/ir/expressions/BinaryExpression.h index 529f6ea90..85b0d18e4 100644 --- a/src/ir/expressions/BinaryExpression.h +++ b/src/ir/expressions/BinaryExpression.h @@ -9,38 +9,48 @@ #define STORM_IR_EXPRESSIONS_BINARYEXPRESSION_H_ #include "src/ir/expressions/BaseExpression.h" -#include -#include namespace storm { - -namespace ir { - -namespace expressions { - -class BinaryExpression : public BaseExpression { -public: - BinaryExpression(ReturnType type, std::shared_ptr left, std::shared_ptr right) - : BaseExpression(type), left(left), right(right) { - } - - std::shared_ptr const& getLeft() const { - return left; - } - - std::shared_ptr const& getRight() const { - return right; - } - -private: - std::shared_ptr left; - std::shared_ptr right; -}; - -} // namespace expressions - -} // namespace ir - + namespace ir { + namespace expressions { + + /*! + * A class representing a generic binary expression. + */ + class BinaryExpression : public BaseExpression { + public: + /*! + * Constructs a binary expression with the given type and children. + * @param type The type of the binary expression. + * @param left The left child of the binary expression. + * @param right The right child of the binary expression. + */ + BinaryExpression(ReturnType type, std::shared_ptr const& left, std::shared_ptr const& right); + + /*! + * Retrieves the left child of the expression node. + * + * @return The left child of the expression node. + */ + std::shared_ptr const& getLeft() const; + + /*! + * Retrieves the right child of the expression node. + * + * @return The right child of the expression node. + */ + std::shared_ptr const& getRight() const; + + private: + // The left child of the binary expression. + std::shared_ptr left; + + // The right child of the binary expression. + std::shared_ptr right; + }; + + } // namespace expressions + } // namespace ir } // namespace storm #endif /* STORM_IR_EXPRESSIONS_BINARYEXPRESSION_H_ */ diff --git a/src/ir/expressions/BinaryNumericalFunctionExpression.cpp b/src/ir/expressions/BinaryNumericalFunctionExpression.cpp new file mode 100644 index 000000000..21052f98c --- /dev/null +++ b/src/ir/expressions/BinaryNumericalFunctionExpression.cpp @@ -0,0 +1,82 @@ +/* + * BinaryBooleanFunctionExpression.cpp + * + * Created on: 10.06.2013 + * Author: Christian Dehnert + */ + +#include + +#include "BinaryNumericalFunctionExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + BinaryNumericalFunctionExpression::BinaryNumericalFunctionExpression(ReturnType type, std::shared_ptr const& left, std::shared_ptr const& right, FunctionType functionType) + : BinaryExpression(type, left, right), functionType(functionType) { + // Nothing to do here. + } + + std::shared_ptr BinaryNumericalFunctionExpression::clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { + return std::shared_ptr(new BinaryNumericalFunctionExpression(this->getType(), this->getLeft()->clone(renaming, variableState), this->getRight()->clone(renaming, variableState), this->functionType)); + } + + BinaryNumericalFunctionExpression::FunctionType BinaryNumericalFunctionExpression::getFunctionType() const { + return functionType; + } + + int_fast64_t BinaryNumericalFunctionExpression::getValueAsInt(std::pair, std::vector> const* variableValues) const { + if (this->getType() != int_) { + BaseExpression::getValueAsInt(variableValues); + } + + int_fast64_t resultLeft = this->getLeft()->getValueAsInt(variableValues); + int_fast64_t resultRight = this->getRight()->getValueAsInt(variableValues); + switch(functionType) { + case PLUS: return resultLeft + resultRight; break; + case MINUS: return resultLeft - resultRight; break; + case TIMES: return resultLeft * resultRight; break; + case DIVIDE: return resultLeft / resultRight; break; + default: throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " + << "Unknown numeric binary operator: '" << functionType << "'."; + } + } + + double BinaryNumericalFunctionExpression::getValueAsDouble(std::pair, std::vector> const* variableValues) const { + if (this->getType() != double_) { + BaseExpression::getValueAsDouble(variableValues); + } + + double resultLeft = this->getLeft()->getValueAsDouble(variableValues); + double resultRight = this->getRight()->getValueAsDouble(variableValues); + switch(functionType) { + case PLUS: return resultLeft + resultRight; break; + case MINUS: return resultLeft - resultRight; break; + case TIMES: return resultLeft * resultRight; break; + case DIVIDE: return resultLeft / resultRight; break; + default: throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " + << "Unknown numeric binary operator: '" << functionType << "'."; + } + } + + void BinaryNumericalFunctionExpression::accept(ExpressionVisitor* visitor) { + visitor->visit(this); + } + + std::string BinaryNumericalFunctionExpression::toString() const { + std::stringstream result; + result << this->getLeft()->toString(); + switch (functionType) { + case PLUS: result << " + "; break; + case MINUS: result << " - "; break; + case TIMES: result << " * "; break; + case DIVIDE: result << " / "; break; + } + result << this->getRight()->toString(); + return result.str(); + } + + } // namespace expressions + } // namespace ir +} // namespace storm \ No newline at end of file diff --git a/src/ir/expressions/BinaryNumericalFunctionExpression.h b/src/ir/expressions/BinaryNumericalFunctionExpression.h index 54239c46a..d30effb8e 100644 --- a/src/ir/expressions/BinaryNumericalFunctionExpression.h +++ b/src/ir/expressions/BinaryNumericalFunctionExpression.h @@ -2,111 +2,62 @@ * BinaryFunctionExpression.h * * Created on: 03.01.2013 - * Author: chris + * Author: Christian Dehnert */ -#ifndef BINARYFUNCTIONEXPRESSION_H_ -#define BINARYFUNCTIONEXPRESSION_H_ +#ifndef STORM_IR_EXPRESSIONS_BINARYFUNCTIONEXPRESSION_H_ +#define STORM_IR_EXPRESSIONS_BINARYFUNCTIONEXPRESSION_H_ -#include "src/ir/expressions/BaseExpression.h" +#include "src/ir/expressions/BinaryExpression.h" namespace storm { - -namespace ir { - -namespace expressions { - -class BinaryNumericalFunctionExpression : public BinaryExpression { -public: - enum FunctionType {PLUS, MINUS, TIMES, DIVIDE}; - - BinaryNumericalFunctionExpression(ReturnType type, std::shared_ptr left, std::shared_ptr right, FunctionType functionType) : BinaryExpression(type, left, right), functionType(functionType) { - - } - - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { - return std::shared_ptr(new BinaryNumericalFunctionExpression(this->getType(), this->getLeft()->clone(renaming, bools, ints), this->getRight()->clone(renaming, bools, ints), this->functionType)); - } - - virtual ~BinaryNumericalFunctionExpression() { - - } - - FunctionType getFunctionType() const { - return functionType; - } - - virtual int_fast64_t getValueAsInt(std::pair, std::vector> const* variableValues) const { - if (this->getType() != int_) { - BaseExpression::getValueAsInt(variableValues); - } - - int_fast64_t resultLeft = this->getLeft()->getValueAsInt(variableValues); - int_fast64_t resultRight = this->getRight()->getValueAsInt(variableValues); - switch(functionType) { - case PLUS: return resultLeft + resultRight; break; - case MINUS: return resultLeft - resultRight; break; - case TIMES: return resultLeft * resultRight; break; - case DIVIDE: return resultLeft / resultRight; break; - default: throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " - << "Unknown numeric binary operator: '" << functionType << "'."; - } - } - - virtual double getValueAsDouble(std::pair, std::vector> const* variableValues) const { - if (this->getType() != double_) { - BaseExpression::getValueAsDouble(variableValues); - } - - double resultLeft = this->getLeft()->getValueAsDouble(variableValues); - double resultRight = this->getRight()->getValueAsDouble(variableValues); - switch(functionType) { - case PLUS: return resultLeft + resultRight; break; - case MINUS: return resultLeft - resultRight; break; - case TIMES: return resultLeft * resultRight; break; - case DIVIDE: return resultLeft / resultRight; break; - default: throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " - << "Unknown numeric binary operator: '" << functionType << "'."; - } - } - - virtual void accept(ExpressionVisitor* visitor) { - visitor->visit(this); - } - - virtual std::string toString() const { - std::string result = this->getLeft()->toString(); - switch (functionType) { - case PLUS: result += " + "; break; - case MINUS: result += " - "; break; - case TIMES: result += " * "; break; - case DIVIDE: result += " / "; break; - } - result += this->getRight()->toString(); - return result; - } - - virtual std::string dump(std::string prefix) const { - std::stringstream result; - result << prefix << "BinaryNumericalFunctionExpression" << std::endl; - result << this->getLeft()->dump(prefix + "\t"); - switch (functionType) { - case PLUS: result << prefix << "+" << std::endl; break; - case MINUS: result << prefix << "-" << std::endl; break; - case TIMES: result << prefix << "*" << std::endl; break; - case DIVIDE: result << prefix << "/" << std::endl; break; - } - result << this->getRight()->dump(prefix + "\t"); - return result.str(); - } -private: - FunctionType functionType; -}; - -} - -} - -} - -#endif /* BINARYFUNCTIONEXPRESSION_H_ */ + namespace ir { + namespace expressions { + + /*! + * A class representing a binary function expression of numerical type. + */ + class BinaryNumericalFunctionExpression : public BinaryExpression { + public: + /*! + * An enum type specifying the different functions applicable. + */ + enum FunctionType {PLUS, MINUS, TIMES, DIVIDE}; + + /*! + * Creates a binary numerical function expression with the given type, children and function type. + * + * @param type The type of the expression tree node. + * @param left The left child of the expression tree node. + * @param right The right child of the expression tree node. + * @param functionType The function that is applied to the children of this node. + */ + BinaryNumericalFunctionExpression(ReturnType type, std::shared_ptr const& left, std::shared_ptr const& right, FunctionType functionType); + + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const override; + + /*! + * Retrieves the operator that is associated with this node. + * + * @param The operator that is associated with this node. + */ + FunctionType getFunctionType() const; + + virtual int_fast64_t getValueAsInt(std::pair, std::vector> const* variableValues) const override; + + virtual double getValueAsDouble(std::pair, std::vector> const* variableValues) const override; + + virtual void accept(ExpressionVisitor* visitor) override; + + virtual std::string toString() const override; + + private: + // The operator that is associated with this node. + FunctionType functionType; + }; + + } // namespace expressions + } // namespace ir +} // namespace storm + +#endif /* STORM_IR_EXPRESSIONS_BINARYFUNCTIONEXPRESSION_H_ */ diff --git a/src/ir/expressions/BinaryRelationExpression.cpp b/src/ir/expressions/BinaryRelationExpression.cpp new file mode 100644 index 000000000..407d3cd31 --- /dev/null +++ b/src/ir/expressions/BinaryRelationExpression.cpp @@ -0,0 +1,66 @@ +/* + * BinaryBooleanFunctionExpression.cpp + * + * Created on: 10.06.2013 + * Author: Christian Dehnert + */ + +#include + +#include "BinaryRelationExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + BinaryRelationExpression::BinaryRelationExpression(std::shared_ptr const& left, std::shared_ptr const& right, RelationType relationType) + : BinaryExpression(bool_, left, right), relationType(relationType) { + // Nothing to do here + } + + std::shared_ptr BinaryRelationExpression::clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { + return std::shared_ptr(new BinaryRelationExpression(this->getLeft()->clone(renaming, variableState), this->getRight()->clone(renaming, variableState), this->relationType)); + } + + bool BinaryRelationExpression::getValueAsBool(std::pair, std::vector> const* variableValues) const { + int_fast64_t resultLeft = this->getLeft()->getValueAsInt(variableValues); + int_fast64_t resultRight = this->getRight()->getValueAsInt(variableValues); + switch(relationType) { + case EQUAL: return resultLeft == resultRight; break; + case NOT_EQUAL: return resultLeft != resultRight; break; + case LESS: return resultLeft < resultRight; break; + case LESS_OR_EQUAL: return resultLeft <= resultRight; break; + case GREATER: return resultLeft > resultRight; break; + case GREATER_OR_EQUAL: return resultLeft >= resultRight; break; + default: throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " + << "Unknown boolean binary relation: '" << relationType << "'."; + } + } + + BinaryRelationExpression::RelationType BinaryRelationExpression::getRelationType() const { + return relationType; + } + + void BinaryRelationExpression::accept(ExpressionVisitor* visitor) { + visitor->visit(this); + } + + std::string BinaryRelationExpression::toString() const { + std::stringstream result; + result << this->getLeft()->toString(); + switch (relationType) { + case EQUAL: result << " = "; break; + case NOT_EQUAL: result << " != "; break; + case LESS: result << " < "; break; + case LESS_OR_EQUAL: result << " <= "; break; + case GREATER: result << " > "; break; + case GREATER_OR_EQUAL: result << " >= "; break; + } + result << this->getRight()->toString(); + + return result.str(); + } + + } // namespace expressions + } // namespace ir +} // namespace storm \ No newline at end of file diff --git a/src/ir/expressions/BinaryRelationExpression.h b/src/ir/expressions/BinaryRelationExpression.h index 8d6184ff7..330152b89 100644 --- a/src/ir/expressions/BinaryRelationExpression.h +++ b/src/ir/expressions/BinaryRelationExpression.h @@ -2,97 +2,59 @@ * BinaryRelationExpression.h * * Created on: 03.01.2013 - * Author: chris + * Author: Christian Dehnert */ -#ifndef BINARYRELATIONEXPRESSION_H_ -#define BINARYRELATIONEXPRESSION_H_ +#ifndef STORM_IR_EXPRESSIONS_BINARYRELATIONEXPRESSION_H_ +#define STORM_IR_EXPRESSIONS_BINARYRELATIONEXPRESSION_H_ #include "src/ir/expressions/BinaryExpression.h" -#include namespace storm { - -namespace ir { - -namespace expressions { - -class BinaryRelationExpression : public BinaryExpression { -public: - enum RelationType {EQUAL, NOT_EQUAL, LESS, LESS_OR_EQUAL, GREATER, GREATER_OR_EQUAL}; - - BinaryRelationExpression(std::shared_ptr left, std::shared_ptr right, RelationType relationType) : BinaryExpression(bool_, left, right), relationType(relationType) { - } - - virtual ~BinaryRelationExpression() { - - } - - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { - return std::shared_ptr(new BinaryRelationExpression(this->getLeft()->clone(renaming, bools, ints), this->getRight()->clone(renaming, bools, ints), this->relationType)); - } - - virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const { - int_fast64_t resultLeft = this->getLeft()->getValueAsInt(variableValues); - int_fast64_t resultRight = this->getRight()->getValueAsInt(variableValues); - switch(relationType) { - case EQUAL: return resultLeft == resultRight; break; - case NOT_EQUAL: return resultLeft != resultRight; break; - case LESS: return resultLeft < resultRight; break; - case LESS_OR_EQUAL: return resultLeft <= resultRight; break; - case GREATER: return resultLeft > resultRight; break; - case GREATER_OR_EQUAL: return resultLeft >= resultRight; break; - default: throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " - << "Unknown boolean binary relation: '" << relationType << "'."; - } - } - - RelationType getRelationType() const { - return relationType; - } - - virtual void accept(ExpressionVisitor* visitor) { - visitor->visit(this); - } - - virtual std::string toString() const { - std::string result = this->getLeft()->toString(); - switch (relationType) { - case EQUAL: result += " = "; break; - case NOT_EQUAL: result += " != "; break; - case LESS: result += " < "; break; - case LESS_OR_EQUAL: result += " <= "; break; - case GREATER: result += " > "; break; - case GREATER_OR_EQUAL: result += " >= "; break; - } - result += this->getRight()->toString(); - - return result; - } - virtual std::string dump(std::string prefix) const { - std::stringstream result; - result << prefix << "BinaryRelationExpression" << std::endl; - result << this->getLeft()->dump(prefix + "\t"); - switch (relationType) { - case EQUAL: result << prefix << "=" << std::endl; break; - case NOT_EQUAL: result << prefix << "!=" << std::endl; break; - case LESS: result << prefix << "<" << std::endl; break; - case LESS_OR_EQUAL: result << prefix << "<=" << std::endl; break; - case GREATER: result << prefix << ">" << std::endl; break; - case GREATER_OR_EQUAL: result << prefix << ">=" << std::endl; break; - } - result << this->getRight()->dump(prefix + "\t"); - return result.str(); - } - -private: - RelationType relationType; -}; - -} - -} - -} - -#endif /* BINARYRELATIONEXPRESSION_H_ */ + namespace ir { + namespace expressions { + + /*! + * A class representing a binary relation expression of boolean type. + */ + class BinaryRelationExpression : public BinaryExpression { + public: + /*! + * An enum type specifying the different relations applicable. + */ + enum RelationType {EQUAL, NOT_EQUAL, LESS, LESS_OR_EQUAL, GREATER, GREATER_OR_EQUAL}; + + /*! + * Creates a binary relation expression tree node with the given children and relation type. + * + * @param left The left child of the binary expression. + * @param right The right child of the binary expression. + * @param relationType The type of the relation associated with this node. + */ + BinaryRelationExpression(std::shared_ptr const& left, std::shared_ptr const& right, RelationType relationType); + + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const override; + + virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const override; + + /*! + * Retrieves the relation that is associated with this node. + * + * @param The relation that is associated with this node. + */ + RelationType getRelationType() const; + + virtual void accept(ExpressionVisitor* visitor) override; + + virtual std::string toString() const override; + + private: + // The relation operator associated with this node. + RelationType relationType; + }; + + } // namespace expressions + } // namespace ir +} // namespace storm + +#endif /* STORM_IR_EXPRESSIONS_BINARYRELATIONEXPRESSION_H_ */ diff --git a/src/ir/expressions/BooleanConstantExpression.cpp b/src/ir/expressions/BooleanConstantExpression.cpp new file mode 100644 index 000000000..71d84e33e --- /dev/null +++ b/src/ir/expressions/BooleanConstantExpression.cpp @@ -0,0 +1,62 @@ +/* + * BooleanConstantExpression.cpp + * + * Created on: 10.06.2013 + * Author: Christian Dehnert + */ + +#include + +#include "BooleanConstantExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + BooleanConstantExpression::BooleanConstantExpression(std::string const& constantName) : ConstantExpression(bool_, constantName) { + defined = false; + value = false; + } + + std::shared_ptr BooleanConstantExpression::clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { + return std::shared_ptr(new BooleanConstantExpression(*this)); + } + + bool BooleanConstantExpression::getValueAsBool(std::pair, std::vector> const* variableValues) const { + if (!defined) { + throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " + << "Boolean constant '" << this->getConstantName() << "' is undefined."; + } else { + return value; + } + } + + void BooleanConstantExpression::accept(ExpressionVisitor* visitor) { + visitor->visit(this); + } + + std::string BooleanConstantExpression::toString() const { + std::stringstream result; + result << this->getConstantName(); + if (defined) { + result << "[" << value << "]"; + } + return result.str(); + } + + bool BooleanConstantExpression::isDefined() const { + return defined; + } + + bool BooleanConstantExpression::getValue() const { + return value; + } + + void BooleanConstantExpression::define(bool value) { + defined = true; + this->value = value; + } + + } // namespace expressions + } // namespace ir +} // namespace storm \ No newline at end of file diff --git a/src/ir/expressions/BooleanConstantExpression.h b/src/ir/expressions/BooleanConstantExpression.h index 2f3889b01..72288588d 100644 --- a/src/ir/expressions/BooleanConstantExpression.h +++ b/src/ir/expressions/BooleanConstantExpression.h @@ -2,88 +2,67 @@ * BooleanConstantExpression.h * * Created on: 04.01.2013 - * Author: chris + * Author: Christian Dehnert */ -#ifndef BOOLEANCONSTANTEXPRESSION_H_ -#define BOOLEANCONSTANTEXPRESSION_H_ +#ifndef STORM_IR_EXPRESSIONS_BOOLEANCONSTANTEXPRESSION_H_ +#define STORM_IR_EXPRESSIONS_BOOLEANCONSTANTEXPRESSION_H_ #include "ConstantExpression.h" -#include - namespace storm { - -namespace ir { - -namespace expressions { - -class BooleanConstantExpression : public ConstantExpression { -public: - BooleanConstantExpression(std::string constantName) : ConstantExpression(bool_, constantName) { - defined = false; - value = false; - } - BooleanConstantExpression(const BooleanConstantExpression& bce) - : ConstantExpression(bce), value(bce.value), defined(bce.defined) { - } - - virtual ~BooleanConstantExpression() { - - } - - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { - return std::shared_ptr(new BooleanConstantExpression(*this)); - } - - virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const { - if (!defined) { - throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " - << "Boolean constant '" << this->getConstantName() << "' is undefined."; - } else { - return value; - } - } - - virtual void accept(ExpressionVisitor* visitor) { - visitor->visit(this); - } - - virtual std::string toString() const { - std::string result = this->constantName; - if (defined) { - result += "[" + boost::lexical_cast(value) + "]"; - } - return result; - } - - virtual std::string dump(std::string prefix) const { - std::stringstream result; - result << prefix << "BooleanConstantExpression " << this->toString() << std::endl; - return result.str(); - } - - bool isDefined() { - return defined; - } - - bool getValue() { - return value; - } - - void define(bool value) { - defined = true; - this->value = value; - } - - bool value; - bool defined; -}; - -} - -} - -} - -#endif /* BOOLEANCONSTANTEXPRESSION_H_ */ + namespace ir { + namespace expressions { + + /*! + * A class representing a boolean constant expression. + */ + class BooleanConstantExpression : public ConstantExpression { + public: + /*! + * Creates a boolean constant expression with the given constant name. + * + * @param constantName The name of the constant to use. + */ + BooleanConstantExpression(std::string const& constantName); + + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const override; + + virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const override; + + virtual void accept(ExpressionVisitor* visitor) override; + + virtual std::string toString() const override; + + /*! + * Retrieves whether the constant is defined or not. + * + * @return True if the constant is defined. + */ + bool isDefined() const; + + /*! + * Retrieves the value of the constant if it is defined and false otherwise. + */ + bool getValue() const; + + /*! + * Defines the constant using the given value. + * + * @param value The value to use for defining the constant. + */ + void define(bool value); + + private: + // This member stores the value of the constant if it is defined. + bool value; + + // A flag indicating whether the member is defined or not. + bool defined; + }; + + } // namespace expressions + } // namespace ir +} // namespace storm + +#endif /* STORM_IR_EXPRESSIONS_BOOLEANCONSTANTEXPRESSION_H_ */ diff --git a/src/ir/expressions/BooleanLiteral.h b/src/ir/expressions/BooleanLiteral.h deleted file mode 100644 index f6bcd3ab3..000000000 --- a/src/ir/expressions/BooleanLiteral.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * BooleanLiteral.h - * - * Created on: 03.01.2013 - * Author: chris - */ - -#ifndef BOOLEANLITERAL_H_ -#define BOOLEANLITERAL_H_ - -#include "src/ir/expressions/BaseExpression.h" - -namespace storm { -namespace ir { -namespace expressions { - -class BooleanLiteral : public BaseExpression { -public: - bool value; - - BooleanLiteral(bool value) : BaseExpression(bool_), value(value) { - - } - - virtual ~BooleanLiteral() { - - } - - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { - return std::shared_ptr(new BooleanLiteral(this->value)); - } - - virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const { - return value; - } - - virtual void accept(ExpressionVisitor* visitor) { - visitor->visit(this); - } - - virtual std::string toString() const { - if (value) { - return std::string("true"); - } else { - return std::string("false"); - } - } - - virtual std::string dump(std::string prefix) const { - std::stringstream result; - result << prefix << "BooleanLiteral " << this->toString() << std::endl; - return result.str(); - } -}; - -} - -} - -} - -#endif /* BOOLEANLITERAL_H_ */ diff --git a/src/ir/expressions/BooleanLiteralExpression.cpp b/src/ir/expressions/BooleanLiteralExpression.cpp new file mode 100644 index 000000000..d0658bd50 --- /dev/null +++ b/src/ir/expressions/BooleanLiteralExpression.cpp @@ -0,0 +1,40 @@ +/* + * BooleanLiteralExpression.cpp + * + * Created on: 04.01.2013 + * Author: Christian Dehnert + */ + +#include "BooleanLiteralExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + BooleanLiteralExpression::BooleanLiteralExpression(bool value) : BaseExpression(bool_), value(value) { + // Nothing to do here. + } + + std::shared_ptr BooleanLiteralExpression::clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { + return std::shared_ptr(new BooleanLiteralExpression(this->value)); + } + + bool BooleanLiteralExpression::getValueAsBool(std::pair, std::vector> const* variableValues) const { + return value; + } + + void BooleanLiteralExpression::accept(ExpressionVisitor* visitor) { + visitor->visit(this); + } + + std::string BooleanLiteralExpression::toString() const { + if (value) { + return std::string("true"); + } else { + return std::string("false"); + } + } + + } // namespace expressions + } // namespace ir +} // namespace storm \ No newline at end of file diff --git a/src/ir/expressions/BooleanLiteralExpression.h b/src/ir/expressions/BooleanLiteralExpression.h new file mode 100644 index 000000000..3879d320f --- /dev/null +++ b/src/ir/expressions/BooleanLiteralExpression.h @@ -0,0 +1,46 @@ +/* + * BooleanLiteralExpression.h + * + * Created on: 03.01.2013 + * Author: Christian Dehnert + */ + +#ifndef STORM_IR_EXPRESSIONS_BOOLEANLITERALEXPRESSION_H_ +#define STORM_IR_EXPRESSIONS_BOOLEANLITERALEXPRESSION_H_ + +#include "src/ir/expressions/BaseExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + /*! + * A class representing a boolean literal. + */ + class BooleanLiteralExpression : public BaseExpression { + public: + /*! + * Creates a boolean literal expression with the given value. + * + * @param value The value for the boolean literal. + */ + BooleanLiteralExpression(bool value); + + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const override; + + virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const override; + + virtual void accept(ExpressionVisitor* visitor) override; + + virtual std::string toString() const override; + + private: + // The value of the boolean literal. + bool value; + }; + + } // namespace expressions + } // namespace ir +} // namespace storm + +#endif /* STORM_IR_EXPRESSIONS_BOOLEANLITERALEXPRESSION_H_ */ diff --git a/src/ir/expressions/ConstantExpression.cpp b/src/ir/expressions/ConstantExpression.cpp new file mode 100644 index 000000000..114ce10b1 --- /dev/null +++ b/src/ir/expressions/ConstantExpression.cpp @@ -0,0 +1,28 @@ +/* + * ConstantExpression.cpp + * + * Created on: 10.06.2013 + * Author: Christian Dehnert + */ + +#include "src/ir/expressions/ConstantExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + ConstantExpression::ConstantExpression(ReturnType type, std::string constantName) : BaseExpression(type), constantName(constantName) { + // Nothing to do here. + } + + std::string const& ConstantExpression::getConstantName() const { + return constantName; + } + + virtual std::string ConstantExpression::toString() const { + return constantName; + } + + } // namespace expressions + } // namespace ir +} // namespace storm \ No newline at end of file diff --git a/src/ir/expressions/ConstantExpression.h b/src/ir/expressions/ConstantExpression.h index 239dca81c..316367f1a 100644 --- a/src/ir/expressions/ConstantExpression.h +++ b/src/ir/expressions/ConstantExpression.h @@ -2,54 +2,48 @@ * ConstantExpression.h * * Created on: 03.01.2013 - * Author: chris + * Author: Christian Dehnert */ -#ifndef CONSTANTEXPRESSION_H_ -#define CONSTANTEXPRESSION_H_ +#ifndef STORM_IR_EXPRESSIONS_CONSTANTEXPRESSION_H_ +#define STORM_IR_EXPRESSIONS_CONSTANTEXPRESSION_H_ #include "src/ir/expressions/BaseExpression.h" namespace storm { - -namespace ir { - -namespace expressions { - -class ConstantExpression : public BaseExpression { -public: - std::string constantName; - - ConstantExpression(ReturnType type, std::string constantName) : BaseExpression(type), constantName(constantName) { - } - ConstantExpression(const ConstantExpression& ce) - : BaseExpression(ce), constantName(ce.constantName) { - - } - - virtual ~ConstantExpression() { - - } - - std::string const& getConstantName() const { - return constantName; - } - - virtual std::string toString() const { - return constantName; - } - - virtual std::string dump(std::string prefix) const { - std::stringstream result; - result << prefix << "ConstantExpression " << this->toString() << std::endl; - return result.str(); - } -}; - -} - -} - -} - -#endif /* CONSTANTEXPRESSION_H_ */ + namespace ir { + namespace expressions { + + /*! + * A class representing a generic constant expression. + */ + class ConstantExpression : public BaseExpression { + public: + + /*! + * Constructs a constant expression of the given type with the given constant name. + * + * @param type The type of the constant. + * @param constantName The name of the constant. + */ + ConstantExpression(ReturnType type, std::string constantName); + + /*! + * Retrieves the name of the constant. + * + * @return The name of the constant. + */ + std::string const& getConstantName() const; + + virtual std::string toString() const override; + + private: + // The name of the constant. + std::string constantName; + }; + + } // namespace expressions + } // namespace ir +} // namespace storm + +#endif /* STORM_IR_EXPRESSIONS_CONSTANTEXPRESSION_H_ */ diff --git a/src/ir/expressions/DoubleConstantExpression.cpp b/src/ir/expressions/DoubleConstantExpression.cpp new file mode 100644 index 000000000..99727e7b7 --- /dev/null +++ b/src/ir/expressions/DoubleConstantExpression.cpp @@ -0,0 +1,61 @@ +/* + * DoubleConstantExpression.cpp + * + * Created on: 10.06.2013 + * Author: Christian Dehnert + */ + +#include + +#include "DoubleConstantExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + DoubleConstantExpression::DoubleConstantExpression(std::string const& constantName) : ConstantExpression(double_, constantName), defined(false), value(0) { + // Nothing to do here. + } + + std::shared_ptr DoubleConstantExpression::clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { + return std::shared_ptr(new DoubleConstantExpression(*this)); + } + + double DoubleConstantExpression::getValueAsDouble(std::pair, std::vector> const* variableValues) const { + if (!defined) { + throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " + << "Double constant '" << this->getConstantName() << "' is undefined."; + } else { + return value; + } + } + + virtual void DoubleConstantExpression::accept(ExpressionVisitor* visitor) { + visitor->visit(this); + } + + virtual std::string DoubleConstantExpression::toString() const { + std::stringstream result; + result << this->constantName; + if (defined) { + result << "[" << value << "]"; + } + return result.str(); + } + + bool DoubleConstantExpression::isDefined() { + return defined; + } + + double DoubleConstantExpression::getValue() { + return value; + } + + void DoubleConstantExpression::define(double value) { + defined = true; + this->value = value; + } + + } // namespace expressions + } // namespace ir +} // namespace storm \ No newline at end of file diff --git a/src/ir/expressions/DoubleConstantExpression.h b/src/ir/expressions/DoubleConstantExpression.h index f6e26271d..48faaa3a0 100644 --- a/src/ir/expressions/DoubleConstantExpression.h +++ b/src/ir/expressions/DoubleConstantExpression.h @@ -2,86 +2,62 @@ * DoubleConstantExpression.h * * Created on: 04.01.2013 - * Author: chris + * Author: Christian Dehnert */ -#ifndef DOUBLECONSTANTEXPRESSION_H_ -#define DOUBLECONSTANTEXPRESSION_H_ +#ifndef STORM_IR_EXPRESSIONS_DOUBLECONSTANTEXPRESSION_H_ +#define STORM_IR_EXPRESSIONS_DOUBLECONSTANTEXPRESSION_H_ #include "ConstantExpression.h" namespace storm { - -namespace ir { - -namespace expressions { - -class DoubleConstantExpression : public ConstantExpression { -public: - DoubleConstantExpression(std::string constantName) : ConstantExpression(double_, constantName), defined(false), value(0) { - } - - DoubleConstantExpression(const DoubleConstantExpression& dce) - : ConstantExpression(dce), defined(dce.defined), value(dce.value) { - } - - virtual ~DoubleConstantExpression() { - - } - - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { - return std::shared_ptr(new DoubleConstantExpression(*this)); - } - - virtual double getValueAsDouble(std::pair, std::vector> const* variableValues) const { - if (!defined) { - throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " - << "Double constant '" << this->getConstantName() << "' is undefined."; - } else { - return value; - } - } - - virtual void accept(ExpressionVisitor* visitor) { - visitor->visit(this); - } - - virtual std::string toString() const { - std::string result = this->constantName; - if (defined) { - result += "[" + boost::lexical_cast(value) + "]"; - } - return result; - } - - virtual std::string dump(std::string prefix) const { - std::stringstream result; - result << prefix << "DoubleConstantExpression " << this->toString() << std::endl; - return result.str(); - } - - bool isDefined() { - return defined; - } - - double getValue() { - return value; - } - - void define(double value) { - defined = true; - this->value = value; - } - -private: - bool defined; - double value; -}; - -} - -} - -} - -#endif /* DOUBLECONSTANTEXPRESSION_H_ */ + namespace ir { + namespace expressions { + + /*! + * A class representing a constant expression of type double. + */ + class DoubleConstantExpression : public ConstantExpression { + public: + DoubleConstantExpression(std::string const& constantName); + + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const override; + + virtual double getValueAsDouble(std::pair, std::vector> const* variableValues) const override; + + virtual void accept(ExpressionVisitor* visitor) override; + + virtual std::string toString() const override; + + /*! + * Retrieves whether the constant is defined or not. + * + * @return True if the constant is defined. + */ + bool isDefined() const; + + /*! + * Retrieves the value of the constant if it is defined and false otherwise. + */ + double getValue() const; + + /*! + * Defines the constant using the given value. + * + * @param value The value to use for defining the constant. + */ + void define(double value); + + private: + // This member stores the value of the constant if it is defined. + bool value; + + // A flag indicating whether the member is defined or not. + bool defined; + }; + + } // namespace expressions + } // namespace ir +} // namespace storm + +#endif /* STORM_IR_EXPRESSIONS_DOUBLECONSTANTEXPRESSION_H_ */ diff --git a/src/ir/expressions/DoubleLiteral.h b/src/ir/expressions/DoubleLiteral.h deleted file mode 100644 index 4717b6708..000000000 --- a/src/ir/expressions/DoubleLiteral.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * DoubleLiteral.h - * - * Created on: 03.01.2013 - * Author: chris - */ - -#ifndef DOUBLELITERAL_H_ -#define DOUBLELITERAL_H_ - -#include "src/ir/expressions/BaseExpression.h" - -#include "boost/lexical_cast.hpp" - -namespace storm { - -namespace ir { - -namespace expressions { - -class DoubleLiteral : public BaseExpression { -public: - double value; - - DoubleLiteral(double value) : BaseExpression(double_), value(value) { - - } - - virtual ~DoubleLiteral() { - - } - - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { - return std::shared_ptr(new DoubleLiteral(this->value)); - } - - virtual double getValueAsDouble(std::pair, std::vector> const* variableValues) const { - return value; - } - - virtual void accept(ExpressionVisitor* visitor) { - visitor->visit(this); - } - - virtual std::string toString() const { - return boost::lexical_cast(value); - } - - virtual std::string dump(std::string prefix) const { - std::stringstream result; - result << prefix << "DoubleLiteral " << this->toString() << std::endl; - return result.str(); - } -}; - -} - -} - -} - -#endif /* DOUBLELITERAL_H_ */ diff --git a/src/ir/expressions/DoubleLiteralExpression.cpp b/src/ir/expressions/DoubleLiteralExpression.cpp new file mode 100644 index 000000000..fecfb4556 --- /dev/null +++ b/src/ir/expressions/DoubleLiteralExpression.cpp @@ -0,0 +1,40 @@ +/* + * DoubleLiteralExpression.cpp + * + * Created on: 10.06.2013 + * Author: Christian Dehnert + */ + +#include + +#include "DoubleLiteralExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + DoubleLiteralExpression::DoubleLiteralExpression(double value) : BaseExpression(double_), value(value) { + // Nothing to do here. + } + + virtual std::shared_ptr DoubleLiteralExpression::clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { + return std::shared_ptr(new DoubleLiteral(this->value)); + } + + virtual double DoubleLiteralExpression::getValueAsDouble(std::pair, std::vector> const* variableValues) const { + return value; + } + + virtual void DoubleLiteralExpression::accept(ExpressionVisitor* visitor) { + visitor->visit(this); + } + + virtual std::string DoubleLiteralExpression::toString() const { + std::stringstream result; + result << value; + return result.str(); + } + + } // namespace expressions + } // namespace ir +} // namespace storm \ No newline at end of file diff --git a/src/ir/expressions/DoubleLiteralExpression.h b/src/ir/expressions/DoubleLiteralExpression.h new file mode 100644 index 000000000..bf37351e8 --- /dev/null +++ b/src/ir/expressions/DoubleLiteralExpression.h @@ -0,0 +1,46 @@ +/* + * DoubleLiteralExpression.h + * + * Created on: 03.01.2013 + * Author: Christian Dehnert + */ + +#ifndef STORM_IR_EXPRESSIONS_DOUBLELITERALEXPRESSION_H_ +#define STORM_IR_EXPRESSIONS_DOUBLELITERALEXPRESSION_H_ + +#include "src/ir/expressions/BaseExpression.h" + +namespace storm { + namespace ir { + namespace expressions { + + /*! + * A class representing a double literal. + */ + class DoubleLiteralExpression : public BaseExpression { + public: + /*! + * Creates a double literal expression with the given value. + * + * @param value The value for the double literal. + */ + DoubleLiteralExpression(double value); + + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const override; + + virtual double getValueAsDouble(std::pair, std::vector> const* variableValues) const override; + + virtual void accept(ExpressionVisitor* visitor) override; + + virtual std::string toString() const override; + + private: + // The value of the boolean literal. + double value; + }; + + } // namespace expressions + } // namespace ir +} // namespace storm + +#endif /* STORM_IR_EXPRESSIONS_DOUBLELITERALEXPRESSION_H_ */ diff --git a/src/ir/expressions/ExpressionVisitor.h b/src/ir/expressions/ExpressionVisitor.h index 6a8d9d813..1ca3021e4 100644 --- a/src/ir/expressions/ExpressionVisitor.h +++ b/src/ir/expressions/ExpressionVisitor.h @@ -9,47 +9,42 @@ #define STORM_IR_EXPRESSIONS_EXPRESSIONVISITOR_H_ namespace storm { - -namespace ir { - -namespace expressions { - -class BaseExpression; -class BinaryBooleanFunctionExpression; -class BinaryNumericalFunctionExpression; -class BinaryRelationExpression; -class BooleanConstantExpression; -class BooleanLiteral; -class DoubleConstantExpression; -class DoubleLiteral; -class IntegerConstantExpression; -class IntegerLiteral; -class UnaryBooleanFunctionExpression; -class UnaryNumericalFunctionExpression; -class VariableExpression; - -class ExpressionVisitor { -public: - virtual void visit(BaseExpression* expression) = 0; - virtual void visit(BinaryBooleanFunctionExpression* expression) = 0; - virtual void visit(BinaryNumericalFunctionExpression* expression) = 0; - virtual void visit(BinaryRelationExpression* expression) = 0; - virtual void visit(BooleanConstantExpression* expression) = 0; - virtual void visit(BooleanLiteral* expression) = 0; - virtual void visit(DoubleConstantExpression* expression) = 0; - virtual void visit(DoubleLiteral* expression) = 0; - virtual void visit(IntegerConstantExpression* expression) = 0; - virtual void visit(IntegerLiteral* expression) = 0; - virtual void visit(UnaryBooleanFunctionExpression* expression) = 0; - virtual void visit(UnaryNumericalFunctionExpression* expression) = 0; - virtual void visit(VariableExpression* expression) = 0; -}; - -} // namespace expressions - -} // namespace ir - + namespace ir { + namespace expressions { + + class BaseExpression; + class BinaryBooleanFunctionExpression; + class BinaryNumericalFunctionExpression; + class BinaryRelationExpression; + class BooleanConstantExpression; + class BooleanLiteral; + class DoubleConstantExpression; + class DoubleLiteral; + class IntegerConstantExpression; + class IntegerLiteral; + class UnaryBooleanFunctionExpression; + class UnaryNumericalFunctionExpression; + class VariableExpression; + + class ExpressionVisitor { + public: + virtual void visit(BaseExpression* expression) = 0; + virtual void visit(BinaryBooleanFunctionExpression* expression) = 0; + virtual void visit(BinaryNumericalFunctionExpression* expression) = 0; + virtual void visit(BinaryRelationExpression* expression) = 0; + virtual void visit(BooleanConstantExpression* expression) = 0; + virtual void visit(BooleanLiteral* expression) = 0; + virtual void visit(DoubleConstantExpression* expression) = 0; + virtual void visit(DoubleLiteral* expression) = 0; + virtual void visit(IntegerConstantExpression* expression) = 0; + virtual void visit(IntegerLiteral* expression) = 0; + virtual void visit(UnaryBooleanFunctionExpression* expression) = 0; + virtual void visit(UnaryNumericalFunctionExpression* expression) = 0; + virtual void visit(VariableExpression* expression) = 0; + }; + + } // namespace expressions + } // namespace ir } // namespace storm - #endif /* STORM_IR_EXPRESSIONS_EXPRESSIONVISITOR_H_ */ diff --git a/src/ir/expressions/IntegerConstantExpression.h b/src/ir/expressions/IntegerConstantExpression.h index 1cc3450e5..00406fe8c 100644 --- a/src/ir/expressions/IntegerConstantExpression.h +++ b/src/ir/expressions/IntegerConstantExpression.h @@ -28,7 +28,7 @@ public: } - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { return std::shared_ptr(new IntegerConstantExpression(*this)); } diff --git a/src/ir/expressions/IntegerLiteral.h b/src/ir/expressions/IntegerLiteral.h index 5a42671cd..d89e63fbf 100644 --- a/src/ir/expressions/IntegerLiteral.h +++ b/src/ir/expressions/IntegerLiteral.h @@ -26,7 +26,7 @@ public: virtual ~IntegerLiteral() { } - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { return std::shared_ptr(new IntegerLiteral(this->value)); } diff --git a/src/ir/expressions/UnaryBooleanFunctionExpression.h b/src/ir/expressions/UnaryBooleanFunctionExpression.h index 9883d6276..06ea12172 100644 --- a/src/ir/expressions/UnaryBooleanFunctionExpression.h +++ b/src/ir/expressions/UnaryBooleanFunctionExpression.h @@ -28,8 +28,8 @@ public: } - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { - return std::shared_ptr(new UnaryBooleanFunctionExpression(this->getChild()->clone(renaming, bools, ints), this->functionType)); + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { + return std::shared_ptr(new UnaryBooleanFunctionExpression(this->getChild()->clone(renaming, variableState), this->functionType)); } FunctionType getFunctionType() const { diff --git a/src/ir/expressions/UnaryNumericalFunctionExpression.h b/src/ir/expressions/UnaryNumericalFunctionExpression.h index d0a770494..73b0dca87 100644 --- a/src/ir/expressions/UnaryNumericalFunctionExpression.h +++ b/src/ir/expressions/UnaryNumericalFunctionExpression.h @@ -28,8 +28,8 @@ public: } - virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { - return std::shared_ptr(new UnaryNumericalFunctionExpression(this->getType(), this->getChild()->clone(renaming, bools, ints), this->functionType)); + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const { + return std::shared_ptr(new UnaryNumericalFunctionExpression(this->getType(), this->getChild()->clone(renaming, variableState), this->functionType)); } virtual int_fast64_t getValueAsInt(std::pair, std::vector> const* variableValues) const { diff --git a/src/ir/expressions/VariableExpression.cpp b/src/ir/expressions/VariableExpression.cpp new file mode 100644 index 000000000..474c67f05 --- /dev/null +++ b/src/ir/expressions/VariableExpression.cpp @@ -0,0 +1,107 @@ +/* + * VariableExpression.cpp + * + * Created on: 10.06.2013 + * Author: Christian Dehnert + */ + +#include "VariableExpression.h" +#include "src/parser/prismparser/VariableState.h" + +namespace storm { + namespace ir { + namespace expressions { + + VariableExpression::VariableExpression(ReturnType type, std::string variableName) : BaseExpression(type), localIndex(0), globalIndex(0), variableName(variableName) { + // Nothing to do here. + } + + VariableExpression::VariableExpression(ReturnType type, uint_fast64_t localIndex, uint_fast64_t globalIndex, std::string variableName) + : BaseExpression(type), localIndex(localIndex), globalIndex(globalIndex), variableName(variableName) { + // Nothing to do here. + } + + VariableExpression::VariableExpression(VariableExpression const& oldExpression, std::string const& newName, uint_fast64_t newGlobalIndex) + : BaseExpression(oldExpression.getType()), localIndex(oldExpression.localIndex), globalIndex(newGlobalIndex), variableName(newName) { + // Nothing to do here. + } + + virtual VariableExpression::~VariableExpression() { + // Nothing to do here. + } + + virtual std::shared_ptr VariableExpression::clone(std::map const& renaming, VariableState const& variableState) { + // Perform the proper cloning. + auto renamingPair = renaming.find(this->variableName); + if (renamingPair != renaming.end()) { + return variableState.getVariableExpression(renamingPair->second); + } else { + return variableState.getVariableExpression(this->variableName); + } + } + + virtual void VariableExpression::accept(ExpressionVisitor* visitor) { + std::cout << "Visitor!" << std::endl; + visitor->visit(this); + } + + virtual std::string VariableExpression::toString() const { + return this->variableName; + } + + virtual std::string VariableExpression::dump(std::string prefix) const { + std::stringstream result; + result << prefix << this->variableName << " " << index << std::endl; + return result.str(); + } + + virtual int_fast64_t VariableExpression::getValueAsInt(std::pair, std::vector> const* variableValues) const { + if (this->getType() != int_) { + BaseExpression::getValueAsInt(variableValues); + } + + if (variableValues != nullptr) { + return variableValues->second[globalIndex]; + } else { + throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression" + << " involving variables without variable values."; + } + } + + virtual bool VariableExpression::getValueAsBool(std::pair, std::vector> const* variableValues) const { + if (this->getType() != bool_) { + BaseExpression::getValueAsBool(variableValues); + } + + if (variableValues != nullptr) { + return variableValues->first[globalIndex]; + } else { + throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression" + << " involving variables without variable values."; + } + } + + virtual double VariableExpression::getValueAsDouble(std::pair, std::vector> const* variableValues) const { + if (this->getType() != double_) { + BaseExpression::getValueAsDouble(variableValues); + } + + throw storm::exceptions::NotImplementedException() << "Cannot evaluate expression with " + << " variable '" << variableName << "' of type double."; + } + + std::string const& VariableExpression::getVariableName() const { + return variableName; + } + + uint_fast64_t VariableExpression::getLocalVariableIndex() const { + return this->localIndex; + } + + uint_fast64_t VariableExpression::getGlobalVariableIndex() const { + return this->globalIndex; + } + + } // namespace expressions + } // namespace ir +} // namespace storm \ No newline at end of file diff --git a/src/ir/expressions/VariableExpression.h b/src/ir/expressions/VariableExpression.h index fa7cfc6c1..ef5b8388c 100644 --- a/src/ir/expressions/VariableExpression.h +++ b/src/ir/expressions/VariableExpression.h @@ -2,7 +2,7 @@ * VariableExpression.h * * Created on: 03.01.2013 - * Author: chris + * Author: Christian Dehnert */ #ifndef VARIABLEEXPRESSION_H_ @@ -11,7 +11,6 @@ #include #include -#include "src/ir/VariableStateInterface.h" #include "BaseExpression.h" #include "src/exceptions/InvalidArgumentException.h" @@ -20,124 +19,53 @@ extern log4cplus::Logger logger; namespace storm { - -namespace ir { - -namespace expressions { - -class VariableExpression : public BaseExpression { -public: - VariableExpression(ReturnType type, std::string variableName) : BaseExpression(type), localIndex(0), globalIndex(0), variableName(variableName) { - // Nothing to do here. - } - - VariableExpression(ReturnType type, uint_fast64_t localIndex, uint_fast64_t globalIndex, std::string variableName) - : BaseExpression(type), localIndex(localIndex), globalIndex(globalIndex), variableName(variableName) { - // Nothing to do here. - } - - VariableExpression(VariableExpression const& oldExpression, std::string const& newName, uint_fast64_t newGlobalIndex) - : BaseExpression(oldExpression.getType()), localIndex(oldExpression.localIndex), globalIndex(newGlobalIndex), variableName(newName) { - // Nothing to do here. - } - - virtual ~VariableExpression() { - // Nothing to do here. - } - - virtual std::shared_ptr clone(std::map const& renaming, std::shared_ptr const& variableState) { - // Perform the proper cloning. - } - - virtual std::shared_ptr clone(std::map const& renaming, std::map const& booleanVariableToIndexMap, std::map const& integerVariableToIndexMap) { - auto renamingPair = renaming.find(this->variableName); - if (renamingPair != renaming.end()) { - if (this->getType() == bool_) { - return std::shared_ptr(new VariableExpression(bool_, this->localIndex, booleanVariableToIndexMap.at(renamingPair->second), renamingPair->second)); - } else if (this->getType() == int_) { - return std::shared_ptr(new VariableExpression(int_, this->localIndex, integerVariableToIndexMap.at(renamingPair->second), renamingPair->second)); - } else { - LOG4CPLUS_ERROR(logger, "Renaming variable " << this->variableName << " that is neither bool nor int."); - throw storm::exceptions::InvalidArgumentException() << "Renaming variable " << this->variableName << " that is neither bool nor int."; - } - } else { - return std::shared_ptr(new VariableExpression(this->getType(), this->localIndex, this->globalIndex, this->variableName)); - } - } - - - virtual void accept(ExpressionVisitor* visitor) { - std::cout << "Visitor!" << std::endl; - visitor->visit(this); - } - - virtual std::string toString() const { - return this->variableName; - } - virtual std::string dump(std::string prefix) const { - std::stringstream result; - result << prefix << this->variableName << " " << index << std::endl; - return result.str(); - } - - virtual int_fast64_t getValueAsInt(std::pair, std::vector> const* variableValues) const { - if (this->getType() != int_) { - BaseExpression::getValueAsInt(variableValues); - } - - if (variableValues != nullptr) { - return variableValues->second[globalIndex]; - } else { - throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression" - << " involving variables without variable values."; - } - } - - virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const { - if (this->getType() != bool_) { - BaseExpression::getValueAsBool(variableValues); - } - - if (variableValues != nullptr) { - return variableValues->first[globalIndex]; - } else { - throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression" - << " involving variables without variable values."; - } - } - - virtual double getValueAsDouble(std::pair, std::vector> const* variableValues) const { - if (this->getType() != double_) { - BaseExpression::getValueAsDouble(variableValues); - } - - throw storm::exceptions::NotImplementedException() << "Cannot evaluate expression with " - << " variable '" << variableName << "' of type double."; - } - - std::string const& getVariableName() const { - return variableName; - } - - uint_fast64_t getLocalVariableIndex() const { - return this->localIndex; - } + // Forward-declare VariableState. + namespace parser { + namespace prismparser { + class VariableState; + } // namespace prismparser + } // namespace parser - uint_fast64_t getGlobalVariableIndex() const { - return this->globalIndex; - } - -private: - uint_fast64_t localIndex; - uint_fast64_t globalIndex; - std::string variableName; -}; - -} - -} - -} + namespace ir { + namespace expressions { + + class VariableExpression : public BaseExpression { + public: + VariableExpression(ReturnType type, std::string variableName); + + VariableExpression(ReturnType type, uint_fast64_t localIndex, uint_fast64_t globalIndex, std::string variableName); + + VariableExpression(VariableExpression const& oldExpression, std::string const& newName, uint_fast64_t newGlobalIndex); + + virtual ~VariableExpression(); + + virtual std::shared_ptr clone(std::map const& renaming, parser::prismparser::VariableState const& variableState) const; + + virtual void accept(ExpressionVisitor* visitor); + + virtual std::string toString() const; + + virtual int_fast64_t getValueAsInt(std::pair, std::vector> const* variableValues) const; + + virtual bool getValueAsBool(std::pair, std::vector> const* variableValues) const; + + virtual double getValueAsDouble(std::pair, std::vector> const* variableValues) const; + + std::string const& getVariableName() const; + + uint_fast64_t getLocalVariableIndex() const; + + uint_fast64_t getGlobalVariableIndex() const; + + private: + uint_fast64_t localIndex; + uint_fast64_t globalIndex; + std::string variableName; + }; + + } // namespace expressions + } // namespace ir +} // namespace storm #endif /* VARIABLEEXPRESSION_H_ */ diff --git a/src/parser/prismparser/Includes.h b/src/parser/prismparser/Includes.h index 3bce26d00..49a720dad 100644 --- a/src/parser/prismparser/Includes.h +++ b/src/parser/prismparser/Includes.h @@ -1,6 +1,6 @@ /* - * File: BoostIncludes.h - * Author: nafur + * File: Includes + * Author: Gereon Kremer * * Created on April 10, 2013, 4:46 PM */ @@ -8,8 +8,6 @@ #ifndef BOOSTINCLUDES_H #define BOOSTINCLUDES_H -#define DEBUGPRISM - // Used for Boost spirit. #include #include diff --git a/src/parser/prismparser/VariableState.h b/src/parser/prismparser/VariableState.h index 60ff2772b..fee670e40 100644 --- a/src/parser/prismparser/VariableState.h +++ b/src/parser/prismparser/VariableState.h @@ -29,7 +29,7 @@ std::ostream& operator<<(std::ostream& out, qi::symbols& symbols); /*! * This class contains the internal state that is needed for parsing a PRISM model. */ -struct VariableState : public storm::ir::VariableAdder { +struct VariableState { /*! * Creates a new variable state object. By default, this object will be set to a state in which @@ -115,7 +115,7 @@ struct VariableState : public storm::ir::VariableAdder { * @param name The name of the variable. * @return A pair containing the local and global index of the variable. */ - std::pair addBooleanVariable(std::string const& name); + virtual std::pair addBooleanVariable(std::string const& name); /*! * Adds a new integer variable with the given name. @@ -123,7 +123,7 @@ struct VariableState : public storm::ir::VariableAdder { * @param name The name of the variable. * @return A pair containing the local and global index of the variable. */ - std::pair addIntegerVariable(std::string const& name); + virtual std::pair addIntegerVariable(std::string const& name); /*! * Retrieves the variable expression for the boolean variable with the given name. diff --git a/src/parser/prismparser/VariableStateInterface.h b/src/parser/prismparser/VariableStateInterface.h deleted file mode 100644 index d6489cb34..000000000 --- a/src/parser/prismparser/VariableStateInterface.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * VariableStateInterface.h - * - * Created on: 10.06.2013 - * Author: Christian Dehnert - */ - -#ifndef STORM_IR_VARIABLESTATEINTERFACE_H_ -#define STORM_IR_VARIABLESTATEINTERFACE_H_ - -namespace storm { -namespace ir { - -struct VariableStateInterface { - /*! - * Adds an integer variable with the given name, lower and upper bound. - * - * @param name The name of the boolean variable to add. - */ - virtual uint_fast64_t addBooleanVariable(std::string const& name) = 0; - - /*! - * Adds an integer variable with the given name, lower and upper bound. - * - * @param name The name of the integer variable to add. - * @param lower The lower bound of the integer variable. - * @param upper The upper bound of the integer variable. - */ - virtual uint_fast64_t addIntegerVariable(std::string const& name) = 0; - - /*! - * Retrieves the variable expression for the boolean variable with the given name. - * - * @param name The name of the boolean variable for which to retrieve the variable expression. - * @return The variable expression for the boolean variable with the given name. - */ - std::shared_ptr getBooleanVariableExpression(std::string const& name) const = 0; - - /*! - * Retrieves the variable expression for the integer variable with the given name. - * - * @param name The name of the integer variable for which to retrieve the variable expression. - * @return The variable expression for the integer variable with the given name. - */ - std::shared_ptr getIntegerVariableExpression(std::string const& name) const = 0; - - /*! - * Retrieve the variable expression for the variable with the given name. - * - * @param name The name of the variable for which to retrieve the variable expression. - * @return The variable expression for the variable with the given name. - */ - std::shared_ptr getVariableExpression(std::string const& name) const = 0; - - /*! - * Retrieves the next free (global) index for a boolean variable. - */ - virtual uint_fast64_t getNextGlobalBooleanVariableIndex() const = 0; - - /*! - * Retrieves the next free (global) index for a integer variable. - */ - virtual uint_fast64_t getNextGlobalIntegerVariableIndex() const = 0; -}; - -} // namespace ir -} // namespace storm - -#endif /* STORM_IR_VARIABLESTATEINTERFACE_H_ */ \ No newline at end of file