Browse Source

Bugfix: undefined constant expressions for the same constant now share a common structure internally that defines their value and definedness.

Former-commit-id: dd2fda5ab5
main
dehnert 12 years ago
parent
commit
3aeb755e61
  1. 34
      src/ir/expressions/BooleanConstantExpression.cpp
  2. 30
      src/ir/expressions/BooleanConstantExpression.h
  3. 32
      src/ir/expressions/ConstantExpression.cpp
  4. 77
      src/ir/expressions/ConstantExpression.h
  5. 33
      src/ir/expressions/DoubleConstantExpression.cpp
  6. 30
      src/ir/expressions/DoubleConstantExpression.h
  7. 36
      src/ir/expressions/IntegerConstantExpression.cpp
  8. 30
      src/ir/expressions/IntegerConstantExpression.h

34
src/ir/expressions/BooleanConstantExpression.cpp

@ -5,34 +5,30 @@
* Author: Christian Dehnert
*/
#include <sstream>
#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>(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<BaseExpression> BooleanConstantExpression::clone(std::map<std::string, std::string> const& renaming, storm::parser::prism::VariableState const& variableState) const {
return std::shared_ptr<BaseExpression>(new BooleanConstantExpression(*this));
}
bool BooleanConstantExpression::getValueAsBool(std::pair<std::vector<bool>, std::vector<int_fast64_t>> 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

30
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<bool> {
public:
/*!
* Creates a boolean constant expression with the given constant name.
@ -38,34 +38,6 @@ namespace storm {
virtual bool getValueAsBool(std::pair<std::vector<bool>, std::vector<int_fast64_t>> 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

32
src/ir/expressions/ConstantExpression.cpp

@ -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

77
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<class T>
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 T>
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<T>()) {
// 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<ConstantDefinitionStruct<T>> valueStructPointer;
};
} // namespace expressions

33
src/ir/expressions/DoubleConstantExpression.cpp

@ -5,20 +5,17 @@
* Author: Christian Dehnert
*/
#include <sstream>
#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>(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<bool>, std::vector<int_fast64_t>> 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

30
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<double> {
public:
/*!
* Creates a double constant expression with the given constant name.
@ -38,34 +38,6 @@ namespace storm {
virtual double getValueAsDouble(std::pair<std::vector<bool>, std::vector<int_fast64_t>> 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

36
src/ir/expressions/IntegerConstantExpression.cpp

@ -5,22 +5,17 @@
* Author: Christian Dehnert
*/
#include <sstream>
#include <iostream>
#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<bool>, std::vector<int_fast64_t>> 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

30
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<int_fast64_t> {
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<bool>, std::vector<int_fast64_t>> 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

Loading…
Cancel
Save