Domain Specific Languages
-
Upload
weerasak -
Category
Technology
-
view
5.189 -
download
0
description
Transcript of Domain Specific Languages
Domain Specific Domain Specific LanguageLanguage
Dr. Weerasak WitthawaskulDr. Weerasak Witthawaskul““Mr. SweetCorn”Mr. SweetCorn”
([email protected])([email protected])
Narisa.com’s NJUG Meeting #3Narisa.com’s NJUG Meeting #322 July 200722 July 2007
Who am I?Who am I? oop guy (Pantip) oop guy (Pantip) SweetCorn (narisa.com)SweetCorn (narisa.com) Wee (weerasak.com)Wee (weerasak.com)
UIUC – Home of Illiac, Transistor, UIUC – Home of Illiac, Transistor, Superconductivity, Mosaic, Paypal, Superconductivity, Mosaic, Paypal, YouTube, Design Patterns, RefactoringYouTube, Design Patterns, Refactoring
Motorola Labs, Schuamburg ILMotorola Labs, Schuamburg IL ThoughtWorks Inc., Chicago ILThoughtWorks Inc., Chicago IL
Graduate LifeGraduate Life
Programming Paradigm Programming Paradigm ShiftsShifts
Domain Specific LanguagesDomain Specific Languages Generative ProgrammingGenerative Programming Model Driven Architecture (MDA) - OMGModel Driven Architecture (MDA) - OMG Intentional Programming – Charles Intentional Programming – Charles
Simonyi, ex-MicrosoftSimonyi, ex-Microsoft Language Oriented Programming – Language Oriented Programming –
Sergey Dmitriev, JetBrains (Home of Sergey Dmitriev, JetBrains (Home of IntelliJ IDEA)IntelliJ IDEA)
Software Factories – Jack Greenfield, Software Factories – Jack Greenfield, MicrosoftMicrosoft
What is DSL?What is DSL?
““วั�นน��มี�ประชุ�มีนร�สาดอทคอมีวั�นน��มี�ประชุ�มีนร�สาดอทคอมี””
ภาษาไทยภาษาไทย
““ท�าตั�วัตั�สตัด�น�กถู�กแมีวั�นมีาย�งท�าตั�วัชุ�วัชุ�วัอย��ท�าตั�วัตั�สตัด�น�กถู�กแมีวั�นมีาย�งท�าตั�วัชุ�วัชุ�วัอย��อ�กอ�ก...” ...”
ภาษาไทยเด กแนวั ภาษาไทยเด กแนวั (domain specific)(domain specific)
What is DSL?What is DSL?
Mini-language to solve specific Mini-language to solve specific “domain” problem“domain” problem
Why is it important?Why is it important? SQL – Relational Database SQL – Relational Database
Manipulation LanguageManipulation Language Domain specific vs General purposeDomain specific vs General purpose What constitutes “domain specific” What constitutes “domain specific”
in programming languages?in programming languages?
Mini GameMini Game
““What languages are they?”What languages are they?”
Are they languages for specific Are they languages for specific domains?domains?
From http://www.ntecs.de/old-hp/uu9r/lang/html/lang.en.htmlFrom http://www.ntecs.de/old-hp/uu9r/lang/html/lang.en.html
What Languages are What Languages are they?they?
#include <stdio.h> #include <stdio.h>
main() { main() {
printf("Hello World\n");printf("Hello World\n");
}}
CC
What Languages are What Languages are they?they?
.MODEL tiny.MODEL tiny
.CODE .CODE ORG 100h ORG 100h HELLO PROCHELLO PROC MOV AH,09h MOV AH,09h LEA DX,msg LEA DX,msg INT 21h ;Display Hello WorldINT 21h ;Display Hello World MOV AX,4C00h ;Exit to DOS MOV AX,4C00h ;Exit to DOS INT 21h INT 21h HELLO ENDP HELLO ENDP msg DB 'Hello World$' msg DB 'Hello World$' END HELLO END HELLO
Assembly Language Intel 80x86 (DOS/MASM)Assembly Language Intel 80x86 (DOS/MASM)
What Languages are What Languages are they?they?
<?php <?php
echo "Hello World\n"; echo "Hello World\n";
?> ?>
PHPPHP
What Languages are What Languages are they?they?
public class HelloWorld { public class HelloWorld {
public static void main(String[] args)public static void main(String[] args)
System.out.println("Hello World"); System.out.println("Hello World");
} }
}}
JavaJava
How about JME? JEE?How about JME? JEE?
What Languages are What Languages are they?they?
PROGRAM HelloWorld; PROGRAM HelloWorld;
BEGIN BEGIN
WRITELN('Hello World'); WRITELN('Hello World');
END.END.
PASCALPASCAL
What Languages are What Languages are they?they?
print "Hello World" print "Hello World"
PRINT “Hello World”PRINT “Hello World”
Basic, Python, Ruby, PerlBasic, Python, Ruby, Perl
What Languages are What Languages are they?they?
SELECT 'Hello World' SELECT 'Hello World'
SQLSQL
What Languages are What Languages are they?they?
BEGIN BEGIN
DBMS_OUTPUT.put_line('Hello World');DBMS_OUTPUT.put_line('Hello World');
END;END;
PL/SQLPL/SQL
What Languages are What Languages are they?they?
Report Hello_World.Report Hello_World.
Write: "Hello World".Write: "Hello World".
ABAPABAP
What Languages are What Languages are they?they?
io:fwrite("Hello World~n"). io:fwrite("Hello World~n").
ErlangErlang
What Languages are What Languages are they?they?
IDENTIFICATION DIVISION. IDENTIFICATION DIVISION. PROGRAM-ID. HelloWorld. PROGRAM-ID. HelloWorld. AUTHOR. Fabritius. AUTHOR. Fabritius. ENVIRONMENT DIVISION. ENVIRONMENT DIVISION. CONFIGURATION SECTION. CONFIGURATION SECTION. INPUT-OUTPUT SECTION. INPUT-OUTPUT SECTION. DATA DIVISION. DATA DIVISION. FILE SECTION. FILE SECTION. WORKING-STORAGE SECTION. WORKING-STORAGE SECTION. LINKAGE SECTION. LINKAGE SECTION. PROCEDURE DIVISION. DISPLAY "Hello World". STOP RUN. PROCEDURE DIVISION. DISPLAY "Hello World". STOP RUN.
COBOLCOBOL
What Languages are What Languages are they?they?
% Hello World % Hello World
/Courier findfont /Courier findfont
28 scalefont 28 scalefont
setfont setfont
0 0 moveto 0 0 moveto
(Hello World) show(Hello World) show
showpageshowpage
PostscriptPostscript
What Languages are What Languages are they?they?
/(?ms)"((?>[^\\"]+)?(?>\\.[^\\"]*)*)//(?ms)"((?>[^\\"]+)?(?>\\.[^\\"]*)*)/
Regex – Regular ExpressionRegex – Regular Expression
What Languages are What Languages are they?they?
describe ArticlesController, " GET index" dodescribe ArticlesController, " GET index" do it "should be successful" doit "should be successful" do get "index"get "index" response.should be_successresponse.should be_success endend
if "should set @articles" doif "should set @articles" do
Article.should_receive(:find).and_return(mock('artArticle.should_receive(:find).and_return(mock('articles'))icles'))
get "index"get "index" assigns[:articles].should_not be_nilassigns[:articles].should_not be_nilendend
RSpec on Ruby - Language of testing from TDD to RSpec on Ruby - Language of testing from TDD to BDDBDD
What Languages are What Languages are they?they?
>gi|2501594|sp|Q57997|Y577_METJA >gi|2501594|sp|Q57997|Y577_METJA PROTEIN PROTEIN MJ0577MSVMYKKILYPTDFSETAEIALKHVKAFKTMJ0577MSVMYKKILYPTDFSETAEIALKHVKAFKTLKAEEVILLHVIDEREIKKRDIFSLLLGVAGLNKSVLKAEEVILLHVIDEREIKKRDIFSLLLGVAGLNKSVEEFENELKNKLTEEAKNKMENIKKELEDVGFKVKDIEEFENELKNKLTEEAKNKMENIKKELEDVGFKVKDIIVVGIPHEEIVKIAEDEGVDIIIMGSHGKTNLKEILIVVGIPHEEIVKIAEDEGVDIIIMGSHGKTNLKEILLGSVTENVIKKSNKPVLVVKRKNSLGSVTENVIKKSNKPVLVVKRKNS
BLAST queryBLAST query
What Languages are What Languages are they?they?
aspect VisitAspect { aspect VisitAspect {
void Point.acceptVisitor(Visitor v) { void Point.acceptVisitor(Visitor v) {
v.visit(this); v.visit(this);
} }
} }
AspectJAspectJ
What Languages are What Languages are they?they?
<!--ADVISOR--><!--ADVISOR--><bean id="<bean id="theCuckoosEggAdvisortheCuckoosEggAdvisor" "
class="org.springframework.aop.support.RegexpMethodPointclass="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> cutAdvisor">
<property name="advice"> <property name="advice"> <ref local="<ref local="theReplacementFeaturePart1AdvicetheReplacementFeaturePart1Advice"/> "/>
</property> </property> <property name="pattern"> <property name="pattern">
<value><value>IBusinessLogic.*IBusinessLogic.*</value> </value> </property> </property> </bean> </bean> <!--ADVICE--> <!--ADVICE--> <bean id="<bean id="theReplacementFeaturePart1AdvicetheReplacementFeaturePart1Advice" "
class="class="CuckoosEggCuckoosEgg"/> "/>
SpringAOPSpringAOP
What Languages are What Languages are they?they?
<html> <html> <head><head> <title>Hello World</title> <title>Hello World</title> </head> </head> <body> <body> Hello World Hello World </body> </body>
</html></html>
Of course – HTML!Of course – HTML!
What is DSL?What is DSL? No standard definitionNo standard definition
Specific vs General depends on point of viewSpecific vs General depends on point of view VB vs VBAVB vs VBA
It helps revealing intentionsIt helps revealing intentions Language that domain experts understandLanguage that domain experts understand
It becomes more and more popular It becomes more and more popular because of tooling supportbecause of tooling support You can apply DSLs to solve your problemsYou can apply DSLs to solve your problems You can even create your own DSL!You can even create your own DSL!
DSL ExamplesDSL Examples
DSLDSL DomainDomain
SQLSQL Database Database ManipulationManipulation
PostscriptPostscript PublishingPublishing
HibernateHibernate Object Relational Object Relational MappingMapping
RegexRegex Pattern MatchingPattern Matching
BNLBNL Business Natural Business Natural LanguageLanguage
AdhersionAdhersion TelecomTelecom
DSL – Telecom App DSL – Telecom App FrameworkFramework
Adhearsion (open source telecom application framework)
+ Asterisk (open source telephony platform)
LEL – Layout Expression LEL – Layout Expression LanguageLanguage
Source: http://pphetra.blogspot.com/2007/07/lel.html
layout = " [ label_1 | label3 ] layout = " [ label_1 | label3 ]
[ (300,300)*text1 | (150)people ] [ (300,300)*text1 | (150)people ]
[ <label2 | _ ] [ <label2 | _ ]
[ message | buttons ] " [ message | buttons ] "
When to justify DSL When to justify DSL
Repeated need of solutions to Repeated need of solutions to particular problemsparticular problems
You have a good understanding of You have a good understanding of problem domainproblem domain
There is a ready-to-use DSL in that There is a ready-to-use DSL in that domaindomain
Or if you have to invent one…Or if you have to invent one… What should it look like?What should it look like?
Example - Coffee Order Example - Coffee Order DSLDSL
Starbucks Coffee Order Starbucks Coffee Order (*)(*)
“Venti half-caf, non-fat, no foam, no whip latte”
Traditional Implementation
(*) Agile DSL Development in Ruby – Obie Fernandez (http://obiefernandez.com)
Problems with the Problems with the exampleexample
Depends on APIsDepends on APIs Code does not match the way the domain Code does not match the way the domain
is describedis described Still difficult to read and verifyStill difficult to read and verify
DSL styleDSL style
Reveal intention betterReveal intention better Take advantage of Ruby featuresTake advantage of Ruby features
Untyped declarationUntyped declaration No parenthesis parameterNo parenthesis parameter Metaprogramming support i.e. create Metaprogramming support i.e. create
class methods on the fly etc.class methods on the fly etc.
Coffee Order DSL in JavaCoffee Order DSL in Java
API approachAPI approachCoffeeOrder order = new Latte(VENTI, CoffeeOrder order = new Latte(VENTI, HALF_CAF, NONFAT_MILK);HALF_CAF, NONFAT_MILK);
CoffeeOrder coffee = order.prepare(false);CoffeeOrder coffee = order.prepare(false);
DSL approachDSL approachCoffeeOrder order = new CoffeeOrder order = new Latte().size(VENTI).caffeine(HALF).milk(NOLatte().size(VENTI).caffeine(HALF).milk(NONFAT).foam(FALSE);NFAT).foam(FALSE);
CoffeeOrder coffee = order.prepare();CoffeeOrder coffee = order.prepare();
How about other How about other representations?representations?
XMLXML<Order type=“Latte”><Order type=“Latte”> <caffeine>half</caffeine><caffeine>half</caffeine> <milk>nonfat</milk><milk>nonfat</milk> <foam>false</form><foam>false</form> <whip>false</whip><whip>false</whip></Order></Order> EnglishEnglish“Venti half-caf, non-fat, no foam, no whip
latte”“Venti latte with no whip cream, no foam,
non-fat milk, half-caffeine”
Types of DSLTypes of DSL Internal DSLInternal DSL
Use an existing “host” programming language Use an existing “host” programming language to describe DSLto describe DSL
Leverage host compiler/interpreterLeverage host compiler/interpreter Some programming languages are better for Some programming languages are better for
describing DSL e.g. dynamic languages; some describing DSL e.g. dynamic languages; some use metaprogramming / annotationsuse metaprogramming / annotations
External DSLExternal DSL Define your own format of the languageDefine your own format of the language Use XML along with XSD or DTDUse XML along with XSD or DTD Must create your own parser/interpreterMust create your own parser/interpreter Or use DSL toolsOr use DSL tools
External DSLExternal DSL
Business Natural Language (BNL)Business Natural Language (BNL)
““exclude offer 30 percent cli if d score exclude offer 30 percent cli if d score is less than or equal to 2”is less than or equal to 2”
Custom DSLCustom DSLshow us-state field when country select box is show us-state field when country select box is
"United States""United States"
show province field when country select box is show province field when country select box is "Canada""Canada"
show brutus when us-state select box is "Ohio" or show brutus when us-state select box is "Ohio" or "Michigan""Michigan"
DSL DesignDSL Design DSL is not just APIDSL is not just API
show us-state field when country select box is "United States"show us-state field when country select box is "United States"show province field when country select box is "Canada"show province field when country select box is "Canada"show brutus when us-state select box is "Ohio" or "Michigan" show brutus when us-state select box is "Ohio" or "Michigan"
JavaScript Solution #1JavaScript Solution #1create_dynamic_field("show", "us-state-field", "country", "United create_dynamic_field("show", "us-state-field", "country", "United
States");States");create_dynamic_field("show", "province-field", "country", create_dynamic_field("show", "province-field", "country",
"Canada");"Canada");create_dynamic_field("show", "brutus", "state", ["Ohio", create_dynamic_field("show", "brutus", "state", ["Ohio",
"Michigan"]);"Michigan"]);
JavaScript Solution #2JavaScript Solution #2show("us-state-field").when("country").is("United States");show("us-state-field").when("country").is("United States");show("province-field").when("country").is("Canada");show("province-field").when("country").is("Canada");show("brutus").when("us-state").is("Ohio, Michigan");show("brutus").when("us-state").is("Ohio, Michigan");
DSL ToolsDSL Tools
Martin Fowler coined the term Martin Fowler coined the term “Language Workbench”“Language Workbench”
Provide GUI frontend, metamodeling Provide GUI frontend, metamodeling and/or code generation to define and and/or code generation to define and use DSLuse DSL Microsoft DSL tool for Visual StudioMicrosoft DSL tool for Visual Studio Intentional SoftwareIntentional Software Eclipse EMFEclipse EMF JetBrains Meta Programing SystemJetBrains Meta Programing System
MS DSL Tool for VS MS DSL Tool for VS StudioStudio
Tutorial: Applying Domain-Specific Modeling to Game Development with the Microsoft DSL Tools. André W. B. Furtado, André L. de M. Santos
Metacase’s MetaEdit+Metacase’s MetaEdit+
http://www.metacase.com/fs.asp?vasen=vasen.html&paa=cases/dsm_examples.html
Jetbrains Meta Jetbrains Meta Programming System Programming System
(MPS)(MPS)
SummarySummary
DSL is one way to increase a level of DSL is one way to increase a level of abstractionabstraction DSL developers define syntax and DSL developers define syntax and
semantics to express solutions closer to semantics to express solutions closer to the domain problemthe domain problem Using DSL toolsUsing DSL tools Creating parser/interpreterCreating parser/interpreter Embedding to host languagesEmbedding to host languages
DSL users can be either programmers DSL users can be either programmers or domain expertsor domain experts
SummarySummary
Types of DSLTypes of DSL Internal DSLInternal DSL
People start to use dynamic languages to People start to use dynamic languages to create DSLcreate DSL
Ruby on Rails/Grails, Rake (Make on Ruby)Ruby on Rails/Grails, Rake (Make on Ruby)
External DSLExternal DSL Design is importantDesign is important
Consult with domain expertsConsult with domain experts
Further InformationFurther Information Wikipedia’s DSL Wikipedia’s DSL
http://en.wikipedia.org/wiki/Domain-http://en.wikipedia.org/wiki/Domain-specific_programming_languagespecific_programming_language
Martin Fowler’s Bliki on DSL Martin Fowler’s Bliki on DSL http://martinfowler.com/bliki/dsl.htmlhttp://martinfowler.com/bliki/dsl.html
Language Workbenches: The Killer-App for Language Workbenches: The Killer-App for Domain Specific Languages? Domain Specific Languages? http://martinfowler.com/articles/languageWorkbehttp://martinfowler.com/articles/languageWorkbench.htmlnch.html
Obie Fernandez’s Agile DSL Development in Ruby (http://obiefernandez.com/presentations/obie_fernandez-agile_dsl_development_in_ruby.pdf)
Sergey Dmitriev’s Language Oriented Sergey Dmitriev’s Language Oriented Programming: The Next Programming Paradigm Programming: The Next Programming Paradigm (www.onboard.jetbrains.com/is1/articles/04/10/lo(www.onboard.jetbrains.com/is1/articles/04/10/lop/mps.pdf)p/mps.pdf)
Q&AQ&A
Thai’s resourcesThai’s resources
Narisa.com’s Design & Narisa.com’s Design & Analysis forum Analysis forum
ThaiDev.orgThaiDev.org
Backup slidesBackup slides