SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
Exploiting JRuby: Building
Domain-Specific Languages
for the Java Virtual Machine™
Rob Harrop
VP
Interface21
http://www.interface21.com
TS-9294
                      2007 JavaOneSM Conference | Session TS-9294 |
Goal of This Talk
      What you will gain




      Learn how to create and exploit
      domain-specific languages for the
      Java Virtual Machine™ (JVM™) using JRuby.




The terms “Java Virtual Machine” and “JVM” mean a Virtual Machine for the Java™ platform.
                                                   2007 JavaOneSM Conference | Session TS-9294 |   2
Agenda
Introduction to DSLs
DSLs in Action
Techniques for DSLs in JRuby
Decomposing DSLs
Elements of DSL Style
Summary
Q&A

                2007 JavaOneSM Conference | Session TS-9294 |   3
Agenda
Introduction to DSLs
DSLs in Action
Techniques for DSLs in JRuby
Decomposing DSLs
Elements of DSL Style
Summary
Q&A

                2007 JavaOneSM Conference | Session TS-9294 |   4
Introduction to DSLs
What is a DSL?
• Domain-specific language
   • Custom language designed for a specific purpose
   • Not just about business-specific domains
• General purpose examples
   • Build language (rake)
   • Application management (MScript—coming soon!)
• Business examples
   • Derivative calculation
   • Corporate action entitlement calculation


                       2007 JavaOneSM Conference | Session TS-9294 |   5
Introduction to DSLs
Why bother?
• Greatly simplify repetitive tasks
   • Java Management Extensions (JMX™)?
• Encapsulate boilerplate code
• Provide an API that expresses the intent
  of the code clearly
• Invest a little up front to save a lot in the future

• DSLs should deliver clear value


                      2007 JavaOneSM Conference | Session TS-9294 |   6
Introduction to DSLs
Use cases for DSLs
• Simplify/encapsulate some API
   • JMX API, for example
• Abstract complex business problems
   • Corporate action processing
• Support operational control of an application
   • Script maintenance tasks




                      2007 JavaOneSM Conference | Session TS-9294 |   7
Introduction to DSLs
Classifying DSLs
• Type
   • External—XML
   • Internal—JRuby, Groovy
• Target
   • General purpose—MScript
   • Business-specific—CAScript




                     2007 JavaOneSM Conference | Session TS-9294 |   8
Agenda
Introduction to DSLs
DSLs in Action
Techniques for DSLs in JRuby
Decomposing DSLs
Elements of DSL Style
Summary
Q&A

                2007 JavaOneSM Conference | Session TS-9294 |   9
DEMO
DSLs in Action—Rake and MScript




                  2007 JavaOneSM Conference | Session TS-9294 |   10
Agenda
Introduction to DSLs
DSLs in Action
Techniques for DSLs in JRuby
Decomposing DSLs
Elements of DSL Style
Summary
Q&A

               2007 JavaOneSM Conference | Session TS-9294 |   11
Techniques for DSLs in JRuby
Operator overloading
• Operator overloading allows natural syntax
  for common functions
   • Concatenation, addition
   • Attribute/index access


Example from MScript:
puts mbean[:SomeAttribute]
mbean[:SomeAttribute] = quot;Hello World!quot;


                       2007 JavaOneSM Conference | Session TS-9294 |   12
Techniques for DSLs in JRuby
Hashes and symbols
• Symbols are a great way to identify objects
  in your DSL
   • Tasks in rake
• Hashes are great for expressing relationships
   • Task dependency in rake

task :run-application => :setup




                     2007 JavaOneSM Conference | Session TS-9294 |   13
Techniques for DSLs in JRuby
Blocks
• Encapsulate executable logic
   • Paired with symbols to identify this logic
• Task actions in rake

task :run-application => :setup do
  ruby quot;application.rbquot;
end



                        2007 JavaOneSM Conference | Session TS-9294 |   14
Techniques for DSLs in JRuby
Method missing
• Great advantage over Java platform
• No need to know all operations in advance—
  but get natural call syntax
   • JMX technology operations on a MBean proxy
     in MScript




                     2007 JavaOneSM Conference | Session TS-9294 |   15
