Metadata-Version: 2.5
Name: trytond_etude_logic
Version: 8.0.0.2
Summary: Etude Form Server — Logic and Expressions
Author: Clemens Hupka
Maintainer-email: Clemens Hupka <clemens.hupka@backend.at>
License-Expression: GPL-3.0-or-later
License-File: COPYRIGHT
License-File: LICENSE
Keywords: expression,form,logic,sandbox,survey,tryton
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Environment :: Plugins
Classifier: Framework :: Tryton
Classifier: Topic :: Office/Business
Requires-Python: >=3.10
Requires-Dist: trytond-etude<8.1,>=8.0
Requires-Dist: trytond<8.1,>=8.0
Provides-Extra: test
Description-Content-Type: text/x-rst

Etude Logic Module
==================

The ``etude_logic`` module adds the logic of a form to the ``etude`` module:
the *formula* that computes the answer of a question from the other answers of
a response, the *conditions* that decide which questions are asked, the *rules*
a response must satisfy before it is stored, the *jumps* that take a response
to another question, group or page, and the *quotas* that cap the responses of
a form or of one of its options.  It is a companion of the core module of the
etude group - it depends on ``etude`` and extends its definition - and it is
what makes a form more than a list of questions.

An expression is never Python code
----------------------------------

A formula and a rule are data: a designer writes them, they are stored in the
database and they are read again every time the form is served.  Reading them
as Python would hand the definition of a form the whole of the server, so the
module does not: an expression is **parsed, walked and interpreted** by a
sandbox with an explicit allowlist of node types, and it is never compiled and
never handed to the interpreter of the host.  There is no ``eval``, no ``exec``
and no ``compile`` on user input anywhere in the module.

The vocabulary of the language is closed at both ends.  An expression reads the
answers of the response under ``answers``, by the code of a question
(``answers['total']``), and the date of the evaluation under ``current_date``,
and it calls the pure functions the module declares: ``len``, ``abs``, ``min``,
``max``, ``sum``, ``avg``, ``round``, ``int``, ``float``, ``bool``, ``text``,
``concat``, ``upper``, ``lower``, ``strip``, ``date`` and ``days_between``.  A
name outside those two tables, a call that is not a call to one of those
functions, an attribute that starts a private name, an import, a lambda, a
comprehension and an assignment are all refused where the expression is saved,
with a message that names the construct that was refused.

Arithmetic is performed on numbers alone, which is what keeps a definition from
building a value whose size it does not bound: text is built by ``concat`` and
its companions and never by repetition.  The length of an expression and the
number of nodes of its syntax tree are bounded, and a part of a date - its
year, its month, its day, the weekday - is the only attribute a value answers.

The formula of a question
-------------------------

A question is answered by a participant or computed by the form.  A computed
question carries an ``etude.formula``: the expression that builds its value
(``answers['number'] * 2``, ``concat(answers['first'], ' ', answers['last'])``,
``days_between(answers['start'], current_date)``).  The answer of the question
the formula computes is not in the symbol table, so a formula reads the other
answers alone and a form may compute its answers in any order.

The conditions of a form
------------------------

A question is asked, or it is skipped.  A question, a group and a page carry
the condition under which they are shown, and a response is only asked for what
is shown: a hidden question is never required and the answer of a hidden
question is discarded when the response is stored.

A condition is data rather than an expression: it is a triple (question,
operator, value) and its value is read as the kind of the question it tests.
The operators are equality (``is``, ``is_not``), membership and the
intersection of two sets (``in``, ``intersects``, ``not_in``), the order of two
numbers, of two dates or of two texts (``lt``, ``le``, ``gt``, ``ge``), a
regular expression (``matches``, ``not_matches``) and emptiness (``is_empty``,
``is_not_empty``).  An operator that reads a list reads its values separated by
commas, and an operator that compares one value is refused on a question that
takes several answers.

Conditions combine through ``etude.condition.group``, a node that holds
conditions and other nodes and combines them with *all* or *any*.  The root of
a tree is attached to the question, the group, the page or the jump whose
relevance it decides; a node nested in another node is attached to nothing, and
an element carries one tree at most.

