5
* Created on 15 February 2014, 10:50
12
#include "PQL/Contexts.h"
13
#include "PetriParse/PNMLParser.h"
14
#include "NetStructures.h"
16
namespace PetriEngine {
18
using ArcIter = std::vector<Arc>::iterator;
20
class PetriNetBuilder;
22
class QueryPlaceAnalysisContext : public PQL::AnalysisContext {
23
std::vector<uint32_t> _placeInQuery;
27
QueryPlaceAnalysisContext(const std::unordered_map<std::string, uint32_t>& pnames, const std::unordered_map<std::string, uint32_t>& tnames, const PetriNet* net)
28
: PQL::AnalysisContext(pnames, tnames, net) {
29
_placeInQuery.resize(_placeNames.size(), 0);
33
virtual ~QueryPlaceAnalysisContext()
37
uint32_t* getQueryPlaceCount(){
38
return _placeInQuery.data();
41
bool hasDeadlock() { return _deadlock; }
43
virtual void setHasDeadlock() override {
47
ResolutionResult resolve(const std::string& identifier, bool place) override {
48
if(!place) return PQL::AnalysisContext::resolve(identifier, false);
49
ResolutionResult result;
51
result.success = false;
52
auto it = _placeNames.find(identifier);
53
if(it != _placeNames.end())
55
uint32_t i = it->second;
56
result.offset = (int)i;
57
result.success = true;
69
ExpandedArc(std::string place, size_t weight) : place(place), weight(weight) {}
71
friend std::ostream& operator<<(std::ostream& os, ExpandedArc const & ea) {
72
for(size_t i = 0; i < ea.weight; ++i)
74
os << "\t\t<token place=\"" << ea.place << "\" age=\"0\"/>\n";
85
Reducer(PetriNetBuilder*);
87
void Print(QueryPlaceAnalysisContext& context); // prints the net, just for debugging
88
void Reduce(QueryPlaceAnalysisContext& context, int enablereduction, bool reconstructTrace, int timeout, bool remove_loops, bool remove_consumers, bool next_safe);
90
size_t RemovedTransitions() const {
91
return _removedTransitions;
94
size_t RemovedPlaces() const {
95
return _removedPlaces;
98
void printStats(std::ostream& out)
100
out << "Removed transitions: " << _removedTransitions << "\n"
101
<< "Removed places: " << _removedPlaces << "\n"
102
<< "Applications of rule A: " << _ruleA << "\n"
103
<< "Applications of rule B: " << _ruleB << "\n"
104
<< "Applications of rule C: " << _ruleC << "\n"
105
<< "Applications of rule D: " << _ruleD << "\n"
106
<< "Applications of rule E: " << _ruleE << "\n"
107
<< "Applications of rule F: " << _ruleF << "\n"
108
<< "Applications of rule G: " << _ruleG << "\n"
109
<< "Applications of rule H: " << _ruleH << "\n"
110
<< "Applications of rule I: " << _ruleI << std::endl;
113
void postFire(std::ostream&, const std::string& transition);
114
void extraConsume(std::ostream&, const std::string& transition);
115
void initFire(std::ostream&);
118
size_t _removedTransitions = 0;
119
size_t _removedPlaces= 0;
120
size_t _ruleA = 0, _ruleB = 0, _ruleC = 0, _ruleD = 0, _ruleE = 0, _ruleF = 0, _ruleG = 0, _ruleH = 0, _ruleI = 0;
121
PetriNetBuilder* parent = nullptr;
122
bool reconstructTrace = false;
123
std::chrono::high_resolution_clock::time_point _timer;
126
// The reduction methods return true if they reduced something and reductions should continue with other rules
127
bool ReducebyRuleA(uint32_t* placeInQuery);
128
bool ReducebyRuleB(uint32_t* placeInQuery);
129
bool ReducebyRuleC(uint32_t* placeInQuery);
130
bool ReducebyRuleD(uint32_t* placeInQuery);
131
bool ReducebyRuleE(uint32_t* placeInQuery);
132
bool ReducebyRuleI(uint32_t* placeInQuery, bool remove_loops, bool remove_consumers);
133
bool ReducebyRuleF(uint32_t* placeInQuery);
134
bool ReducebyRuleG(uint32_t* placeInQuery, bool remove_loops, bool remove_consumers);
135
bool ReducebyRuleH(uint32_t* placeInQuery);
137
std::string getTransitionName(uint32_t transition);
138
std::string getPlaceName(uint32_t place);
140
Transition& getTransition(uint32_t transition);
141
ArcIter getOutArc(Transition&, uint32_t place);
142
ArcIter getInArc(uint32_t place, Transition&);
143
void eraseTransition(std::vector<uint32_t>&, uint32_t);
144
void skipTransition(uint32_t);
145
void skipPlace(uint32_t);
148
bool hasTimedout() const {
149
auto end = std::chrono::high_resolution_clock::now();
150
auto diff = std::chrono::duration_cast<std::chrono::seconds>(end - _timer);
151
return (diff.count() >= _timeout);
154
std::vector<std::string> _initfire;
155
std::unordered_map<std::string, std::vector<std::string>> _postfire;
156
std::unordered_map<std::string, std::vector<ExpandedArc>> _extraconsume;
165
#endif /* REDUCER_H */