A Domain-Specific Language with Semantics for Shot Puzzles COREF - - PowerPoint PPT Presentation

a domain specific language with semantics for shot
SMART_READER_LITE
LIVE PREVIEW

A Domain-Specific Language with Semantics for Shot Puzzles COREF - - PowerPoint PPT Presentation

A Domain-Specific Language with Semantics for Shot Puzzles COREF work in progress Presented at SET Meeting 11 January 2011, TU/e Tom Verhoeff In cooperation with Ulyana Tikhonova and Maarten Manders Eindhoven University of Technology Dept. of


slide-1
SLIDE 1

A Domain-Specific Language with Semantics for Shot Puzzles COREF work in progress Presented at SET Meeting 11 January 2011, TU/e Tom Verhoeff In cooperation with Ulyana Tikhonova and Maarten Manders Eindhoven University of Technology

  • Dept. of Math. & CS

Model-Drive Software Engineering Software Engineering & Technology

c

2012, T. Verhoeff @ TUE.NL

1/30 DSL for Shot Puzzles

slide-2
SLIDE 2

Motivation behind COREF

  • Software components are systems exhibiting behavior
  • Wanted: tools to define, to generate code, to analyze, to . . .
  • Approach: Domain Specific Language (syntax) to describe . . .
  • Define formal semantics for this language once in DSL framework
  • Use model transformations to realize tools

c

2012, T. Verhoeff @ TUE.NL

2/30 DSL for Shot Puzzles

slide-3
SLIDE 3

Goals of Shot Puzzle Case Study

  • Become familiar with state of the art in DSL tooling

Use Eclipse Modeling Framework (EMF)

  • Find out what it takes to define semantics in a DSL framework

Take ad hoc approach Why Shot puzzles:

  • involve behavior
  • are simple but not trivial

c

2012, T. Verhoeff @ TUE.NL

3/30 DSL for Shot Puzzles

slide-4
SLIDE 4

Shot Puzzle

c

2012, T. Verhoeff @ TUE.NL

4/30 DSL for Shot Puzzles

slide-5
SLIDE 5

Shot Puzzle: Dynamics (informal)

  • A move consists of pushing a marble (horizontally or vertically)
  • ver an empty cell hitting another marble.
  • The push propagates across a chain of aligned marbles.
  • The last marble being hit disappears from the grid.

⑦ ⑦ ⑦ ✲

A B C

⑦ ⑦ ⑦ ✲

A B C

⑦ ⑦ ⑦ ✲

A B C

⑦ ⑦

A B start configuration intermediate 1 intermediate 2 end configuration

c

2012, T. Verhoeff @ TUE.NL

5/30 DSL for Shot Puzzles

slide-6
SLIDE 6

Shot Puzzle: Objective The objective is to remove all marbles but one.

c

2012, T. Verhoeff @ TUE.NL

6/30 DSL for Shot Puzzles

slide-7
SLIDE 7

Mathematical Model for Shot Puzzles: Statics A Shot puzzle is a triple (M, G, g), where

  • M is a finite set (the marbles),
  • G ⊆ Z × Z (the grid), and
  • g : M ֌ G is an injection from M to G (marble locations).

Mapping g is called the initial configuration of the puzzle. Injection: no two marbles at the same location

c

2012, T. Verhoeff @ TUE.NL

7/30 DSL for Shot Puzzles

slide-8
SLIDE 8

Mathematical Model for Shot Puzzles: Example

({ A, B, C }, G, { A → (0, 2), B → (3, 2), C → (5, 2) })

where G = Z6 × Z5 = { (x, y) ∈ Z × Z | 0 ≤ x < 6 ∧ 0 ≤ y < 5 } 1 2 3 4 1 2 3 4 5 A B C

c

2012, T. Verhoeff @ TUE.NL

8/30 DSL for Shot Puzzles

slide-9
SLIDE 9

Mathematical Model for Shot Puzzles: Push A push is a pair (m, v), where

  • m ∈ M is a marble of the puzzle, and
  • v ∈ DIR = { EAST, NORTH, WEST, SOUTH } is a direction vector

with

EAST = (1, 0) NORTH = (0, 1) WEST = (−1, 0) SOUTH = (0, −1)

c

2012, T. Verhoeff @ TUE.NL

9/30 DSL for Shot Puzzles

slide-10
SLIDE 10

Mathematical Model for Shot Puzzles: Current Configuration The current configuration is given by a partial injection from M to G, that is, g : M ֌ G. The set of marbles present on the grid is given by dom(g) , that is, the subset of M for which g is defined. The range of g, denoted by ran(g) , is the set of grid locations with a marble.

c

2012, T. Verhoeff @ TUE.NL

10/30 DSL for Shot Puzzles

slide-11
SLIDE 11

Mathematical Model for Shot Puzzles: Push Applicability A push (m, v) can be applied in configuration g whenever

  • m is present on the grid:

