How to hide your Java with a DSL using JRuby

Last time I showed some Java code for building a simple tree. Then I showed a DSL for representing that tree. The point was basically to make you want to create such a DSL. This time I am going to show you how to make the DSL actually do something. We are going to write all the code that will allow that DSL to build a tree. If you've never seen it, or don't remember, please look back at the code from the last post. It's important to see the java code we started with and the DSL that we are going to implement. It's kind of like those before and after pictures you're always looking at. Seriously, come back when you are done, and this will make more sense.

It's not odd to start with the DSL. In fact I usually start with a language that would be ideal and then modify it just enough so that it is valid Ruby syntax. It is important to understand the language features and know how much you can get away with as far as syntax. As you begin implementing it, your language will probably change. In the end you hopefully end up with a concise and powerful abstraction.

Ruby has a lot of features that make it ideal for internal DSL's. Literal Arrays and Hashes are simple and very useful here, even though we won't use them today. We are going to focus on blocks and method_missing. Note that all of these concepts are missing from Java.

We are going to decorate our Java TreeNode class with JRuby. That's right, we are going to slap behavior implemented in Ruby directly on our Java class. And we are going to make it do things that can't even be done in Java. But first I want to introduce the IRB.

The IRB is a command line that you can execute Ruby in. With JRuby you get a JIRB, which is the same thing except you can execute Java there too. Sweet, interactive Java! As a Java developer, it is worth learning about JRuby just for this new ability. It's a more efficient way to explore how Java classes work than implementing main methods all over the place. I have also prototyped new behavior on my Java objects very effectively through the JIRB. The following examples are what I typed into the JIRB to test my ideas for the implementation of the DSL. If something doesn't work out, you just keep typing. The JIRB lets you try something and see the results very quickly. I have never felt so close to my Java code.


This first piece of new behavior we added to TreeNode is a good example. The new line character is different in Java and Ruby so we switch them out in the toString method. We had to require the jar for this class. Then we assign the qualified class name to a constant for convenience. After that adding a new method to our Java class is done as if it were Ruby. We could call toString, but JRuby allows us to call the Java methods with Ruby style so we call to_string instead. Note that is is completely unnecessary for the DSL. I only did it because I already had a toString that wrote out in the DSL format, and I wanted it to look right in the JIRB. The next example is where the meat is.


This code does most of the work for us. Method_missing is a special method in Ruby. A call to method_missing is what happens if you call a method that doesn't explicitly exist. Java wouldn't even compile if you did something like this. In this case we find or create a TreeNode with the name of the method. This lets us represent a path in our tree like this: shape.ellipse.circle

As opposed to this in Java.


So this is 1 line of our DSL rather than 22 lines of Java. Even if your developers don't know Ruby it should be obvious that they are going to have an easier time writing this immediately.

This path stuff is cool, but it's not what I promised. Since our data structure is a tree, we want to express multiple children for each TreeNode. To do this we are going to use blocks. Blocks are a way to pass executable code into a method. A good example of using a block is the call to the find method in the first line of our method_missing way up there. For each child in the Collection returned by getChildren() on this TreeNode we are checking to see if it has the same name as the method name that invoked method_missing.

Blocks will also allow us to describe a tree structure. The way this works is that method_missing gets called recursively. Each time it will find or create the named TreeNode, and then it allows that TreeNode to evaluate the remaining structure. All methods in Ruby can be passed a block. Blocks are placed after a method call and fit within '{...}' or 'do...end.' I chose the brackets because I liked the way it made the language look, but either would actually work. Nesting the blocks allows for a natural tree structure. The block can be accessed in the method with an extra parameter. In this case &block. Don't get too worried about the '&' just yet, it is the concepts that are important. The second to the last line deals with constructing the children. If a block which represents substructure of the tree is given, it is evaluated on our child. The way that we recursively traverse the described children is the most complicated and important part of the whole thing. Now we can build a tree described like this.


For readability it is nice to leave off the parens on the methods called. Everything that is a word in this DSL is actually a method call that invokes method missing, which creates a TreeNode that is added as a child. There is a bit of a chicken and egg problem with this example though. Once we have a TreeNode we are rolling, but how do we get our hands on the root TreeNode, in this case "shape?" Here is a way to start the whole thing off that is consistent with our DSL.


To make this work we just have to implement Tree to create our first TreeNode, and then hand the rest of the hierarchy off to it.


