Generating Assertion Code From OCL:: A Transformational Approach Based On Similarities of Implementation Languages
Generating Assertion Code From OCL:: A Transformational Approach Based On Similarities of Implementation Languages
9 MODELS 2009
Abstract
Results
A translation framework has been developed Showed ~50% of effort to construct translation rules saved
2
Background
Model-centric approaches are becoming important in academia and industry OCL is often the language of choice for stipulating constraints on models Successful application depends on mature tool support:
Support for multiple programming languages Customizable assertion code/test case generation
3
Motivating Example
OCL
context MyApplication::doQuery( q: Query): Integer pre: not q = null post: result >= 0
4
Motivating Example
Client 1
Java
MyApplication
doQuery(q : Query) : Integer
<<interface>>
Client 2
Haskell
context MyApplication::doQuery( q: Query): Integer pre: not q = null post: result >= 0
<<write code>>
OCL
Problems
Language Similarities
E.g.,
for (Iterator it = apples.iterator(); it.hasNext(); ) { Apple apple = (Apple) it.next(); if (apple.color == red) { for apple in apples: if apple.color == red: Java Python
8
The Approach
Java, C# like
Python
Lisp
Haskell
JML
<<described in terms of>>
Java MultiJava
C#
Step-by-step Translation
High concreteness
Advantages
Provide a single framework for creating OCL translators for any language
Reuse translator implementations
10
Generation Process
Parser + type checker
ASTImperative DL OCL AST
ENV
ENV
AST
OCL OCL
rewriting rules
Python DL
ENV
Python Code
printing rules
DL = Definition Language
written in
written in
Imperative DL PIM
Imper. DLPython DL
Python DL PSM
12
Transformation Example
employees->forAll( e : Employee | e.salary > 10000)
OCL AST: iteratorExp (assocEndCallExp simpleName("self") . simpleName("employees")) -> simpleName("forAll") ( varDecl(simpleName("e"), pathName("Employee")) | operCallExp( attrCallExp simpleName("e") . simpleName("salary"), simpleName(">"), intLitExp("10000") ) ) Company 1 .. * employees Employee salary : Int
13
Rewriting Rules
iteratorExp OE -> simpleName(forAll) `( varDecl(SN, TYPE) | OE `) impMethodExtract( { impForLoop( OE, impVarDecl(TYPE, SN), { impIf( impNot(OE''), { impReturn( boolLitFalse ) }) }) ; impReturn( boolLitTrue ) }, pathName("Boolean")) .
14
Imperative DL
impMethodExtract( {emptyCtx | impForLoop( assocEndCallExp simpleName("self"). simpleName("employees"), impVarDecl(pathName("Employee"),simpleName("e")), { "ctx:iter" = simpleName("it_e") | impIf( impNot(operCallExp( attrCallExp simpleName("e"). simpleName("salary"), simpleName(">"), intLitExp(10000"))), {emptyCtx | impReturn(boolLitFalse) }) }) }; {emptyCtx | impReturn(boolLitTrue) },pathName("Boolean"))
15
Generation Result
class Employee: def __init__(self, name, salary): self.name = name self.salary = salary
From UML
class Company: def __init__(self, employees): self.employees = employees def callExtMethod_A(self): for e in self.employees: if (not (e.salary > 10000)): return False return True def inv(self): return self.callExtMethod_A()
From OCL
16
Approach Evaluation
Implemented a tool in the Maude System (*) To show the ability to implement several OCL translators with significant effort savings Target languages
Imperative
Java Python
Imperative Java Python OCL
Predefined
Functional Haskell OHaskell
Functional
Haskell OHaskell
* http://maude.cs.uiuc.edu/
17
Approach Evaluation
Compare
Java Code
Direct Approach
Java Code
Proposed Approach
18
Evaluation Results
Imperative languages
Java
85
Effort
36
Direct
Our approach
Effort
99 59
Direct
Our approach
19
Evaluation Results
Functional languages
Haskell
76 Effort 52
Effort
76
Evaluation Results
Direct Approach Proposed Approach
85 99 76 76 316
36 59 52 1 148
Conclusion
Proposed a novel technique to generate assertion code from OCL constraints Used structural similarities of programming languages to enable reusing in OCL translators for multiple languages
Saving manual effort
Run an experiment to show the claimed effort savings by using our approach
22
Future Work
Hierarchy Limitations
Difficult to extend hierarchy from the middle Does not allow multiple parent nodes (e.g. useful for implementing an imperative/functional hybrid)