Line data Source code
1 : /* 2 : * This file is part of PTN Engine 3 : * 4 : * Copyright (c) 2023-2024 Eduardo ValgĂ´de 5 : * 6 : * Licensed under the Apache License, Version 2.0 (the "License"); 7 : * you may not use this file except in compliance with the License. 8 : * You may obtain a copy of the License at 9 : * 10 : * http://www.apache.org/licenses/LICENSE-2.0 11 : * 12 : * Unless required by applicable law or agreed to in writing, software 13 : * distributed under the License is distributed on an "AS IS" BASIS, 14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 : * See the License for the specific language governing permissions and 16 : * limitations under the License. 17 : */ 18 : 19 : #include "PTN_Engine/PlacesManager.h" 20 : #include "PTN_Engine/PTN_Exception.h" 21 : #include "PTN_Engine/Utilities/DetectRepeated.h" 22 : #include "PTN_Engine/Utilities/LockWeakPtr.h" 23 : #include <mutex> 24 : 25 : namespace ptne 26 : { 27 : using namespace std; 28 : 29 80 : PlacesManager::~PlacesManager() = default; 30 80 : PlacesManager::PlacesManager() = default; 31 : 32 24 : bool PlacesManager::contains(const string &itemName) const 33 : { 34 24 : shared_lock itemsGuard(m_itemsMutex); 35 48 : return ManagerBase<Place>::contains(itemName); 36 24 : } 37 : 38 205 : void PlacesManager::insert(const shared_ptr<Place> &spPlace) 39 : { 40 205 : unique_lock itemsGuard(m_itemsMutex); 41 205 : ManagerBase<Place>::insert(spPlace); 42 201 : if (spPlace->isInputPlace()) 43 : { 44 47 : m_inputPlaces.push_back(spPlace); 45 : } 46 205 : } 47 : 48 3 : void PlacesManager::clear() 49 : { 50 3 : unique_lock itemsGuard(m_itemsMutex); 51 3 : ManagerBase<Place>::clear(); 52 3 : m_inputPlaces.clear(); 53 3 : } 54 : 55 399 : shared_ptr<Place> PlacesManager::getPlace(const string &placeName) const 56 : { 57 399 : shared_lock itemsGuard(m_itemsMutex); 58 798 : return ManagerBase<Place>::getItem(placeName); 59 399 : } 60 : 61 2 : void PlacesManager::clearInputPlaces() const 62 : { 63 2 : unique_lock placesGuard(m_itemsMutex); 64 4 : for (const WeakPtrPlace &place : m_inputPlaces) 65 : { 66 2 : SharedPtrPlace spPlace = lockWeakPtr(place); 67 2 : spPlace->setNumberOfTokens(0); 68 2 : } 69 2 : } 70 : 71 0 : void PlacesManager::printState(ostream &o) const 72 : { 73 0 : shared_lock placesGuard(m_itemsMutex); 74 0 : o << "Place; Tokens" << endl; 75 0 : for (const auto &[placeName, place] : m_items) 76 : { 77 0 : o << placeName.c_str() << ": " << place->getNumberOfTokens() << endl; 78 : } 79 0 : o << endl << endl; 80 0 : } 81 : 82 3 : void PlacesManager::setActionsExecutor(shared_ptr<IActionsExecutor> &actionsExecutor) 83 : { 84 3 : unique_lock placesGuard(m_itemsMutex); 85 3 : for (auto &place : m_items) 86 : { 87 0 : place.second->setActionsExecutor(actionsExecutor); 88 : } 89 3 : } 90 : 91 188 : size_t PlacesManager::getNumberOfTokens(const string &place) const 92 : { 93 188 : shared_lock placesGuard(m_itemsMutex); 94 188 : if (!m_items.contains(place)) 95 : { 96 2 : throw InvalidNameException(place); 97 : } 98 372 : return m_items.at(place)->getNumberOfTokens(); 99 188 : } 100 : 101 2477 : void PlacesManager::incrementInputPlace(const string &place) 102 : { 103 2477 : unique_lock placesGuard(m_itemsMutex); 104 2477 : if (!m_items.contains(place)) 105 : { 106 2 : throw InvalidNameException(place); 107 : } 108 2475 : if (!m_items.at(place)->isInputPlace()) 109 : { 110 1 : throw NotInputPlaceException(place); 111 : } 112 2474 : m_items.at(place)->enterPlace(1); 113 2477 : } 114 : 115 27 : vector<PlaceProperties> PlacesManager::getPlacesProperties() const 116 : { 117 27 : shared_lock placesGuard(m_itemsMutex); 118 : 119 27 : vector<PlaceProperties> placesProperties; 120 45 : for (const auto &[_, place] : m_items) 121 : { 122 18 : placesProperties.push_back(place->placeProperties()); 123 : } 124 54 : return placesProperties; 125 27 : } 126 : 127 4 : vector<WeakPtrPlace> PlacesManager::getPlaces(const vector<string> &placesNames) const 128 : { 129 4 : shared_lock placesGuard(m_itemsMutex); 130 : 131 5 : utility::detectRepeatedNames<string, RepeatedPlaceNamesException>(placesNames); 132 : 133 3 : vector<WeakPtrPlace> placesVector; 134 8 : for (const auto &placeName : placesNames) 135 : { 136 6 : if (!m_items.contains(placeName)) 137 : { 138 1 : throw InvalidNameException(placeName); 139 : } 140 5 : placesVector.push_back(m_items.at(placeName)); 141 : } 142 4 : return placesVector; 143 5 : } 144 : } // namespace ptne