BESSER OCL

Metamodel

class besser.BUML.metamodel.ocl.ocl.BagType(name)[source]

Bases: CollectionType

A class to define the Bag Type

Parameters:

name – Name of the expression

class besser.BUML.metamodel.ocl.ocl.BooleanLiteralExpression(name: str, value: bool)[source]

Bases: LiteralExpression

A Boolean literal Expression of type LiteralExpression

Parameters:
  • name (str) – the name of the expression

  • value – value of the expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.CallExp(name: str, type: Type)[source]

Bases: OCLExpression

A class to define call expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.Classifier(name=None)[source]

Bases: object

A class to define classifier

Parameters:

name (str) – the name of the classifier

class besser.BUML.metamodel.ocl.ocl.CollectionItem(name, item)[source]

Bases: CollectionLiteralPart

A class to define the Collection Item

Parameters:

name – Name of the expression

value

Value of the item

_abc_impl = <_abc._abc_data object>
get()[source]

Get the value of item

set(value)[source]

set the value of item

class besser.BUML.metamodel.ocl.ocl.CollectionLiteralExp(name, type)[source]

Bases: LiteralExp

A class to define the Collection Literal Expression

Parameters:
  • name – Name of the expression

  • type – type of expression

kind

type of expression

collectionItems

Items in the collection literal expression

_abc_impl = <_abc._abc_data object>
add(item)[source]

Method to add item in the collection items

class besser.BUML.metamodel.ocl.ocl.CollectionLiteralPart(name)[source]

Bases: TypedElement

A class to define the Collection Literal Part

Parameters:

name – Name of the expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.CollectionRange(name)[source]

Bases: CollectionLiteralPart

A class to define collection range

Parameters:

name – name of expression

first

first item of collection

last

last item of collection

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.CollectionType(name)[source]

Bases: DataType

A class to define the Collection Type

Parameters:

name – Name of the expression

class besser.BUML.metamodel.ocl.ocl.DataType(name)[source]

Bases: Classifier

A class to define the data type

Parameters:

name – Name of the expression

class besser.BUML.metamodel.ocl.ocl.DateLiteralExpression(name: str, value: str)[source]

Bases: LiteralExpression

A Date literal Expression of type LiteralExpression

Parameters:
  • name (str) – the name of the expression

  • value – value of the expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.FeatureCallExp(name: str, type: Type)[source]

Bases: CallExp

A class to define feature call expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.IfExp(name: str, type: Type, ifcond=None, elseExp=None, thenExp=None)[source]

Bases: OCLExpression

A class to represents IfExpressions

Parameters:
  • name – name of constraint

  • type – type of constraint

  • ifcond – If Condition of constraint

  • elseExp – Else expression of constraint

  • Exp (then) – then expression of constraint

_abc_impl = <_abc._abc_data object>
property elseCondition: OCLExpression

Get the else Condition of expression

property ifCondition: OCLExpression

Get the if Condition of expression

property thenExpression: OCLExpression

Get the then expression of OCLexpression

class besser.BUML.metamodel.ocl.ocl.InfixOperator(operator)[source]

Bases: object

A class to define Infinix Operator Expression

Parameters:

operator – Operator of the expression

operator

Operator of the expression

get_infix_operator()[source]

Get the infinix Operator

class besser.BUML.metamodel.ocl.ocl.IntegerLiteralExpression(name: str, value: int)[source]

Bases: LiteralExpression

A Integer literal Expression of type LiteralExpression

Parameters:
  • name (str) – the name of the expression

  • value – value of the expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.InvalidLiteralExp(name: str, type: Type)[source]

Bases: LiteralExp

A class to define invalid literal expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.IterateExp(name: str, type: Type)[source]

Bases: LoopExp

A class to define Iterate expression

Parameters:
  • name – name of expression

  • type – type of expression

result

variable to store the result

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.IteratorExp(name: str, type: Type)[source]

Bases: LoopExp

A class to define Iterator expression

Parameters:
  • name – name of expression

  • type – type of expression

