1 / 48

Domain Specific Language

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)

roden
Télécharger la présentation

Domain Specific Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Domain Specific Language Dr. Weerasak Witthawaskul “Mr. SweetCorn” (weerasak@hotmail.com) Narisa.com’s NJUG Meeting #3 22 July 2007

  2. 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

  3. Graduate Life

  4. 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

  5. What is DSL? “วันนี้มีประชุมนริสาดอทคอม” ภาษาไทย “ทำตัวติสตดีนักถูกแมวีนมายังทำตัวชิวชิวอยู่อีก...” ภาษาไทยเด็กแนว (domain specific)

  6. 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?

  7. 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

  8. What Languages are they? #include <stdio.h> main() { printf("Hello World\n"); } C

  9. 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)

  10. What Languages are they? <?php echo "Hello World\n"; ?> PHP

  11. What Languages are they? public class HelloWorld { public static void main(String[] args) System.out.println("Hello World"); } } Java How about JME? JEE?

  12. What Languages are they? PROGRAM HelloWorld; BEGIN WRITELN('Hello World'); END. PASCAL

  13. What Languages are they? print "Hello World" PRINT “Hello World” Basic, Python, Ruby, Perl

  14. What Languages are they? SELECT 'Hello World' SQL

  15. What Languages are they? BEGIN DBMS_OUTPUT.put_line('Hello World'); END; PL/SQL

  16. What Languages are they? Report Hello_World. Write: "Hello World". ABAP

  17. What Languages are they? io:fwrite("Hello World~n"). Erlang

  18. 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

  19. What Languages are they? % Hello World /Courier findfont 28 scalefont setfont 0 0 moveto (Hello World) show showpage Postscript

  20. What Languages are they? /(?ms)"((?>[^\\"]+)?(?>\\.[^\\"]*)*)/ Regex – Regular Expression

  21. 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

  22. What Languages are they? >gi|2501594|sp|Q57997|Y577_METJA PROTEIN MJ0577MSVMYKKILYPTDFSETAEIALKHVKAFKTLKAEEVILLHVIDEREIKKRDIFSLLLGVAGLNKSVEEFENELKNKLTEEAKNKMENIKKELEDVGFKVKDIIVVGIPHEEIVKIAEDEGVDIIIMGSHGKTNLKEILLGSVTENVIKKSNKPVLVVKRKNS BLAST query

  23. What Languages are they? aspect VisitAspect { void Point.acceptVisitor(Visitor v) { v.visit(this); } } AspectJ

  24. 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

  25. What Languages are they? <html> <head> <title>Hello World</title> </head> <body> Hello World </body> </html> Of course – HTML!

  26. 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!

  27. DSL Examples

  28. DSL – Telecom App Framework Adhearsion (open source telecom application framework) + Asterisk (open source telephony platform)

  29. 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

  30. 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?

  31. 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)

  32. Problems with the example • Depends on APIs • Code does not match the way the domain is described • Still difficult to read and verify

  33. 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.

  34. 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();

  35. 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”

  36. 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

  37. 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"

  38. 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");

  39. 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

  40. 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

  41. Metacase’s MetaEdit+ http://www.metacase.com/fs.asp?vasen=vasen.html&paa=cases/dsm_examples.html

  42. Jetbrains Meta Programming System (MPS)

  43. 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

  44. 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

  45. 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)

  46. Q&A Thai’s resources Narisa.com’s Design & Analysis forum ThaiDev.org

  47. Backup slides

More Related