B-OCL: An Object Constraint Language Interpreter in Python

Fitash Ul Haq, Jordi CabotarXiv 2025 · OpenReview

Abstract

The Object Constraint Language (OCL) has been widely used in the modeling community to complement software models for precisely defining constraints and business rules for the modeled systems. There is a limited number of tools supporting the definition and interpretation of OCL constraints, even less for a Python-based modelling approaches. In this paper, we introduce an OCL interpreter for Python. The interpreter has two components: parser and evaluator. We implement the OCL metamodel as a set of Python classes and design the grammar for the parser using the state-of-the-art ANTLR parser generator. The parser generates the syntax tree, that conforms with the OCL metamodel, after parsing each part of the OCL constraint. The evaluator then interprets the constraints using this syntax tree and the object diagram. In the end, the interpreter reports the result for all the constraints.

Introduction

In modern software design, visual models such as the Unified Modeling Language (UML) are often insufficient for describing complex logic and fine-grained invariants. For instance, a model might need to specify that a book cannot have a negative page count or that a specific library section is restricted to children's literature based on page count. The Object Constraint Language (OCL) was developed to address these requirements by providing a formal way to define "fine print" details.

Despite its utility, robust OCL tooling has historically been concentrated in the Java ecosystem (e.g., Eclipse OCL). As Python has emerged as the primary language for AI and data science, the absence of a native, reliable OCL interpreter has hindered the integration of formal Model-Driven Engineering (MDE) with Python's extensive library ecosystem. B-OCL addresses this gap by providing an open-source, Python-based interpreter designed for the BESSER low-code platform.

System Architecture

B-OCL is structured as a two-stage pipeline consisting of a parser and an evaluator, integrated within the broader BESSER (BESSER Universal Modeling Language) ecosystem.

The B-OCL workflow, illustrating the path from user input to evaluation result.
  1. The Parser: Utilizing the ANTLR 4 parser generator, the system defines a formal grammar for OCL. This component processes textual OCL constraints into a Syntax Tree that conforms to a Python-implemented OCL metamodel.
  2. The Evaluator: This component takes the Syntax Tree and an Object Diagram (a collection of instantiated Python objects). By traversing the tree and pulling values directly from these objects at runtime, the evaluator determines if the specified constraints are satisfied.

The interpreter supports core OCL constructs, including invariants, conditional logic (if-then-else), and collection iterators such as forAll, select, exists, and reject.

Validation and Case Study

To validate the interpreter's functionality, the authors utilized a Library Domain Model benchmark. This model provides a structured environment to test various levels of constraint complexity across multiple classes and associations.

The Library Domain Model used for validating the B-OCL interpreter.

The benchmark includes classes for Library, Book, and Author. Constraints tested include attribute-level checks (e.g., pages > 0) and relational checks, such as verifying attributes of books within a specific library's collection.

Discussion and Key Results

B-OCL represents a shift from traditional "transpilers" that convert OCL to static code. As a true interpreter, it allows for dynamic evaluation and runtime debugging, which is crucial for agile development and AI-integrated workflows. Key achievements include:

  • Functional Parity: Implementation of the most frequently used OCL constructs.
  • Accessibility: The tool is available via the Python Package Index (PyPI) and hosted on GitHub, encouraging community adoption.
  • Ecosystem Integration: It serves as a foundational component for Python-based low-code development within the BESSER platform.

Conclusion and Future Work

While B-OCL successfully bridges the gap between MDE and Python, several areas for improvement remain. Current limitations include a lack of support for three-valued logic (null values), enumerations, and pre/post conditions for operations. Future development will focus on mapping OCL constraints to the Z3 theorem prover for formal verification and implementing optimized code generation to improve performance in large-scale systems.

Summary updated

Abstract

The Object Constraint Language (OCL) has been widely used in the modeling community to complement software models for precisely defining constraints and business rules for the modeled systems. There is a limited number of tools supporting the definition and interpretation of OCL constraints, even less for a Python-based modelling approaches. In this paper, we introduce an OCL interpreter for Python. The interpreter has two components: parser and evaluator. We implement the OCL metamodel as a set of Python classes and design the grammar for the parser using the state-of-the-art ANTLR parser generator. The parser generates the syntax tree, that conforms with the OCL metamodel, after parsing each part of the OCL constraint. The evaluator then interprets the constraints using this syntax tree and the object diagram. In the end, the interpreter reports the result for all the constraints.
BibTeX
@misc{haq2025b,
  author = {Fitash Ul Haq and Jordi Cabot},
  title = {{B-OCL: An Object Constraint Language Interpreter in Python}},
  year = {2025},
  eprint = {2503.00944},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.00944},
}
Figure