Relationship Generator 1.2.2
Believable families around a person: parents, siblings, partners and children, with ages and names for C++23
Loading...
Searching...
No Matches
dasmig::relg Class Reference

Relationship Generator: builds believable families around a person. More...

#include <relgen.hpp>

Classes

struct  sources
 The library instances a family is drawn from. More...
 

Public Member Functions

 relg ()=default
 Default constructor — creates an empty generator with no data, drawing from the birth-generator singleton.
 
 relg (sources from)
 A generator with no data that draws from the given library instances (which must outlive it).
 
 relg (const relg &)=delete
 Not copyable.
 
relg & operator= (const relg &)=delete
 Not copyable.
 
 relg (relg &&) noexcept=default
 Movable.
 
relg & operator= (relg &&) noexcept=default
 Movable.
 
 ~relg ()=default
 Destructor.
 
family get_family (const family_request &request)
 Generate a random family. Use family_request{} for the defaults.
 
family get_family (const family_request &request, std::uint64_t call_seed) const
 Generate a deterministic family.
 
family expand (const family &base, std::size_t member, family_request request, std::uint64_t call_seed) const
 Generate the family of a member of a family, so that the two agree.
 
family expand (const family &base, std::size_t member, const family_request &request)
 Generate a random family of a member of a family; see the seeded overload.
 
std::vector< family > get_family_batch (const family_request &request, const core::batch_options &options)
 Generate a batch of family results from a seed drawn like get_family() draws its own.
 
std::vector< family > get_family_batch (const family_request &request, const core::batch_options &options, std::uint64_t batch_seed) const
 Generate a batch of family results, deterministically.
 
template<typename Sink >
void for_each_family (const family_request &request, const core::batch_options &options, std::uint64_t batch_seed, Sink &&sink) const
 Stream a batch of family results to a sink without holding the whole batch.
 
template<typename Sink >
void for_each_family (const family_request &request, const core::batch_options &options, Sink &&sink)
 Stream a batch of family results with a batch seed drawn like get_family() draws its own.
 
bool has_data () const
 Check whether this generator and birth-generator have data.
 
bool has_family_data () const
 Check whether the family tables are loaded (birth-generator is not asked).
 
std::vector< std::string > countries () const
 The countries with figures of their own in any table.
 
void load (const std::filesystem::path &dir)
 Load data from a resource directory, replacing all data and layers loaded before.
 
bool load (dataset tier)
 Load a dataset tier, replacing all data and layers loaded before.
 
void extend (const std::filesystem::path &path, core::layer_mode mode=core::layer_mode::override)
 Add data layers from a directory or a file.
 
const core::layer_info & extend (family_table which, core::tsv_document doc, core::layer_mode mode=core::layer_mode::override)
 Add a data layer to one table.
 
const core::layer_info & extend_from_string (family_table which, std::string_view text, std::string source, core::layer_mode mode=core::layer_mode::override)
 Add a data layer to one table from TSV text (for example from a WebAssembly host).
 
const std::vector< core::layer_info > & layers (family_table which) const
 The layers in effect for one table, in order.
 
std::uint64_t data_hash () const
 Hash of the loaded data: this generator's tables and birth-generator's data. Recorded in each result's signature().
 

Static Public Member Functions

static relg & instance ()
 Access the global singleton instance.
 

Detailed Description

Relationship Generator: builds believable families around a person.

