strategies for design & implementation of domain-specific languages

63
Strategies for Design & Implementation of Domain-Specific Languages From Java to WebDSL and Back Course IN4308 Master Computer Science Delft University of Technology Eelco Visser http://eelcovisser.org Lecture 5

Upload: eelco-visser

Post on 26-May-2015

808 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Strategies for Design & Implementation of Domain-Specific Languages

Strategies for Design & Implementation of Domain-Specific Languages

From Java to WebDSL and Back

Course IN4308Master Computer Science

Delft University of Technology

Eelco Visserhttp://eelcovisser.org

Lecture 5

Page 2: Strategies for Design & Implementation of Domain-Specific Languages

Warning: the code fragments in this presentation have been grossly

simplified, even when seemingly bulky.

Consult actual code generated by WebDSL compiler to get the full story.

Page 3: Strategies for Design & Implementation of Domain-Specific Languages

Outline

- Language design strategies

★ inductive vs deductive design

- Compiler architecture

★ compilation by normalization

- WebDSL translation schemes (sketch)

★ entities, templates, access control, validation

Page 4: Strategies for Design & Implementation of Domain-Specific Languages

Design Strategies

Page 5: Strategies for Design & Implementation of Domain-Specific Languages

Designing a Domain-Specific Language

How does one find domain-specific abstractions?

- Deductive (top-down) design

- Inductive (bottom-up) design

Page 6: Strategies for Design & Implementation of Domain-Specific Languages

Deductive (top-down)

- Analyze problem domain

- Identify elements to specify

★ language constructs

- Identify/design solution domain

★ architecture, technology,

- Investigate implementation

★ code generation templates

- Risk

★ design language that is hard to implement

Page 7: Strategies for Design & Implementation of Domain-Specific Languages

Inductive (bottom-up) design

- Consider best practices in solution domain

- Identify coding patterns

- Identify commonality

★ code generation templates

- Identify variability

★ language constructs

- Risk

★ design language that is too low-level

Page 8: Strategies for Design & Implementation of Domain-Specific Languages

Designing a Domain-Specific Language

From JPA/Seam/JSF to WebDSL

- JPA to entity declarations

- JSF to templates

- Seam actions

See CodeGen 2008 slides

Page 9: Strategies for Design & Implementation of Domain-Specific Languages

Compiler Architecture

Page 10: Strategies for Design & Implementation of Domain-Specific Languages

Compiler Architecture

- Syntax

★ check syntactic well-formedness

- Static semantics

★ consistency checking

- Model-to-model transformation

★ desugaring: express constructs in core language

★ add higher-level abstractions

- Code generation

★ translate core language models to implementation

Page 11: Strategies for Design & Implementation of Domain-Specific Languages

Compilation by Normalization

Page 12: Strategies for Design & Implementation of Domain-Specific Languages

Data Model

Page 13: Strategies for Design & Implementation of Domain-Specific Languages

object identity

inverse relation

entity declaration

one-to-many

property

Page 14: Strategies for Design & Implementation of Domain-Specific Languages

Entity Declarations to JPA Entity Classes

- Java Persistence API

- Entity declaration to Java POJO

★ class with private fields, getters, and setters

★ annotations

• values

• relations: one-to-many, many-to-one

★ object identity

- Object-relational mapping

★ entity classes to database tables

★ properties to columns

Page 15: Strategies for Design & Implementation of Domain-Specific Languages

Object Identity

- Automatic identity column for all entities

★ UUID

★ primary key

★ used as foreign key in relations

- Custom symbolic id property

★ key :: String (id)★ can be changed without compromising foreign keys

★ symbolic id used in URL

Page 16: Strategies for Design & Implementation of Domain-Specific Languages

Entity Class

avoid name clashes

symbolic identity should be unique

Page 17: Strategies for Design & Implementation of Domain-Specific Languages

Object Identity

Page 18: Strategies for Design & Implementation of Domain-Specific Languages

Object Identity

object identity equals primary key

Page 19: Strategies for Design & Implementation of Domain-Specific Languages

Value Type Properties

avoid name clashes

Page 20: Strategies for Design & Implementation of Domain-Specific Languages

Many-to-One Properties

cascading

lazy loading

Page 21: Strategies for Design & Implementation of Domain-Specific Languages

Inverse Properties

update other side of relation

Page 22: Strategies for Design & Implementation of Domain-Specific Languages

HQL Queries

Page 23: Strategies for Design & Implementation of Domain-Specific Languages

Standard Queries

Page 24: Strategies for Design & Implementation of Domain-Specific Languages

Standard Queries

Page 25: Strategies for Design & Implementation of Domain-Specific Languages

Search

- Lucene / Hibernate Search

★ annotations indicate fields to index

- Search queries

Page 26: Strategies for Design & Implementation of Domain-Specific Languages

Generating Hibernate Search Annotations

Page 27: Strategies for Design & Implementation of Domain-Specific Languages

Page Definitions & Navigation

Page 28: Strategies for Design & Implementation of Domain-Specific Languages

Server

Request URL

GET | POST

Parameters

Response

status

mime type

body

Client

Page

Page 29: Strategies for Design & Implementation of Domain-Specific Languages

page dispatch

parameter passing

Page 30: Strategies for Design & Implementation of Domain-Specific Languages

Pages and Templates to Java Classes

Page 31: Strategies for Design & Implementation of Domain-Specific Languages

Navigate to URL

