diff --git a/.gitignore b/.gitignore index 8d835a83a..19716c6a4 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ nbproject/ cmake-build-* .idea/ +.vscode src/PetriEngine/PQL/PQLQueryParser.parser.cpp src/PetriEngine/PQL/PQLQueryParser.parser.hpp src/PetriEngine/PQL/PQLQueryTokens.lexer.cpp diff --git a/include/PetriEngine/Colored/ArcIntervals.h b/include/PetriEngine/Colored/ArcIntervals.h index b382c1d4e..d950e1dc6 100644 --- a/include/PetriEngine/Colored/ArcIntervals.h +++ b/include/PetriEngine/Colored/ArcIntervals.h @@ -39,7 +39,7 @@ namespace PetriEngine { : _varIndexModMap(std::move(varIndexModMap)), _intervalTupleVec(std::move(ranges)) { }; - void print() { + void print() const { std::cout << "[ "; for(auto& varModifierPair : _varIndexModMap){ std::cout << "(" << varModifierPair.first->name << ", " << varModifierPair.first->colorType->productSize() << ") "; diff --git a/include/PetriEngine/Colored/Expressions.h b/include/PetriEngine/Colored/Expressions.h index 35cdd738c..25ece30ec 100644 --- a/include/PetriEngine/Colored/Expressions.h +++ b/include/PetriEngine/Colored/Expressions.h @@ -55,6 +55,10 @@ namespace PetriEngine { ColorExpression() {} virtual ~ColorExpression() {} virtual const ColorType* getColorType(const ColorTypeMap& colorTypes) const = 0; + virtual bool is_variable() const { return false; } + virtual bool is_predecessor() const { return false; } + virtual bool is_successor() const { return false; } + virtual bool is_tuple() const { return false; } }; class DotConstantExpression : public ColorExpression { @@ -85,6 +89,7 @@ namespace PetriEngine { return _variable->colorType; } + bool is_variable() const override { return true; } }; class UserOperatorExpression : public ColorExpression { @@ -113,6 +118,8 @@ namespace PetriEngine { return _color; } + bool is_successor() const override { return true; } + SuccessorExpression(ColorExpression_ptr&& color) : _color(std::move(color)) {} @@ -131,6 +138,8 @@ namespace PetriEngine { return _color; } + bool is_predecessor() const override { return true; } + PredecessorExpression(ColorExpression_ptr&& color) : _color(std::move(color)) {} @@ -151,6 +160,8 @@ namespace PetriEngine { return _colors.size(); } + bool is_tuple() const override { return true; } + auto begin() const { return _colors.begin(); } @@ -285,6 +296,7 @@ namespace PetriEngine { virtual void visit(ColorExpressionVisitor& visitor) const = 0; virtual uint32_t weight() const = 0; virtual bool is_single_color() const = 0; + virtual bool is_number_of() const { return false; } }; typedef std::shared_ptr ArcExpression_ptr; @@ -322,10 +334,12 @@ namespace PetriEngine { return _number * _color.size(); } - bool is_single_color() const { + bool is_single_color() const override { return _color.size() == 1; } + bool is_number_of() const override { return true; } + uint32_t number() const { return _number; } @@ -346,6 +360,10 @@ namespace PetriEngine { return _color.end(); } + auto& back() const { + return _color.back(); + } + NumberOfExpression(std::vector&& color, uint32_t number = 1) : _number(number), _color(std::move(color)) {} NumberOfExpression(AllExpression_ptr&& all, uint32_t number = 1) @@ -368,7 +386,7 @@ namespace PetriEngine { return res; } - bool is_single_color() const { + bool is_single_color() const override { return _constituents.size() == 1 && _constituents[0]->is_single_color(); } @@ -388,6 +406,10 @@ namespace PetriEngine { return _constituents.end(); } + auto& back() const { + return _constituents.back(); + } + AddExpression(std::vector &&constituents) : _constituents(std::move(constituents)) {} @@ -404,7 +426,7 @@ namespace PetriEngine { return _left->weight() - _right->weight(); } - bool is_single_color() const { + bool is_single_color() const override { return false; } @@ -434,7 +456,7 @@ namespace PetriEngine { return _scalar * _expr->weight(); } - bool is_single_color() const { + bool is_single_color() const override { return _expr->is_single_color(); } diff --git a/include/PetriEngine/Colored/VariableVisitor.h b/include/PetriEngine/Colored/VariableVisitor.h index 9afbe7eca..04e0aa573 100644 --- a/include/PetriEngine/Colored/VariableVisitor.h +++ b/include/PetriEngine/Colored/VariableVisitor.h @@ -29,6 +29,8 @@ #ifndef VARIABLESVISITOR_H #define VARIABLESVISITOR_H +#include + #include "Expressions.h" #include "Colors.h" @@ -206,23 +208,47 @@ namespace PetriEngine { virtual void accept(const AddExpression* add) { for (const auto& elem : *add) { - for(auto& pair : _varModifiers){ - std::unordered_map newMap; - pair.second.push_back(newMap); + for (auto& pair : _varModifiers) { + pair.second.emplace_back(); } + _index = 0; elem->visit(*this); } + + if (add->back()->is_number_of()) { + const auto& numberOfExpr = std::static_pointer_cast(add->back()); + if (is_last_variable_expr(*numberOfExpr->back())) { + for (auto& pair : _varModifiers) { + pair.second.emplace_back(); + } + } + } } virtual void accept(const SubtractExpression* sub) { + for (auto& pair : _varModifiers) { + pair.second.emplace_back(); + } _index = 0; (*sub)[0]->visit(*this); //We ignore the restrictions imposed by the subtraction for now if(_include_subtracts){ + for (auto& pair : _varModifiers) { + pair.second.emplace_back(); + } _index = 0; (*sub)[1]->visit(*this); + + if ((*sub)[1]->is_number_of()) { + const auto& numberOfExpr = std::static_pointer_cast((*sub)[1]); + if (is_last_variable_expr(*numberOfExpr->back())) { + for (auto& pair : _varModifiers) { + pair.second.emplace_back(); + } + } + } } } @@ -262,6 +288,32 @@ namespace PetriEngine { get_variables(e, variables, varPositions, varModifierMap, tuples, false); } + + private: + bool is_last_variable_expr(const ColorExpression& expr) const { + std::stack stack; + stack.push(&expr); + + while (!stack.empty()) { + const auto* currentExpr = stack.top(); + stack.pop(); + + if (currentExpr->is_variable()) { + return true; + } else if (currentExpr->is_predecessor()) { + stack.push(static_cast(currentExpr)->child().get()); + } else if (currentExpr->is_successor()) { + stack.push(static_cast(currentExpr)->child().get()); + } else if (currentExpr->is_tuple()) { + const auto* tupleExpr = static_cast(currentExpr); + for (const auto& tupleElem : *tupleExpr) { + stack.push(tupleElem.get()); + } + } + } + + return false; + } }; } } diff --git a/src/PetriEngine/Colored/EvaluationVisitor.cpp b/src/PetriEngine/Colored/EvaluationVisitor.cpp index f7ac8a6a1..d94292d37 100644 --- a/src/PetriEngine/Colored/EvaluationVisitor.cpp +++ b/src/PetriEngine/Colored/EvaluationVisitor.cpp @@ -129,8 +129,6 @@ namespace PetriEngine { (*sub)[0]->visit(*this); auto lhs = _mres; (*sub)[1]->visit(*this); - if (!_mres.isSubsetOf(lhs)) - throw base_error("RHS of subtraction is not a subset of LHS"); _mres = lhs - _mres; } diff --git a/src/PetriEngine/Colored/IntervalGenerator.cpp b/src/PetriEngine/Colored/IntervalGenerator.cpp index 042996ccc..d1b5f05e2 100644 --- a/src/PetriEngine/Colored/IntervalGenerator.cpp +++ b/src/PetriEngine/Colored/IntervalGenerator.cpp @@ -23,8 +23,8 @@ namespace PetriEngine { std::vector IntervalGenerator::getIntervalsFromInterval(const interval_t &interval, uint32_t varPosition, int32_t varModifier, const std::vector &varColorTypes) { std::vector varIntervals; - interval_t firstVarInterval; - varIntervals.push_back(firstVarInterval); + varIntervals.emplace_back(); + assert(varPosition + varColorTypes.size() <= interval.size()); for(uint32_t i = varPosition; i < varPosition + varColorTypes.size(); i++){ auto ctSize = varColorTypes[i - varPosition]->size(); int32_t lower_val = ctSize + (interval[i]._lower + varModifier); @@ -89,6 +89,7 @@ namespace PetriEngine { interval_vector_t varIntervals; std::vector varColorTypes; pair.first->colorType->getColortypes(varColorTypes); + assert(pair.second.size() > tuplePos); getArcVarIntervals(varIntervals, pair.second[tuplePos], interval, varColorTypes); @@ -131,6 +132,8 @@ namespace PetriEngine { interval_vector_t varIntervals; std::vector varColorTypes; pair.first->colorType->getColortypes(varColorTypes); + + assert(pair.second.size() > tuplePos); getArcVarIntervals(varIntervals, pair.second[tuplePos], interval, varColorTypes); if(arcIntervals._intervalTupleVec.size() > 1 && pair.second[tuplePos].empty()){ diff --git a/src/PetriEngine/Colored/PartitionBuilder.cpp b/src/PetriEngine/Colored/PartitionBuilder.cpp index 451dc6553..06a40ab74 100644 --- a/src/PetriEngine/Colored/PartitionBuilder.cpp +++ b/src/PetriEngine/Colored/PartitionBuilder.cpp @@ -422,8 +422,7 @@ namespace PetriEngine { const VariableModifierMap &varModifierMap, const EquivalenceClass& eqClass , const Arc *arc, uint32_t placeId){ std::vector varMaps; - VariableIntervalMap varMap; - varMaps.push_back(varMap); + varMaps.emplace_back(); std::unordered_map placeArcIntervals; ColorFixpoint postPlaceFixpoint; postPlaceFixpoint.constraints = eqClass.intervals();