Techniques for DSLs in JRuby
Dynamic type extension
• Another brilliant advantage over Java platform
• Dynamically add methods to classes and objects
• Attribute access in MScript




                     2007 JavaOneSM Conference | Session TS-9294 |   16
Techniques for DSLs in JRuby
Java technology integration
• Manipulate Java platform domain logic
  using JRuby
• Take full advantage of existing investment
• Add in DSLs where they make sense
• Get the best of both worlds




                       2007 JavaOneSM Conference | Session TS-9294 |   17
Techniques for DSLs in JRuby
Options for Java technology integration
• Access Java platform from Ruby using JRuby
   • include_class etc
• Access Ruby from Java platform using JRuby API
   • This is reasonably complex
• Bi-directional access
   • Critical for DSLs that aim to simplify Java
     technology usage
• Spring integration
   • Create beans using JRuby


                         2007 JavaOneSM Conference | Session TS-9294 |   18
DEMO
Techniques for DSLs in JRuby




                   2007 JavaOneSM Conference | Session TS-9294 |   19
Agenda
Introduction to DSLs
DSLs in Action
Techniques for DSLs in JRuby
Decomposing DSLs
Elements of DSL Style
Summary
Q&A

                2007 JavaOneSM Conference | Session TS-9294 |   20
Decomposing DSLs
• All DSLs are composed of these common
  building blocks
• As expected a higher level of abstraction
  is provided so as not to appear like basic
  method calls
• Let’s dive into the code and look at these
  techniques in action




                    2007 JavaOneSM Conference | Session TS-9294 |   21
DEMO
Decomposing DSLs




                   2007 JavaOneSM Conference | Session TS-9294 |   22
Agenda
Introduction to DSLs
DSLs in Action
Techniques for DSLs in JRuby
Decomposing DSLs
Elements of DSL Style
Summary
Q&A

                2007 JavaOneSM Conference | Session TS-9294 |   23
Elements of DSL Style
Identify the problem, express the solution
• DSLs are not simply Java code expressed
  in a dynamic language
• Identify the problem and create a syntax that
  clearly expresses the solution

task :run_application do
 ruby “application.rb”
end


                        2007 JavaOneSM Conference | Session TS-9294 |   24
Elements of DSL Style
Metadata and behaviour
• DSL expressions typically contain metadata
  and behaviour as part of a single expression

corporate_action :bonus_issue do |position, payout|
  entitlement :stock =>
      position.quantity * payout.rate
end


Don’t let DSLs become another configuration file

                                2007 JavaOneSM Conference | Session TS-9294 |   25
Elements of DSL Style
Thinking the Ruby way
• Use type extension in favour of if/else
• Use blocks rather than anonymous types
• Use methods on objects rather than statics




                        2007 JavaOneSM Conference | Session TS-9294 |   26
Elements of DSL Style
Characteristics of a DSL
• Limited in scope
   • Address a small part of your domain
• Limited in functionality
   • Only the features needed to address that part




                       2007 JavaOneSM Conference | Session TS-9294 |   27
For More Information
• Books
  • Programming Ruby (Thomas, Fowler and Hunt)
  • The Ruby Way (Fulton)
• Web
  • http://jruby.codehaus.org
  • http://blog.interface21.com




                      2007 JavaOneSM Conference | Session TS-9294 |   28
Q&A


      2007 JavaOneSM Conference | Session TS-9294 |   29
Exploiting JRuby: Building
Domain-Specific Languages
for the Java Virtual Machine™
Rob Harrop
VP
Interface21
http://www.interface21.com
TS-9294
                      2007 JavaOneSM Conference | Session TS-9294 |

Contenu connexe

En vedette (6)

Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)
 
The elephant in the room mongo db + hadoop
The elephant in the room  mongo db + hadoopThe elephant in the room  mongo db + hadoop
The elephant in the room mongo db + hadoop
 
Trabalho
Trabalho Trabalho
Trabalho
 
MongoDB Advanced Topics
MongoDB Advanced TopicsMongoDB Advanced Topics
MongoDB Advanced Topics
 
