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; its body is the expression evaluated for each element, and iterators are the bound Variable instances.

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.