One of my first Ruby projects was a DSL, and it was not a bad way to learn the language. I have taken these concepts further in production code by describing a complicated meta model with frames and slots, effective dating, and internal rules. The ideas presented here scaled well to the more complicated project. The performance was also acceptable. There are a few problems that I neglected to mention because their solutions are straight forward. One of the more noteworthy is the possibility that you would have to encode and decode the names of TreeNodes into valid method names if they weren't already. This is one of the costs of using an internal DSL. That's fine with me because it is much easier than writing a parser. In the next entry I would like to show how this DSL can be used for more than just building the tree.

40 comments:

kimjhon said...

Amazing article! I was confused about Java Training in Chennai , but now got a clear view of the definition. Appreciate your hard work!
Java Course in Chennai
Best Java Training in Chennai

Unknown said...

Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!


Java Training in Chennai


Java Training in Bangalore


Java Training in Bangalore

Mageshkumar said...

This was very informative for me. Please continue this awesome work. Thank you.
Best Java Training in Chennai

Anuradha Singhaniya said...

Excellent and very cool idea and the subject at the top of magnificence and I am happy to this post. Interesting post! I am very glad to read your informative blog...thanks a lot for your valuable sharing. I was reading through some of your content on this article and I conceive this blog is really instructive! If you are looking for in depth Java certification training delhi

samala swathi said...

This is very blog ,and it's very usefull for developers
Java Online Training

Unknown said...

Thanks for your information. such a useful information.

Java training in Bangalore

Unknown said...

Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post.
Java Training in Chennai | Java Training Institute in Chennai

Dipanwita said...

I loved your blog. Never came across anything so interesting. java training in chennai

prabha said...

Thanks a lot for sharing us about this update. Hope you will not get tired on making posts as informative as this. 
angularjs Training in marathahalli

angularjs interview questions and answers

angularjs Training in bangalore

angularjs Training in bangalore

angularjs Training in chennai

automation anywhere online Training

simbu said...

I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
Java training in Bangalore | Java training in Btm layout

Java training in Bangalore |Java training in Rajaji nagar

Java training in Bangalore | Java training in Kalyan nagar

gowsalya said...

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
Python training in bangalore
Python course in pune
Python training in bangalore

Unknown said...

I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
Data Science Training in Chennai | Best Data science Training in Chennai
Data Science training in anna nagar | Data science training in Chennai
Data Science training in chennai | Best Data Science training in chennai
Data science training in Bangalore | Data Science training institute in Bangalore
Data Science training in marathahalli | Data Science training in Bangalore
Data Science interview questions and answers

gowsalya said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Best Devops Training in pune
excel advanced excel training in bangalore
Devops Training in Chennai

Dharani M said...

Great blog
java training in Marathahalli

spring training in Marathahalli

java training institute in Marathahalli

spring and hibernate training in Marathahalli

Dharani M said...

Nice blog


java training in Marathahalli

spring training in Marathahalli

java training institute in Marathahalli

spring and hibernate training in Marathahalli

mounika said...

Nice post..

DOT NET training in btm

dot net training institute in btm

dot net course in btm

best dot net training institute in btm

Unknown said...

After reading your post I understood that last week was with full of surprises and happiness for you. Congratz! Even though the website is work related, you can update small events in your life and share your happiness with us too.
Selenium training in Chennai

Selenium training in Bangalore

sathish said...

I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!


Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

Anonymous said...

Great Post,really it was very helpful for us.
Thanks a lot for sharing!
I found this blog to be very useful!!
Web Design and Development training in Bangalore

Unknown said...

Lalu hal lain yang tak boleh anda lupakan adalah tentang bagaimana anda bisa meningkatkan skill anda dalam bermain
asikqq
http://dewaqqq.club/
http://sumoqq.today/
interqq
pionpoker
bandar ceme
freebet tanpa deposit
paito warna terlengkap
syair sgp

kavitha said...

Nice Information..

how to hack flipkart
tp link wifi password hack
power bi developer resume
android secret codes and hacks pdf
slideshow html code for website
javascript max integer
tell me about yourself
given signs signify something and on that basis
kumaran systems interview pattern
bangalore traffic essay

saishree said...

awesome.
Acceptance is to offer what a lighted
A reduction of 20 in the price of salt
Power bi resumes
Qdxm:sfyn::uioz:?
If 10^0.3010 = 2, then find the value of log0.125 (125) ?
A dishonest dealer professes to sell his goods at cost price

but still gets 20% profit by using a false weight. what weight does he substitute for a kilogram?

