From 3aeb755e61a162070c1c3c9220fc96cb61af5042 Mon Sep 17 00:00:00 2001 From: dehnert Date: Mon, 16 Sep 2013 20:21:24 +0200 Subject: [PATCH] Bugfix: undefined constant expressions for the same constant now share a common structure internally that defines their value and definedness. Former-commit-id: dd2fda5ab5f6a3d09f9dc2dbdd633e18ecfba76f --- .../expressions/BooleanConstantExpression.cpp | 34 +------- .../expressions/BooleanConstantExpression.h | 30 +------- src/ir/expressions/ConstantExpression.cpp | 32 -------- src/ir/expressions/ConstantExpression.h | 77 +++++++++++++++++-- .../expressions/DoubleConstantExpression.cpp | 33 +------- src/ir/expressions/DoubleConstantExpression.h | 30 +------- .../expressions/IntegerConstantExpression.cpp | 36 +-------- .../expressions/IntegerConstantExpression.h | 30 +------- 8 files changed, 87 insertions(+), 215 deletions(-) delete mode 100644 src/ir/expressions/ConstantExpression.cpp diff --git a/src/ir/expressions/BooleanConstantExpression.cpp b/src/ir/expressions/BooleanConstantExpression.cpp index 65859db50..6b0ad2082 100644 --- a/src/ir/expressions/BooleanConstantExpression.cpp +++ b/src/ir/expressions/BooleanConstantExpression.cpp @@ -5,34 +5,30 @@ * Author: Christian Dehnert */ -#include - #include "BooleanConstantExpression.h" namespace storm { namespace ir { namespace expressions { - BooleanConstantExpression::BooleanConstantExpression(std::string const& constantName) : ConstantExpression(bool_, constantName), value(false), defined(false) { + BooleanConstantExpression::BooleanConstantExpression(std::string const& constantName) : ConstantExpression(bool_, constantName) { // Nothing to do here. } - BooleanConstantExpression::BooleanConstantExpression(BooleanConstantExpression const& booleanConstantExpression) - : ConstantExpression(booleanConstantExpression), value(booleanConstantExpression.value), defined(booleanConstantExpression.defined) { + BooleanConstantExpression::BooleanConstantExpression(BooleanConstantExpression const& booleanConstantExpression) : ConstantExpression(booleanConstantExpression) { // Nothing to do here. } - std::shared_ptr BooleanConstantExpression::clone(std::map const& renaming, storm::parser::prism::VariableState const& variableState) const { return std::shared_ptr(new BooleanConstantExpression(*this)); } bool BooleanConstantExpression::getValueAsBool(std::pair, std::vector> const* variableValues) const { - if (!defined) { + if (!this->isDefined()) { throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " << "Boolean constant '" << this->getConstantName() << "' is undefined."; } else { - return value; + return this->getValue(); } } @@ -40,28 +36,6 @@ namespace storm { 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 14736b51c..837c318f9 100644 --- a/src/ir/expressions/BooleanConstantExpression.h +++ b/src/ir/expressions/BooleanConstantExpression.h @@ -17,7 +17,7 @@ namespace storm { /*! * A class representing a boolean constant expression. */ - class BooleanConstantExpression : public ConstantExpression { + class BooleanConstantExpression : public ConstantExpression { public: /*! * Creates a boolean constant expression with the given constant name. @@ -38,34 +38,6 @@ namespace storm { 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 diff --git a/src/ir/expressions/ConstantExpression.cpp b/src/ir/expressions/ConstantExpression.cpp deleted file mode 100644 index 1eda8a9ab..000000000 --- a/src/ir/expressions/ConstantExpression.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * ConstantExpression.cpp - * - * Created on: 10.06.2013 - * Author: Christian Dehnert - */ - -#include "ConstantExpression.h" - -namespace storm { - namespace ir { - namespace expressions { - - ConstantExpression::ConstantExpression(ReturnType type, std::string const& constantName) : BaseExpression(type), constantName(constantName) { - // Nothing to do here. - } - - ConstantExpression::ConstantExpression(ConstantExpression const& constantExpression) : BaseExpression(constantExpression), constantName(constantExpression.constantName) { - // Nothing to do here - } - - std::string const& ConstantExpression::getConstantName() const { - return constantName; - } - - 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 968d757bf..e23cc454e 100644 --- a/src/ir/expressions/ConstantExpression.h +++ b/src/ir/expressions/ConstantExpression.h @@ -14,39 +14,106 @@ namespace storm { namespace ir { namespace expressions { + // A struct for storing whether the constant was defined and if so, what value it holds. + template + struct ConstantDefinitionStruct { + /*! + * Constructs a structure indicating that the constant has been defined with the given value. + * + * @param value The value with which the constant was defined. + */ + ConstantDefinitionStruct(T value) : value(value), defined(true) { + // Nothing to do here. + } + + /*! + * Constructs a structure indicating that the constant has not yet been defined. + * + * @param value The value with which the constant was defined. + */ + ConstantDefinitionStruct() : value(), defined(false) { + // Nothing to do here. + } + + T value; + bool defined; + }; + /*! * A class representing a generic constant expression. */ + template 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 const& constantName); + ConstantExpression(ReturnType type, std::string const& constantName) : BaseExpression(type), constantName(constantName), valueStructPointer(new ConstantDefinitionStruct()) { + // Nothing to do here. + } /*! * Copy-constructs from the given expression. * * @param constantExpression The expression to copy. */ - ConstantExpression(ConstantExpression const& constantExpression); + ConstantExpression(ConstantExpression const& constantExpression) : BaseExpression(constantExpression), constantName(constantExpression.constantName), valueStructPointer(constantExpression.valueStructPointer) { + // Nothing to do here. + } /*! * Retrieves the name of the constant. * * @return The name of the constant. */ - std::string const& getConstantName() const; + std::string const& getConstantName() const { + return constantName; + } - virtual std::string toString() const override; + virtual std::string toString() const override { + std::stringstream result; + result << this->getConstantName(); + if (this->valueStructPointer->defined) { + result << "[" << this->valueStructPointer->value << "]"; + } + return result.str(); + } + + /*! + * Retrieves whether the constant is defined or not. + * + * @return True if the constant is defined. + */ + bool isDefined() const { + return this->valueStructPointer->defined; + } + + /*! + * Retrieves the value of the constant if it is defined. + */ + T getValue() const { + return this->valueStructPointer->value; + } + + /*! + * Defines the constant using the given value. + * + * @param value The value to use for defining the constant. + */ + void define(T value) { + this->valueStructPointer->defined = true; + this->valueStructPointer->value = value; + } private: // The name of the constant. std::string constantName; + + // The definedness status and (if applicable) the value of the constant. + std::shared_ptr> valueStructPointer; }; } // namespace expressions diff --git a/src/ir/expressions/DoubleConstantExpression.cpp b/src/ir/expressions/DoubleConstantExpression.cpp index d7482d31b..48fab0785 100644 --- a/src/ir/expressions/DoubleConstantExpression.cpp +++ b/src/ir/expressions/DoubleConstantExpression.cpp @@ -5,20 +5,17 @@ * Author: Christian Dehnert */ -#include - #include "DoubleConstantExpression.h" namespace storm { namespace ir { namespace expressions { - DoubleConstantExpression::DoubleConstantExpression(std::string const& constantName) : ConstantExpression(double_, constantName), value(0), defined(false) { + DoubleConstantExpression::DoubleConstantExpression(std::string const& constantName) : ConstantExpression(double_, constantName) { // Nothing to do here. } - DoubleConstantExpression::DoubleConstantExpression(DoubleConstantExpression const& doubleConstantExpression) - : ConstantExpression(doubleConstantExpression), value(doubleConstantExpression.value), defined(doubleConstantExpression.defined) { + DoubleConstantExpression::DoubleConstantExpression(DoubleConstantExpression const& doubleConstantExpression) : ConstantExpression(doubleConstantExpression) { // Nothing to do here. } @@ -27,11 +24,11 @@ namespace storm { } double DoubleConstantExpression::getValueAsDouble(std::pair, std::vector> const* variableValues) const { - if (!defined) { + if (!this->isDefined()) { throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " << "Double constant '" << this->getConstantName() << "' is undefined."; } else { - return value; + return this->getValue(); } } @@ -39,28 +36,6 @@ namespace storm { visitor->visit(this); } - std::string DoubleConstantExpression::toString() const { - std::stringstream result; - result << this->getConstantName(); - if (defined) { - result << "[" << value << "]"; - } - return result.str(); - } - - bool DoubleConstantExpression::isDefined() const { - return defined; - } - - double DoubleConstantExpression::getValue() const { - 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 fb8e07977..b4f275690 100644 --- a/src/ir/expressions/DoubleConstantExpression.h +++ b/src/ir/expressions/DoubleConstantExpression.h @@ -17,7 +17,7 @@ namespace storm { /*! * A class representing a constant expression of type double. */ - class DoubleConstantExpression : public ConstantExpression { + class DoubleConstantExpression : public ConstantExpression { public: /*! * Creates a double constant expression with the given constant name. @@ -38,34 +38,6 @@ namespace storm { 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 diff --git a/src/ir/expressions/IntegerConstantExpression.cpp b/src/ir/expressions/IntegerConstantExpression.cpp index 40676ab7b..9108aa794 100644 --- a/src/ir/expressions/IntegerConstantExpression.cpp +++ b/src/ir/expressions/IntegerConstantExpression.cpp @@ -5,22 +5,17 @@ * Author: Christian Dehnert */ -#include -#include #include "IntegerConstantExpression.h" namespace storm { namespace ir { namespace expressions { - IntegerConstantExpression::IntegerConstantExpression(std::string const& constantName) : ConstantExpression(int_, constantName), value(0), defined(false) { - std::cout << "Creating constant integer expression with constant name " << constantName << "[" << this << "]" << std::endl; + IntegerConstantExpression::IntegerConstantExpression(std::string const& constantName) : ConstantExpression(int_, constantName) { // Nothing to do here. } - IntegerConstantExpression::IntegerConstantExpression(IntegerConstantExpression const& integerConstantExpression) - : ConstantExpression(integerConstantExpression), value(integerConstantExpression.value), defined(integerConstantExpression.defined) { - std::cout << "Creating constant integer expression as copy of " << integerConstantExpression.toString() << "[" << this << "]" << std::endl; + IntegerConstantExpression::IntegerConstantExpression(IntegerConstantExpression const& integerConstantExpression) : ConstantExpression(integerConstantExpression) { // Nothing to do here. } @@ -33,40 +28,17 @@ namespace storm { } int_fast64_t IntegerConstantExpression::getValueAsInt(std::pair, std::vector> const* variableValues) const { - if (!defined) { + if (!this->isDefined()) { throw storm::exceptions::ExpressionEvaluationException() << "Cannot evaluate expression: " << "Integer constant '" << this->getConstantName() << "' is undefined."; } else { - return value; + return this->getValue(); } } void IntegerConstantExpression::accept(ExpressionVisitor* visitor) { visitor->visit(this); } - - std::string IntegerConstantExpression::toString() const { - std::stringstream result; - result << this->getConstantName(); - if (defined) { - result << "[" << value << "]"; - } - return result.str(); - } - - bool IntegerConstantExpression::isDefined() const { - return defined; - } - - int_fast64_t IntegerConstantExpression::getValue() const { - return value; - } - - void IntegerConstantExpression::define(int_fast64_t value) { - defined = true; - this->value = value; - } - } // namespace expressions } // namespace ir } // namespace storm \ No newline at end of file diff --git a/src/ir/expressions/IntegerConstantExpression.h b/src/ir/expressions/IntegerConstantExpression.h index 399c04bca..d494e7700 100644 --- a/src/ir/expressions/IntegerConstantExpression.h +++ b/src/ir/expressions/IntegerConstantExpression.h @@ -17,7 +17,7 @@ namespace storm { /*! * A class representing a constant expression of type integer. */ - class IntegerConstantExpression : public ConstantExpression { + class IntegerConstantExpression : public ConstantExpression { public: /*! * Creates an integer constant expression with the given constant name. @@ -40,34 +40,6 @@ namespace storm { virtual int_fast64_t getValueAsInt(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. - */ - int_fast64_t getValue() const; - - /*! - * Defines the constant using the given value. - * - * @param value The value to use for defining the constant. - */ - void define(int_fast64_t value); - - private: - // This member stores the value of the constant if it is defined. - int_fast64_t value; - - // A flag indicating whether the member is defined or not. - bool defined; }; } // namespace expressions