1
/* VerifyPN - TAPAAL Petri Net Engine
2
* Copyright (C) 2016 Peter Gjøl Jensen <root@petergjoel.dk>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "PetriEngine/Structures/Queue.h"
19
#include "PetriEngine/PQL/Contexts.h"
24
namespace PetriEngine {
25
namespace Structures {
26
Queue::Queue(StateSetInterface* states) : _states(states) {}
32
BFSQueue::BFSQueue(StateSetInterface* states) : Queue(states), _cnt(0), _nstates(0) {}
33
BFSQueue::~BFSQueue(){}
35
bool BFSQueue::pop(Structures::State& state)
39
_states->decode(state, _cnt);
49
void BFSQueue::push(size_t id, PQL::DistanceContext&,
50
std::shared_ptr<PQL::Condition>& query)
56
DFSQueue::DFSQueue(StateSetInterface* states) : Queue(states) {}
57
DFSQueue::~DFSQueue(){}
59
bool DFSQueue::pop(Structures::State& state)
61
if(_stack.empty()) return false;
62
uint32_t n = _stack.top();
64
_states->decode(state, n);
68
void DFSQueue::push(size_t id, PQL::DistanceContext&,
69
std::shared_ptr<PQL::Condition>& query)
74
RDFSQueue::RDFSQueue(StateSetInterface* states) : Queue(states) {}
75
RDFSQueue::~RDFSQueue(){}
77
auto rng = std::default_random_engine {};
78
bool RDFSQueue::pop(Structures::State& state)
82
if(_stack.empty()) return false;
83
uint32_t n = _stack.top();
85
_states->decode(state, n);
90
std::shuffle ( _cache.begin(), _cache.end(), rng );
91
uint32_t n = _cache.back();
92
_states->decode(state, n);
93
for(size_t i = 0; i < (_cache.size() - 1); ++i)
95
_stack.push(_cache[i]);
102
void RDFSQueue::push(size_t id, PQL::DistanceContext&,
103
std::shared_ptr<PQL::Condition>& query)
105
_cache.push_back(id);
108
HeuristicQueue::HeuristicQueue(StateSetInterface* states) : Queue(states) {}
109
HeuristicQueue::~HeuristicQueue(){}
111
bool HeuristicQueue::pop(Structures::State& state)
113
if(_queue.empty()) return false;
114
uint32_t n = _queue.top().item;
116
_states->decode(state, n);
120
void HeuristicQueue::push(size_t id, PQL::DistanceContext& context,
121
std::shared_ptr<PQL::Condition>& query)
123
// invert result, highest numbers are on top!
124
uint32_t dist = query->distance(context);
125
_queue.emplace(dist, (uint32_t)id);