name

name of expression

Type:

str

type

type of expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.LetExp(name: str, type: Type)[source]

Bases: OCLExpression

A class to define Let expression

Parameters:
  • name – name of expression

  • type – type of expression

OCLExpression

OCL expression of the LetExp

variable

variable of the Let expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.LiteralExp(name: str, type: Type)[source]

Bases: OCLExpression

A class to define Literal expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.LiteralExpression(name: str, type: Type, value: Any)[source]

Bases: OCLExpression

A literal value part of an OCL expression

Parameters:
  • name (str) – the name of the expression

  • type – type of the expression.

  • value – value of the expression

value

value of the expression

_abc_impl = <_abc._abc_data object>
property value: Any

Get the Value of Expression

class besser.BUML.metamodel.ocl.ocl.LoopExp(name: str, type: Type)[source]

Bases: CallExp

A class to define loop expression

Parameters:
  • name – name of the expression

  • type – type of the expression

name

name of the expression

Type:

str

type

type of the expression

body

a list to store expressions in the body of loop expression

iterator

list to store all the iterators

_abc_impl = <_abc._abc_data object>
addIterator(iterator)[source]

add iterator to the expression

add_body(body)[source]

Add expression to the body

property get_body

Get body of the expression

property get_iterator

add iterator of the expression

class besser.BUML.metamodel.ocl.ocl.MessageExp(name: str, type: Type)[source]

Bases: OCLExpression

A class to define message expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.NavigationCallExp(name: str, type: Type)[source]

Bases: OCLExpression

A class to define navigation call expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.NullLiteralExp(name: str, type: Type)[source]

Bases: LiteralExp

A class to define null literal expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.NumericLiteralExp(name: str, type: Type)[source]

Bases: PrimitiveLiteralExp

A class to define numeric literal expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.OCLConstraint(name: str, context: Class, expression: OCLExpression, language: str = 'OCL')[source]

Bases: Constraint

An OCL constraint — a Constraint whose expression has been parsed into an OCLExpression AST.

The expression attribute (inherited from Constraint) holds the pretty-printed OCL source text. The ast attribute holds the parsed AST. Use parse_ocl(...) to obtain an AST from raw OCL source.

Parameters:
  • name – name of the constraint.

  • context – the class on which the constraint is defined.

  • expression – the parsed OCL AST (an OCLExpression). The constructor pretty-prints this to populate the source text on the base Constraint.

  • language – language tag (default "OCL").

_abc_impl = <_abc._abc_data object>
property ast: OCLExpression

Get the parsed AST of the OCL constraint.

Type:

OCLExpression

class besser.BUML.metamodel.ocl.ocl.OCLExpression(name: str, type: Type)[source]

Bases: TypedElement

The OCLExpression is the Superclass of all OCL elements with.

Parameters:
  • name (str) – the name of the expression

  • type – type of the expression.

source

the source of expression

Type:

OCLExpression

_referredOperation

points to any refferred operation that the expression has.

_abc_impl = <_abc._abc_data object>
property referredOperation: Any

Get the referredOperation of OCL Expression

property source: Any

Get the source of OCL Expression

class besser.BUML.metamodel.ocl.ocl.OperationCallExpression(name: str, operation: str, arguments: list[OCLExpression])[source]

Bases: OCLExpression

Expression that returns the value of a given Operation on a given set of ordered arguments The operation could also be a reference to any operation in an existing class but for now we stick to simple arithmetic comparison operations from the standard OCL library.

An Operation Call Expression of type OCL expression.

Parameters:
  • name – Name of the function

  • operation – Operation of the expression

  • arguments – Arguments of the function

operation

Operation of the expression

arguments

Arguments of the function

_abc_impl = <_abc._abc_data object>
add(item)[source]

Add in the arguments of Operation

property arguments: list[OCLExpression]

Get the arguments of Operation

property operation: str

Get the operation of Expression

class besser.BUML.metamodel.ocl.ocl.OrderedSetType(name)[source]

