Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Static Private Attributes | List of all members
Utopia::DataIO::TaskFactory< Model, typetag > Class Template Reference

Functor for building a writetask from arguments. More...

#include <factory.hh>

Public Member Functions

template<class SourceGetter , class Getter , class Group_attribute = Nothing, class Dataset_attribute = Nothing>
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. It is inteded to make the setup of a WriteTask simpler for common cases.
 
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 by providing all the functions the latter employs by hand. This is intended for cases where the other operator() is too restrictive.
 

Private Member Functions

template<class Func , class AttributeHandle >
Func _make_attribute_writer (AttributeHandle &&attr)
 Function which produces functions for writing attributes to a dataset or group, and which are used by a writer task to write attributes to the dataset and basegroup it creates.
 
Default::DefaultBuilder< Model_make_dataset_builder (DatasetDescriptor dataset_descriptor)
 Function producing a dataset builder function of type Default::DefaultBuilder<Model>, which is responsible for creating new HDF5 datasets on request.
 
template<class SourceGetter , class Getter >
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 etc.
 

Static Private Attributes

static std::unordered_map< std::string, std::function< std::string(std::string, Model &) > > _modifiers
 Initialization of the modifier map.
 

Detailed Description

template<class Model, TypeTag typetag = TypeTag::plain>
class Utopia::DataIO::TaskFactory< Model, typetag >

Functor for building a writetask from arguments.

Template Parameters
ModelModel type.
TypeTag::plainTypetag, indicates what type of data source is used, has only to be changed when using graphs.

Member Function Documentation

◆ _adapt_graph_writer()

template<class Model , TypeTag typetag = TypeTag::plain>
Default::DefaultDataWriter< Model > Utopia::DataIO::TaskFactory< Model, typetag >::_adapt_graph_writer ( SourceGetter &&  get_source,
Getter &&  getter 
)
inlineprivate

Function which adapts getter functions for the correct graph accessor type, i.e., vertex_descriptor etc.