m ∈ dom(g) , and

  • its v-neighbor is empty:

g(m) + v ∈ ran(g) , and

  • there exists a k ∈ N+ such that

– m ‘sees’ a marble in direction v: g(m) + kv ∈ ran(g) , and – the path to that marble belongs to the grid:

{ ℓ : N | 1 ≤ ℓ ≤ k • g(m) + ℓv } ⊆ G

The latter condition is superfluous when the grid is convex .

c

2012, T. Verhoeff @ TUE.NL

11/30 DSL for Shot Puzzles

slide-12
SLIDE 12

Mathematical Model for Shot Puzzles: Dynamic Configuration A dynamic configuration is a pair (g, h) of a configuration and a push mapping h : M

→ DIR, such that

  • only marbles present on the grid can have a push:

dom(h) ⊆ dom(g)

  • and at most one marble has a push:

# dom(h) ≤ 1

Configuration g with push (m, v) corresponds to dynamic configura- tion (g, { m → v }) .

c

2012, T. Verhoeff @ TUE.NL

12/30 DSL for Shot Puzzles

slide-13
SLIDE 13

Mathematical Model for Shot Puzzles: Step A dynamic configuration (g, h) where h = { m → v } evolves in one of three ways to a next dynamic configuration step(g, h) = (g′, h′) : 1.

⑦ ✲

If g(m) + v ∈ G and g(m) + v ∈ ran(g) then

⑦ ✲

(g′, h′) = (g ⊕ { m → g(m) + v }, h)

2.

⑦ ⑦ ✲

If g(m) + v ∈ G and g(m) + v ∈ ran(g), let m′ ∈ M such that g(m′) = g(m) + v; then

⑦ ⑦ ✲

(g′, h′) = (g, { m′ → v })

3.

⑦ ✲

If g(m) + v ∈ G then

(g′, h′) = ({ m } −

⊳ g, ∅)

c

2012, T. Verhoeff @ TUE.NL

13/30 DSL for Shot Puzzles

slide-14
SLIDE 14

Mathematical Model for Shot Puzzles: Move If grid G is finite , then repeated (nested) application of step to dy- namic configuration (g, h) eventually yields a next configuration of the form (g′, ∅). Repeated application of step is as a function steps from dynamic con- figurations to configurations, inductively defined by

steps(g, ∅) =

g

steps(g, { m → v }) = steps(step(g, { m → v }))

Note that steps(g, h) indeed has one fewer marble than g. Define move(g, (m, v)) = steps(g, { m → v }) as the result of the move.

c

2012, T. Verhoeff @ TUE.NL

14/30 DSL for Shot Puzzles

slide-15
SLIDE 15

Mathematical Model for Shot Puzzles: Moves Example

(B,E)

ր

B

C

(C,W )

← −

⑦ ⑦

B C

B

↑(C,W ) ↑(B,W ) ↑(B,W )

⑦ ⑦

B C

(C,W )

← −

⑦ ⑦ ⑦

A B C

(A,E)

− →

⑦ ⑦

A B

↓(B,E) ↓(B,E) ↓(A,E)

B

⑦ ⑦

A B

(A,E)

− →

A

B

(B,W )

ւ

c

2012, T. Verhoeff @ TUE.NL

15/30 DSL for Shot Puzzles

slide-16
SLIDE 16

Mathematical Model for Shot Puzzles: Objective Reduce the set of marbles on the grid to a singleton by a sequence

  • f moves.

c

2012, T. Verhoeff @ TUE.NL

16/30 DSL for Shot Puzzles

slide-17
SLIDE 17

Alternative Mathematical Model for Shot Puzzles There are numerous, equivalent ways to model Shot puzzles formally. Model development is a process with trade offs and design decisions . Alternative: Simplify statics by omitting the grid. A Shot puzzle is a pair (M, g), where

  • M is a finite set (the marbles), and
  • g : M ֌ Z2 is an injection from M to Z × Z (marble locations).

Complicates dynamics a bit (requiring quantifiers).

c

2012, T. Verhoeff @ TUE.NL

17/30 DSL for Shot Puzzles

slide-18
SLIDE 18

Alternative Mathematical Model for Shot Puzzles: Dynamics Dynamic configuration (g, h) where h = { m → v } evolves in one of two ways to a next dynamic configuration step(g, h) = (g′, h′):

  • 1. m will hit m′ If ∃ j : N+ • g(m) + jv ∈ ran(g), then let

k = min{ j : N+ | g(m) + jv ∈ ran(g) • j } and let m′ ∈ M such that g(m′) = g(m) + kv; then

(g′, h′) = (g ⊕ { m → g(m) + (k − 1)v }, { m } −

⊳ h ⊕ { m′ → v })

  • 2. m disappears If ∀ j : N+ • g(m) + jv ∈ ran(g), then

(g′, h′) = ({ m } −

⊳ g, ∅)

c

