Models, Prompts, and Code! A Semi-Formal State Machine Language for Multi-Paradigmatic Software Development
Abstract
: Model-Driven Software Engineering has long excelled at generating code from static structural models, yet the specification and generation of dynamic behavioral models remains a persistent challenge. Meanwhile, Large Language Models (LLMs) offer flexible, natural-language based code generation but suffer from non-determinism and hallucinations. This paper presents a semi-formal approach that bridges these two paradigms for behavioral modeling via UML state machines. We contribute a textual modeling language that captures the essential elements of UML state diagrams—states, transitions, events, guards, and entry/exit actions—alongside a deterministic code generator that transforms state machine models into Java code following the Gang of Four State design pattern. The language supports two complementary action annotation styles: direct code fragments for concise, self-contained actions, and natural language descriptions for semantically richer behavior to be completed by an LLM weaver. LLM involvement is deliberately scoped to small, well-constrained action bodies, reducing token consumption and non-determinism compared to fully LLM-based approaches. Validated through the Gumball Machine case study, correctness is confirmed by automated tests covering state and transition coverage criteria, and repeating the LLM weaving step produced consistent results across all runs. Compared to both classical UML tooling and fully LLM-based generation, the approach offers stronger determinism, better traceability, lower cognitive modeling effort, and reduced computational cost, while retaining the flexibility to express complex action behavior in natural language where formal specification would be unnecessarily burdensome.