Skip to content

Commit 88a1f47

Browse files
authored
Split OCL Grammar and Add Support for Invariants and Derived Attributes (#4451)
* Splitting the OCL grammar from UML-RSDS tool * Updating the desc.xml file and the examples * Fix derived attribute parsing with qualified_name context rule * Adding a new example to test derived attributes * desc.xml fixed according to path patterns allowed
1 parent 27322b7 commit 88a1f47

File tree

7 files changed

+236
-128
lines changed

7 files changed

+236
-128
lines changed

ocl/OCL.g4

Lines changed: 35 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -18,96 +18,22 @@
1818

1919
grammar OCL;
2020

21-
specification
22-
: 'package' ID '{' classifier* '}' EOF
21+
multipleContextSpecifications
22+
: (singleInvariant | singleDerivedAttribute)+ EOF
2323
;
2424

25-
expressions
26-
: expression (';' expression)* ';'? EOF
25+
contextSpecification
26+
: (singleInvariant | singleDerivedAttribute) EOF
2727
;
2828

29-
classifier
30-
: classDefinition
31-
| interfaceDefinition
32-
| usecaseDefinition
33-
| datatypeDefinition
34-
| enumeration
29+
singleInvariant
30+
: 'context' ID 'inv' ID? ':' expression
3531
;
3632

37-
interfaceDefinition
38-
: 'interface' identifier ('extends' identifier)? '{' classBody? '}'
33+
singleDerivedAttribute
34+
: 'context' qualified_name ':' type ('init:' expression)? 'derive:' expression
3935
;
4036

41-
classDefinition
42-
: 'class' identifier ('extends' identifier)? ('implements' idList)? '{' classBody? '}'
43-
;
44-
45-
classBody
46-
: classBodyElement+
47-
;
48-
49-
classBodyElement
50-
: attributeDefinition
51-
| operationDefinition
52-
| invariant
53-
| stereotype
54-
;
55-
56-
attributeDefinition
57-
: 'attribute' identifier ('identity' | 'derived')? ':' type ';'
58-
| 'static' 'attribute' identifier ':' type ';'
59-
;
60-
61-
operationDefinition
62-
: ('static')? 'operation' identifier '(' parameterDeclarations? ')' ':' type 'pre:' expression 'post:' expression ('activity:' statementList)? ';'
63-
;
64-
65-
parameterDeclarations
66-
: (parameterDeclaration ',')* parameterDeclaration
67-
;
68-
69-
parameterDeclaration
70-
: identifier ':' type
71-
;
72-
73-
idList
74-
: (identifier ',')* identifier
75-
;
76-
77-
usecaseDefinition
78-
: 'usecase' identifier (':' type)? '{' usecaseBody? '}'
79-
| 'usecase' identifier '(' parameterDeclarations ')' (':' type)? '{' usecaseBody? '}'
80-
;
81-
82-
usecaseBody
83-
: usecaseBodyElement+
84-
;
85-
86-
usecaseBodyElement
87-
: 'parameter' identifier ':' type ';'
88-
| 'precondition' expression ';'
89-
| 'extends' identifier ';'
90-
| 'extendedBy' identifier ';'
91-
| 'activity:' statementList ';'
92-
| '::' expression
93-
| stereotype
94-
;
95-
96-
invariant
97-
: 'invariant' expression ';'
98-
;
99-
100-
stereotype
101-
: 'stereotype' identifier ';'
102-
| 'stereotype' identifier '=' STRING1_LITERAL ';'
103-
| 'stereotype' identifier '=' STRING2_LITERAL ';'
104-
| 'stereotype' identifier '=' identifier ';'
105-
;
106-
107-
datatypeDefinition
108-
: 'datatype' identifier '=' type
109-
;
110-
11137
enumeration
11238
: 'enumeration' identifier '{' enumerationLiteral+ '}'
11339
;
@@ -150,6 +76,7 @@ basicExpression
15076
| FLOAT_LITERAL
15177
| STRING1_LITERAL
15278
| STRING2_LITERAL
79+
| ENUMERATION_LITERAL
15380
| ID
15481
| '(' expression ')'
15582
;
@@ -185,9 +112,8 @@ equalityExpression
185112
;
186113

187114
additiveExpression
188-
: additiveExpression '+' additiveExpression
189-
| additiveExpression '-' factorExpression
190-
| factorExpression ('..' | '|->') factorExpression
115+
: additiveExpression ('+' | '-') additiveExpression
116+
| additiveExpression ('..' | '|->' | '.') additiveExpression
191117
| factorExpression
192118
;
193119

@@ -283,17 +209,18 @@ factor2Expression
283209
| '->equalsIgnoreCase'
284210
) '(' expression ')'
285211
| factor2Expression ('->oclAsType' | '->oclIsTypeOf' | '->oclIsKindOf' | '->oclAsSet') '(' expression ')'
286-
| factor2Expression '->collect' '(' identifier '|' expression ')'
287-
| factor2Expression '->select' '(' identifier '|' expression ')'
288-
| factor2Expression '->reject' '(' identifier '|' expression ')'
289-
| factor2Expression '->forAll' '(' identifier '|' expression ')'
290-
| factor2Expression '->exists' '(' identifier '|' expression ')'
291-
| factor2Expression '->exists1' '(' identifier '|' expression ')'
292-
| factor2Expression '->one' '(' identifier '|' expression ')'
293-
| factor2Expression '->any' '(' identifier '|' expression ')'
294-
| factor2Expression '->closure' '(' identifier '|' expression ')'
295-
| factor2Expression '->sortedBy' '(' identifier '|' expression ')'
296-
| factor2Expression '->isUnique' '(' identifier '|' expression ')'
212+
| factor2Expression '->collect' '(' identOptType '|' expression ')'
213+
| factor2Expression '->select' '(' identOptType '|' expression ')'
214+
| factor2Expression '->reject' '(' identOptType '|' expression ')'
215+
| factor2Expression '->forAll' '(' identOptType '|' expression ')'
216+
| factor2Expression '->exists' '(' identOptType '|' expression ')'
217+
| factor2Expression '->exists1' '(' identOptType '|' expression ')'
218+
| factor2Expression '->one' '(' identOptType '|' expression ')'
219+
| factor2Expression '->any' '(' identOptType '|' expression ')'
220+
| factor2Expression '->closure' '(' identOptType '|' expression ')'
221+
| factor2Expression '->sortedBy' '(' identOptType '|' expression ')'
222+
| factor2Expression '->sortedBy' '(' identifier ')'
223+
| factor2Expression '->isUnique' '(' identOptType '|' expression ')'
297224
| factor2Expression '->subrange' '(' expression ',' expression ')'
298225
| factor2Expression '->replace' '(' expression ',' expression ')'
299226
| factor2Expression '->replaceAll' '(' expression ',' expression ')'
@@ -307,6 +234,10 @@ factor2Expression
307234
| basicExpression
308235
;
309236

