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

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

Embed Size (px)

DESCRIPTION

 

Citation preview

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

2007 JavaOneSM Conference | Session TS-9294 |

TS-9294

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

Rob HarropVPInterface21http://www.interface21.com

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

2007 JavaOneSM Conference | Session TS-9294 | 2

Goal of This Talk

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

What you will gain

The terms “Java Virtual Machine” and “JVM” mean a Virtual Machine for the Java™ platform.

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

2007 JavaOneSM Conference | Session TS-9294 | 3

Agenda

Introduction to DSLsDSLs in ActionTechniques for DSLs in JRubyDecomposing DSLsElements of DSL StyleSummaryQ&A

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

2007 JavaOneSM Conference | Session TS-9294 | 4

Agenda

Introduction to DSLsDSLs in ActionTechniques for DSLs in JRubyDecomposing DSLsElements of DSL StyleSummaryQ&A

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

2007 JavaOneSM Conference | Session TS-9294 | 5

What is a DSL?Introduction to DSLs

• 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

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

2007 JavaOneSM Conference | Session TS-9294 | 6

Why bother?Introduction to DSLs

• 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

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

2007 JavaOneSM Conference | Session TS-9294 | 7

Use cases for DSLsIntroduction to 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

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

2007 JavaOneSM Conference | Session TS-9294 | 8

Classifying DSLsIntroduction to DSLs

• Type• External—XML• Internal—JRuby, Groovy

• Target• General purpose—MScript• Business-specific—CAScript

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

2007 JavaOneSM Conference | Session TS-9294 | 9

Agenda

Introduction to DSLsDSLs in ActionTechniques for DSLs in JRubyDecomposing DSLsElements of DSL StyleSummaryQ&A

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

102007 JavaOneSM Conference | Session TS-9294 |

DEMODSLs in Action—Rake and MScript

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

2007 JavaOneSM Conference | Session TS-9294 | 11

Agenda

Introduction to DSLsDSLs in ActionTechniques for DSLs in JRubyDecomposing DSLsElements of DSL StyleSummaryQ&A

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

2007 JavaOneSM Conference | Session TS-9294 | 12

Operator overloadingTechniques for DSLs in JRuby

• Operator overloading allows natural syntax for common functions• Concatenation, addition• Attribute/index access

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

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

2007 JavaOneSM Conference | Session TS-9294 | 13

Hashes and symbolsTechniques for DSLs in JRuby

• Symbols are a great way to identify objectsin your DSL• Tasks in rake

• Hashes are great for expressing relationships• Task dependency in rake

task :run-application => :setup

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

2007 JavaOneSM Conference | Session TS-9294 | 14

BlocksTechniques for DSLs in JRuby

• Encapsulate executable logic• Paired with symbols to identify this logic

• Task actions in rake

task :run-application => :setup do ruby "application.rb"

end

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

2007 JavaOneSM Conference | Session TS-9294 | 15

Method missingTechniques for DSLs in JRuby

• 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

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

2007 JavaOneSM Conference | Session TS-9294 | 16

Dynamic type extensionTechniques for DSLs in JRuby

• Another brilliant advantage over Java platform• Dynamically add methods to classes and objects• Attribute access in MScript

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

2007 JavaOneSM Conference | Session TS-9294 | 17

Java technology integrationTechniques for DSLs in JRuby

• Manipulate Java platform domain logicusing JRuby

• Take full advantage of existing investment• Add in DSLs where they make sense• Get the best of both worlds

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

2007 JavaOneSM Conference | Session TS-9294 | 18

Options for Java technology integrationTechniques for DSLs in JRuby

• 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

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

192007 JavaOneSM Conference | Session TS-9294 |

DEMOTechniques for DSLs in JRuby

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

2007 JavaOneSM Conference | Session TS-9294 | 20

Agenda

Introduction to DSLsDSLs in ActionTechniques for DSLs in JRubyDecomposing DSLsElements of DSL StyleSummaryQ&A

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

2007 JavaOneSM Conference | Session TS-9294 | 21

Decomposing DSLs

• All DSLs are composed of these commonbuilding blocks

• As expected a higher level of abstraction is provided so as not to appear like basicmethod calls

• Let’s dive into the code and look at these techniques in action

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

222007 JavaOneSM Conference | Session TS-9294 |

DEMODecomposing DSLs

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

2007 JavaOneSM Conference | Session TS-9294 | 23

Agenda

Introduction to DSLsDSLs in ActionTechniques for DSLs in JRubyDecomposing DSLsElements of DSL StyleSummaryQ&A

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

2007 JavaOneSM Conference | Session TS-9294 | 24

Identify the problem, express the solutionElements of DSL Style

• 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

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

2007 JavaOneSM Conference | Session TS-9294 | 25

Metadata and behaviourElements of DSL Style

• 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

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

2007 JavaOneSM Conference | Session TS-9294 | 26

Thinking the Ruby wayElements of DSL Style

• Use type extension in favour of if/else• Use blocks rather than anonymous types• Use methods on objects rather than statics

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

2007 JavaOneSM Conference | Session TS-9294 | 27

Characteristics of a DSLElements of DSL Style

• Limited in scope• Address a small part of your domain

• Limited in functionality• Only the features needed to address that part

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

2007 JavaOneSM Conference | Session TS-9294 | 28

For More Information

• Books• Programming Ruby (Thomas, Fowler and Hunt)• The Ruby Way (Fulton)

• Web• http://jruby.codehaus.org• http://blog.interface21.com

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

292007 JavaOneSM Conference | Session TS-9294 |

Q&A

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

2007 JavaOneSM Conference | Session TS-9294 |

TS-9294

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

Rob HarropVPInterface21http://www.interface21.com