The OCL AST

The classes in besser.BUML.metamodel.ocl.ocl define the abstract syntax tree produced by parse_ocl(). A tool that translates OCL into another language (e.g. evaluating constraints, encoding them into a proof assistant, emitting them as target-specific assertions) walks this tree.

Inheritance

Element
  └── NamedElement
        └── TypedElement
              └── OCLExpression  ◄── base class for every node
                    ├── OperationCallExpression
                    ├── PropertyCallExpression
                    ├── VariableExp
                    ├── TypeExp
                    ├── IfExp
                    ├── LoopExp
                    ├── IteratorExp
                    └── LiteralExpression
                          ├── IntegerLiteralExpression
                          ├── RealLiteralExpression
                          ├── BooleanLiteralExpression
                          ├── StringLiteralExpression
                          └── DateLiteralExpression

Major node types

OperationCallExpression

A binary or unary operation. Notable shape: arguments is a list of operands plus the operator marker. For a binary op, the layout is [lhs, InfixOperator, rhs]. The InfixOperator entry is not an OCLExpression; consumers walking arguments should filter on isinstance(child, OCLExpression) when they only want operands.

The operator name is also exposed on the call as operation (e.g. '>=', 'and', '+').

PropertyCallExpression

A property access. The source attribute is the receiver (typically another OCLExpression, e.g. self); the referredProperty is the resolved Property from the domain model.

For chained access like self.account.balance, parsing produces a nested chain of PropertyCallExpression nodes via the source link.

VariableExp

A reference to a name introduced by a quantifier (forAll(e | ...)) or a let binding. The referredVariable points at the Variable that introduces the name.

TypeExp

A type reference, used by oclIsTypeOf(T) / oclAsType(T) / oclIsKindOf(T). The referredType is the target Type.

IfExp

A conditional. Children: ifCondition, thenExpression, elseExpression — all OCLExpression.

LoopExp and IteratorExp

Quantifiers and bulk operations over collections. IteratorExp covers forAll / exists / select / reject / collect / closure; its body is the expression evaluated for each element, and iterators are the bound Variable instances.

In the short form (src->forAll(age > 16)) iterators is empty and a bare name in the body is resolved against the iterated element first, then against self (OCL 2.4 §7.8).

closure (OCL 2.4 §7.6.5) collects the source elements and everything reachable from them by applying the body repeatedly, so self.parents->closure(parents) is all the ancestors of self. The body must yield the source element type or a collection of it. Unlike the other iterators, its type is set: a SetType, or an OrderedSetType when the source is ordered, named after the element type (Set(Person)). The B-OCL evaluator behind the editor’s validator does not evaluate closure yet; such invariants are reported as syntactically valid and not evaluated.

Literals

The literal subclasses (IntegerLiteralExpression, RealLiteralExpression, BooleanLiteralExpression, StringLiteralExpression, DateLiteralExpression) hold the parsed value on a value attribute. Booleans are coerced to Python bool by the wrapping visitor.

The wrapping visitor

parse_ocl uses besser.BUML.notations.ocl.wrapping_visitor.WrappingVisitor, which post-processes the raw ANTLR-derived AST to:

  • Reconstruct property chains so that self.account.balance is a clean chain of PropertyCallExpression nodes (rather than a flat list of names).

  • Coerce boolean literals from ANTLR strings to Python bool.

  • Resolve property and type references against the supplied DomainModel.

A consumer walking the AST should expect the wrapped shape, not the raw ANTLR shape. The non-wrapping BOCLVisitorImpl is an internal helper used for source-location population during parsing — direct use is rarely needed.