[][src]Function graph_builder::gen

pub fn gen<T, U, F, G, H, E>(
    (nodes, edges): Graph<T, U>,
    n: usize,
    f: F,
    g: G,
    h: H,
    settings: &GenerateSettings
) -> Result<Graph<T, U>, (Graph<T, U>, E)> where
    T: Eq + Hash + Clone,
    F: Fn(&T, usize) -> Result<(T, U), E>,
    G: Fn(&T) -> bool,
    H: Fn(&U, &U) -> Result<U, Option<E>>,
    E: From<GenerateError>, 

Generates a graph from:

  • an initial seed state a
  • maximum n edges
  • a function f to generate a new node with edge
  • a filter g to post-process nodes
  • a composer h to use for transforming edges when nodes are filtered
  • settings to control usage of memory

Returns a list of nodes and a list of edges.

  • Ok if generation was successful without hitting memory limits
  • Err if generation hit memory limits

Waiting with filtering until post-processing makes it possible to create an edge between nodes that require multiple steps.

The maximum number of edges is usually determined from the length of a list of valid operations.

Error handling

The algorithm continues to post-processing when hitting memory limits. It is assumed that one wants to make use of the data generated, where memory limits are met often due to combinatorial explosions.

The algorithm assumes that there are other limits besides those in GenerateSettings. To handle errors, one adds a type that implements From<GenerateError>. The unit type () can be used when error handling is not needed.

Tip: Since this function requires 6 generic type arguments, it is easier to set the error type on the output type and let Rust infer the rest.

For example:

This example is not tested
let (nodes, edges) = match gen(start, n, f, g, h, &settings) {
    Ok(x) => x,
    Err((x, ())) => x,
};

When an error happens during composing edges, one can choose whether to report the error with Err(Some(err)), or ignore it with Err(None). This is useful because sometimes you want to filter edges without reporting errors. The algorithm assumes that one wishes to continue generating the graph when encountering an error. Only the first error will be reported.