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¶
OperationCallExpressionA binary or unary operation. Notable shape:
argumentsis a list of operands plus the operator marker. For a binary op, the layout is[lhs, InfixOperator, rhs]. TheInfixOperatorentry is not anOCLExpression; consumers walkingargumentsshould filter onisinstance(child, OCLExpression)when they only want operands.The operator name is also exposed on the call as
operation(e.g.'>=','and','+').PropertyCallExpressionA property access. The
sourceattribute is the receiver (typically anotherOCLExpression, e.g.self); thereferredPropertyis the resolvedPropertyfrom the domain model.For chained access like
self.account.balance, parsing produces a nested chain ofPropertyCallExpressionnodes via thesourcelink.VariableExpA reference to a name introduced by a quantifier (
forAll(e | ...)) or aletbinding. ThereferredVariablepoints at theVariablethat introduces the name.TypeExpA type reference, used by
oclIsTypeOf(T)/oclAsType(T)/oclIsKindOf(T). ThereferredTypeis the targetType.IfExpA conditional. Children:
ifCondition,thenExpression,elseExpression— allOCLExpression.LoopExpandIteratorExpQuantifiers and bulk operations over collections.
IteratorExpcoversforAll/exists/select/reject/collect/closure; itsbodyis the expression evaluated for each element, anditeratorsare the boundVariableinstances.In the short form (
src->forAll(age > 16))iteratorsis empty and a bare name in the body is resolved against the iterated element first, then againstself(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, soself.parents->closure(parents)is all the ancestors ofself. The body must yield the source element type or a collection of it. Unlike the other iterators, itstypeis set: aSetType, or anOrderedSetTypewhen the source is ordered, named after the element type (Set(Person)). The B-OCL evaluator behind the editor’s validator does not evaluateclosureyet; such invariants are reported as syntactically valid and not evaluated.- Literals
The literal subclasses (
IntegerLiteralExpression,RealLiteralExpression,BooleanLiteralExpression,StringLiteralExpression,DateLiteralExpression) hold the parsed value on avalueattribute. Booleans are coerced to Pythonboolby 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.balanceis a clean chain ofPropertyCallExpressionnodes (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.