Monday, February 05, 2007

Eclipse Modeling Project and OCL

Anyone who believes that modeling at Eclipse is limited to EMF and possibly GMF, really should checkout the Eclipse (Top Level) Modeling Project (EMP). Ed Merks and Rich Gronback have done an excellent job of bringing together all of the Eclipse modeling work under one umbrella. A quick count reveals over 25 components to this project (including EMF and GMF).

Why should you use this technology? Of course if you are focusing on SWT extensions or device drivers this may not be the technology for you. But if you are writing an application (rich client or otherwise) that uses a data model, leveraging the EMP may save you a ton of time.

Recently (for part of my PhD) I needed a data model with a lot of data. I created a model (using EMF) for National Hockey League statistics. I created a class for Player, Team, PlayerYearlyStats, etc... The stats had attributes such as goals, assists, PIM (penalty minutes), etc... Using EMF I hit “Generate” and I had a fully working Java application that allowed me to create players, edit statistics and save all the data to an XML file (not bad for 10 minutes work). From here I populated the data with all the NHL stats from 1917 (no, I did not do this by hand!).

Next I decided to checkout a few other modeling components. In particular, I looked at the Object Constraint Language. OCL allows you to specify constraints on your Object Model in a platform (language) independent way. For example, I can say that if a player earned 200 points in a season, then the player must be Wayne Gretzky (self.points > 200 implies self.name = 'Wayne Gretzky'). While this record may never be broken, I would probably use OCL to specify more “predictable” constraints such as 2 players on the same team can't have the same jersey number, etc...
OCL can also be used to query your data. Christian Damus (the author of OCL for Eclipse) has provided great examples to help you get up and running. Once installed, the OCL examples will generate a simple interpreter plugin that extends the Eclipse Console. Using the OCL interpreter plugin you can write OCL queries. For example, I can write:

Player.allInstances()->select( p | p.yearlyStats.goals->sum() > 800)

To select all the players who goals each year sum up to a total greater than 800. If I change this to 700 goals I get 6 players. I will leave that result as an exercise for the reader

3 comments:

Chris Aniszczyk (zx) said...

cool beans, only if I was a full-time student again, and not a full-time worker posing as full-time student :)

I think the modeling facilities in Eclipse are incredible, I just wish I had more time to invest to learn some of the cool things out there.

jonny said...

I like fashion shows & fashion events in my country. I like it.

Sankar said...

very informative post.

sankar
java training in chennai