Bitter Java, Sweeten with JRuby
Bitter Java, Sweeten with JRubyBitter Java, Sweeten with JRuby
Bitter Java, Sweeten with JRuby
 
Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)
 

Similaire à Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Machine

Agile DSL Development in Ruby
Agile DSL Development in RubyAgile DSL Development in Ruby
Agile DSL Development in Ruby
elliando dias
 
Writing Your Own JSR-Compliant, Domain-Specific Scripting Language
Writing Your Own JSR-Compliant, Domain-Specific Scripting LanguageWriting Your Own JSR-Compliant, Domain-Specific Scripting Language
Writing Your Own JSR-Compliant, Domain-Specific Scripting Language
elliando dias
 
Nuxeo JavaOne 2007 presentation (in original format)
Nuxeo JavaOne 2007 presentation (in original format)Nuxeo JavaOne 2007 presentation (in original format)
Nuxeo JavaOne 2007 presentation (in original format)
Stefane Fermigier
 
Building DSLs On CLR and DLR (Microsoft.NET)
Building DSLs On CLR and DLR (Microsoft.NET)Building DSLs On CLR and DLR (Microsoft.NET)
Building DSLs On CLR and DLR (Microsoft.NET)
Vitaly Baum
 
Stress Your Web App Before It Stresses You: Tools and Techniques for Extreme ...
Stress Your Web App Before It Stresses You: Tools and Techniques for Extreme ...Stress Your Web App Before It Stresses You: Tools and Techniques for Extreme ...
Stress Your Web App Before It Stresses You: Tools and Techniques for Extreme ...
elliando dias
 
Venkat Subramaniam Building DSLs In Groovy
Venkat Subramaniam Building DSLs In GroovyVenkat Subramaniam Building DSLs In Groovy
Venkat Subramaniam Building DSLs In Groovy
deimos
 

Similaire à Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Machine (20)

Agile DSL Development in Ruby
Agile DSL Development in RubyAgile DSL Development in Ruby
Agile DSL Development in Ruby
 
Real World Technologies
Real World TechnologiesReal World Technologies
Real World Technologies
 
Blue Ruby SDN Webinar
Blue Ruby SDN WebinarBlue Ruby SDN Webinar
Blue Ruby SDN Webinar
 
Writing Your Own JSR-Compliant, Domain-Specific Scripting Language
Writing Your Own JSR-Compliant, Domain-Specific Scripting LanguageWriting Your Own JSR-Compliant, Domain-Specific Scripting Language
Writing Your Own JSR-Compliant, Domain-Specific Scripting Language
 
Nuxeo JavaOne 2007 presentation (in original format)
Nuxeo JavaOne 2007 presentation (in original format)Nuxeo JavaOne 2007 presentation (in original format)
Nuxeo JavaOne 2007 presentation (in original format)
 
COP_RoR_QuArrk_Session_Oct_2022.pptx
COP_RoR_QuArrk_Session_Oct_2022.pptxCOP_RoR_QuArrk_Session_Oct_2022.pptx
COP_RoR_QuArrk_Session_Oct_2022.pptx
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
 
CS6270 Virtual Machines - Retargetable Binary Translators
CS6270 Virtual Machines - Retargetable Binary TranslatorsCS6270 Virtual Machines - Retargetable Binary Translators
CS6270 Virtual Machines - Retargetable Binary Translators
 
Jaoo Michael Neale 09
Jaoo Michael Neale 09Jaoo Michael Neale 09
Jaoo Michael Neale 09
 
ivanova-samba_backend.pdf
ivanova-samba_backend.pdfivanova-samba_backend.pdf
ivanova-samba_backend.pdf
 
Sap java
Sap javaSap java
Sap java
 
DSL Construction with Ruby - ThoughtWorks Masterclass Series 2009
DSL Construction with Ruby - ThoughtWorks Masterclass Series 2009DSL Construction with Ruby - ThoughtWorks Masterclass Series 2009
DSL Construction with Ruby - ThoughtWorks Masterclass Series 2009
 
Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...
Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...
Greg Maxey - Electric Cloud - Process as Code: An Introduction to the Electri...
 
