Line data Source code
1 : /* 2 : * This file is part of PTN Engine 3 : * 4 : * Copyright (c) 2017 Eduardo Valgôde 5 : * Copyright (c) 2021 Kale Evans 6 : * Copyright (c) 2024 Eduardo Valgôde 7 : * 8 : * Licensed under the Apache License, Version 2.0 (the "License"); 9 : * you may not use this file except in compliance with the License. 10 : * You may obtain a copy of the License at 11 : * 12 : * http://www.apache.org/licenses/LICENSE-2.0 13 : * 14 : * Unless required by applicable law or agreed to in writing, software 15 : * distributed under the License is distributed on an "AS IS" BASIS, 16 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 : * See the License for the specific language governing permissions and 18 : * limitations under the License. 19 : */ 20 : 21 : #include "PTN_Engine/TransitionsManager.h" 22 : #include "PTN_Engine/Transition.h" 23 : #include <algorithm> 24 : #include <mutex> 25 : #include <random> 26 : 27 : namespace ptne 28 : { 29 : using namespace std; 30 : 31 79 : TransitionsManager::~TransitionsManager() = default; 32 79 : TransitionsManager::TransitionsManager() = default; 33 : 34 165 : bool TransitionsManager::contains(const string &itemName) const 35 : { 36 165 : shared_lock itemsGuard(m_itemsMutex); 37 330 : return ManagerBase<Transition>::contains(itemName); 38 165 : } 39 : 40 149 : void TransitionsManager::insert(shared_ptr<Transition> transition) 41 : { 42 149 : unique_lock itemsGuard(m_itemsMutex); 43 149 : ManagerBase<Transition>::insert(transition); 44 149 : } 45 : 46 3 : void TransitionsManager::clear() 47 : { 48 3 : unique_lock itemsGuard(m_itemsMutex); 49 3 : ManagerBase<Transition>::clear(); 50 3 : } 51 : 52 807718 : vector<weak_ptr<Transition>> TransitionsManager::collectEnabledTransitionsRandomly() const 53 : { 54 807718 : shared_lock transitionsGuard(m_itemsMutex); 55 : 56 807718 : vector<weak_ptr<Transition>> enabledTransitions; 57 3056307 : for (const auto &[_, transition] : m_items) 58 : { 59 2248589 : if (transition->isEnabled()) 60 : { 61 828464 : enabledTransitions.push_back(transition); 62 : } 63 : } 64 : 65 : // TO DO check performance 66 807718 : random_device randomDevice; 67 807718 : mt19937_64 seed(randomDevice()); 68 807718 : ranges::shuffle(enabledTransitions, seed); 69 1615436 : return enabledTransitions; 70 807718 : } 71 : 72 23 : SharedPtrTransition TransitionsManager::getTransition(const string &transitionName) const 73 : { 74 23 : shared_lock itemsGuard(m_itemsMutex); 75 44 : return ManagerBase<Transition>::getItem(transitionName); 76 23 : } 77 : 78 17 : vector<TransitionProperties> TransitionsManager::getTransitionsProperties() const 79 : { 80 17 : shared_lock itemsGuard(m_itemsMutex); 81 17 : vector<TransitionProperties> transitionsProperties; 82 30 : for (const auto &[_, transition] : m_items) 83 : { 84 13 : transitionsProperties.push_back(transition->getTransitionProperties()); 85 : } 86 34 : return transitionsProperties; 87 17 : } 88 : 89 : } // namespace ptne