developing a new epsilon language through annotations: testlang
TRANSCRIPT
MoDELS 2016 Advanced Model Management with EpsilonAdvanced Model Management with EpsilonMoDELS 2016
Developing a new Epsilon Language through Annotations:
TestLangAntonio Garcia-Dominguez
@antoniogado
1
MoDELS 2016 Advanced Model Management with Epsilon
When should I use annotations?
• One of the Epsilon languages does almost everything that you need, but…
• … you need to add a thin layer of orchestration logic on top of it.
• Some examples:– Unit testing (EOL + orchestration)– Model generation (EOL + ...)– Model mutation (EOL/ETL + ...)– Categorized model validation (EVL + ...)– Model-driven game engine...
2
MoDELS 2016 Advanced Model Management with Epsilon
Example TestLang script (v1)var rnd = new Native('java.util.Random');
@test
operation allInstancesWorks() {
if (Class.all.size != 6) { “FAILED aiw”.println(); }
}
@test
$times 10 * 10
operation randomTest() {
var b = 2 + rnd.nextInt(9); var e = rnd.nextInt(10);
if (slowPow(b, e) != fastPow(b, e)) { “FAILED rt”.println(); }
} 3
MoDELS 2016 Advanced Model Management with Epsilon
Overview
• TestLang is divided into:– org.eclipse.epsilon.examples.testlang.engine
implements the language (Eclipse-agnostic)– org.eclipse.epsilon.examples.testlang.dt
provides Eclipse dev tools
• Sources here:– https://git.eclipse.org/c/epsilon/org.eclipse.e
psilon.git/tree/examples
4
MoDELS 2016 Advanced Model Management with Epsilon
TESTLANG EXECUTION ENGINEImplementing a simple unit testing language on top of EOL
5
MoDELS 2016 Advanced Model Management with Epsilon
TestLangModule: execution engine
● Inherits from EolModule, overrides methods○ execute(): interprets @test and $times
● 68 lines of Java code6
MoDELS 2016 Advanced Model Management with Epsilon
AnnotatableModuleElement
• Superclass of anything that can be annotated– EOL operations– ETL rules– EVL contexts and constraints...
• Two types of annotations supported:– Simple: “@test a” - constant value– Executable: “$test self.x * 2” - uses expression– Repeated annotations are allowed
• Provides methods for handling annotations
7
MoDELS 2016 Advanced Model Management with Epsilon
Example TestLang script (v2)var rnd = new Native('java.util.Random');
@test
operation allInstancesWorks() { areEqual(6, Class.all.size); }
@test
$times 10 * 10
operation randomTest() {
var base = 2 + rnd.nextInt(9);
var exp = rnd.nextInt(10);
areEqual(slowPow(base, exp), fastPow(base, exp));
}8
MoDELS 2016 Advanced Model Management with Epsilon
TestLangModule: add areEqual
● By overriding prepareContext(), we can add task-specific operations/variables
● Operations provided by OperationContributor9
MoDELS 2016 Advanced Model Management with Epsilon
Operation contributors
• When processing x.f(args):– contributesTo(x) must return true– f(args) must be a method of the contributor
• Contextless operations (e.g. “areEqual”) receive a special value in contributesTo:– EolNoType.NoInstance
10
MoDELS 2016 Advanced Model Management with Epsilon
TESTLANG ECLIPSE-BASED DEVELOPER TOOLS
Integration of TestLang into Eclipse
11
MoDELS 2016 Advanced Model Management with Epsilon 12
MoDELS 2016 Advanced Model Management with Epsilon
TestLang developer tools
• Same approach as the engine: inherit from common.dt / eol.dt and refine as desired– 65 lines of Java code
• plugin.xml (49 lines):– Integrates all our subclasses– Associates .eolt with the “New TestLang” wizard– Reuses EOL editor/debugger for .eolt files– Provides a custom icon for .eolt launch configs
• In short: mostly plumbing!
13
MoDELS 2016 Advanced Model Management with Epsilon
TestLang editor: same as EOL
14
MoDELS 2016 Advanced Model Management with Epsilon
TestLang launch facilities
15
MoDELS 2016 Advanced Model Management with Epsilon
Summary
• Showed how to implement a unit testing language on top of EOL:– @test tags an operation as a test case– $times allows for repeating the same test
• Reuses all EOL tooling:– Editor and debugger “for free”– Can work with EMF, CSV, BibTeX, XML, ...
• < 200 lines of code: 133 Java, 49 XML
16
MoDELS 2016 Advanced Model Management with EpsilonAdvanced Model Management with EpsilonMoDELS 2016
Questions?
@antoniogado
17
MoDELS 2016 Advanced Model Management with Epsilon
Other examples
@operator
$condition self.isKindOf(Addition)
operation add2sub() {
// … return Subtraction
}
@operator
$condition self.isKindOf(BooleanLiteral)
operation flipLiteral() {
// … return negated BooleanLiteral
}
For a model mutation language:
18
MoDELS 2016 Advanced Model Management with Epsilon
Other examples
@enemy
@after blinky
@sprite red_ghost.png
operation inky(player, enemies) {
// … compute which way to turn next …
// … compute speed …
}
In a model-driven game engine, operations could provide behavior for AI actors:
The language can provide built-in operations for common/expensive tasks (e.g. pathfinding algorithms).
19