Browse Source

Further work on new expressions.

Former-commit-id: 1be5abbd68
tempestpy_adaptions
dehnert 11 years ago
parent
commit
1d6c25547b
  1. 31
      src/exceptions/ExceptionMacros.h
  2. 23
      src/storage/expressions/BaseExpression.cpp
  3. 43
      src/storage/expressions/BaseExpression.h
  4. 7
      src/storage/expressions/Expression.cpp
  5. 8
      src/storage/expressions/Expression.h
  6. 17
      src/storage/expressions/SubstitutionVisitor.cpp
  7. 4
      src/storage/expressions/SubstitutionVisitor.h
  8. 56
      src/storage/expressions/VariableExpression.cpp
  9. 47
      src/storage/expressions/VariableExpression.h

31
src/exceptions/ExceptionMacros.h

@ -0,0 +1,31 @@
#ifndef STORM_EXCEPTIONS_EXCEPTIONMACROS_H_
#define STORM_EXCEPTIONS_EXCEPTIONMACROS_H_
#include <cassert>
#include "log4cplus/logger.h"
#include "log4cplus/loggingmacros.h"
extern log4cplus::Logger logger;
#ifndef NDEBUG
#define LOG_ASSERT(cond, message) \
{ \
if (!(cond)) { \
LOG4CPLUS_ERROR(logger, message); \
assert(cond); \
} \
} while (false)
#define LOG_THROW(cond, exception, message) \
{ \
if (!(cond)) { \
LOG4CPLUS_ERROR(logger, message); \
throw exception() << message; \
} \
} while (false)
#else
#define LOG_ASSERT(cond, message) /* empty */
#define LOG_THROW(cond, exception, message) /* empty */
#endif
#endif /* STORM_EXCEPTIONS_EXCEPTIONMACROS_H_ */

23
src/storage/expressions/BaseExpression.cpp

@ -0,0 +1,23 @@
#include "src/storage/expressions/BaseExpression.h"
namespace storm {
namespace expressions {
BaseExpression::BaseExpression() : returnType(undefined) {
// Intentionally left empty.
}
BaseExpression::BaseExpression(ExpressionReturnType returnType) : returnType(returnType) {
// Intentionally left empty.
}
ExpressionReturnType BaseExpression::getReturnType() const {
return this->returnType;
}
void BaseExpression::checkType(ExpressionReturnType actualType, ExpressionReturnType expectedType, std::string const& errorMessage) const {
if (actualType != expectedType) {
throw storm::exceptions::InvalidArgumentException() << errorMessage;
}
}
}
}

43
src/storage/expressions/BaseExpression.h