Building DSLs On CLR and DLR (Microsoft.NET)
Building DSLs On CLR and DLR (Microsoft.NET)Building DSLs On CLR and DLR (Microsoft.NET)
Building DSLs On CLR and DLR (Microsoft.NET)
 
Stress Your Web App Before It Stresses You: Tools and Techniques for Extreme ...
Stress Your Web App Before It Stresses You: Tools and Techniques for Extreme ...Stress Your Web App Before It Stresses You: Tools and Techniques for Extreme ...
Stress Your Web App Before It Stresses You: Tools and Techniques for Extreme ...
 
Venkat Subramaniam Building DSLs In Groovy
Venkat Subramaniam Building DSLs In GroovyVenkat Subramaniam Building DSLs In Groovy
Venkat Subramaniam Building DSLs In Groovy
 
Domain Driven Design Development Spring Portfolio
Domain Driven Design Development Spring PortfolioDomain Driven Design Development Spring Portfolio
Domain Driven Design Development Spring Portfolio
 
NoSQL and ACID
NoSQL and ACIDNoSQL and ACID
NoSQL and ACID
 
OpenStack Dragonflow shenzhen and Hangzhou meetups
OpenStack Dragonflow shenzhen and Hangzhou  meetupsOpenStack Dragonflow shenzhen and Hangzhou  meetups
OpenStack Dragonflow shenzhen and Hangzhou meetups
 
Resume gunasundari dba
Resume gunasundari dbaResume gunasundari dba
Resume gunasundari dba
 

Plus de elliando dias

Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
elliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
elliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
elliando dias
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
elliando dias
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
elliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
elliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
elliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
elliando dias
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
elliando dias
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
elliando dias
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
elliando dias
 

Plus de elliando dias (20)

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
 
Geometria Projetiva
Geometria ProjetivaGeometria Projetiva
Geometria Projetiva
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
 
Ragel talk
Ragel talkRagel talk
Ragel talk
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
 
Minicurso arduino
Minicurso arduinoMinicurso arduino
Minicurso arduino
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
 
Rango
RangoRango
Rango
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makes
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
 

Dernier