navigate(topic(t){output(t.name)}

<a href=”/topic/FooBar”>FooBar</a>

<a href=”/topic/<%t.id%>”> <%t.name%> </a>

Page 32: Strategies for Design & Implementation of Domain-Specific Languages

Request URL

GET | POST

Parameters

Response

status

mime type

body

param value

t Foobar

param map

name class

topic

edittopicpage dispatch table

TopicPage

serv

Dispatch

Page 33: Strategies for Design & Implementation of Domain-Specific Languages

TopicPage.serv

Page 34: Strategies for Design & Implementation of Domain-Specific Languages

TopicPage.serv

request transaction

Page 35: Strategies for Design & Implementation of Domain-Specific Languages

TopicPage.serv

handle request

Page 36: Strategies for Design & Implementation of Domain-Specific Languages

TopicPage.serv

handle request

Page 37: Strategies for Design & Implementation of Domain-Specific Languages

TopicPage.initVarsAndArgs

Page 38: Strategies for Design & Implementation of Domain-Specific Languages

TopicPage.loadArguments

Page 39: Strategies for Design & Implementation of Domain-Specific Languages

TopicPage.loadArguments

Page 40: Strategies for Design & Implementation of Domain-Specific Languages

Render Page

Page 41: Strategies for Design & Implementation of Domain-Specific Languages

Render Page

Page 42: Strategies for Design & Implementation of Domain-Specific Languages

TopicPage.serv

handle post request

Page 43: Strategies for Design & Implementation of Domain-Specific Languages

TopicPage.serv

handle post request

Page 44: Strategies for Design & Implementation of Domain-Specific Languages

class TopicPage extends PageServlet { serv(request, response, params) { transaction = newTransaction(); initArguments(params); if(isPost()) { storeInputs(); validateInputs(); handleAction(); } handleExceptions(); render(); storeSessionEntities(); storeMessages(); transaction.commit(); }}

Page 45: Strategies for Design & Implementation of Domain-Specific Languages

class TopicPage { render() { s = templateServlet.render(); response.out(“ <html> <head> <%includeCSS()%> <%includeJS()%> <%title()%> </head> <body> <%s.write()%> </body> </html> “); }}

Page 46: Strategies for Design & Implementation of Domain-Specific Languages

Templates

Page 47: Strategies for Design & Implementation of Domain-Specific Languages

Template Lifecycle

- Store inputs

★ bind request parameters to form inputs

- Validate inputs

★ check validation rules

- Handle action

★ execute selected action

- Render

★ print markup

★ render properties of objects

for each template

Page 48: Strategies for Design & Implementation of Domain-Specific Languages

render

filter special characters

section nesting

Page 49: Strategies for Design & Implementation of Domain-Specific Languages

render

rendering WikiText

preventing XSS

Page 50: Strategies for Design & Implementation of Domain-Specific Languages

render

parameter passing

Page 51: Strategies for Design & Implementation of Domain-Specific Languages

form

input

binding

submitaction

Forms & Data Binding

issue: bind request parameters to correct binding expressions

Page 52: Strategies for Design & Implementation of Domain-Specific Languages

Data Binding Issues

- Template control-flow

★ template calls: inputs may be nested

★ if-then-else: elements not always present

★ for-loop: multiple occurrences of same form

Page 53: Strategies for Design & Implementation of Domain-Specific Languages

Repeated Form Elements

multiple instances per page

Page 54: Strategies for Design & Implementation of Domain-Specific Languages

Form Abstraction

Page 55: Strategies for Design & Implementation of Domain-Specific Languages

<p> <label for="input221c5e4216d4669e62bee33efc845eb627"> Title </label> <input type="text" value="LoremIpsum" class="inputString" id="input221c5e4216d4669e62bee33efc845eb627" name="input231c5e4216d4669e62bee33efc845eb627"></p>

render

store inputs

Page 56: Strategies for Design & Implementation of Domain-Specific Languages

<form onsubmit="return clickFirstButton(this);" method="POST" accept-charset="UTF-8" action="http://localhost:8080/wiki/editpage2/LoremIpsum" id="form51c5e4216d4669e62bee33efc845eb627" name="form51c5e4216d4669e62bee33efc845eb627">

<input type="hidden" value="1" name="form51c5e4216d4669e62bee33efc845eb627"> <input type="hidden" value="LoremIpsum" name="p"> ... <input type="submit" class="button" value="Save" name="action61c5e4216d4669e62bee33efc845eb627"></form>

...

render

bind action to action code

handle action

Page 57: Strategies for Design & Implementation of Domain-Specific Languages

<p> <label for="input241c5e4216d4669e62bee33efc845eb627"> Text </label> <textarea id="input241c5e4216d4669e62bee33efc845eb627" class="inputTextarea inputWikiText" name="input251c5e4216d4669e62bee33efc845eb627"> value </textarea></p>

render

Page 58: Strategies for Design & Implementation of Domain-Specific Languages

More Template Issues

- Template calls

★ element parameters

- Nested template definitions

★ closures

- Email templates

★ absolute URLs

- AJAX templates

★ accessible directly from client as page

Page 59: Strategies for Design & Implementation of Domain-Specific Languages

Access Control

Page 60: Strategies for Design & Implementation of Domain-Specific Languages

access control implemented by model-to-model transformation

Page 61: Strategies for Design & Implementation of Domain-Specific Languages

page dispatch takes care of loading & storing session entities

Page 62: Strategies for Design & Implementation of Domain-Specific Languages

More Compilation by Normalization

- Validation

★ validation rules translated to exceptions and

★ see paper

- Workflow

★ workflow procedures & processes

★ translated to functions, access control, user interface

Page 63: Strategies for Design & Implementation of Domain-Specific Languages

Schedule

Lab this week

★ 10 days until deadline Design 1

Cases

★ Case 2: web abstractions

★ See Case 2 for papers to read

Next

★ Lecture 6: language workbenches, modeling languages

★ Lecture 7: basic code generation