Template Parameters
SourceGetterautomatically determined
Getterautomatically determined
Parameters
get_sourceFunction which extracts the source to get data from from its superior structure, e.g., extracting a vertex list from a graph
getterFunction which extracts the data to write from the source, e.g., from the vertex
Returns
Default::DefaultDataWriter< Model >
262 {
263 Default::DefaultDataWriter< Model > writer;
264
265 using GraphType = std::decay_t<
266 std::invoke_result_t< std::decay_t< SourceGetter >, Model& > >;
267
268 if constexpr (typetag == TypeTag::vertex_property)
269 {
270 writer = [&getter, &get_source](
271 std::shared_ptr< HDFDataset >& dataset,
272 Model& m) -> void {
273 // Collect some information on the graph
274 auto& graph = get_source(m);
275
276 // Make vertex iterators
277 typename GraphType::vertex_iterator v, v_end;
278 boost::tie(v, v_end) = boost::vertices(graph);
279
280 dataset->write(v, v_end, [&getter, &graph](auto&& vd) {
281 return getter(graph[vd]);
282 });
283 };
284 }
285 else if constexpr (typetag == TypeTag::edge_property)
286 {
287 writer = [&getter, &get_source](
288 std::shared_ptr< HDFDataset >& dataset,
289 Model& m) -> void {
290 auto& graph = get_source(m);
291
292 // Make edge iterators
293 typename GraphType::edge_iterator v, v_end;
294 boost::tie(v, v_end) = boost::edges(graph);
295
296 dataset->write(v, v_end, [&getter, &graph](auto&& vd) {
297 return getter(graph[vd]);
298 });
299 };
300 }
301 else if constexpr (typetag == TypeTag::vertex_descriptor and
302 not std::is_same_v< std::decay_t< Getter >,
303 std::function< void() > >)
304 {
305
306 writer = [&getter, &get_source](
307 std::shared_ptr< HDFDataset >& dataset,
308 Model& m) -> void {
309 auto& graph = get_source(m);
310
311 // Make edge iterators
312 typename GraphType::vertex_iterator v, v_end;
313 boost::tie(v, v_end) = boost::vertices(graph);
314 dataset->write(v, v_end, [&getter, &graph](auto&& vd) {
315 return getter(graph, vd);
316 });
317 };
318 }
319 else if constexpr (typetag == TypeTag::edge_descriptor and
320 not std::is_same_v< std::decay_t< Getter >,
321 std::function< void() > >)
322 {
323 writer = [&getter, &get_source](
324 std::shared_ptr< HDFDataset >& dataset,
325 Model& m) -> void {
326 auto& graph = get_source(m);
327
328 // Make edge iterators
329 typename GraphType::edge_iterator v, v_end;
330 boost::tie(v, v_end) = boost::edges(graph);
331
332 dataset->write(v, v_end, [&getter, &graph](auto&& vd) {
333 return getter(graph, vd);
334 });
335 };
336 }
337 // for writing pure graphs
338
339 // vertex
340 else if constexpr (std::is_same_v< std::decay_t< Getter >,
341 std::function< void() > > and
343 {
344 writer = [&get_source](
345 std::shared_ptr< HDFDataset >& dataset,
346 Model& m) -> void {
347 auto& graph = get_source(m);
348
349 auto [v, v_end] = boost::vertices(graph);
350 dataset->write(v, v_end, [&](auto&& vd) {
351 return boost::get(boost::vertex_index_t(), graph, vd);
352 });
353 };
354 }
355 // edges
356 else if constexpr (std::is_same_v< std::decay_t< Getter >,
357 std::function< void() > > and
359 {
360 writer =
361 [get_source](std::shared_ptr< HDFDataset >& dataset,
362 Model& m) -> void {
363 auto& graph = get_source(m);
364
365 auto [e, e_end] = boost::edges(graph);
366
367 dataset->write(e, e_end, [&](auto&& ed) {
368 return boost::get(boost::vertex_index_t(),
369 graph,
370 boost::source(ed, graph));
371 });
372
373 dataset->write(e, e_end, [&](auto&& ed) {
374 return boost::get(boost::vertex_index_t(),
375 graph,
376 boost::target(ed, graph));
377 });
378 };
379 }
380 else
381 {
382 // user fucked up
383 throw std::invalid_argument("Unknown ObjectType.");
384 }
385
386 return writer;
387 }
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
boost::adjacency_list< EdgeContainer, VertexContainer, boost::bidirectionalS, Vertex, Edge > GraphType
The type of the graph.
Definition CopyMeGraph.hh:146

◆ _make_attribute_writer()

template<class Model , TypeTag typetag = TypeTag::plain>
Func Utopia::DataIO::TaskFactory< Model, typetag >::_make_attribute_writer ( AttributeHandle &&  attr)
inlineprivate

Function which produces functions for writing attributes to a dataset or group, and which are used by a writer task to write attributes to the dataset and basegroup it creates.

Template Parameters
FuncFunction type to produce
Attributeautomatically determined
Parameters
attrEither 'Nothing' if no attributes shall be written, or a tuple/pair (name, attribute_data) or a function convertible to type Func which takes care of writing dataset attributes, and receives a reference to a model as argument
Returns
Func Function which writes attributes as constructed from arguments.
132 {
133 using AttrType = std::decay_t< AttributeHandle >;
134 Func writer{};
135
136 if constexpr (std::is_same_v< AttrType, Nothing >)
137 {
138 // do nothing here, because nothing shall be done ;)
139 }
140 else if constexpr (Utopia::Utils::has_static_size_v< AttrType >)
141 {
142
143 using std::get;
144 using Nametype =
145 std::decay_t< std::tuple_element_t< 0, AttrType > >;
146
147 // if the first thing held by the Dataset_attribute tuplelike is
148 // not convertible to stirng, and hence cannot be used to name
149 // the thing, error is thrown.
150 static_assert(
151 Utils::is_string_v< Nametype >,
152 "Error, first entry of Dataset_attribute must be a string "
153 "naming the attribute");
154
155 // check if the $ indicating string interpolation like behavior
156 // is found. If yes, invoke path modifier, else leave as is
157 std::string name = get< 0 >(attr);
158 auto pos = name.find('$'); // find indicator
159
160 if (pos != std::string::npos)
161 {
162 auto path_builder = _modifiers[name.substr(pos + 1)];
163 std::string new_path = name.substr(0, pos);
164
166 auto&& m) -> void {
167 hdfobject->add_attribute(path_builder(new_path, m),
168 get< 1 >(attr));
169 };
170 }
171 else
172 {
173 writer = [attr](auto&& hdfobject, auto &&) -> void {
174 hdfobject->add_attribute(get< 0 >(attr),
175 get< 1 >(attr));
176 };
177 }
178 }
179 else
180 {
181 static_assert(
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");
185
186 writer = attr;
187 }
188
189 return writer;
190 }
static std::unordered_map< std::string, std::function< std::string(std::string, Model &) > > _modifiers
Initialization of the modifier map.
Definition factory.hh:110

◆ _make_dataset_builder()

template<class Model , TypeTag typetag = TypeTag::plain>
Default::DefaultBuilder< Model > Utopia::DataIO::TaskFactory< Model, typetag >::_make_dataset_builder ( DatasetDescriptor  dataset_descriptor)
inlineprivate

Function producing a dataset builder function of type Default::DefaultBuilder<Model>, which is responsible for creating new HDF5 datasets on request.

Parameters
dataset_descriptorDescribes dataset properties.
Returns
auto Builder object
202 {
203 Default::DefaultBuilder< Model > dataset_builder;
204
205 auto pos = dataset_descriptor.path.find('$'); // find indicator
206
207 // $ indicates that string interpolation shall be used
208 if (pos != std::string::npos)
209 {
210 // get the path builder corresponding to the flag extracted after
211 // indicator
212 auto path_builder =
213 _modifiers[dataset_descriptor.path.substr(pos + 1)];
214
215 std::string new_path = dataset_descriptor.path.substr(0, pos);
216 // put the latter into the dataset builder
219 auto&& group,
220 auto&& model) -> std::shared_ptr< HDFDataset > {
221 return group->open_dataset(
223 dataset_descriptor.dataset_capacity,
224 dataset_descriptor.dataset_chunksize,
225 dataset_descriptor.dataset_compression);
226 };
227 }
228 else // no string to interpolate something
229 {
230
233 std::shared_ptr< HDFGroup >& group,
234 Model&) -> std::shared_ptr< HDFDataset > {
235
236 return group->open_dataset(
238 dataset_descriptor.dataset_capacity,
239 dataset_descriptor.dataset_chunksize,
240 dataset_descriptor.dataset_compression);
241 };
242 }
243 return dataset_builder;
244 }