Dernier (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Machine

  • 1. Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Machine™ Rob Harrop VP Interface21 http://www.interface21.com TS-9294 2007 JavaOneSM Conference | Session TS-9294 |
  • 2. Goal of This Talk What you will gain Learn how to create and exploit domain-specific languages for the Java Virtual Machine™ (JVM™) using JRuby. The terms “Java Virtual Machine” and “JVM” mean a Virtual Machine for the Java™ platform. 2007 JavaOneSM Conference | Session TS-9294 | 2
  • 3. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 3
  • 4. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 4
  • 5. Introduction to DSLs What is a DSL? • Domain-specific language • Custom language designed for a specific purpose • Not just about business-specific domains • General purpose examples • Build language (rake) • Application management (MScript—coming soon!) • Business examples • Derivative calculation • Corporate action entitlement calculation 2007 JavaOneSM Conference | Session TS-9294 | 5
  • 6. Introduction to DSLs Why bother? • Greatly simplify repetitive tasks • Java Management Extensions (JMX™)? • Encapsulate boilerplate code • Provide an API that expresses the intent of the code clearly • Invest a little up front to save a lot in the future • DSLs should deliver clear value 2007 JavaOneSM Conference | Session TS-9294 | 6
  • 7. Introduction to DSLs Use cases for DSLs • Simplify/encapsulate some API • JMX API, for example • Abstract complex business problems • Corporate action processing • Support operational control of an application • Script maintenance tasks 2007 JavaOneSM Conference | Session TS-9294 | 7
  • 8. Introduction to DSLs Classifying DSLs • Type • External—XML • Internal—JRuby, Groovy • Target • General purpose—MScript • Business-specific—CAScript 2007 JavaOneSM Conference | Session TS-9294 | 8
  • 9. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 9
  • 10. DEMO DSLs in Action—Rake and MScript 2007 JavaOneSM Conference | Session TS-9294 | 10
  • 11. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 11
  • 12. Techniques for DSLs in JRuby Operator overloading • Operator overloading allows natural syntax for common functions • Concatenation, addition • Attribute/index access Example from MScript: puts mbean[:SomeAttribute] mbean[:SomeAttribute] = quot;Hello World!quot; 2007 JavaOneSM Conference | Session TS-9294 | 12
  • 13. Techniques for DSLs in JRuby Hashes and symbols • Symbols are a great way to identify objects in your DSL • Tasks in rake • Hashes are great for expressing relationships • Task dependency in rake task :run-application => :setup 2007 JavaOneSM Conference | Session TS-9294 | 13
  • 14. Techniques for DSLs in JRuby Blocks • Encapsulate executable logic • Paired with symbols to identify this logic • Task actions in rake task :run-application => :setup do ruby quot;application.rbquot; end 2007 JavaOneSM Conference | Session TS-9294 | 14
  • 15. Techniques for DSLs in JRuby Method missing • Great advantage over Java platform • No need to know all operations in advance— but get natural call syntax • JMX technology operations on a MBean proxy in MScript 2007 JavaOneSM Conference | Session TS-9294 | 15
  • 16. Techniques for DSLs in JRuby Dynamic type extension • Another brilliant advantage over Java platform • Dynamically add methods to classes and objects • Attribute access in MScript 2007 JavaOneSM Conference | Session TS-9294 | 16
  • 17. Techniques for DSLs in JRuby Java technology integration • Manipulate Java platform domain logic using JRuby • Take full advantage of existing investment • Add in DSLs where they make sense • Get the best of both worlds 2007 JavaOneSM Conference | Session TS-9294 | 17
  • 18. Techniques for DSLs in JRuby Options for Java technology integration • Access Java platform from Ruby using JRuby • include_class etc • Access Ruby from Java platform using JRuby API • This is reasonably complex • Bi-directional access • Critical for DSLs that aim to simplify Java technology usage • Spring integration • Create beans using JRuby 2007 JavaOneSM Conference | Session TS-9294 | 18
  • 19. DEMO Techniques for DSLs in JRuby 2007 JavaOneSM Conference | Session TS-9294 | 19
  • 20. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 20
  • 21. Decomposing DSLs • All DSLs are composed of these common building blocks • As expected a higher level of abstraction is provided so as not to appear like basic method calls • Let’s dive into the code and look at these techniques in action 2007 JavaOneSM Conference | Session TS-9294 | 21
  • 22. DEMO Decomposing DSLs 2007 JavaOneSM Conference | Session TS-9294 | 22
  • 23. Agenda Introduction to DSLs DSLs in Action Techniques for DSLs in JRuby Decomposing DSLs Elements of DSL Style Summary Q&A 2007 JavaOneSM Conference | Session TS-9294 | 23
  • 24. Elements of DSL Style Identify the problem, express the solution • DSLs are not simply Java code expressed in a dynamic language • Identify the problem and create a syntax that clearly expresses the solution task :run_application do ruby “application.rb” end 2007 JavaOneSM Conference | Session TS-9294 | 24
  • 25. Elements of DSL Style Metadata and behaviour • DSL expressions typically contain metadata and behaviour as part of a single expression corporate_action :bonus_issue do |position, payout| entitlement :stock => position.quantity * payout.rate end Don’t let DSLs become another configuration file 2007 JavaOneSM Conference | Session TS-9294 | 25
  • 26. Elements of DSL Style Thinking the Ruby way • Use type extension in favour of if/else • Use blocks rather than anonymous types • Use methods on objects rather than statics 2007 JavaOneSM Conference | Session TS-9294 | 26
  • 27. Elements of DSL Style Characteristics of a DSL • Limited in scope • Address a small part of your domain • Limited in functionality • Only the features needed to address that part 2007 JavaOneSM Conference | Session TS-9294 | 27
  • 28. For More Information • Books • Programming Ruby (Thomas, Fowler and Hunt) • The Ruby Way (Fulton) • Web • http://jruby.codehaus.org • http://blog.interface21.com 2007 JavaOneSM Conference | Session TS-9294 | 28
  • 29. Q&A 2007 JavaOneSM Conference | Session TS-9294 | 29
  • 30. Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Machine™ Rob Harrop VP Interface21 http://www.interface21.com TS-9294 2007 JavaOneSM Conference | Session TS-9294 |