A family is drawn from demography: the mother's age at the anchor's birth from her country's fertility by age, the father from the typical difference in marriage ages, the number of siblings from the total fertility rate of the anchor's birth year (the family size seen from a child is size-biased, so a family with more children is more likely to be the anchor's), and who is alive from a Gompertz survival curve that matches the country's life expectancy. Everything is drawn from named streams split from the call seed ("anchor", "parents", "mother", "father", "siblings", "grandparents"), so asking for grandparents or for no siblings does not change the rest. Output is identical on every compiler and platform (dasmig-core).

The anchor, the country's age pyramid and life expectancy come from birth-generator (the singleton by default, or an instance passed in).

Can be used as a singleton via instance() or constructed independently.

Thread safety
get_family(request, seed) is const and only calls const members of birth-generator, so it may run concurrently. get_family(request) draws from the generator's engine and needs external synchronisation on the same instance; so do load() and extend().

Definition at line 633 of file relgen.hpp.

Constructor & Destructor Documentation

◆ relg()

dasmig::relg::relg ( sources  from)
inlineexplicit

A generator with no data that draws from the given library instances (which must outlive it).

Parameters
fromThe instances to use.

Definition at line 650 of file relgen.hpp.

Member Function Documentation

◆ countries()

std::vector< std::string > dasmig::relg::countries ( ) const
inline

The countries with figures of their own in any table.

Returns
ISO 3166-1 alpha-2 codes, sorted.

Definition at line 1263 of file relgen.hpp.

◆ data_hash()

std::uint64_t dasmig::relg::data_hash ( ) const
inline

Hash of the loaded data: this generator's tables and birth-generator's data. Recorded in each result's signature().

Returns
A hash that changes when either does; 0 when this generator's tables are empty.

Definition at line 1401 of file relgen.hpp.

◆ expand() [1/2]

family dasmig::relg::expand ( const family &  base,
std::size_t  member,
const family_request &  request 
)
inline

Generate a random family of a member of a family; see the seeded overload.

Parameters
baseThe family the member belongs to.
memberThe member's id.
requestWhat to generate around the member.
Returns
The member's family.

Definition at line 1170 of file relgen.hpp.

◆ expand() [2/2]

family dasmig::relg::expand ( const family &  base,
std::size_t  member,
family_request  request,
std::uint64_t  call_seed 
) const
inline

Generate the family of a member of a family, so that the two agree.

The member's own birth, sex, name and country are kept, and so are the relatives base already has for the member: their parents (and the grandparents, if base has them), siblings and half-siblings, and the partners and children that base lists. Whatever base does not hold is drawn, as get_family() draws it: a partner, say, whom base left out is drawn again, and children whose other parent base left out are kept without that partner.

Only what base lists is kept: a member that base generated with partners = false or down = 0 has its partners or children drawn again. The request's anchor is ignored (member takes its place), and so is its reference_date (that of base is used); up, down, siblings, partners, names, name_culture and weighted apply.

Parameters
baseThe family the member belongs to.
memberThe member's id (base.members[member]).
requestWhat to generate around the member.
call_seedSeed; the same base, member, request and seed give the same family.
Returns
The member's family; its anchor is the member.
Exceptions
std::invalid_argumentIf member is not an id of base.

Definition at line 1155 of file relgen.hpp.

◆ extend() [1/2]

void dasmig::relg::extend ( const std::filesystem::path &  path,
core::layer_mode  mode = core::layer_mode::override 
)
inline

Add data layers from a directory or a file.

A directory may hold any of the table files (see family_table); all are applied together or, if any fails, none. A file must have one of those names.

Parameters
pathDirectory or file.
modeHow the layers combine with the data loaded before.
Exceptions
core::tsv_errorIf a file cannot be read or a row is invalid; the data is then unchanged.
std::invalid_argumentIf path is a file with another name.

Definition at line 1333 of file relgen.hpp.

◆ extend() [2/2]

const core::layer_info & dasmig::relg::extend ( family_table  which,
core::tsv_document  doc,
core::layer_mode  mode = core::layer_mode::override 
)
inline

Add a data layer to one table.

Rows are keyed by the table's key column (cca2, subregion or key). In the default override mode a row replaces the loaded row with the same key and new keys are added; a row with disabled set to 1 removes its key.

Parameters
whichThe table.
docTSV with that table's columns.
modeHow the layer combines with the data loaded before.
Returns
What the layer did (rows added, replaced, removed, warnings).
Exceptions
core::tsv_errorIf a row is invalid; the data is unchanged.

Definition at line 1366 of file relgen.hpp.

◆ extend_from_string()

const core::layer_info & dasmig::relg::extend_from_string ( family_table  which,
std::string_view  text,
std::string  source,
core::layer_mode  mode = core::layer_mode::override 
)
inline

Add a data layer to one table from TSV text (for example from a WebAssembly host).

Parameters
whichThe table.
textTSV with that table's columns.
sourceName used in errors.
modeHow the layer combines with the data loaded before.
Returns
What the layer did.
Exceptions
core::tsv_errorIf a row is invalid; the data is unchanged.

Definition at line 1383 of file relgen.hpp.

◆ for_each_family() [1/2]

template<typename Sink >
void dasmig::relg::for_each_family ( const family_request &  request,
const core::batch_options &  options,
Sink &&  sink 
)
inline

Stream a batch of family results with a batch seed drawn like get_family() draws its own.

Template Parameters
SinkCallable taking (std::uint64_t, family&).
Parameters
requestWhat every item is generated from.
optionsCount, first index, threads and uniqueness.
sinkReceives the items.

Definition at line 1244 of file relgen.hpp.

◆ for_each_family() [2/2]

template<typename Sink >
void dasmig::relg::for_each_family ( const family_request &  request,
const core::batch_options &  options,
std::uint64_t  batch_seed,
Sink &&  sink 
) const
inline

Stream a batch of family results to a sink without holding the whole batch.

Generates the same items as get_family_batch() and calls sink(index, item) in index order on the calling thread; index includes options.first.

Template Parameters
SinkCallable taking (std::uint64_t, family&); for example core::jsonl_writer or core::csv_writer.
Parameters
requestWhat every item is generated from.
optionsCount, first index, threads and uniqueness.
batch_seedSeed of the whole batch.
sinkReceives the items.

Definition at line 1229 of file relgen.hpp.

◆ get_family() [1/2]

family dasmig::relg::get_family ( const family_request &  request)
inline

Generate a random family. Use family_request{} for the defaults.

Parameters
requestWhat to generate.
Returns
The family.
Exceptions
std::runtime_errorIf this generator or birth-generator has no data loaded.
std::invalid_argumentIf the request is invalid.

Definition at line 678 of file relgen.hpp.

◆ get_family() [2/2]

family dasmig::relg::get_family ( const family_request &  request,
std::uint64_t  call_seed 
) const
inline

Generate a deterministic family.

Parameters
requestWhat to generate.
call_seedSeed; the same request, seed and data give the same result. Does not touch the instance engine.
Returns
The family.
Exceptions
std::runtime_errorIf this generator or birth-generator has no data loaded, or names are requested and name-generator has none.
std::invalid_argumentIf up or down is not 0 to 2, the anchor's age is outside 0 to 122, its birth is after the reference date or makes it older than 122, or its country code (matched without regard to case) has no data in birth-generator.

Definition at line 696 of file relgen.hpp.

◆ get_family_batch() [1/2]

std::vector< family > dasmig::relg::get_family_batch ( const family_request &  request,
const core::batch_options &  options 
)
inline

Generate a batch of family results from a seed drawn like get_family() draws its own.

Item i is get_family(request, core::item_seed(batch_seed, options.first + i)) for the drawn batch seed; see the seeded overload.

Parameters
requestWhat every item is generated from.
optionsCount, first index, threads and uniqueness.
Returns
The results, in index order.

Definition at line 1186 of file relgen.hpp.

◆ get_family_batch() [2/2]

std::vector< family > dasmig::relg::get_family_batch ( const family_request &  request,
const core::batch_options &  options,
std::uint64_t  batch_seed 
) const
inline

Generate a batch of family results, deterministically.

Item i is exactly get_family(request, core::item_seed(batch_seed, options.first + i)), so an item replays on its own, a range can be generated directly, two batches that follow each other equal one, and the output does not depend on options.threads.

Parameters
requestWhat every item is generated from.
optionsCount, first index, threads and uniqueness (see core::unique_rule; the rule must be made for family).
batch_seedSeed of the whole batch.
Returns
The results, in index order.
Exceptions
std::invalid_argumentIf the request or options are invalid.
std::runtime_errorIf no data has been loaded, or a unique batch runs out of attempts.

Definition at line 1207 of file relgen.hpp.

◆ has_data()

bool dasmig::relg::has_data ( ) const
inline

Check whether this generator and birth-generator have data.

Returns
True when a family can be generated.

Definition at line 1254 of file relgen.hpp.

◆ has_family_data()

bool dasmig::relg::has_family_data ( ) const
inline

Check whether the family tables are loaded (birth-generator is not asked).

Returns
True when fertility, parameters and defaults are non-empty.

Definition at line 1259 of file relgen.hpp.

◆ instance()

static relg & dasmig::relg::instance ( )
inlinestatic

Access the global singleton instance.

Loads the lite tier (or full if lite is missing) on first access, from compiled-in data or an auto-probed resources/.

Returns
The singleton.

Definition at line 663 of file relgen.hpp.

◆ layers()

const std::vector< core::layer_info > & dasmig::relg::layers ( family_table  which) const
inline

The layers in effect for one table, in order.

Parameters
whichThe table.
Returns
The layer records.

Definition at line 1392 of file relgen.hpp.

◆ load() [1/2]

void dasmig::relg::load ( const std::filesystem::path &  dir)
inline

Load data from a resource directory, replacing all data and layers loaded before.

The directory must contain regions.tsv, fertility.tsv, unions.tsv, defaults.tsv and parameters.tsv; households.tsv is read when present. A path without them is ignored.

Parameters
dirDirectory such as resources/lite.
Exceptions
core::tsv_errorIf a file is malformed; the data is then unchanged.

Definition at line 1282 of file relgen.hpp.

◆ load() [2/2]

bool dasmig::relg::load ( dataset  tier)
inline

Load a dataset tier, replacing all data and layers loaded before.

Uses the compiled-in copy when the library is built with DASMIG_RELATIONSHIP_EMBED, otherwise searches resources/, ../resources, relationship-generator/resources and ../relationship-generator/resources.

Parameters
tierThe tier.
Returns
False when the tier was not found.
Exceptions
core::tsv_errorIf a file is malformed.

Definition at line 1303 of file relgen.hpp.


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