Oops concepts in c# pdf
Resume for bca freshers
Attempt by security transparent method
'webmatrix.webdata.preapplicationstartcode.start()' to access security critical method 'system.web.webpages.razor.webpagerazorhost.addglobalimport(system.string)' failed.

Node js foreach loop

sanashree said...

nice blog.
Internship for mba
Internships in chennai for cse students
Robotics training
Ccna certification in chennai
Industrial training for diploma ece students in hyderabad
Internship certificate for bba student
Internships in bangalore for ece
Internship
Inplant training report
Internship in coimbatore for eee

sanashree said...

great blog.
Complaint letter to bank for deduction
Cisco aci interview questions
Type 2 coordination chart l&t
Mccb selection formula
Given signs signify something and on that basis assume the given statement
Adder and subtractor using op amp theory
Power bi resume for 3 years experience
Power bi resume for experience
Php developer resume for 2 year experience
Ayfy cable



Softgen Infotech said...

Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective...

Looking for Best Training Institute in Bangalore , India. Softgen Infotech is the best one to offers 85+ computer training courses including IT Software Course in Bangalore , India. Also, it provides placement assistance service in Bangalore for IT.

Realtime Experts said...

thank you so much for this nice information Article, Digitahanks for sharing your post with us.web designing training in bangalore

Realtime Experts said...

Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.student review for Realtime Experts marathahalli bangalore

raju said...

nice ..!!
poland web hosting
russian federation web hosting
slovakia web hosting
spain web hosting
suriname
syria web hosting
united kingdom
united kingdom shared web hosting
zambia web hosting
inplant training in chennai













Rakesh Kumar said...

home tuition
home tuition
home tuition
home tuition
home tuition
home tuition
home tuition
home tuition
home tuition
home tuition

Rakesh Kumar said...

home tuition in Delhi
home tuition in Delhi
home tuition in Delhi
home tuition in Delhi
home tuition in Delhi
home tuition in Delhi
home tuition in Delhi

Rakesh Kumar said...

home tution
home tution
home tution
home tution
home tution

Arunvijay said...

great information...


Intern Ship In Chennai
Inplant Training In Chennai
Internship For CSE Students
Online Internships
Coronavirus Update
Internship For MBA Students
ITO Internship

Quadsel Systems said...

Pretty article! I found some useful information in your blog....

so here we provide,

We provide you with flexible services and complete hybrid network solutions. It can provide your organisation with exceptional data speeds, advanced external security protection, and high-resilience by leveraging the latest SD-WAN and networking technologies to monitor, manage and strengthening your organisation’s existing network devices.

https://www.quadsel.in/networking/>
https://twitter.com/quadsel/
https://www.linkedin.com/company/quadsel-systems-private-limited/
https://www.facebook.com/quadselsystems/

#quadsel #network #security #technologies #managedservices #Infrastructure #Networking #OnsiteResources #ServiceDeskSupport #StorageServices #WarrantyAMCServices #datacentersolutions #DataCenterBuild #EWaste #InfraConsolidation #DisasterRecovery #NetworkingServices #ImagingServices #MPS #Consulting #WANOptimisation #enduserservices

jenani said...

Great information. Finally, thanking the blogger to launch more further too.
Java Training in Chennai

Java Training in Velachery

Java Training inTambaram

Java Training in Porur

Java Training in Omr

Java Training in Annanagar


vanathi said...

i fully read your blog its very nice and usefull to me ,thank for this blog
Software Testing Training in Chennai

Software Testing Training in Velachery

Software Testing Training in Tambaram

Software Testing Training in Porur

Software Testing Training in Omr
Software Testing Training in Annanagar

subathara said...

Amazing article! I was confused about Java Training in Chennai , but now got a clear view of the definition.
Digital Marketing Training in Chennai

Digital Marketing Training in Velachery

Digital Marketing Training in Tambaram

Digital Marketing Training in Porur

Digital Marketing Training in Omr

Digital MarketingTraining in Annanagar

Gopinath N said...

I'm very much inspired when I've visited your blog. Your blog is really informative. Hope you will continue with new article.
aws training in chennai
aws course in chennai

vcube said...

I appreciate you sharing this fantastic article.

software testing institute in Hyderabad

Hema Yadav said...

Thanks for sharing valuable piece of content on Java. Consider enrolling in Online Java Course in Pune, Kolkata, Alwar, Noida, Delhi and in many more cities of India, to make your career in IT Sector.

iteducationcentre said...

Great Article.Thanks for the post.
Java training in Nagpur