BESSER OCL¶
Metamodel¶
- class besser.BUML.metamodel.ocl.ocl.BagType(name)[source]¶
Bases:
CollectionTypeA 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:
LiteralExpressionA 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:
OCLExpressionA class to define call expression
- _abc_impl = <_abc._abc_data object>¶
- class besser.BUML.metamodel.ocl.ocl.Classifier(name=None)[source]¶
Bases:
objectA class to define classifier
- Parameters:
name (str) – the name of the classifier
- class besser.BUML.metamodel.ocl.ocl.CollectionItem(name, item)[source]¶
Bases:
CollectionLiteralPartA class to define the Collection Item
- Parameters:
name – Name of the expression
- value¶
Value of the item
- _abc_impl = <_abc._abc_data object>¶
- class besser.BUML.metamodel.ocl.ocl.CollectionLiteralExp(name, type)[source]¶
Bases:
LiteralExpA 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>¶
- class besser.BUML.metamodel.ocl.ocl.CollectionLiteralPart(name)[source]¶
Bases:
TypedElementA 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:
CollectionLiteralPartA 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:
DataTypeA class to define the Collection Type
- Parameters:
name – Name of the expression
- class besser.BUML.metamodel.ocl.ocl.DataType(name)[source]¶
Bases:
ClassifierA 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:
LiteralExpressionA 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:
CallExpA 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:
OCLExpressionA 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:
objectA class to define Infinix Operator Expression
- Parameters:
operator – Operator of the expression
- operator¶
Operator of the expression
- class besser.BUML.metamodel.ocl.ocl.IntegerLiteralExpression(name: str, value: int)[source]¶
Bases:
LiteralExpressionA 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:
LiteralExpA 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:
LoopExpA 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:
LoopExpA class to define Iterator expression
- Parameters:
name – name of expression
type – type 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:
OCLExpressionA 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:
OCLExpressionA 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:
OCLExpressionA 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>¶
- class besser.BUML.metamodel.ocl.ocl.LoopExp(name: str, type: Type)[source]¶
Bases:
CallExpA class to define loop expression
- Parameters:
name – name of the expression
type – type 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
- _abc_impl = <_abc._abc_data object>¶
- 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:
OCLExpressionA class to define message expression
- _abc_impl = <_abc._abc_data object>¶
Bases:
OCLExpressionA class to define navigation call expression
- class besser.BUML.metamodel.ocl.ocl.NullLiteralExp(name: str, type: Type)[source]¶
Bases:
LiteralExpA 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:
PrimitiveLiteralExpA 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:
ConstraintAn OCL constraint — a
Constraintwhose expression has been parsed into anOCLExpressionAST.The
expressionattribute (inherited fromConstraint) holds the pretty-printed OCL source text. Theastattribute holds the parsed AST. Useparse_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 baseConstraint.language – language tag (default
"OCL").
- _abc_impl = <_abc._abc_data object>¶
- property ast: OCLExpression¶
Get the parsed AST of the OCL constraint.
- Type:
- class besser.BUML.metamodel.ocl.ocl.OCLExpression(name: str, type: Type)[source]¶
Bases:
TypedElementThe 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:
- _referredOperation¶
points to any refferred operation that the expression has.
- _abc_impl = <_abc._abc_data object>¶
- class besser.BUML.metamodel.ocl.ocl.OperationCallExpression(name: str, operation: str, arguments: list[OCLExpression])[source]¶
Bases:
OCLExpressionExpression 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>¶
- property arguments: list[OCLExpression]¶
Get the arguments of Operation
- class besser.BUML.metamodel.ocl.ocl.OrderedSetType(name)[source]¶
Bases:
CollectionTypeA class to define the Ordered Set Type
- Parameters:
name – Name of the expression
- class besser.BUML.metamodel.ocl.ocl.Parameter(val)[source]¶
Bases:
objectA class to define parameter
- Parameters:
val – Value of the parameter
- value¶
Value of the parameter
- class besser.BUML.metamodel.ocl.ocl.PrimitiveLiteralExp(name: str, type: Type)[source]¶
Bases:
LiteralExpA 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:
OCLExpressionA 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:
LiteralExpressionA 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:
CollectionTypeA class to define the Sequence Type
- Parameters:
name – Name of the expression
- class besser.BUML.metamodel.ocl.ocl.SetType(name)[source]¶
Bases:
CollectionTypeA class to define the Set Type
- Parameters:
name – Name of the expression
- class besser.BUML.metamodel.ocl.ocl.StateExp(name: str, type: Type)[source]¶
Bases:
OCLExpressionA 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:
LiteralExpressionA 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:
OCLExpressionA 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:
OCLExpressionA 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>¶
- class besser.BUML.metamodel.ocl.ocl.VariableExp(name: str, type: Type)[source]¶
Bases:
OCLExpressionA class to define the variable expression of the type OCL expression
- Parameters:
name – Name of the expression
type – Type of the expression
- variable¶
variable of the expression
- _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.
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
WrappingVisitorso the returned AST has reconstructed property chains and Pythonboolvalues for boolean literals.It raises
BOCLSyntaxErroron parse errors instead of silently returning a partial AST.It returns an
OCLConstraintso 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
DomainModelused to resolve the context class and property bindings.context_class – Optional explicit context
Class. If omitted, the class is parsed from thecontext X inv|pre|post|initheader and resolved againstmodel.types.
- Returns:
An
OCLConstraintwhoseexpressionis the parsed AST and whosecontextis the resolved context class.- Raises:
BOCLSyntaxError – if the text fails to lex or parse.
ValueError – if the context class cannot be located in the model.
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.
- 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
OCLConstraintto OCL source text.For an
OCLConstraint, emitscontext <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
DomainModelproviding class and property bindings.- Type:
besser.BUML.metamodel.structural.structural.DomainModel
- constraint¶
The original
OCLConstraint(read-only) — gives rules access to the context class viaconstraint.context.
- log¶
Trace of rule applications as
(rule_name, before, after)triples.beforeandafterare short string repr’s, suitable for debugging or assertion in tests.
- constraint: OCLConstraint¶
- model: DomainModel¶
- besser.BUML.notations.ocl.normalization.normalize.normalize(constraint: OCLConstraint, model: DomainModel, max_iterations: int = 1000) OCLConstraint[source]¶
Return a fresh
OCLConstraintin 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
OCLConstraintwith the same name, context, and language as the input, and a normalized expression.