Bases: CollectionType

A class to define the Ordered Set Type

Parameters:

name – Name of the expression

class besser.BUML.metamodel.ocl.ocl.Parameter(val)[source]

Bases: object

A class to define parameter

Parameters:

val – Value of the parameter

value

Value of the parameter

get_value()[source]

Get the value of parameter in the expression

class besser.BUML.metamodel.ocl.ocl.PrimitiveLiteralExp(name: str, type: Type)[source]

Bases: LiteralExp

A class to define primitive literal expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.PropertyCallExpression(name: str, property: Property)[source]

Bases: OCLExpression

A Property Call Expression of type OCLExpression

Parameters:
  • name (str) – the name of the expression

  • property – property of the expression

property

Property of the expression

_abc_impl = <_abc._abc_data object>
property property: Property

Get the Property of Expression

class besser.BUML.metamodel.ocl.ocl.RealLiteralExpression(name: str, value: float)[source]

Bases: LiteralExpression

A Real literal Expression of type LiteralExpression

Parameters:
  • name (str) – the name of the expression

  • value – value of the expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.SequenceType(name)[source]

Bases: CollectionType

A class to define the Sequence Type

Parameters:

name – Name of the expression

class besser.BUML.metamodel.ocl.ocl.SetType(name)[source]

Bases: CollectionType

A class to define the Set Type

Parameters:

name – Name of the expression

class besser.BUML.metamodel.ocl.ocl.State[source]

Bases: object

A class to define state

class besser.BUML.metamodel.ocl.ocl.StateExp(name: str, type: Type)[source]

Bases: OCLExpression

A class to define the state expressions of type OCL expression

Parameters:
  • name – name of the expression

  • type – type of the expression

ReferedState

Referred state of the expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.StringLiteralExpression(name: str, value: str)[source]

Bases: LiteralExpression

A String literal Expression of type LiteralExpression

Parameters:
  • name (str) – the name of the expression

  • value – value of the expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.TypeExp(name: str, type: Type)[source]

Bases: OCLExpression

A class to define the type expression of the type OCL Expressions

Parameters:
  • name – name of the classifier

  • type – type of expressions

referedType

classifier of the expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.Variable(name: str, type: Type)[source]

Bases: OCLExpression

A class to define the variable in the OCL expression

Parameters:
  • name – name of the expression

  • type – type of the expression

_abc_impl = <_abc._abc_data object>
get_value()[source]

Get the value of variable in the expression

set_value(val)[source]

set the value of variable in the expression

class besser.BUML.metamodel.ocl.ocl.VariableExp(name: str, type: Type)[source]

Bases: OCLExpression

A class to define the variable expression of the type OCL expression

Parameters:
  • name – Name of the expression

  • type – Type of the expression

name

name of the expression

Type:

str

variable

variable of the expression

_abc_impl = <_abc._abc_data object>
getVal()[source]

Get the value of variable in the expression

set_refferred_variable(val)[source]

Set the value of variable in the expression

Structural cloning of OCL ASTs.

clone(node) returns a fresh AST tree that mirrors node. References to domain-model objects (Property, Class, primitive types) are shared with the original, not duplicated — these belong to the structural model and must remain identity-equal across clones.

Use this whenever a transformation must not mutate its input. copy.deepcopy is unsuitable because it would clone the entire DomainModel graph reachable through Property.owner.

besser.BUML.metamodel.ocl.clone.clone(node)[source]

Return a fresh copy of node with shared structural-model references.

Notations (parsing, pretty-printing, normalization)

Public parsing API for B-OCL.

parse_ocl() is the canonical entry point for any consumer that needs an OCL AST. It mirrors the wiring already used by OCLWrapper.evaluate in the B-OCL-Interpreter, with three differences:

  • It uses WrappingVisitor so the returned AST has reconstructed property chains and Python bool values for boolean literals.

  • It raises BOCLSyntaxError on parse errors instead of silently returning a partial AST.

  • It returns an OCLConstraint so the context class is preserved for downstream pretty-printing and rule lookups.