What is refused is refused where it is written: the value of a condition is
read as the kind of the question, a regular expression is compiled and bounded
and an emptiness reads no value, so a definition that can not be evaluated is
refused when it is saved rather than when a response reaches it.  The
evaluation is deterministic and read-only - it reads the answers it is handed
and writes nothing - so the client which renders a form and the server which
stores a response read one state of the response.

The rules of a form
-------------------

A rule is an ``etude.validation``: an expression, a severity an *Error* or a
*Warning*, the message a failure is reported with, and the question a failure
is reported beside.  The rules of a form are read in their order, which is the
sequence of the rule, so the first failure a participant is shown is the one
the designer wrote first.

The message of a rule is a message of the server (``ir.message``) rather than a
text written on the rule, so it is translated like every other message of the
deployment; the module ships a catalogue of standard messages, and an
administrator writes the ones a form needs beside them.  A message may hold
``%(question)s``, ``%(label)s`` and ``%(code)s``, filled with the question the
rule is about.

Validation runs on the client flow, where a participant is told what to correct
as soon as an answer changes, and again on the server before the response is
stored: ``Validation.check`` answers the failures in order, and
``Validation.check_or_raise`` refuses a response whose first failing error
blocks it.

The jumps of a form
-------------------

A jump takes a response to a question, to a group or to a page other than the
next one when a condition of the form holds, which is the skip logic of a
longer form.  A jump names one target of its own form and carries the condition
tree under which it is taken; the jumps of a form are read in their order and
the first one whose condition holds decides where the response goes, so a
definition reads as the list of the branches a participant may take.

A jump without a condition is never taken: a condition tree is written after
the jump that holds it, so a jump saved before its tree is filled in is a
definition being written rather than a branch for every response.  A jump the
designer wants unconditional is written with a condition that always holds.

The quotas of a form
--------------------

A quota caps the number of responses a form accepts, or the number of responses
that choose one option of it: the first questions of a study may be answered by
anyone and its last places by the first ones to come.  A quota whose limit is
zero caps nothing.

A response is counted against the quotas of its form inside the transaction
that stores it: ``Quota.check_or_raise`` locks the quotas a response is counted
against before it counts them, so that two responses submitted at the same
moment are counted one after the other rather than both against the same count.

The logic of a form owns the cap and knows nothing of the responses of a form,
which are stored by the module that collects them: the count is therefore read
from ``Quota.get_count``, which ``etude_collect`` overrides to answer how many
responses a quota already holds.  A count that is not known holds nothing back,
so the form of a deployment that stores no response yet is served with the
quotas it declares.

The logic of a form
-------------------

``etude.form`` gains ``get_logic``, which returns a ``Logic`` built for one set
of answers: the entry point a client and a server read to know what a
participant is asked, what a response must answer, which jump takes it and how
a label reads an earlier answer.  The answers are a mapping of the code of a
question to the value of its answer, and the evaluation is read once and
answered many times, so a page rendered question by question evaluates the
definition once.

The relevance of an element is read from the answers alone: a question is shown
when it is shown itself and every element that holds it is shown, so the
question of a hidden group is hidden with the group and the question of a
hidden page with the page.  ``Logic`` answers the codes that are shown, the
codes that are hidden and the codes that are required - a question that is not
asked is never required - and ``filter_answers`` discards the answers of the
hidden questions, which is what a server stores.

A label reads an earlier answer through *piping*: a placeholder is the code of
a question between braces (``{total}``) and it is replaced by the text of the
answer of that question, read as the labels of the chosen options for a
question that offers a choice.  A placeholder that names no question of the
form is left as it is written, so a label which holds braces reads as its
author wrote it.

The logic a version freezes
---------------------------

Editing the logic of a form is an edit of its definition: a published form
opens the draft version the next publish completes, exactly like editing a
question or an option.  The formula of a question, the condition that decides
whether it is asked, the conditions of a page and of a group, the rules, the
jumps and the quotas of a form are part of the definition payload a version
freezes, so a response that pins a version is read against the logic of that
version, and restoring a version writes the logic back with the pages of the
form.

See ``doc/index.rst`` for the module documentation and ``ETUDE.md`` in
``modules-cch/`` for the architecture of the whole group.
