|
| 1 | +/* Copyright (C) 2024 Davide Faconti - All Rights Reserved |
| 2 | +* |
| 3 | +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 4 | +* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 5 | +* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 6 | +* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 7 | +* |
| 8 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 10 | +* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 11 | +*/ |
| 12 | + |
| 13 | +#include "behaviortree_cpp/actions/updated_action.h" |
| 14 | +#include "behaviortree_cpp/bt_factory.h" |
| 15 | + |
| 16 | +namespace BT |
| 17 | +{ |
| 18 | + |
| 19 | +EntryUpdatedAction::EntryUpdatedAction(const std::string& name, const NodeConfig& config) |
| 20 | + : SyncActionNode(name, config) |
| 21 | +{ |
| 22 | + auto it = config.input_ports.find("entry"); |
| 23 | + if(it == config.input_ports.end() || it->second.empty()) |
| 24 | + { |
| 25 | + throw LogicError("Missing port 'entry' in ", name); |
| 26 | + } |
| 27 | + const auto entry_str = it->second; |
| 28 | + StringView stripped_key; |
| 29 | + if(isBlackboardPointer(entry_str, &stripped_key)) |
| 30 | + { |
| 31 | + entry_key_ = stripped_key; |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + entry_key_ = entry_str; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +NodeStatus EntryUpdatedAction::tick() |
| 40 | +{ |
| 41 | + if(auto entry = config().blackboard->getEntry(entry_key_)) |
| 42 | + { |
| 43 | + std::unique_lock lk(entry->entry_mutex); |
| 44 | + const uint64_t current_id = entry->sequence_id; |
| 45 | + const uint64_t previous_id = sequence_id_; |
| 46 | + sequence_id_ = current_id; |
| 47 | + /* |
| 48 | + uint64_t previous_id = 0; |
| 49 | + auto& previous_id_registry = details::GlobalSequenceRegistry(); |
| 50 | +
|
| 51 | + // find the previous id in the registry. |
| 52 | + auto it = previous_id_registry.find(entry.get()); |
| 53 | + if(it != previous_id_registry.end()) |
| 54 | + { |
| 55 | + previous_id = it->second; |
| 56 | + } |
| 57 | + if(previous_id != current_id) |
| 58 | + { |
| 59 | + previous_id_registry[entry.get()] = current_id; |
| 60 | + }*/ |
| 61 | + return (previous_id != current_id) ? NodeStatus::SUCCESS : NodeStatus::FAILURE; |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + return NodeStatus::FAILURE; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +} // namespace BT |
0 commit comments