@ -3,25 +3,56 @@
#include "src/storage/expressions/Valuation.h"
#include "src/storage/expressions/ExpressionVisitor.h"
#include "src/exceptions/InvalidArgumentException.h"
namespace storm {
namespace expressions {
/*!
* Each node in an expression tree has a uniquely defined type from this enum.
*/
enum ExpressionReturnType {undefined, bool_, int_, double_};
class BaseExpression {
public:
/*!
* Each node in an expression tree has a uniquely defined type from this enum.
*/
enum ReturnType {undefined, bool_, int_, double_};
virtual int_fast64_t evaluateAsInt(Valuation const& evaluation) const = 0;
BaseExpression();
BaseExpression(ExpressionReturnType returnType);
virtual ~BaseExpression() = default;
virtual bool evaluateAsBool(Valuation const& evaluation) const = 0;
ExpressionReturnType getReturnType() const;
virtual int_fast64_t evaluateAsInt(Valuation const& evaluation) const = 0;
virtual bool evaluateAsBool(Valuation const& evaluation) const = 0;
virtual double evaluateAsDouble(Valuation const& evaluation) const = 0;
virtual std::unique_ptr<BaseExpression> operator+(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> operator-(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> operator-() const = 0;
virtual std::unique_ptr<BaseExpression> operator*(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> operator/(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> operator&(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> operator|(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> operator~() const = 0;
virtual std::unique_ptr<BaseExpression> equals(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> notEquals(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> greater(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> greaterOrEqual(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> less(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> lessOrEqual(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> minimum(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> maximum(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> mod(BaseExpression const& other) const = 0;
virtual std::unique_ptr<BaseExpression> floor() const = 0;
virtual std::unique_ptr<BaseExpression> ceil() const = 0;
virtual void visit(ExpressionVisitor* visitor) const = 0;
protected:
void checkType(ExpressionReturnType actualType, ExpressionReturnType expectedType, std::string const& errorMessage) const;
private:
ExpressionReturnType returnType;
};
}
}

7
src/storage/expressions/Expression.cpp

@ -2,6 +2,7 @@
#include <unordered_map>
#include "src/storage/expressions/Expression.h"
#include "src/storage/expressions/SubstitutionVisitor.h"
namespace storm {
namespace expressions {
@ -12,7 +13,7 @@ namespace storm {
template<template<typename... Arguments> class MapType>
Expression Expression::substitute(MapType<std::string, Expression> const& identifierToExpressionMap) const {
SubstitutionVisitor visitor;
return visitor.substitute(this->getBaseExpression(), identifierToExpressionMap);
return visitor.substitute<MapType>(this->getBaseExpressionPointer(), identifierToExpressionMap);
}
Expression Expression::operator+(Expression const& other) {
@ -23,6 +24,10 @@ namespace storm {
return *this->expressionPtr;
}
BaseExpression const* Expression::getBaseExpressionPointer() const {
return this->expressionPtr.get();
}
template Expression Expression::substitute<std::map>(std::map<std::string, storm::expressions::Expression> const&) const;
template Expression Expression::substitute<std::unordered_map>(std::unordered_map<std::string, storm::expressions::Expression> const&) const;
}

8
src/storage/expressions/Expression.h

@ -8,6 +8,7 @@
namespace storm {
namespace expressions {
class Expression {
public:
Expression() = default;
// Static factory methods to create atomic expression parts.
@ -43,6 +44,13 @@ namespace storm {
*/
BaseExpression const& getBaseExpression() const;
/*!
* Retrieves a pointer to the base expression underlying this expression object.
*
* @return A pointer to the underlying base expression.
*/
BaseExpression const* getBaseExpressionPointer() const;
// A pointer to the underlying base expression.
std::unique_ptr<BaseExpression> expressionPtr;
};

17
src/storage/expressions/SubstitutionVisitor.cpp

@ -0,0 +1,17 @@
#include <map>
#include <unordered_map>
#include "src/storage/expressions/SubstitutionVisitor.h"
namespace storm {
namespace expressions {
template<template<typename... Arguments> class MapType>
Expression SubstitutionVisitor::substitute(BaseExpression const* expression, MapType<std::string, Expression> const& identifierToExpressionMap) {
return Expression();
}
// Explicitly instantiate substitute with map and unordered_map.
template Expression SubstitutionVisitor::substitute<std::map>(BaseExpression const* expression, std::map<std::string, Expression> const& identifierToExpressionMap);
template Expression SubstitutionVisitor::substitute<std::unordered_map>(BaseExpression const* expression, std::unordered_map<std::string, Expression> const& identifierToExpressionMap);
}
}

4
src/storage/expressions/SubstitutionVisitor.h

@ -1,7 +1,7 @@
#ifndef STORM_STORAGE_EXPRESSIONS_SUBSTITUTIONVISITOR_H_
#define STORM_STORAGE_EXPRESSIONS_SUBSTITUTIONVISITOR_H_
#include "src/storage/expressions/BaseExpression.h"
#include "src/storage/expressions/Expression.h"
#include "src/storage/expressions/ExpressionVisitor.h"
namespace storm {
@ -10,6 +10,8 @@ namespace storm {
public:
template<template<typename... Arguments> class MapType>
Expression substitute(BaseExpression const* expression, MapType<std::string, Expression> const& identifierToExpressionMap);
};
}
}

56
src/storage/expressions/VariableExpression.cpp

@ -0,0 +1,56 @@
#include "src/storage/expressions/VariableExpression.h"
#include "src/exceptions/ExceptionMacros.h"
namespace storm {
namespace expressions {
VariableExpression::VariableExpression(ExpressionReturnType returnType, std::string const& variableName) : BaseExpression(returnType), variableName(variableName) {
// Intentionally left empty.
}
std::string const& VariableExpression::getVariableName() const {
return this->variableName;
}
int_fast64_t VariableExpression::evaluateAsInt(Valuation const& evaluation) const {
LOG_ASSERT((this->getReturnType() == ExpressionReturnType::int_), "Cannot evaluate expression as integer: return type is not an integer.");
return evaluation.getIntegerValue(this->getVariableName());
}
bool VariableExpression::evaluateAsBool(Valuation const& evaluation) const {
LOG_ASSERT((this->getReturnType() == ExpressionReturnType::bool_), "Cannot evaluate expression as integer: return type is not an integer.");
return evaluation.getBooleanValue(this->getVariableName());
}
double VariableExpression::evaluateAsDouble(Valuation const& evaluation) const {
LOG_ASSERT((this->getReturnType() == ExpressionReturnType::double_), "Cannot evaluate expression as integer: return type is not an integer.");
return evaluation.getDoubleValue(this->getVariableName());
}
std::unique_ptr<BaseExpression> VariableExpression::operator+(BaseExpression const& other) const {
// FIXME
return nullptr;
}
std::unique_ptr<BaseExpression> operator-(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> operator-() const;
std::unique_ptr<BaseExpression> operator*(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> operator/(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> operator&(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> operator|(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> operator~() const;
std::unique_ptr<BaseExpression> equals(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> notEquals(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> greater(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> greaterOrEqual(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> less(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> lessOrEqual(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> minimum(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> maximum(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> mod(BaseExpression const& other) const;
std::unique_ptr<BaseExpression> floor() const;
std::unique_ptr<BaseExpression> ceil() const;
void visit(ExpressionVisitor* visitor) const;
}
}

47
src/storage/expressions/VariableExpression.h

@ -0,0 +1,47 @@
#ifndef STORM_STORAGE_EXPRESSIONS_VARIABLEEXPRESSION_H_
#define STORM_STORAGE_EXPRESSIONS_VARIABLEEXPRESSION_H_
#include "src/storage/expressions/BaseExpression.h"
namespace storm {
namespace expressions {
class VariableExpression : public BaseExpression {
VariableExpression(ExpressionReturnType returnType, std::string const& variableName);
virtual ~VariableExpression() = default;
std::string const& getVariableName() const;
virtual int_fast64_t evaluateAsInt(Valuation const& evaluation) const;
virtual bool evaluateAsBool(Valuation const& evaluation) const;
virtual double evaluateAsDouble(Valuation const& evaluation) const;
virtual std::unique_ptr<BaseExpression> operator+(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> operator-(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> operator-() const;
virtual std::unique_ptr<BaseExpression> operator*(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> operator/(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> operator&(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> operator|(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> operator~() const;
virtual std::unique_ptr<BaseExpression> equals(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> notEquals(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> greater(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> greaterOrEqual(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> less(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> lessOrEqual(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> minimum(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> maximum(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> mod(BaseExpression const& other) const;
virtual std::unique_ptr<BaseExpression> floor() const;
virtual std::unique_ptr<BaseExpression> ceil() const;
virtual void visit(ExpressionVisitor* visitor) const;
private:
std::string variableName;
};
}
}
#endif /* STORM_STORAGE_EXPRESSIONS_VARIABLEEXPRESSION_H_ */
Loading…
Cancel
Save