Skip to content

API Reference parrot_formdesigner.services.rule_evaluator

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_formdesigner.services.rule_evaluator

View source

Authoritative server-side rule evaluator for FormSchema conditional sections.

Given a :class:~parrot_formdesigner.core.schema.FormSchema and a dict of current field answers, :class:RuleEvaluator resolves visibility, required state, computed values (from DependencyOperation), and cascade-clears — processing pre-dependencies, post-dependencies, and operations in topological order.

Design notes (spec §8):

  • The JSON schema representation is declarative and intended for client-side interpretation. This Python evaluator is the authoritative server-side implementation.
  • Evaluation order: topological sort of the dependency graph; cycles were already rejected by :class:~parrot_formdesigner.services.validators.FormValidator — any residual cycle is skipped with a warning.
  • NOT logic negates the AND-combination of conditions (spec §8 explicit default).
  • reload_options and ARRAY-operand aggregation scope are open questions in the spec (§8). The evaluator records reload_options targets in computed as a sentinel ("__reload__") and uses a flat list for AGGREGATE operands. TODO(FEAT-234 open question): revisit once spec §8 is finalised.

Alias ConditionOperator

Re-exported from parrot_formdesigner.core.constraints.ConditionOperator.

Alias DependencyOperation

Re-exported from parrot_formdesigner.core.constraints.DependencyOperation.

Alias DependencyRule

Re-exported from parrot_formdesigner.core.constraints.DependencyRule.

Alias FieldCondition

Re-exported from parrot_formdesigner.core.constraints.FieldCondition.

Alias FormField

Re-exported from parrot_formdesigner.core.schema.FormField.

Alias FormSchema

Re-exported from parrot_formdesigner.core.schema.FormSchema.

Alias PostDependency

Re-exported from parrot_formdesigner.core.constraints.PostDependency.

Class RuleEvaluator

Authoritative server-side rule evaluator for FormSchema conditional sections.

Given a :class:~parrot_formdesigner.core.schema.FormSchema and current answers, resolves visibility, required state, computed values, and cascade-clears for all fields.

Pre-dependencies (FormField.depends_on) are evaluated first, then post-dependencies (FormField.post_depends) in topological order.

Example::

evaluator = RuleEvaluator()
resolution = await evaluator.resolve(form_schema, {"age": 30})
if not resolution.visible.get("guardian_name", True):
    # field is hidden — skip it
    ...

Method RuleEvaluator.resolve

Resolve all conditional-section rules for form against answers.

resolve(form: FormSchema, answers: dict[str, Any], *, locale: str = 'en', location_vars: dict[str, Any] | None = None, visit_context: dict[str, Any] | None = None) -> RuleResolution

Parameter Type Default Description
form FormSchema The form schema to evaluate.
answers dict[str, Any] Current answer dict, keyed by field_id.
locale str 'en' BCP 47 locale tag (reserved for future localised-label ops).
location_vars dict[str, Any] | None None
visit_context dict[str, Any] | None None

Returns: RuleResolution — class:RuleResolution with visibility, required-state, computed values, and cleared fields.

Class RuleResolution

Result of evaluating all conditional-section rules for a form submission.

Fields:

Field Type Default Description
visible dict[str, bool] {} Maps field_id → True (visible) / False (hidden). Fields with no applicable rule default to True.
required dict[str, bool] {} Maps field_id → True (required) / False (not required). Inherits the FormField.required baseline; rules may flip this.
computed dict[str, Any] {} Maps field_id → computed value produced by a DependencyOperation or post_depends set/calc effect. "__reload__" is a sentinel for reload_options targets (TODO(FEAT-234 open question)).
cleared list[str] [] List of field_id values whose answers should be cleared (cascade_clear effect).

Alias find_field_by_uid

Re-exported from parrot_formdesigner.core.resolution.find_field_by_uid.

Attribute logger

Alias resolve_answer

Re-exported from parrot_formdesigner.core.resolution.resolve_answer.

Alias walk_fields

Re-exported from parrot_formdesigner.core.schema.walk_fields.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally