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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
/************************************************************************
*
* Copyright (C) 2009-2025 IRCAD France
* Copyright (C) 2012-2020 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.hpp"
#include <core/runtime/module.hpp>
#include <core/runtime/path.hpp>
#include <core/runtime/runtime.hpp>
#include <data/activity.hpp>
#include <data/vector.hpp>
#include <boost/range/iterator_range_core.hpp>
#include <limits>
#include <regex>
namespace sight::activity::extension
{
activity_config_param::activity_config_param(const config_t& _config) :
replace(_config.get<std::string>("<xmlattr>.replace")),
// @deprecated This is no longer necessary to use "uid" to get the prefix replacement, since
// this is now done in config. However we keep that code for a while for backward compatibility
by(_config.get_optional<std::string>("<xmlattr>.uid").get_value_or(""))
{
if(by.empty())
{
by = _config.get<std::string>("<xmlattr>.by");
}
}
//-----------------------------------------------------------------------------
activity_config::activity_config(const config_t& _config) :
id(_config.get<std::string>("<xmlattr>.id"))
{
if(_config.count("parameters") == 1)
{
const config_t& config_parameters = _config.get_child("parameters");
for(const auto& v : boost::make_iterator_range(config_parameters.equal_range("parameter")))
{
activity_config_param parameter(v.second);
parameters.push_back(parameter);
}
}
SIGHT_ASSERT("At most 1 <parameters> tag is allowed", _config.count("parameters") < 2);
}
//-----------------------------------------------------------------------------
activity_requirement_key::activity_requirement_key(const config_t& _config) :
key(_config.get_value<std::string>())
{
}
//-----------------------------------------------------------------------------
activity_requirement::activity_requirement(const config_t& _config) :
name(_config.get<std::string>("<xmlattr>.name")),
type(_config.get<std::string>("<xmlattr>.type")),
container(_config.get<std::string>("<xmlattr>.container", "")),
description(_config.get<std::string>("desc", "")),
validator(_config.get<std::string>("validator", "")),
min_occurs(_config.get<unsigned int>("<xmlattr>.minOccurs", 1)),
max_occurs(_config.get<unsigned int>("<xmlattr>.maxOccurs", 1))
{
if(const auto& optional = _config.get_child_optional("config"); optional)
{
object_config = *optional;
}
for(const auto& v : boost::make_iterator_range(_config.equal_range("key")))
{
keys.emplace_back(v.second);
}
if(_config.get<std::string>("<xmlattr>.maxOccurs", "") == "*")
{
this->max_occurs = std::numeric_limits<unsigned int>::max();
}
const std::string& create_str = _config.get<std::string>("<xmlattr>.create", "false");
SIGHT_ASSERT("'create' attribute must be 'true' or 'false'", create_str == "true" || create_str == "false");
create = (create_str == "true");
SIGHT_ASSERT(
"Create option is only available if minOccurs = 0 and maxOccurs = 1",
!create || (min_occurs == 0 && max_occurs == 1)
);
SIGHT_ASSERT(
"minOccurs value shall be equal or greater than 0 and lower or equal to maxOccurs (" << max_occurs << ")",
min_occurs <= max_occurs
);
}
//-----------------------------------------------------------------------------
activity_info::activity_info(const SPTR(core::runtime::extension)& _ext) :
id(_ext->get_config().get<std::string>("id")),
title(_ext->get_config().get<std::string>("title")),
description(_ext->get_config().get<std::string>("desc")),
icon(core::runtime::get_module_resource_file_path(_ext->get_config().get<std::string>("icon")).string()),
tab_info(title),
bundle_id(_ext->get_module()->identifier()),
app_config(_ext->get_config().get_child("appConfig"))
{
const auto& config = _ext->get_config();
tab_info = config.get<std::string>("tabinfo", tab_info);
if(const auto& requirements_cfg = config.get_child_optional("requirements"); requirements_cfg.has_value())
{
for(const auto& elem : boost::make_iterator_range(requirements_cfg->equal_range("requirement")))
{
activity_requirement requirement(elem.second);
requirements.push_back(requirement);
min_max_t& min_max = m_requirement_count[requirement.type];
min_max.first += requirement.min_occurs;
if(requirement.max_occurs < (std::numeric_limits<unsigned int>::max() - min_max.second))
{
min_max.second += requirement.max_occurs;
}
else
{
min_max.second = std::numeric_limits<unsigned int>::max();
}
}
}
builder_impl = config.get<std::string>("builder", "sight::activity::builder::Activity");
// backward compatibility
if(const auto& validator_cfg = config.get_optional<std::string>("validator"); validator_cfg.has_value())
{
const auto& validator_impl_str = validator_cfg.value();
if(!validator_impl_str.empty())
{
validators_impl.push_back(validator_impl_str);
}
}
if(const auto& validators = config.get_child_optional("validators"); validators.has_value())
{
for(const auto& validator : boost::make_iterator_range(validators->equal_range("validator")))
{
validators_impl.push_back(validator.second.get_value<std::string>());
}
}
// Set Default validator if none is defined
if(validators_impl.empty())
{
validators_impl.emplace_back("sight::activity::validator::default_activity");
}
}
//-----------------------------------------------------------------------------
bool activity_info::usable_with(data_count_t _data_counts) const
{
bool ok = _data_counts.size() <= m_requirement_count.size();
if(ok)
{
for(const auto& req_count : m_requirement_count)
{
const min_max_t& req_min_max = req_count.second;
auto iter = _data_counts.find(req_count.first);
if(iter != _data_counts.end())
{
unsigned int data_count = iter->second;
ok = (data_count != 0U) && req_min_max.first <= data_count && data_count <= req_min_max.second;
_data_counts.erase(iter);
}
else
{
ok = (req_min_max.first == 0);
}
if(!ok)
{
break;
}
}
if(ok)
{
for(const auto& data_count : _data_counts)
{
if(m_requirement_count.find(data_count.first) == m_requirement_count.end())
{
ok = false;
break;
}
}
}
}
return ok;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
activity::sptr activity::get_default()
{
static auto s_activity = std::make_shared<sight::activity::extension::activity>();
return s_activity;
}
//-----------------------------------------------------------------------------
void activity::parse_plugin_infos()
{
std::vector<SPTR(core::runtime::extension)> extensions =
core::runtime::get_all_extensions_for_point("sight::activity::extension");
for(const SPTR(core::runtime::extension) & ext : extensions)
{
SIGHT_DEBUG("Parsing <" << ext->get_module()->identifier() << "> Activity");
activity_info info(ext);
core::mt::write_lock lock(m_registry_mutex);
SIGHT_ASSERT(
"The id " << info.id << "(" << info.title << ")"
<< " already exists in the Activity registry",
m_reg.find(info.id) == m_reg.end()
);
m_reg.insert(registry::value_type(info.id, info));
}
}
//-----------------------------------------------------------------------------
void activity::clear_registry()
{
core::mt::write_lock lock(m_registry_mutex);
m_reg.clear();
}
//-----------------------------------------------------------------------------
bool activity::has_info(const std::string& _extension_id) const
{
core::mt::read_lock lock(m_registry_mutex);
auto iter = m_reg.find(_extension_id);
return iter != m_reg.end();
}
//-----------------------------------------------------------------------------
std::vector<activity_info> activity::get_infos() const
{
std::vector<activity_info> infos;
core::mt::read_lock lock(m_registry_mutex);
for(const auto& val : m_reg)
{
infos.push_back(val.second);
}
return infos;
}
//-----------------------------------------------------------------------------
activity_info::data_count_t activity::get_data_count(const data::vector::csptr& _data)
{
activity_info::data_count_t data_count;
for(const data::object::csptr obj : *_data)
{
++data_count[obj->get_classname()];
}
return data_count;
}
//-----------------------------------------------------------------------------
std::vector<activity_info> activity::get_infos(const data::vector::csptr& _data) const
{
activity_info::data_count_t data_count = get_data_count(_data);
std::vector<activity_info> infos;
core::mt::read_lock lock(m_registry_mutex);
for(const auto& reg_value : m_reg)
{
const activity_info& activity = reg_value.second;
if(activity.usable_with(data_count))
{
infos.push_back(activity);
}
}
return infos;
}
//-----------------------------------------------------------------------------
std::vector<std::string> activity::get_keys() const
{
std::vector<std::string> keys;
core::mt::read_lock lock(m_registry_mutex);
for(const auto& val : m_reg)
{
keys.push_back(val.first);
}
return keys;
}
//-----------------------------------------------------------------------------
activity_info activity::get_info(const std::string& _extension_id) const
{
core::mt::read_lock lock(m_registry_mutex);
auto iter = m_reg.find(_extension_id);
SIGHT_ASSERT(
"The id " << _extension_id << " is not found in the application configuration parameter registry",
iter != m_reg.end()
);
return iter->second;
}
//-----------------------------------------------------------------------------
std::tuple<activity_info, std::map<std::string, std::string> > activity::get_info_and_replacement_map(
const data::activity& _activity,
const activity_config_params_type& _parameters
) const
{
// Retrieve the activity informations
const auto& info = get_info(_activity.get_activity_config_id());
return {info, get_replacement_map(_activity, info, _parameters)};
}
//------------------------------------------------------------------------------
std::map<std::string, std::string> activity::get_replacement_map(
const data::activity& _activity,
const activity_info& _info,
const activity_config_params_type& _parameters
)
{
std::map<std::string, std::string> replacement_map;
// First, use requirements to populate replacement map with an object from the root map
for(const auto& requirement : _info.requirements)
{
// Use the name as "key" for the config parameter
const auto& it = _activity.find(requirement.name);
if(it != _activity.end() && it->second)
{
replacement_map[requirement.name] = it->second->get_id();
}
}
const auto add_parameters =
[&replacement_map](const auto& _parameters)
{
for(const auto& parameter : _parameters)
{
// @TODO This should be removed later when legacy xml code is cleaned.
SIGHT_FATAL_IF(
"Cannot replace '" << parameter.replace << "' by CAMP path '" << parameter.by << "'.",
parameter.is_object_path()
);
replacement_map[parameter.replace] = parameter.by;
}
};
// Then, use config parameters
// Parameters will override already present requirements with the same name
add_parameters(_info.app_config.parameters);
// Finally, use additional parameters...
add_parameters(_parameters);
// Store the activity UID
replacement_map["AS_UID"] = _activity.get_id();
return replacement_map;
}
} // namespace sight::activity::extension
|