550 likes | 1.04k Vues
Domain Specific Language. Dr. Weerasak Witthawaskul “Mr. SweetCorn” (weerasak@hotmail.com) Narisa.com’s NJUG Meeting #3 22 July 2007. Who am I?. oop guy (Pantip) SweetCorn (narisa.com) Wee (weerasak.com)
E N D
Domain Specific Language Dr. Weerasak Witthawaskul “Mr. SweetCorn” (weerasak@hotmail.com) Narisa.com’s NJUG Meeting #3 22 July 2007
Who am I? • oop guy (Pantip) • SweetCorn (narisa.com) • Wee (weerasak.com) • UIUC – Home of Illiac, Transistor, Superconductivity, Mosaic, Paypal, YouTube, Design Patterns, Refactoring • Motorola Labs, Schuamburg IL • ThoughtWorks Inc., Chicago IL
Programming Paradigm Shifts • Domain Specific Languages • Generative Programming • Model Driven Architecture (MDA) - OMG • Intentional Programming – Charles Simonyi, ex-Microsoft • Language Oriented Programming – Sergey Dmitriev, JetBrains (Home of IntelliJ IDEA) • Software Factories – Jack Greenfield, Microsoft
What is DSL? “วันนี้มีประชุมนริสาดอทคอม” ภาษาไทย “ทำตัวติสตดีนักถูกแมวีนมายังทำตัวชิวชิวอยู่อีก...” ภาษาไทยเด็กแนว (domain specific)
What is DSL? • Mini-language to solve specific “domain” problem • Why is it important? • SQL – Relational Database Manipulation Language • Domain specific vs General purpose • What constitutes “domain specific” in programming languages?
Mini Game “What languages are they?” Are they languages for specific domains? From http://www.ntecs.de/old-hp/uu9r/lang/html/lang.en.html
What Languages are they? #include <stdio.h> main() { printf("Hello World\n"); } C
What Languages are they? .MODEL tiny .CODE ORG 100h HELLO PROC MOV AH,09h LEA DX,msg INT 21h ;Display Hello World MOV AX,4C00h ;Exit to DOS INT 21h HELLO ENDP msg DB 'Hello World$' END HELLO Assembly Language Intel 80x86 (DOS/MASM)
What Languages are they? <?php echo "Hello World\n"; ?> PHP
What Languages are they? public class HelloWorld { public static void main(String[] args) System.out.println("Hello World"); } } Java How about JME? JEE?
What Languages are they? PROGRAM HelloWorld; BEGIN WRITELN('Hello World'); END. PASCAL
What Languages are they? print "Hello World" PRINT “Hello World” Basic, Python, Ruby, Perl
What Languages are they? SELECT 'Hello World' SQL
What Languages are they? BEGIN DBMS_OUTPUT.put_line('Hello World'); END; PL/SQL
What Languages are they? Report Hello_World. Write: "Hello World". ABAP
What Languages are they? io:fwrite("Hello World~n"). Erlang
What Languages are they? IDENTIFICATION DIVISION. PROGRAM-ID. HelloWorld. AUTHOR. Fabritius. ENVIRONMENT DIVISION. CONFIGURATION SECTION. INPUT-OUTPUT SECTION. DATA DIVISION. FILE SECTION. WORKING-STORAGE SECTION. LINKAGE SECTION. PROCEDURE DIVISION. DISPLAY "Hello World". STOP RUN. COBOL
What Languages are they? % Hello World /Courier findfont 28 scalefont setfont 0 0 moveto (Hello World) show showpage Postscript
What Languages are they? /(?ms)"((?>[^\\"]+)?(?>\\.[^\\"]*)*)/ Regex – Regular Expression
What Languages are they? describe ArticlesController, " GET index" do it "should be successful" do get "index" response.should be_success end if "should set @articles" do Article.should_receive(:find).and_return(mock('articles')) get "index" assigns[:articles].should_not be_nil end RSpec on Ruby - Language of testing from TDD to BDD
What Languages are they? >gi|2501594|sp|Q57997|Y577_METJA PROTEIN MJ0577MSVMYKKILYPTDFSETAEIALKHVKAFKTLKAEEVILLHVIDEREIKKRDIFSLLLGVAGLNKSVEEFENELKNKLTEEAKNKMENIKKELEDVGFKVKDIIVVGIPHEEIVKIAEDEGVDIIIMGSHGKTNLKEILLGSVTENVIKKSNKPVLVVKRKNS BLAST query
What Languages are they? aspect VisitAspect { void Point.acceptVisitor(Visitor v) { v.visit(this); } } AspectJ
What Languages are they? <!--ADVISOR--> <bean id="theCuckoosEggAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="theReplacementFeaturePart1Advice"/> </property> <property name="pattern"> <value>IBusinessLogic.*</value> </property> </bean> <!--ADVICE--> <bean id="theReplacementFeaturePart1Advice" class="CuckoosEgg"/> SpringAOP
What Languages are they? <html> <head> <title>Hello World</title> </head> <body> Hello World </body> </html> Of course – HTML!
What is DSL? • No standard definition • Specific vs General depends on point of view • VB vs VBA • It helps revealing intentions • Language that domain experts understand • It becomes more and more popular because of tooling support • You can apply DSLs to solve your problems • You can even create your own DSL!
DSL – Telecom App Framework Adhearsion (open source telecom application framework) + Asterisk (open source telephony platform)
LEL – Layout Expression Language layout = " [ label_1 | label3 ] [ (300,300)*text1 | (150)people ] [ <label2 | _ ] [ message | buttons ] " Source: http://pphetra.blogspot.com/2007/07/lel.html
When to justify DSL • Repeated need of solutions to particular problems • You have a good understanding of problem domain • There is a ready-to-use DSL in that domain • Or if you have to invent one… • What should it look like?
Example - Coffee Order DSL • 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 example • Depends on APIs • Code does not match the way the domain is described • Still difficult to read and verify
DSL style • Reveal intention better • Take advantage of Ruby features • Untyped declaration • No parenthesis parameter • Metaprogramming support i.e. create class methods on the fly etc.
Coffee Order DSL in Java API approach CoffeeOrder order = new Latte(VENTI, HALF_CAF, NONFAT_MILK); CoffeeOrder coffee = order.prepare(false); DSL approach CoffeeOrder order = new Latte().size(VENTI).caffeine(HALF).milk(NONFAT).foam(FALSE); CoffeeOrder coffee = order.prepare();
How about other representations? • XML <Order type=“Latte”> <caffeine>half</caffeine> <milk>nonfat</milk> <foam>false</form> <whip>false</whip> </Order> • English “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 DSL • Internal DSL • Use an existing “host” programming language to describe DSL • Leverage host compiler/interpreter • Some programming languages are better for describing DSL e.g. dynamic languages; some use metaprogramming / annotations • External DSL • Define your own format of the language • Use XML along with XSD or DTD • Must create your own parser/interpreter • Or use DSL tools
External DSL • Business Natural Language (BNL) “exclude offer 30 percent cli if d score is less than or equal to 2” • Custom DSL show us-state field when country select box is "United States" show province field when country select box is "Canada" show brutus when us-state select box is "Ohio" or "Michigan"
DSL Design • DSL is not just API show us-state field when country select box is "United States" show province field when country select box is "Canada" show brutus when us-state select box is "Ohio" or "Michigan" JavaScript Solution #1 create_dynamic_field("show", "us-state-field", "country", "United States"); create_dynamic_field("show", "province-field", "country", "Canada"); create_dynamic_field("show", "brutus", "state", ["Ohio", "Michigan"]); JavaScript Solution #2 show("us-state-field").when("country").is("United States"); show("province-field").when("country").is("Canada"); show("brutus").when("us-state").is("Ohio, Michigan");
DSL Tools • Martin Fowler coined the term “Language Workbench” • Provide GUI frontend, metamodeling and/or code generation to define and use DSL • Microsoft DSL tool for Visual Studio • Intentional Software • Eclipse EMF • JetBrains Meta Programing System
MS DSL Tool for VS Studio 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+ http://www.metacase.com/fs.asp?vasen=vasen.html&paa=cases/dsm_examples.html
Summary • DSL is one way to increase a level of abstraction • DSL developers define syntax and semantics to express solutions closer to the domain problem • Using DSL tools • Creating parser/interpreter • Embedding to host languages • DSL users can be either programmers or domain experts
Summary • Types of DSL • Internal DSL • People start to use dynamic languages to create DSL • Ruby on Rails/Grails, Rake (Make on Ruby) • External DSL • Design is important • Consult with domain experts
Further Information • Wikipedia’s DSL http://en.wikipedia.org/wiki/Domain-specific_programming_language • Martin Fowler’s Bliki on DSL http://martinfowler.com/bliki/dsl.html • Language Workbenches: The Killer-App for Domain Specific Languages? http://martinfowler.com/articles/languageWorkbench.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 Programming: The Next Programming Paradigm (www.onboard.jetbrains.com/is1/articles/04/10/lop/mps.pdf)
Q&A Thai’s resources Narisa.com’s Design & Analysis forum ThaiDev.org