Domain Modeling Based on Formal Methods: Deriving Aggregate Candidates from Invariants
The author's criterion for deciding what to leave to AI and what to handle personally is how irreversible a judgment is: implementation details that are easy to fix can be delegated to AI, while decisions that are hard to back out of still call for a human. Even when AI writes code correctly, the finished system may not match the business if the rules built into it drift from the rules the business actually follows, and correcting that after implementation can mean revisiting business logic and data design. Domain modeling brings such rules and unstated assumptions into a model to be checked against the AI's understanding before implementation, which amounts to shift-left — running these checks early in development — a practice the author considers still effective in AI-driven development. While applying formal methods in TAKT, an AI orchestration tool the author is developing, invariants[1] emerged as a promising clue: if even part of modeling follows a fixed procedure, more of it can be delegated to AI.
With Quint, a formal specification language[2], the author tests how far aggregate[3] candidates from domain-driven design can be mechanically derived from invariants, with the stated aim of turning modeling into a fixed procedure and making it a commodity. An invariant is a condition the system's state must always satisfy, such as 'a confirmed order has at least one line item', and model checking[4] repeatedly applies operations starting from a defined initial state to see whether an invariant-breaking state is reachable, reporting the concrete sequence as a counterexample. Because an invariant shows which states it involves and an operation definition shows which states it checks and changes, comparing the two indicates which states should be handled together as one aggregate — the domain-driven design boundary within which related objects are kept consistent immediately.
In the order example, the list of line items and the confirmation status form an aggregate candidate, and after model checking shows a zero-item draft being confirmed, the confirmOrder operation gains a check that lines.length() > 0; in the product example, an order line records the unit price agreed at the time of the order, so current product prices and order lines can be handled in separate aggregates. In the budget example, when the budget caps spending and is fixed once a request is approved, budget amount, request amount, approval status and the budget recorded at approval are handled as one consistency boundary, whereas a budget treated only as a guideline allows the budget amount to be managed separately from approvals. In the event reservation example, with a capacity of 10 people per session modeled with a capacity of 1 person to keep the number of checked states down, securing a seat at reception ties applications, confirmed seats and free seats into one aggregate, whereas collecting participation wishes first and assigning seats later allows accepted applications to remain without seats.
Sources
-
JPZenn JA
集約をどこまで機械的に導けるか――形式手法をベースとしたドメインモデリング


