Exploiting JRuby: Building
Domain-Specific Languages
for the Java Virtual Machine™
Rob Harrop
VP
Interface21
http://www.in...
Goal of This Talk
      What you will gain




      Learn how to create and exploit
      domain-specific languages for t...
Agenda
Introduction to DSLs
DSLs in Action
Techniques for DSLs in JRuby
Decomposing DSLs
Elements of DSL Style
Summary
Q&A...
Agenda
Introduction to DSLs
DSLs in Action
Techniques for DSLs in JRuby
Decomposing DSLs
Elements of DSL Style
Summary
Q&A...
Introduction to DSLs
What is a DSL?
• Domain-specific language
   • Custom language designed for a specific purpose
   • N...
Introduction to DSLs
Why bother?
• Greatly simplify repetitive tasks
   • Java Management Extensions (JMX™)?
• Encapsulate...
Introduction to DSLs
Use cases for DSLs
• Simplify/encapsulate some API
   • JMX API, for example
• Abstract complex busin...
Introduction to DSLs
Classifying DSLs
• Type
   • External—XML
   • Internal—JRuby, Groovy
• Target
   • General purpose—M...
Agenda
Introduction to DSLs
DSLs in Action
Techniques for DSLs in JRuby
Decomposing DSLs
Elements of DSL Style
Summary
Q&A...
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...
Techniques for DSLs in JRuby
Operator overloading
• Operator overloading allows natural syntax
  for common functions
   •...
Techniques for DSLs in JRuby
Hashes and symbols
• Symbols are a great way to identify objects
  in your DSL
   • Tasks in ...
Techniques for DSLs in JRuby
Blocks
• Encapsulate executable logic
   • Paired with symbols to identify this logic
• Task ...
Techniques for DSLs in JRuby
Method missing
• Great advantage over Java platform
• No need to know all operations in advan...
Techniques for DSLs in JRuby
Dynamic type extension
• Another brilliant advantage over Java platform
• Dynamically add met...
Techniques for DSLs in JRuby
Java technology integration
• Manipulate Java platform domain logic
  using JRuby
• Take full...
Techniques for DSLs in JRuby
Options for Java technology integration
• Access Java platform from Ruby using JRuby
   • inc...
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...
Decomposing DSLs
• All DSLs are composed of these common
  building blocks
• As expected a higher level of abstraction
  i...
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...
Elements of DSL Style
Identify the problem, express the solution
• DSLs are not simply Java code expressed
  in a dynamic ...
Elements of DSL Style
Metadata and behaviour
• DSL expressions typically contain metadata
  and behaviour as part of a sin...
Elements of DSL Style
Thinking the Ruby way
• Use type extension in favour of if/else
• Use blocks rather than anonymous t...
Elements of DSL Style
Characteristics of a DSL
• Limited in scope
   • Address a small part of your domain
• Limited in fu...
For More Information
• Books
  • Programming Ruby (Thomas, Fowler and Hunt)
  • The Ruby Way (Fulton)
• Web
  • http://jru...
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.in...
Upcoming SlideShare
Loading in...5
×

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

1,368

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,368
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

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

  1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 10. DEMO DSLs in Action—Rake and MScript 2007 JavaOneSM Conference | Session TS-9294 | 10
  11. 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. 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. 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. 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. 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. 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. 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. 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. 19. DEMO Techniques for DSLs in JRuby 2007 JavaOneSM Conference | Session TS-9294 | 19
  20. 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. 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. 22. DEMO Decomposing DSLs 2007 JavaOneSM Conference | Session TS-9294 | 22
  23. 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. 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. 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. 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. 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. 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. 29. Q&A 2007 JavaOneSM Conference | Session TS-9294 | 29
  30. 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 |
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×