Sonntag, 29. November 2009

Create a EMF Model Instance with JAVA internal DSL

Sometimes a Java internal DSL is engough for creating a model instance (ok Eclipse xText is a cool tool :-)) a good example for Java DSL is the JMock DSL for creating mock objects. Here in my Blog I will show a small example for a Java internal DSL to get a EMF model instance. Here is the EMF ECore model (meta model) for my running example:


A small example model to describe components of an application and which services this components provides, only example stuff not a good model anyway.

Here a model instance with plain EMF Java code:

ComponentFactory factory = ComponentFactory.eINSTANCE;
   
Interface orderServiceInterface = factory.createInterface();
orderServiceInterface.setName("dsl.OrderService");

Port orderServicePort = factory.createPort();
orderServicePort.setProvides(orderServiceInterface);
  
Component order = factory.createComponent();
order.setName("order");
order.getPorts().add(orderServicePort);
  
Interface loggerServiceInterface = factory.createInterface();
loggerServiceInterface.setName("dsl.LoggerService");
 
Port loggerServicePort = factory.createPort();
loggerServicePort.setProvides(loggerServiceInterface);
  
Component logger = factory.createComponent();
logger.setName("logger");
logger.getPorts().add(loggerServicePort);
 
Application shop = factory.createApplication();
shop.setName("shop");
shop.getComponents().add(order);
shop.getComponents().add(logger);

And here the same model instance created with a Java internal DSL (Gof Builder Pattern):

Application shop = ApplicationBuilder.create("shop", new Components() {{

 component("order", new Ports() {{

  provides( OrderService.class );

 }});
 
 component("logger", new Ports() {{
  
  provides( LoggerService.class );
  
 }});

}});


Links:
In one of my next posts I will show the results of a Eclipse xText generated text DSL for the example model.