InfoQ

News

Building Domain-Specific Languages in JRuby

Posted by Ian Roughley on May 21, 2007 10:19 AM

Community
Java,
Ruby
Topics
Domain Specific Languages
Tags
JRuby,
Java One
Closing out the Java One conference last week was Rob Harrop's presentation "Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Machine." Domain specific languages (DSLs) have been gaining popularity, as shown on InfoQ with a presentation on an introduction to domain specific languages by Martin Fowler and posts on the debates in the blogsphere, and provide a way to create a custom language for a specific programming or business purpose.

But why would you want to do this? There are many possible answers, some of these are: repetitive tasks can be simplified; boiler-plate code can be encapsulated; or an API can be better express the intent of the code. No matter what your particular reason, all DSLs should deliver a clear value to your project.

The approach explored in the presentation was to create a JRuby DSL that is utilized from within Java classes. As byte code generated from JRuby will run in the JRE, the DSL is categorized as "internal" - which uses a general programming language to create a specific programming language. The alternative is an "external" DSL that requires an external compiler and generator, making the integration much more difficult.

The benefit in this approach is in creating the DSL, and several JRuby language techniques were presented to make developing DSLs easier:

  • Operator overloading - overloading operator allows the DSL to provide a natural syntax
  • Hashes and symbols - using hashes and symbols make it easy to express relationships and identify objects
  • Blocks - blocks encapsulate executable logic, and allow the logic to be stored (in hashes) for later execution
  • Dynamic type extension - methods can be added dynamically to classes and objects
  • Method missing - when a method is called that doesn't exists it can be trapped, avoiding the need to know all the operations in advance
  • Integration - take advantage to existing Java code

To take full advantage of using JRuby in creating the DSL, tips on style were provided:

  • Ensure that you are not writing Java code in a dynamic language
  • Build up the DSL from common building blocks
  • Identify the problem and then create a syntax to express the solution
  • Provide metadata and behavior - don't let DSLs become configuration files
  • Think the ruby way: use type extensions; use blocks; use methods on objects
  • Keep the scope limited; address a small part of your domain and don't try to make the DSL a general programming language

For developers, DSLs are another tool that can be utilized. By investing a little time upfront, large time savings can be achieved.

1 comment

Reply

Groovy, an other option by Guillaume Laforge Posted May 21, 2007 4:29 PM
  1. Back to top

    Groovy, an other option

    May 21, 2007 4:29 PM by Guillaume Laforge

    Additionally, there's the option to use Groovy for embedding DSLs in your Java EE application. There's a short page on the Groovy site with a couple pointers to how to create Domain-Specific language in Groovy -- especially the tutorial from QCon 2007 which provides some explanations on the various tricks that Groovy offers.

Exclusive Content

Measuring Agile in the Enterprise: 5 Success Factors for Large-Scale Agile Adoption

Michael Mah analyzes the development process in 5 companies: 2 Agile (one of them BMC) and 3 classic. He presents the factors which contributed to the success of BMC's Agile adoption.

Tom Preston-Werner on Powerset, GitHub, Ruby and Erlang

In this interview filmed at RubyFringe 2008, Tom Preston-Werner talks about how both Powerset and GitHub use Ruby and Erlang, as well as tools like Fuzed, god, and more.

David Laribee on Alt.NET and its Mission

David Laribee discusses the purpose of ALT.NET, its mission and future.

Discover RailsKits and Stop Writing Redundant Code

Ruby on Rails has become a popular Ruby framework for creating web applications in recent years. An aspect of creating a web application is the need to repeatedly create the same base functionality.

A Formal Performance Tuning Methodology: Wait-Based Tuning

Steven Haines talks about tackling web application performance tuning by proposing a method called wait-based tuning.

Shaw and Fowler About Forging a New Alliance

Shaw and Fowler talk about the need for a new relationship between the business department and the IT department. Studies have shown that projects mostly fail due to miscommunication between the two.

How to GET a Cup of Coffee

In this article, Jim Webber, Savas Parastatidis and Ian Robinson show how to drive an application's flow through the use of hypermedia in a RESTful application.

Archaeopteryx: A Ruby MIDI Generator

Eccentric artist turned overnight anti-celebrity, Giles Bowkett captures the heart and soul of RubyFringe as he demonstrates his revolutionary Archaeopteryx MIDI drum pattern generator.