How to link your DSL with Java

Sometimes it is reasonable to re-use existing languages like Java and incorporate them into your domain-specific language. E.g. an interface of your domain-specific concept "component" might be best described with a standard Java interface. Don't reinvent the wheel, just link standard Java artifacts to your DSL.

Illustrating the linking feature

This feature can be instantly illustrated with a standard OOMEGA model repository - even without creating a DSL in the first place. OOMEGA Core contains a concept called URIRef that covers links to other resources like websites or e.g. Java files in the Eclipse workspace. Let's demonstrate how to create a link to a Java file in your model repository.

  • Switch to the OOMEGA perspective.
  • Create a model repository by selecting e.g. File > New > Text-based Repository Project. Choose the name Tutorial.
  • Create a Java source folder within the repository project, e.g. src/java.
  • Create a Java package within the Java source folder; at least create respective sub-folders, e.g. org/oomega/tutorial.
  • Create a Java class within the newly created package, e.g. Tutorial.java, and enter some Java code.
    package org.oomega.tutorial;
    
    public class Tutorial {
    	
      public Tutorial() {
      }
    	
      public static void main(String[] args) {
        new Tutorial();
      }
    	
    }
    
  • Create your first URIRef object by choosing Create ... as entry > URIRef in the context menu of ModelFolder Models.
  • Select the newly created object and enter the following in the Properties view:
    • name = Tutorial
    • staticURI = platform:/resource/Tutorial/src/java/org/oomega/tutorial/Tutorial.java

Now you can double-click on the URIRef object and you will notice that the standard Java editor will be opened and shows the Tutorial.java file. Please note: your Java files can reside anywhere in the workspace (not necessarily in your repository project), because the project name is part of the URI and can be changed accordingly.

Extending the concept AbstractURIRef

In practice you probably won't choose to enter static URIs in order to create links to Java files in the workspace. Rather you will introduce some naming convention and automatically derive the file location from other informations in your model.

For this purpose you can extend the concept org.oomega.base.AbstractURIRef and overload the inferred property String uri. Here's an example.

package org.metamodels.softwareengineering.common;

import org.oomega.base.*;
import org.metamodels.softwareengineering.component.*;

@entity (weak=true, cid=10355) class JavaFile
  extends org.oomega.base.AbstractURIRef
{
  @association (foreign="javaFile") Interface belongsToInterface;
  @association (foreign="javaFile") Clazz belongsToClazz;
	
  @inferred String uri() {
    if (getBelongsToInterface() != null) {
      return getBelongsToInterface().getJavaFileURI();
    }
    else if (getBelongsToClazz() != null) {
      return getBelongsToClazz().getJavaFileURI();
    }
    return null;
  }
}

This sample code is an excerpt of the metamodel org.metamodels.softwareengineering. The URI is actually calculated; the result depends on the following informations which are part of the model:

  • the name of the interface or class
  • to which component the interface/class belongs
  • to which subsystem and layer the component belongs

The source of this example can be found in metaMODELS.org's SVN repository.

  • Repository location: https://svn.metamodels.org/repository
  • Path to project: /Metamodels/Modelling Languages/Software and Systems Engineering/SoftwareEngineering/trunk/org.metamodels.softwareengineering
  • Relevant model files: org.metamodels.softwareengineering.common.JavaFile, org.metamodels.softwareengineering.component.Interface

Finally, let's show the result. When you browse a software engineering model, you can follow a hyperlink in the text editor by clicking "CTRL + Left Mouse Button" on the respective text. In the example beneath you would click on org.metamodels.softwareengineering.exemplarymodel.orgunit.model.CompanyDao. Again, the standard Java editor will be opened and shows the appropriate Java file.


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.