besser.BUML.notations.ocl.api.parse_ocl(text: str, model: DomainModel, context_class: Class = None) OCLConstraint[source]

Parse an OCL constraint string into a fresh AST.

Parameters:
  • text – The OCL source, e.g. "context Employee inv: self.age > 16".

  • model – The DomainModel used to resolve the context class and property bindings.

  • context_class – Optional explicit context Class. If omitted, the class is parsed from the context X inv|pre|post|init header and resolved against model.types.

Returns:

An OCLConstraint whose expression is the parsed AST and whose context is the resolved context class.

Raises:

OCL parser error handling.

The raw ANTLR diagnostics (“mismatched input ‘then’ expecting <EOF>”, “missing ‘)’ at ‘<EOF>’”, etc.) expose internal parser state and use token names that mean little to someone writing an OCL constraint. This module sits on top of ANTLR’s error listener and rewrites the diagnostics into hints phrased in OCL terms — “the ‘then’ keyword must follow ‘if <condition>’”, “missing closing ‘)’”, etc. — so users can diagnose their constraints without reading the grammar.

Relates to BESSER-PEARL/BESSER#202.

class besser.BUML.notations.ocl.error_handling.BOCLErrorListener[source]

Collects syntax errors and rewrites them into OCL-level hints.

has_errors()[source]
syntaxError(recognizer, offendingSymbol, line, column, msg, e)[source]
exception besser.BUML.notations.ocl.error_handling.BOCLSyntaxError(errors)[source]

Raised when OCL parsing fails.

Pretty-print B-OCL ASTs back to OCL source text.

Renders an OCL expression — or a full OCLConstraint — to a canonical string using the precedence hierarchy locked in by the post-grammar- fix B-OCL grammar:

Postfix > Unary > Mul > Add > Comp > AND > XOR > OR > IMPLIES

Parentheses are added only where strictly required by the precedence relation between a node and its parent.

besser.BUML.notations.ocl.pretty_printer.pretty_print(node) str[source]

Render an OCL AST or OCLConstraint to OCL source text.

For an OCLConstraint, emits context <ClassName> inv: <expr>. For a raw expression, emits the expression alone.

Bottom-up fixed-point rewrite driver.

normalize() clones the input AST, then iterates: each pass walks the AST in post-order, asks every rule (in fixed order) whether it applies at each node, and rewrites at the first matching rule. The pass terminates when a full traversal produces no rewrite. Termination is guaranteed by the lexicographic measure ⟨#sugar-operators, AST size, #negations⟩ — every rule strictly decreases that measure.

The driver returns a fresh OCLConstraint. Inputs are not mutated.

class besser.BUML.notations.ocl.normalization.normalize.Context(model: DomainModel, constraint: OCLConstraint, log: Tuple[str, str, str]]=<factory>)[source]

Shared state passed to every rule.

model

The DomainModel providing class and property bindings.

Type:

besser.BUML.metamodel.structural.structural.DomainModel

constraint

The original OCLConstraint (read-only) — gives rules access to the context class via constraint.context.

Type:

besser.BUML.metamodel.ocl.ocl.OCLConstraint

log

Trace of rule applications as (rule_name, before, after) triples. before and after are short string repr’s, suitable for debugging or assertion in tests.

Type:

List[Tuple[str, str, str]]

constraint: OCLConstraint
log: List[Tuple[str, str, str]]
model: DomainModel
besser.BUML.notations.ocl.normalization.normalize.normalize(constraint: OCLConstraint, model: DomainModel, max_iterations: int = 1000) OCLConstraint[source]

Return a fresh OCLConstraint in B-OCL normal form.

Parameters:
  • constraint – The constraint to normalize. Not mutated.

  • model – The owning DomainModel (used for class/property lookups in some rules).

  • max_iterations – Hard cap on outer fixed-point iterations. Default is 1000; reaching this implies a rule is non-terminating, which is a bug.

Returns:

A new OCLConstraint with the same name, context, and language as the input, and a normalized expression.