1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/************************************************************************
*
* Copyright (C) 2019-2025 IRCAD France
* Copyright (C) 2019-2021 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#include "activity/sequencer.hpp"
#include "activity/builder/base.hpp"
#include "activity/builder/data.hpp"
#include <data/mt/locked_ptr.hpp>
namespace sight::activity
{
static std::int64_t activity_id_counter = 0;
//------------------------------------------------------------------------------
int sequencer::parse_activities(data::activity_set& _activity_set)
{
const auto scoped_emitter = _activity_set.scoped_emit();
std::size_t index = 0;
for(auto it = _activity_set.cbegin() ; it != _activity_set.cend() ; ++it)
{
if(*it == nullptr)
{
SIGHT_ERROR("One activity is unknown, it will be removed");
it = _activity_set.erase(it);
}
else if(!(index < m_activity_ids.size() && m_activity_ids[index] == (*it)->get_activity_config_id()))
{
// Remove the wrong data
SIGHT_ERROR("The activity '" + (*it)->get_activity_config_id() + "' is unknown, it will be removed");
it = _activity_set.erase(it);
}
else if(!sight::activity::sequencer::validate_activity(*it).first)
{
break;
}
else
{
this->store_activity_data(_activity_set, index++);
}
}
return int(index) - 1;
}
//------------------------------------------------------------------------------
void sequencer::store_activity_data(
const data::activity_set& _activity_set,
std::size_t _index,
const data::map::csptr& _overrides
)
{
// Retrives the current activity data
SIGHT_ASSERT("ActivitySet does not contain enough activities.", _activity_set.size() > _index);
const auto& activity = _activity_set[_index];
SIGHT_ASSERT("ActivitySet contains an unknown activity.", activity);
for(const auto& [key, value] : *activity)
{
// Do not store overriden requirements
if(!_overrides || _overrides->count(key) == 0)
{
m_requirements[key] = value;
}
}
}
//------------------------------------------------------------------------------
data::activity::sptr sequencer::get_activity(
data::activity_set& _activity_set,
std::size_t _index,
const core::com::slot_base::sptr& _slot
)
{
data::activity::sptr activity;
const auto& activity_id = m_activity_ids[_index];
const auto& info = activity::extension::activity::get_default()->get_info(activity_id);
if(_activity_set.size() > _index) // The activity already exists, update the data
{
activity = _activity_set[_index];
SIGHT_ASSERT("ActivitySet contains an unknown activity.", activity);
// FIXME: update all the data or only the requirement ?
for(const auto& req : info.requirements)
{
// Look at the non overriden requirements
if(const auto& it = m_requirements.find(req.name); it != m_requirements.cend())
{
activity->insert_or_assign(req.name, it->second);
}
}
}
else // create a new activity
{
// try to create the intermediate activities
if(_index > 0 && (_index - 1) >= _activity_set.size())
{
get_activity(_activity_set, _index - 1, _slot);
}
// Create the activity
activity = std::make_shared<data::activity>();
activity->set_activity_config_id(info.id);
activity->set_description(info.description);
for(const auto& req : info.requirements)
{
// Look at the non overriden requirements
if(const auto& it = m_requirements.find(req.name); it != m_requirements.cend())
{
activity->insert_or_assign(req.name, it->second);
}
else if(req.create || (req.min_occurs == 0 && req.max_occurs == 0))
{
// Create the new data
auto object = sight::activity::detail::data::create(req.type, req.object_config);
// Generate a unique id for each object according to the requirement name, otherwise the ids are based
// on data type. This would make debugging harder and it is required for some other services to work.
const auto id = core::id::join("activity", std::to_string(activity_id_counter), req.name);
object->set_id(id);
activity->insert_or_assign(req.name, object);
m_requirements.insert_or_assign(req.name, object);
}
else if(req.min_occurs == 0)
{
// Create an empty map for optional data
auto object = std::make_shared<data::map>();
activity->insert_or_assign(req.name, object);
m_requirements.insert_or_assign(req.name, object);
}
}
auto scoped_emitter = _activity_set.scoped_emit();
_activity_set.push_back(activity);
if(_slot)
{
auto sig = _activity_set.signal<data::activity_set::added_signal_t>(data::activity_set::ADDED_OBJECTS_SIG);
core::com::connection::blocker block(sig->get_connection(_slot));
// Force signal emission while blocker exists
scoped_emitter.reset();
}
}
return activity;
}
//------------------------------------------------------------------------------
void sequencer::remove_last_activities(data::activity_set& _activity_set, std::size_t _index)
{
if(_activity_set.size() > _index)
{
const auto scoped_emitter = _activity_set.scoped_emit();
// Remove the activities behind the index
_activity_set.erase(_activity_set.cbegin() + int(_index), _activity_set.cend());
// clear the requirements and parse the remaining activities to recreate the requirements
m_requirements.clear();
// increase the id counter, because we will recreate new requirements if we start again activities
// and the current requirements can still be referenced by some services until the restart is processed;
++activity_id_counter;
this->parse_activities(_activity_set);
}
}
//------------------------------------------------------------------------------
void sequencer::reset_requirements()
{
// For all registered activities
for(const auto& id : m_activity_ids)
{
// Get the information about the activity
const auto& info =
sight::activity::extension::activity::get_default()->get_info(id);
// For all registered requirements of the current activity
for(const auto& requirement : info.requirements)
{
// Only reset the requirements that are created here
if((requirement.create || requirement.min_occurs == 0)
&& m_requirements.contains(requirement.name))
{
// Get the data object and lock it
auto object = m_requirements[requirement.name];
data::mt::locked_ptr locked_object(object);
if(!requirement.create && requirement.max_occurs != 0)
{
object->shallow_copy(std::make_shared<data::map>());
}
else
{
object->shallow_copy(detail::data::create(requirement.type, requirement.object_config));
}
}
}
}
}
//------------------------------------------------------------------------------
} // namespace sight::activity
|