237+
identOptType
238+
: ID (':' type)?
239+
;
240+
310241
setExpression
311242
: 'OrderedSet{' expressionList? '}'
312243
| 'Bag{' expressionList? '}'
@@ -315,32 +246,14 @@ setExpression
315246
| 'Map{' expressionList? '}'
316247
;
317248

318-
statement
319-
: 'skip'
320-
| 'return'
321-
| 'continue'
322-
| 'break'
323-
| 'var' ID ':' type
324-
| 'if' expression 'then' statement 'else' statement
325-
| 'while' expression 'do' statement
326-
| 'for' ID ':' expression 'do' statement
327-
| 'return' expression
328-
| basicExpression ':=' expression
329-
| statement ';' statement
330-
| 'execute' expression
331-
| 'call' basicExpression
332-
| '(' statementList ')'
333-
;
334-
335-
336-
statementList
337-
: statement (';' statement)*
338-
;
339-
340249
identifier
341250
: ID
342251
;
343252

253+
qualified_name
254+
: ENUMERATION_LITERAL
255+
;
256+
344257
FLOAT_LITERAL
345258
: Digits '.' Digits
346259
;
@@ -352,6 +265,10 @@ STRING1_LITERAL
352265
STRING2_LITERAL
353266
: '\'' (~['\\\r\n] | EscapeSequence)* '\'';
354267

268+
ENUMERATION_LITERAL
269+
: ID '::' ID
270+
;
271+
355272
NULL_LITERAL
356273
: 'null'
357274
;

ocl/README.md

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,55 @@
11
# OCL Antlr Grammar
22

3-
Based on the OCL 2.4 definition https://www.omg.org/spec/OCL/2.4/PDF but with a more consistent use of the arrow operators ->op: these are almost always used for operations on non-primitive types (strings, collections, functions, maps). Thus str.isMatch(patt) is written as str->isMatch(patt) for strings str, patt.
3+
This repository provides grammars for the Object Constraint Language (OCL) 2.4, along with its integration with UML-RSDS, a tool supporting UML 2.5 class diagram notation and OCL 2.4. These grammars aim to enhance the precision and usability of OCL while making it easier to integrate with UML modeling tools.
44

5-
One exception to this rule is string addition: s1 + s2
5+
## Overview
66

7-
Boolean and numeric operators are written with infix or prefix notation as usual, thus:
7+
The repository is structured around one main grammar: OCL. However, it has been split into two files to separate the core OCL grammar from the UML-RSDS specification tool that also includes UML. The separation makes it easier to use and understand the grammars while still allowing their combined use when needed.
88

9+
### Key Features
10+
11+
- **OCL Core Grammar (`OCL.g4`)**: This file contains the core OCL 2.4 grammar, providing a consistent and robust foundation for working with OCL expressions.
12+
- **UML-RSDS Grammar (`UMLRSDS.g4`)**: This file integrates the OCL core grammar with UML-RSDS, enabling the specification of UML models using OCL expressions.
13+
14+
These grammars are designed to support the consistent use of the arrow operator (`->`) for operations on non-primitive types (e.g., strings, collections, functions, maps). For example:
15+
16+
- `str->isMatch(patt)` (for strings `str` and `patt`)
17+
18+
However, string concatenation remains an exception to this rule:
19+
20+
- `s1 + s2`
21+
22+
Boolean and numeric operators retain their traditional infix or prefix notations:
23+
24+
```ocl
925
a xor b
26+
a * bm div netc.
27+
```
28+
29+
### Composition
30+
31+
Using OCL without a tool grammar to specify the UML model is often impractical. While the grammars are modular, they are designed to work seamlessly together:
1032

11-
a * b
33+
- `OCL.g4` provides the foundation for OCL expressions.
34+
- `UMLRSDS.g4` integrates the core OCL grammar to enable UML modeling.
1235

13-
m div n
36+
The repository’s structure ensures flexibility, allowing users to utilize either grammar independently or together, depending on their needs.
1437

15-
etc.
38+
### Future Contributions
1639

40+
This repository currently focuses on the OCL core and UML-RSDS grammars. In the future, there is potential to include grammars from other tools, such as USE-OCL, to further expand the repository’s utility and scope.
1741

18-
## Main contributors
42+
## Main Contributors
1943

2044
* Kevin Lano, 2022
2145

46+
## References
47+
48+
- OCL 2.4 Specification (OMG OCL 2.4 PDF) https://www.omg.org/spec/OCL/2.4/PDF
49+
- UML-RSDS Tool (Supports UML 2.5 and OCL 2.4):
50+
- UML-RSDS at [WIKIPEDIA](https://en.wikipedia.org/wiki/UML-RSDS)
51+
- Lano, K. (2016). Agile Model-Based Development Using UML-RSDS (1st ed.). CRC Press. https://doi.org/10.1201/9781315368153
52+
53+
---
54+
55+
Feel free to contribute to this repository or suggest additional grammars and tools that could enhance its capabilities.

0 commit comments

Comments
 (0)