Groovy DSL and User Interaction

  • Groovy DSL and User Interaction


    Corinne Krych

    Fabrice Matrat


    RivieraGUG

    meets with

    Riviera Scala Clojure

    to run this presentation: be full screen (F11)

    to submit the editor code to cloud foundry: ctrl S (command S on Mac)

    be careful using 1,2,3... it makes code snippet appears in the editor


  • Ladies first : Corinne




    @corinnekrych

    https://github.com/corinnekrych

    corinnekrych@gmail.com


  • Fabrice : my geek husband




    @fabricematrat

    https://github.com/fabricematrat

    fabricematrat@gmail.com


  • WHAT IS A DSL?



    Domain-specific language (noun)

    a computer programming language of limited expressiveness focused on a particular domain

    - Martin Fowler


  •         10000110 10000000 11000000 00000101 10000110 10000000 11000000 00000101 10000110 10000000
            11000000 00000101 10000110 10000000 11000000 00000101 00000000 00000000 00000000 00010100
            00000000 00000000 00000000 00010100 10000110 10000000 11000000 00000101 10000110 10000000
            10000110 10000000 11000000 00000101 10000110 10000000 11000000 00000101 10000110 10000000
            11000000 00000101 10000110 10000000 11000000 00000101 00000000 00000000 00000000 00010100
            00000000 00000000 00000000 00010100 10000110 10000000 11000000 00000101 10000110 10000000
            10000110 10000000 11000000 00000101 10000110 10000000 11000000 00000101 10000110 10000000
            11000000 00000101 10000110 10000000 11000000 00000101 00000000 00000000 00000000 00010100
            00000000 00000000 00000000 00010100 10000110 10000000 11000000 00000101 10000110 10000000
            10000110 10000000 11000000 00000101 10000110 10000000 11000000 00000101 10000110 10000000
            11000000 00000101 10000110 10000000 11000000 00000101 00000000 00000000 00000000 00010100
            00000000 00000000 00000000 00010100 10000110 10000000 11000000 00000101 10000110 10000000
            10000110 10000000 11000000 00000101 10000110 10000000 11000000 00000101 10000110 10000000
            11000000 00000101 10000110 10000000 11000000 00000101 00000000 00000000 00000000 00010100
            00000000 00000000 00000000 00010100 10000110 10000000 11000000 00000101 10000110 10000000
            10000110 10000000 11000000 00000101 10000110 10000000 11000000 00000101 10000110 10000000
            11000000 00000101 10000110 10000000 11000000 00000101 00000000 00000000 00000000 00010100
            00000000 00000000 00000000 00010100 10000110 10000000 11000000 00000101 10000110 10000000
                


  •     SELECT Bride
        FROM SingleWomen
        WHERE FatherInLaw = 'Millionaire'
        AND Count(Car) > 20
    
        SELECT Cash, Car, BankBalance
        FROM FatherInLaw
    
        UPDATE MyBankAccout
        SET MyBal = MyBal + FatherInLawBal
    
        INSERT INTO MyCarShed
        VALUES('BMW')
                        




  • RR U F B' RR F' B U RR

                        Terminology
                            F = front face
                            B = back face
                            R = right face
                            L = left face
                            U = up face
                            D = down face
                            A letter by itself means turn that face 90 degrees clockwise (eg. F).
                            A letter followed by an apostrophe means turn that face 90 degrees anti-clockwise (eg. F').
                        


  • Boundaries are blurry




    Mini Language


    drink beers at green king

    Scripting


    ['Groovy', 'Scala'].each{ talkAbout it }

    Fluent API


    diner.willBe 40.minutes.from.now



  • External vs Internal DSL




    External

    Internal

    class HelloSpock extends spock.lang.Specification {
      def "length of Spock's and his friends' names"() {
        expect:
        name.size() == length
    
        where:
        name     | length
        "Spock"  | 5
        "Kirk"   | 4
        "Scotty" | 6
      }
    }
                    

  • Why Groovy is the perfect embedded DSL companion?


  • Syntax sugar: let's play striptease!



  •                 


  • List made easy


                

  • Map made easy


                

  • MOP: let's get the dynamic groove


                

  • AST transform: Great power


      • hook into the compilation process

       to modify AST before it is turned into bytecode.

      • flexibility without runtime performance penalty

      • can be used:

        → either with standard annotation like:

            • @ThreadInterrupt and co.

            • @TypeChecked + extension: error handling in your DSL

            • @Scalify

        → or implement your own:

            • SecureASTCustomizer


  • 9 phases of AST transform



  • Scripting


      • native support for scripting:

         new GroovyShell().evaluate(content)

      • standard support JSR223:

         ScriptEngineManager factory = new ScriptEngineManager();

         ScriptEngine engine = factory.getEngineByName("groovy");

         engine.eval(content))


  •                 


  •                 



  • Command chaining




    Method chaining is a common technique for invoking multiple method calls in object-oriented programming languages. Each method returns an object (possibly the current object itself), allowing the calls to be chained together in a single statement.

    - wikipedia

  •                 



  • Back to command chaining





    ask "question" into var
    => ask(question).into(var)


    ask "question" assign to var
    => ask("question").assign(to).var


  •                 


  •                 


  •                 


  •                 


  •                 
                    
                    


  •                 




  • Like the presentation format?
    Curious about code?
    https://github.com/corinnekrych/DslPrez
    Want to play with it?
    http://dslprez.cloudfoundry.com
    Special Thanks
    Sebastien Blanc
    References
    Domain-Specific Languages
    Martin Fowler, with Rebecca Parsons
    Groovy for Domain-Specific Languages
    Fergal Dearle
    Groovy in Action
    Guillaume Laforge, Paul King, Jon Skeet
Previous
Groovy DSL and User Interaction
Next