1#ifndef UTOPIA_DATAIO_FACTORY_HH
2#define UTOPIA_DATAIO_FACTORY_HH
7#include <unordered_map>
11#include <boost/graph/adjacency_list.hpp>
12#include <boost/graph/adjacency_matrix.hpp>
13#include <boost/graph/graph_traits.hpp>
14#include <boost/graph/properties.hpp>
16#include <boost/hana/ext/std/tuple.hpp>
17#include <boost/hana/integral_constant.hpp>
18#include <boost/hana/remove_at.hpp>
19#include <boost/hana/transform.hpp>
22#include "../../core/logging.hh"
23#include "../../core/type_traits.hh"
24#include "../cfg_utils.hh"
103template <
class Model, TypeTag typetag = TypeTag::plain >
107 static std::unordered_map<
109 std::function< std::string(std::string,
Model&) > >
129 template <
class Func,
class AttributeHandle >
133 using AttrType = std::decay_t< AttributeHandle >;
136 if constexpr (std::is_same_v< AttrType, Nothing >)
140 else if constexpr (Utopia::Utils::has_static_size_v< AttrType >)
145 std::decay_t< std::tuple_element_t< 0, AttrType > >;
151 Utils::is_string_v< Nametype >,
152 "Error, first entry of Dataset_attribute must be a string "
153 "naming the attribute");
158 auto pos = name.find(
'$');
160 if (
pos != std::string::npos)
182 Utils::is_callable_v< AttrType >,
183 "Error, if the given attribute argument is not a tuple/pair "
184 "and not 'Nothing', it has to be a function");
208 if (
pos != std::string::npos)
220 auto&&
model) -> std::shared_ptr< HDFDataset > {
221 return group->open_dataset(
233 std::shared_ptr< HDFGroup >&
group,
234 Model&) -> std::shared_ptr< HDFDataset > {
236 return group->open_dataset(
259 template <
class SourceGetter,
class Getter >
265 using GraphType = std::decay_t<
266 std::invoke_result_t< std::decay_t< SourceGetter >,
Model& > >;
271 std::shared_ptr< HDFDataset >&
dataset,
277 typename GraphType::vertex_iterator
v,
v_end;
288 std::shared_ptr< HDFDataset >&
dataset,
293 typename GraphType::edge_iterator
v,
v_end;
302 not std::is_same_v< std::decay_t< Getter >,
303 std::function<
void() > >)
307 std::shared_ptr< HDFDataset >&
dataset,
312 typename GraphType::vertex_iterator
v,
v_end;
320 not std::is_same_v< std::decay_t< Getter >,
321 std::function<
void() > >)
324 std::shared_ptr< HDFDataset >&
dataset,
329 typename GraphType::edge_iterator
v,
v_end;
340 else if constexpr (std::is_same_v< std::decay_t< Getter >,
345 std::shared_ptr< HDFDataset >&
dataset,
351 return boost::get(boost::vertex_index_t(),
graph,
vd);
356 else if constexpr (std::is_same_v< std::decay_t< Getter >,
368 return boost::get(boost::vertex_index_t(),
374 return boost::get(boost::vertex_index_t(),
383 throw std::invalid_argument(
"Unknown ObjectType.");
452 std::pair< std::string,
453 std::shared_ptr< Default::DefaultWriteTask< Model > > >
467 std::decay_t< decltype(get_source(std::declval< Model& >())) >;
471 Utils::is_container_v< std::decay_t< Container > >
or
472 Utils::is_graph_v< std::decay_t< Container > >,
473 "Error, the argument 'get_source' must return a container "
474 "type or graph, i.e., "
475 "a type with an iterator");
483 Default::DefaultAttributeWriterGroup< Model > >(
487 Default::DefaultAttributeWriterDataset< Model > >(
490 Default::DefaultDataWriter< Model >
datawriter;
497 "Error in WriteTask factory:, a graph cannot be written "
498 "with TypeTag::plain, see documentation of TypeTag enum");
500 if constexpr (Utils::is_graph_v< std::decay_t< Container > >
and
505 std::forward< Getter >(
getter));
511 std::shared_ptr< HDFDataset >&
dataset,
519 return std::make_pair(
521 std::make_shared< Default::DefaultWriteTask< Model > >(
552 std::pair< std::string,
553 std::shared_ptr< Default::DefaultWriteTask< Model > > >
561 return std::make_pair(
574template <
typename Model, TypeTag typetag >
575std::unordered_map< std::string,
576 std::function< std::string(std::string,
Model&) > >
578 std::unordered_map< std::string,
579 std::function< std::string(std::string,
Model&) > >{
581 [](std::string path,
Model&
m) {
582 return path +
"_" + std::to_string(
m.get_time());
597template <
class Model >
611 template <
typename ArgTpl >
612 std::pair< std::string,
613 std::shared_ptr< Default::DefaultWriteTask< Model > > >
623 else if (
typetag ==
"edge_property")
629 else if (
typetag ==
"vertex_descriptor")
635 else if (
typetag ==
"vertex_property")
673 template <
typename...
Args >
676 const std::tuple< Args... >&
args,
678 Default::default_deciders<Model>,
680 Default::default_triggers<Model>)
683 const auto _log = spdlog::get(
"data_mngr");
685 if constexpr (
sizeof...(Args) == 0)
687 _log->info(
"Empty argument tuple for DataManager factory, "
688 "building default ...");
695 std::map< std::string, Config >
tasknodes;
698 _log->info(
"Name of current task: {}",
699 node.first.as< std::string >());
708 std::shared_ptr< Default::DefaultWriteTask< Model > > >
711 boost::hana::for_each(
721 std::string name =
"";
733 throw std::invalid_argument(
735 "' was not found in the config!");
752 boost::hana::remove_at(
arg_tpl,
753 boost::hana::size_t< 0 >{}),
755 return boost::hana::type_c<
756 std::decay_t<
decltype(
t) > >;
760 decltype(boost::hana::unpack(
762 boost::hana::template_<
768 _log->info(
"Building write task '{}' via factory ...",
785 get_as< std::string >(
"dataset_path",
786 tasknode_iter->second),
787 (tasknode_iter->second[
"capacity"]
788 ? get_as< std::vector< hsize_t > >(
789 "capacity", tasknode_iter->second)
790 : std::vector< hsize_t >{}),
794 : std::vector< hsize_t >{}),
804 boost::hana::remove_at(
arg_tpl,
805 boost::hana::size_t< 0 >{}));
807 _log->info(
"Building write task '{}' via factory ...",
814 _log->info(
"Forwarding arguments to DataManager constructor ...");
Factory function which produces a Datamanager of type Default::DefaultDataManager<Model> from a confi...
Definition factory.hh:599
std::pair< std::string, std::shared_ptr< Default::DefaultWriteTask< Model > > > _call_taskfactory(std::string typetag, ArgTpl &&arg_tpl)
Function which calls the taskfactory with argument tuples, and takes care of using the correct type-t...
Definition factory.hh:614
auto operator()(const Config &conf, const std::tuple< Args... > &args, const Default::DefaultDecidermap< Model > &deciderfactories=Default::default_deciders< Model >, const Default::DefaultTriggermap< Model > &triggerfactories=Default::default_triggers< Model >)
Builds a new datamanager from a config file and a tuple of tuples of arguments.
Definition factory.hh:675
Manage different tasks of writing out data from a source in a uniform yet flexible way....
Definition data_manager.hh:131
Functor for building a writetask from arguments.
Definition factory.hh:105
std::pair< std::string, std::shared_ptr< Default::DefaultWriteTask< Model > > > operator()(std::string name, std::string basegroup_path, DatasetDescriptor dataset_descriptor, SourceGetter &&get_source, Getter &&getter, Group_attribute &&group_attribute=Nothing{}, Dataset_attribute &&dataset_attribute=Nothing{})
Basic factory function producing Default::DefaultWriteTask<Model> intstances, for writing out data....
Definition factory.hh:454
Func _make_attribute_writer(AttributeHandle &&attr)
Function which produces functions for writing attributes to a dataset or group, and which are used by...
Definition factory.hh:131
std::pair< std::string, std::shared_ptr< Default::DefaultWriteTask< Model > > > operator()(std::string name, Default::DefaultBaseGroupBuilder group_builder, Default::DefaultDataWriter< Model > writer, Default::DefaultBuilder< Model > dataset_builder, Default::DefaultAttributeWriterGroup< Model > group_attr, Default::DefaultAttributeWriterDataset< Model > dset_attr)
Thin wrapper around the writetask constructor which allows to construct a writetask via the factory b...
Definition factory.hh:554
Default::DefaultBuilder< Model > _make_dataset_builder(DatasetDescriptor dataset_descriptor)
Function producing a dataset builder function of type Default::DefaultBuilder<Model>,...
Definition factory.hh:201
Default::DefaultDataWriter< Model > _adapt_graph_writer(SourceGetter &&get_source, Getter &&getter)
Function which adapts getter functions for the correct graph accessor type, i.e., vertex_descriptor e...
Definition factory.hh:261
Base class interface for Models using the CRT Pattern.
Definition model.hh:112
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition types.hh:71
std::function< void(std::shared_ptr< HDFDataset > &, Model &) > DefaultAttributeWriterDataset
Type of the default attribute writer for datasets.
Definition defaults.hh:54
std::function< std::shared_ptr< HDFGroup >(std::shared_ptr< HDFGroup > &&) > DefaultBaseGroupBuilder
Type of the default group builder.
Definition defaults.hh:34
std::unordered_map< std::string, std::function< std::shared_ptr< Decider< Model > >() > > DefaultDecidermap
Definition defaults.hh:406
std::function< std::shared_ptr< HDFDataset >(std::shared_ptr< HDFGroup > &, Model &) > DefaultBuilder
Type of the default dataset builder.
Definition defaults.hh:44
std::function< void(std::shared_ptr< HDFGroup > &, Model &) > DefaultAttributeWriterGroup
Type of the default attribute writer for groups.
Definition defaults.hh:49
DefaultDecidermap< Model > DefaultTriggermap
Definition defaults.hh:454
std::function< void(std::shared_ptr< HDFDataset > &, Model &) > DefaultDataWriter
Type of the default data writer.
Definition defaults.hh:39
static std::unordered_map< std::string, std::function< std::string(std::string, Model &) > > _modifiers
Initialization of the modifier map.
Definition factory.hh:110
TypeTag
TypeTag enumerates the kind of access which is used to write data. It became necessary after integrat...
Definition factory.hh:84
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
constexpr bool is_graph_v
Shorthand for is_graph<T>::value.
Definition type_traits.hh:418
Metafunction for use with boost hana, check if all T are callable types.
Definition utils.hh:39
Descriptor for a dataset. Contains: path: string giving the path of the dataset in its group or file ...
Definition factory.hh:65
std::vector< hsize_t > dataset_chunksize
Definition factory.hh:68
std::string path
Definition factory.hh:66
int dataset_compression
Definition factory.hh:69
std::vector< hsize_t > dataset_capacity
Definition factory.hh:67
Functor representing what is considered the most widely used execution process for writing data.
Definition defaults.hh:84
Encapsulate a task for writing data to a destination. Containes a callable 'writer' responisible for ...
Definition write_task.hh:50
Represent a type that does nothing and represents nothing, hence can be used in metaprogramming whene...
Definition type_traits.hh:742