persona-generator 4.1.3
Complete characters composed from the other dasmig generators for C++23
Loading...
Searching...
No Matches
dasmig::psg Class Reference

persona-generator: runs the persona pipeline. More...

#include <personagen.hpp>

Classes

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

Public Member Functions

 psg ()
 Draws from the singletons of the eight libraries.
 
 psg (sources from)
 Draws from the given library instances (which must outlive this generator).
 
 psg (const psg &)=delete
 Not copyable.
 
psg & operator= (const psg &)=delete
 Not copyable.
 
 psg (psg &&) noexcept=default
 Movable.
 
psg & operator= (psg &&) noexcept=default
 Movable.
 
 ~psg ()=default
 Destructor.
 
bool has_data () const
 Check whether every library has data.
 
bool load (dataset tier)
 Load a tier into every library.
 
std::uint64_t data_hash () const
 Hash of the data of all eight libraries.
 
persona get_persona (const persona_request &request)
 Generate a persona with a new random seed.
 
persona get_persona (const persona_request &request, std::uint64_t call_seed) const
 Generate the persona for a call seed. Deterministic; does not touch the generator's engine.
 
std::vector< persona > get_persona_batch (const persona_request &request, const core::batch_options &options)
 Generate a batch of persona results from a seed drawn like get_persona() draws its own.
 
std::vector< persona > get_persona_batch (const persona_request &request, const core::batch_options &options, std::uint64_t batch_seed) const
 Generate a batch of persona results, deterministically.
 
template<typename Sink >
void for_each_persona (const persona_request &request, const core::batch_options &options, std::uint64_t batch_seed, Sink &&sink) const
 Stream a batch of persona results to a sink without holding the whole batch.
 
template<typename Sink >
void for_each_persona (const persona_request &request, const core::batch_options &options, Sink &&sink)
 Stream a batch of persona results with a batch seed drawn like get_persona() draws its own.
 

Static Public Member Functions

static psg & instance ()
 Access the global singleton instance.
 
static std::span< const std::string_view > supported_countries ()
 The countries a random persona is drawn from.
 

Detailed Description

persona-generator: runs the persona pipeline.

The pipeline draws a country, then a city, a birth, a body, a name, interests, an occupation and a personality, each conditioned on what was drawn before (the order and dependencies are declared as entity-generator fields, so they are checked, not assumed). Each library gets its own call seed, split from the persona's seed by the field's key, so the persona's seed replays all of it, and forcing one choice (a country, a sex) does not shift the draws of the others. Output is identical on every compiler and platform (dasmig-core).

The generator holds no data of its own: it calls the instances of the eight libraries it was built on (the singletons by default), so their data, their layers and their lite or full tiers are the persona's.

Thread safety
get_persona(request, seed) is const and only calls const members of the libraries, so it may run concurrently. get_persona(request) draws from the generator's engine and needs external synchronisation on the same instance.

Definition at line 224 of file personagen.hpp.

Constructor & Destructor Documentation

◆ psg() [1/2]

dasmig::psg::psg ( )
inline

Draws from the singletons of the eight libraries.

Definition at line 242 of file personagen.hpp.

◆ psg() [2/2]

dasmig::psg::psg ( sources  from)
inlineexplicit

Draws from the given library instances (which must outlive this generator).

Parameters
fromThe instances to use.

Definition at line 247 of file personagen.hpp.

Member Function Documentation

◆ data_hash()

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

Hash of the data of all eight libraries.

Returns
A hash that changes when any library's loaded data does.

Definition at line 307 of file personagen.hpp.

◆ for_each_persona() [1/2]

template<typename Sink >
void dasmig::psg::for_each_persona ( const persona_request &  request,
const core::batch_options &  options,
Sink &&  sink 
)
inline

Stream a batch of persona results with a batch seed drawn like get_persona() draws its own.

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

Definition at line 470 of file personagen.hpp.

◆ for_each_persona() [2/2]

template<typename Sink >
void dasmig::psg::for_each_persona ( const persona_request &  request,
const core::batch_options &  options,
std::uint64_t  batch_seed,
Sink &&  sink 
) const
inline

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

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

Template Parameters
SinkCallable taking (std::uint64_t, persona&); 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 455 of file personagen.hpp.

◆ get_persona() [1/2]

persona dasmig::psg::get_persona ( const persona_request &  request)
inline

Generate a persona with a new random seed.

Parameters
requestWhat to ask for; {} for the defaults.
Returns
The persona; seed() replays it.
Exceptions
std::runtime_errorIf a library has no data.
std::invalid_argumentIf the request is invalid.

Definition at line 325 of file personagen.hpp.

◆ get_persona() [2/2]

persona dasmig::psg::get_persona ( const persona_request &  request,
std::uint64_t  call_seed 
) const
inline

Generate the persona for a call seed. Deterministic; does not touch the generator's engine.

Parameters
requestWhat to ask for.
call_seedAny 64-bit seed.
Returns
The same persona for the same seed, request and data.
Exceptions
std::runtime_errorIf a library has no data.
std::invalid_argumentIf the request is invalid.

Definition at line 337 of file personagen.hpp.

◆ get_persona_batch() [1/2]

std::vector< persona > dasmig::psg::get_persona_batch ( const persona_request &  request,
const core::batch_options &  options 
)
inline

Generate a batch of persona results from a seed drawn like get_persona() draws its own.

Item i is get_persona(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 412 of file personagen.hpp.

◆ get_persona_batch() [2/2]

std::vector< persona > dasmig::psg::get_persona_batch ( const persona_request &  request,
const core::batch_options &  options,
std::uint64_t  batch_seed 
) const
inline

Generate a batch of persona results, deterministically.

Item i is exactly get_persona(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 persona).
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 433 of file personagen.hpp.

◆ has_data()

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

Check whether every library has data.

Returns
True when all eight libraries can generate.

Definition at line 278 of file personagen.hpp.

◆ instance()

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

Access the global singleton instance.

Returns
A reference to the singleton.

Definition at line 257 of file personagen.hpp.

◆ load()

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

Load a tier into every library.

Parameters
tierdataset::lite or dataset::full.
Returns
True when all eight libraries found their data.

Definition at line 289 of file personagen.hpp.

◆ supported_countries()

static std::span< const std::string_view > dasmig::psg::supported_countries ( )
inlinestatic

The countries a random persona is drawn from.

The countries for which all eight libraries have data in their lite tier. A country can be asked for by persona_request::country outside this list; the libraries then use what they have for it, or reject it (South Korea, for example, needs name-generator's full tier: its lite tier has no Korean male names).

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

Definition at line 271 of file personagen.hpp.


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