Skip to content
Talk to our solutions team

Stateflows

In kis.ai, we introduce a powerful feature called stateflows, which function similarly to state machines. Stateflows are assigned to fields within an application, defining the finite set of values a field can hold and the specific order in which these values can be transitioned.

  1. Finite State Management: Stateflows allow developers to define a finite set of permissible values for a field. This ensures that fields can only take on values that are pre-defined, maintaining data integrity and consistency.

  2. Controlled Transitions: Stateflows specify the order of transitions between states. This means that a field can only change its value in a manner that follows the pre-defined stateflow, preventing invalid state changes and ensuring logical consistency within the application.

  3. Validation and Enforcement: By using stateflows, kis.ai enforces validation rules directly within the application’s data model. This reduces the need for additional validation logic in the application code, simplifying development and maintenance.

  4. Workflow Automation: Stateflows automate workflows by dictating the sequence of states a field can traverse. This is particularly useful for business processes that require fields to follow a specific progression, such as order statuses, approval processes, or lifecycle stages.

  5. Error Reduction: The structured nature of stateflows helps in reducing errors that can arise from manual state management. Developers can rely on the defined stateflow to handle state transitions correctly, leading to fewer bugs and more predictable application behavior.

Consider a ticketing system where a ticket can be in one of the following states: “Open,” “In Progress,” “Resolved,” and “Closed.” The stateflow for the ticket status field might be defined as follows:

Ticket & Ticketflow

ticket.yaml

entities:
- name: ticket
inherits: kisai.id
fields:
- name: name
type: string
validations:
- type: required
- name: description
type: string
- name: duedate
type: date
- name: priority
type: taskpriority
- name: assignedto
type: string
- name: state
type: string
stateflow: ticketflow
stateflows:
- name: ticketflow
initial: Open
states:
- name: Open
on:
START:
target: accepted
- name: In Progress
on:
RESOLVE:
target: Resolved
- name: Resolved
on:
CLOSE:
target: Closed
REOPEN:
target: Open
- name: Closed
type: final

Each transition from one state to another is controlled and validated by the stateflow, ensuring that a ticket cannot be moved directly from “Open” to “Resolved” without first being “In Progress.”

In this example, the state field can only transition between states in the order specified. Attempting to set the field to a state outside this order would result in a validation error, enforcing the logical flow of the ticket lifecycle.