2012, T. Verhoeff @ TUE.NL

18/30 DSL for Shot Puzzles

slide-19
SLIDE 19

Mathematical Model for Shot Puzzles: Validation Formulate basic properties and prove them:

  • A move reduces the number of marbles by one.
  • Marble identity is irrelevant for solutions (isomorphism).
  • The rules are symmetric under rotation over 90◦ and reflection.
  • The size of a rectangular grid does not matter (for solvability).

Compare to unit test cases expressing expectations about a program.

c

2012, T. Verhoeff @ TUE.NL

19/30 DSL for Shot Puzzles

slide-20
SLIDE 20

Mathematical Model for Shot Puzzles: Reflection

  • Implicitly defined state space
  • More work than expected: Shot puzzles are not so trivial

Two levels of granularity: step and move

  • More useful than expected: details matter and are discovered
  • Easy to make mistakes
  • Tool support desirable
  • Z notation is very compact and usable

(and I did not (yet) use Z schemas)

c

2012, T. Verhoeff @ TUE.NL

20/30 DSL for Shot Puzzles

slide-21
SLIDE 21

Enter Software

  • How to describe concrete Shot puzzles? Need notation/language.
  • How to process Shot puzzles?
  • Ad hoc approach: define Shot-specific data types and I/O routines
  • MDSE approach:

– Define DSL and generate I/O routines – Define transformations to other domains, and use their tools

c

2012, T. Verhoeff @ TUE.NL

21/30 DSL for Shot Puzzles

slide-22
SLIDE 22

Domain Specific Languages

  • Benefits:

– Tooling available to support DSL development – DSL is independent of programming language – Syntax-aware editor for your DSL comes free

  • Challenges:

– Deal with semantics – Deal with (meta)model (co)evolution

c

2012, T. Verhoeff @ TUE.NL

22/30 DSL for Shot Puzzles

slide-23
SLIDE 23

DSL for Shot

  • Abstract Syntax, the meta-model

Compare to Abstract Data Type

  • Eclipse Modeling Framework (EMF), using Ecore

OCL for contraints

  • Concrete Syntax (using EMFText)
  • Semantics via meta-model extension of abstract syntax

and model-to-model transformation

  • Connect to other tools: via M2M, M2T, and T2M transformations

c

2012, T. Verhoeff @ TUE.NL

23/30 DSL for Shot Puzzles

slide-24
SLIDE 24

Abstract Syntax: Meta-Model for Statics

c

2012, T. Verhoeff @ TUE.NL

24/30 DSL for Shot Puzzles

slide-25
SLIDE 25

Meta-Model Derived Attributes In EClass Configuration: property marbles : Marble[*] { derived volatile } { derivation: positions->collect(marble); } property points : Point[*] { derived volatile } { derivation: positions->collect(point); }

c

2012, T. Verhoeff @ TUE.NL

25/30 DSL for Shot Puzzles

slide-26
SLIDE 26

Meta-Model Constraints In EClass Puzzle: invariant ExactlyAllMarblesHaveInitialPosition: marbles->asBag() = configuration.marbles->asBag(); invariant MarbleNamesUnique: marbles->forAll( m1 : Marble, m2 : Marble | m1.name = m2.name implies m1 = m2) In EClass Configuration: invariant MarblePositionsUnique: points->forAll( p1 : Point, p2 : Point | p1.equals(p2) implies p1 = p2)

c

2012, T. Verhoeff @ TUE.NL

26/30 DSL for Shot Puzzles

slide-27
SLIDE 27

Conrete Syntax: Grammar (without pretty-printing info) RULES { Puzzle ::= "Shot_puzzle" name[IDENTIFIER] "{" "marbles" marbles ("," marbles)* ";" configuration "}"; Marble ::= name[IDENTIFIER]; Configuration ::= "configuration" "{" (positions)+ "}"; MarblePosition ::= marble[] "@" point ";"; Point ::= "(" x[INTEGER] "," y[INTEGER] ")"; }

c

2012, T. Verhoeff @ TUE.NL

27/30 DSL for Shot Puzzles

slide-28
SLIDE 28

Concrete Syntax: Example of Textual Shot Puzzle Description Shot_puzzle Easy { marbles A, B, C; configuration { A @ (0, 2); B @ (3, 2); C @ (5, 2); } }

c

2012, T. Verhoeff @ TUE.NL

28/30 DSL for Shot Puzzles

slide-29
SLIDE 29

Meta-Model for Semantic Concepts (old version)

c

2012, T. Verhoeff @ TUE.NL

29/30 DSL for Shot Puzzles

slide-30
SLIDE 30

Conclusion

  • Modeling concepts are usable to express semantics

But: still useful to develop a mathematical model first

  • EMF tools are quite usable, but still need further maturing

Especially: Refactoring (cf. Refactory)

c

2012, T. Verhoeff @ TUE.NL

30/30 DSL for Shot Puzzles