BESSER OCL

Metamodel

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.

Attributes:
  • source (OCLExpression) – the source of expression

  • _referredOperation – points to any refferred operation that the expression has.

property source : Any

Get the source of OCL Expression

property referredOperation : Any

Get the referredOperation of OCL 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

Attributes:

value – value of the expression

property value : Any

Get the Value of Expression

_abc_impl = <_abc._abc_data object>
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.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

Attributes:

property – Property of the expression

property property : Property

Get the Property of Expression

_abc_impl = <_abc._abc_data object>
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

Attributes:
  • operation – Operation of the expression

  • arguments – Arguments of the function

property operation : str

Get the operation of Expression

property arguments : list[OCLExpression]

Get the arguments of Operation

add(item)[source]

Add in the arguments of Operation

_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").

property ast : OCLExpression

Get the parsed AST of the OCL constraint.

Type:

OCLExpression

_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

  • then Exp – then expression of constraint

property ifCondition : OCLExpression

Get the if Condition of expression

property elseCondition : OCLExpression

Get the else Condition of expression

property thenExpression : OCLExpression

Get the then expression of OCLexpression

_abc_impl = <_abc._abc_data object>
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

Attributes:
  • name – name of the expression

  • variable – variable of the expression

set_refferred_variable(val)[source]

Set the value of variable in the expression

getVal()[source]

Get the value of variable in 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

set_value(val)[source]

set the value of variable in the expression

get_value()[source]

Get the value of variable in 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

Attributes:

referedType – classifier of the expression

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

Bases: object

A class to define parameter

Parameters:

val – Value of the parameter

Attributes:

value – Value of the parameter

get_value()[source]

Get the value of parameter in the expression

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

Attributes:

ReferedState – Referred state of the expression

_abc_impl = <_abc._abc_data object>
class besser.BUML.metamodel.ocl.ocl.State[source]

Bases: object

A class to define state

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.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.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.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.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.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.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

Attributes:
  • name – name of the expression

  • type – type of the expression

  • body – a list to store expressions in the body of loop expression

  • iterator – list to store all the iterators

add_body(body)[source]

Add expression to the body

property get_body

Get body of the expression

addIterator(iterator)[source]

add iterator to the expression

property get_iterator

add iterator of the expression

_abc_impl = <_abc._abc_data object>
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.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.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.IterateExp(name: str, type: Type)[source]

Bases: LoopExp

A class to define Iterate expression

Parameters:
  • name – name of expression

  • type – type of expression

Attributes:

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

Attributes:
  • name – name of expression

  • 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

Attributes:
  • OCLExpression – OCL expression of the LetExp

  • variable – variable of the Let expression

_abc_impl = <_abc._abc_data object>
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.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.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.InfixOperator(operator)[source]

Bases: object

A class to define Infinix Operator Expression

Parameters:

operator – Operator of the expression

Attributes:

operator – Operator of the expression

get_infix_operator()[source]

Get the infinix Operator

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.CollectionType(name)[source]

Bases: DataType

A class to define the Collection Type

Parameters:

name – Name of the 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.SequenceType(name)[source]

Bases: CollectionType

A class to define the Sequence Type

Parameters:

name – Name of the expression

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.SetType(name)[source]

Bases: CollectionType

A class to define the Set Type

Parameters:

name – Name of the expression

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

Attributes:
  • 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.CollectionItem(name, item)[source]

Bases: CollectionLiteralPart

A class to define the Collection Item

Parameters:

name – Name of the expression

Attributes:

value – Value of the item

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

set the value of item

get()[source]

Get the value of item

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

Bases: CollectionLiteralPart

A class to define collection range

Parameters:

name – name of expression

Attributes:
  • first – first item of collection

  • last – last item of collection

_abc_impl = <_abc._abc_data object>

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.

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

Raised when OCL parsing fails.

missing_element names what an unresolved navigation looked for ("Book.pages"), so callers need not parse the message.

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.metamodel.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: ~besser.BUML.metamodel.structural.structural.DomainModel, constraint: ~besser.BUML.metamodel.ocl.ocl.OCLConstraint, log: ~typing.List[~typing.Tuple[str, str, str]] = <factory>)[source]

Shared state passed to every rule.

Attributes:
  • model – The DomainModel providing class and property bindings.

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

  • 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.

model : DomainModel
constraint : OCLConstraint
log : List[Tuple[str, str, str]]
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.