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
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.
Outline
- Language design strategies
★ inductive vs deductive design
- Compiler architecture
★ compilation by normalization
- WebDSL translation schemes (sketch)
★ entities, templates, access control, validation
Design Strategies
Designing a Domain-Specific Language
How does one find domain-specific abstractions?
- Deductive (top-down) design
- Inductive (bottom-up) design
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
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
Designing a Domain-Specific Language
From JPA/Seam/JSF to WebDSL
- JPA to entity declarations
- JSF to templates
- Seam actions
See CodeGen 2008 slides
Compiler Architecture
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
Compilation by Normalization
Data Model
object identity
inverse relation
entity declaration
one-to-many
property
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
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
Entity Class
avoid name clashes
symbolic identity should be unique
Object Identity
Object Identity
object identity equals primary key
Value Type Properties
avoid name clashes
Many-to-One Properties
cascading
lazy loading
Inverse Properties
update other side of relation
HQL Queries
Standard Queries
Standard Queries
Search
- Lucene / Hibernate Search
★ annotations indicate fields to index
- Search queries
Generating Hibernate Search Annotations
Page Definitions & Navigation
Server
Request URL
GET | POST
Parameters
Response
status
mime type
body
Client
Page
page dispatch
parameter passing
Pages and Templates to Java Classes
Navigate to URL
navigate(topic(t){output(t.name)}
<a href=”/topic/FooBar”>FooBar</a>
<a href=”/topic/<%t.id%>”> <%t.name%> </a>
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
TopicPage.serv
TopicPage.serv
request transaction
TopicPage.serv
handle request
TopicPage.serv
handle request
TopicPage.initVarsAndArgs
TopicPage.loadArguments
TopicPage.loadArguments
Render Page
Render Page
TopicPage.serv
handle post request
TopicPage.serv
handle post request
class TopicPage extends PageServlet { serv(request, response, params) { transaction = newTransaction(); initArguments(params); if(isPost()) { storeInputs(); validateInputs(); handleAction(); } handleExceptions(); render(); storeSessionEntities(); storeMessages(); transaction.commit(); }}
class TopicPage { render() { s = templateServlet.render(); response.out(“ <html> <head> <%includeCSS()%> <%includeJS()%> <%title()%> </head> <body> <%s.write()%> </body> </html> “); }}
Templates
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
render
filter special characters
section nesting
render
rendering WikiText
preventing XSS
render
parameter passing
form
input
binding
submitaction
Forms & Data Binding
issue: bind request parameters to correct binding expressions
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
Repeated Form Elements
multiple instances per page
Form Abstraction
<p> <label for="input221c5e4216d4669e62bee33efc845eb627"> Title </label> <input type="text" value="LoremIpsum" class="inputString" id="input221c5e4216d4669e62bee33efc845eb627" name="input231c5e4216d4669e62bee33efc845eb627"></p>
render
store inputs
<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
<p> <label for="input241c5e4216d4669e62bee33efc845eb627"> Text </label> <textarea id="input241c5e4216d4669e62bee33efc845eb627" class="inputTextarea inputWikiText" name="input251c5e4216d4669e62bee33efc845eb627"> value </textarea></p>
render
More Template Issues
- Template calls
★ element parameters
- Nested template definitions
★ closures
- Email templates
★ absolute URLs
- AJAX templates
★ accessible directly from client as page
Access Control
access control implemented by model-to-model transformation
page dispatch takes care of loading & storing session entities
More Compilation by Normalization
- Validation
★ validation rules translated to exceptions and
★ see paper
- Workflow
★ workflow procedures & processes
★ translated to functions, access control, user interface
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