feat(assessment): add table families that pick a variant row by seed

This commit is contained in:
shira committed 2026-10-11 08:19:17 +03:00
1 parent f1147b1885
commit 5e1db37746
16 files changed
+1023 -56

No files matched your search

@@ -36,23 +36,30 @@ type AssessmentParameterRange struct {
Max int32 `json:"max"`
}
// AssessmentSeedGenerator describes how seeds become variants: a named
// algorithm, the ranges of its parameters, and the statement template. The
// algorithm is code that is registered in this package under a versioned
// name, so an old family version keeps its algorithm after a newer one
// exists.
// AssessmentSeedGenerator describes how seeds become variants. There are two
// kinds. An algorithmic generator names an algorithm, the ranges of its
// parameters, and the statement template; the algorithm is code that is
// registered in this package under a versioned name, so an old family version
// keeps its algorithm after a newer one exists. A table generator holds the
// variants themselves as rows with their answers and the statement template;
// the seed selects a row and no code computes the answer. A table generator
// has no algorithm and no parameters.
type AssessmentSeedGenerator struct {
Algorithm string `json:"algorithm"`
Parameters map[string]AssessmentParameterRange `json:"parameters"`
Algorithm string `json:"algorithm,omitempty"`
Parameters map[string]AssessmentParameterRange `json:"parameters,omitempty"`
// StatementTemplate is plain text with {{.name}} placeholders for the
// parameters and the generated input data of the algorithm. Nothing else
// of the text/template language is accepted; see compileStatementTemplate.
// parameters and the generated input data of the algorithm, or for the
// columns of the table. Nothing else of the text/template language is
// accepted; see compileStatementTemplate.
StatementTemplate string `json:"statement_template"`
// Table is set for a table generator and nil for an algorithmic one.
Table *AssessmentVariantTable `json:"table,omitempty"`
}
func (generator AssessmentSeedGenerator) clone() AssessmentSeedGenerator {
copied := generator
copied.Parameters = maps.Clone(generator.Parameters)
copied.Table = generator.Table.clone()
return copied
}
@@ -63,6 +70,9 @@ func (generator AssessmentSeedGenerator) clone() AssessmentSeedGenerator {
// proves that the content is the one that passed them. It returns the
// algorithm.
func (generator AssessmentSeedGenerator) validate(location string, probe bool) (assessmentAlgorithm, error) {
if generator.Table != nil {
return assessmentAlgorithm{}, invalidAssessmentFamily(location, "a table generator has no algorithm")
}
algorithm, ok := assessmentAlgorithms[generator.Algorithm]
if !ok {
return assessmentAlgorithm{}, invalidAssessmentFamily(location, "generator algorithm is unknown")
@@ -187,6 +197,9 @@ func (family AssessmentFamily) NewVariant(seed VariantSeed) (AssessmentVariant,
if family.IsZero() {
return AssessmentVariant{}, invalidAssessmentFamily(location, "family was not built by RestoreAssessmentFamily")
}
if family.core.Generator.Table != nil {
return family.newTableVariant(seed)
}
algorithm, ok := assessmentAlgorithms[family.core.Generator.Algorithm]
if !ok {
return AssessmentVariant{}, invalidAssessmentFamily(location, "generator algorithm is unknown")