◆ operator()() [1/2]

template<class Model , TypeTag typetag = TypeTag::plain>
std::pair< std::string, std::shared_ptr< Default::DefaultWriteTask< Model > > > Utopia::DataIO::TaskFactory< Model, typetag >::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 
)
inline

Thin wrapper around the writetask constructor which allows to construct a writetask via the factory by providing all the functions the latter employs by hand. This is intended for cases where the other operator() is too restrictive.

Parameters
nameName of the task to be build
group_builderFunction to build a HDFGroup to store datasets in
writerFunction which writes out data
dataset_builderFunction to build Datasets
group_attrFunction writing attributes to the task's base group
dset_attrFunction writing attrbutes to the task's currently active dataset
Returns
std::pair<std::string, Default::DefaultWriteTask<Model>>
560 {
561 return std::make_pair(
562 name,
563 std::make_shared< Default::DefaultWriteTask< Model > >(
565 }

◆ operator()() [2/2]

template<class Model , TypeTag typetag = TypeTag::plain>
std::pair< std::string, std::shared_ptr< Default::DefaultWriteTask< Model > > > Utopia::DataIO::TaskFactory< Model, typetag >::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{} 
)
inline

Basic factory function producing Default::DefaultWriteTask<Model> intstances, for writing out data. It is inteded to make the setup of a WriteTask simpler for common cases.

Template Parameters
ModelThe model class the task refers to. The model is the ultimate source of data in utopia context, hence has to be given.
SourceGetterA container type, in this context something which has an iterator, automatically determined.
GetterUnary function type getting SourceGetter::value_type as argument and returning data to be written, automatically determined.
Group_attributeEither callable of type Default::GroupAttributeWriter or a tuplelike object.
Dataset_attributeEither callable of type Default::DatasetAttributeWriter or a tuplelike object.
Parameters
nameString naming this task, to be used with config.
basegroup_pathPath in the HDF5 file to the base_group this task stores its produced datasets in.
get_sourceFunction which returns a container or graph holding the data to use for write
getterUnary function getting a SourceGetter::value_type argument and returning data to be written.
dataset_descriptorDatasetDescriptor instance which gives the properties constructed datasets should have, at the very least its path in the basegroup.
group_attributeEither a callable of type DefaultGroupAttributeWriter or a tuplelike object containing [attribute_name, attribute_data]. Usually, the latter is a descriptive string. The last possiblity is to give 'Nothing', which means that the Attribute should be ignored
dataset_attributeEither a callable of type DefaultGroupAttributeWriter or some a tuplelike object containing [attribute_name, attribute_data]. Usually, the latter is a descriptive string. The last possiblity is to give 'Nothing', which means that the Attribute should be ignored
Returns
std::pair<std::string, Default::DefaultWriteTask<Model>> pair containing a name and a writetask, to be used with the datamanager.
462 {},
464 {
465 // return type of get_source when used with model
466 using Container =
467 std::decay_t< decltype(get_source(std::declval< Model& >())) >;
468
469 // asserts that the SourceGetter-type is really a container type.
470 static_assert(
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");
476
477 // make basegroup builder and attribute builders
478
479 Default::DefaultBuilder< Model > dataset_builder =
481
483 Default::DefaultAttributeWriterGroup< Model > >(
484 std::forward< Group_attribute >(group_attribute));
485
487 Default::DefaultAttributeWriterDataset< Model > >(
488 std::forward< Dataset_attribute >(dataset_attribute));
489
490 Default::DefaultDataWriter< Model > datawriter;
491
492 // assert that the given template argument combination is valid:
493 // A graph cannot be written with TypeTag::plain
494
495 static_assert(not(Utils::is_graph_v< std::decay_t< Container > > and
497 "Error in WriteTask factory:, a graph cannot be written "
498 "with TypeTag::plain, see documentation of TypeTag enum");
499
500 if constexpr (Utils::is_graph_v< std::decay_t< Container > > and
502 {
503 datawriter =
504 _adapt_graph_writer(std::forward< SourceGetter >(get_source),
505 std::forward< Getter >(getter));
506 }
507 else
508 {
509
511 std::shared_ptr< HDFDataset >& dataset,
512 Model& m) -> void {
513 dataset->write(
514 get_source(m).begin(), get_source(m).end(), getter);
515 };
516 }
517
518 // build a defaultwriteTask
519 return std::make_pair(
520 name,
521 std::make_shared< Default::DefaultWriteTask< Model > >(
522 // builds the basegroup to write datasets in
524 [basegroup_path](std::shared_ptr< HDFGroup > parent) {
525 return parent->open_group(basegroup_path);
526 }),
527 // writes out data
529 // builds datasets as needed
531 // writes attributes to base group
533 // writes attributes to dataset
535 }
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
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
std::function< std::shared_ptr< HDFGroup >(std::shared_ptr< HDFGroup > &&) > DefaultBaseGroupBuilder
Type of the default group builder.
Definition defaults.hh:34
constexpr bool is_graph_v
Shorthand for is_graph<T>::value.
Definition type_traits.hh:418

The documentation for this class was generated from the following file: