Example: Use Epsilon in standalone Java applications

/*******************************************************************************
 * Copyright (c) 2008 The University of York.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Dimitrios Kolovos - initial API and implementation
 ******************************************************************************/
package org.eclipse.epsilon.examples.standalone.eol;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.epsilon.eol.EolModule;
import org.eclipse.epsilon.eol.IEolExecutableModule;
import org.eclipse.epsilon.eol.models.IModel;
import org.eclipse.epsilon.examples.standalone.EpsilonStandaloneExample;

/**
 * This example demonstrates using the 
 * Epsilon Object Language, the core language
 * of Epsilon, in a stand-alone manner 
 * @author Dimitrios Kolovos
 */
public class EolStandaloneExample extends EpsilonStandaloneExample {
  
  public static void main(String[] args) throws Exception {
    new EolStandaloneExample().execute();
  }
  
  @Override
  public IEolExecutableModule createModule() {
    return new EolModule();
  }

  @Override
  public List<IModel> getModels() throws Exception {
    List<IModel> models = new ArrayList<IModel>();
    models.add(createEmfModel("Model", "models/Tree.xmi", 
        "models/Tree.ecore", true, true));
    return models;
  }

  @Override
  public String getSource() throws Exception {
    return "eol/Return.eol";
  }

  @Override
  public void postProcess() {
    
  }
  
}
/*******************************************************************************
 * Copyright (c) 2008 The University of York.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Dimitrios Kolovos - initial API and implementation
 ******************************************************************************/
package org.eclipse.epsilon.examples.standalone;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import org.eclipse.epsilon.common.parse.problem.ParseProblem;
import org.eclipse.epsilon.common.util.StringProperties;
import org.eclipse.epsilon.emc.emf.EmfModel;
import org.eclipse.epsilon.eol.IEolExecutableModule;
import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
import org.eclipse.epsilon.eol.exceptions.models.EolModelLoadingException;
import org.eclipse.epsilon.eol.models.IModel;

public abstract class EpsilonStandaloneExample {
  
  protected IEolExecutableModule module;
  
  protected Object result;
  
  public abstract IEolExecutableModule createModule();
  
  public abstract String getSource() throws Exception;
  
  public abstract List<IModel> getModels() throws Exception;
  
  public void postProcess() {};
  
  public void preProcess() {};
  
  public void execute() throws Exception {
    
    module = createModule();
    module.parse(getFile(getSource()));
    
    if (module.getParseProblems().size() > 0) {
      System.err.println("Parse errors occured...");
      for (ParseProblem problem : module.getParseProblems()) {
        System.err.println(problem.toString());
      }
      System.exit(-1);
    }
    
    for (IModel model : getModels()) {
      module.getContext().getModelRepository().addModel(model);
    }
    
    preProcess();
    result = execute(module);
    postProcess();
    
    module.getContext().getModelRepository().dispose();
  }
  
  protected Object execute(IEolExecutableModule module) 
      throws EolRuntimeException {
    return module.execute();
  }
  
  protected EmfModel createEmfModel(String name, String model, 
      String metamodel, boolean readOnLoad, boolean storeOnDisposal) 
          throws EolModelLoadingException, URISyntaxException {
    EmfModel emfModel = new EmfModel();
    StringProperties properties = new StringProperties();
    properties.put(EmfModel.PROPERTY_NAME, name);
    properties.put(EmfModel.PROPERTY_FILE_BASED_METAMODEL_URI,
        getFile(metamodel).toURI().toString());
    properties.put(EmfModel.PROPERTY_MODEL_URI, 
        getFile(model).toURI().toString());
    properties.put(EmfModel.PROPERTY_READONLOAD, readOnLoad + "");
    properties.put(EmfModel.PROPERTY_STOREONDISPOSAL, 
        storeOnDisposal + "");
    emfModel.load(properties, null);
    return emfModel;
  }

  protected EmfModel createEmfModelByURI(String name, String model, 
      String metamodel, boolean readOnLoad, boolean storeOnDisposal) 
          throws EolModelLoadingException, URISyntaxException {
    EmfModel emfModel = new EmfModel();
    StringProperties properties = new StringProperties();
    properties.put(EmfModel.PROPERTY_NAME, name);
    properties.put(EmfModel.PROPERTY_METAMODEL_URI, metamodel);
    properties.put(EmfModel.PROPERTY_MODEL_URI, 
        getFile(model).toURI().toString());
    properties.put(EmfModel.PROPERTY_READONLOAD, readOnLoad + "");
    properties.put(EmfModel.PROPERTY_STOREONDISPOSAL, 
        storeOnDisposal + "");
    emfModel.load(properties, null);
    return emfModel;
  }
  
  protected File getFile(String fileName) throws URISyntaxException {
    
    URI binUri = EpsilonStandaloneExample.class.
        getResource(fileName).toURI();
    URI uri = null;
    
    if (binUri.toString().indexOf("bin") > -1) {
      uri = new URI(binUri.toString().replaceAll("bin", "src"));
    }
    else {
      uri = binUri;
    }
    
    return new File(uri);
  }
  
}

Check out the code from the SVN:

  • go to the SVN repository
  • navigate to trunk/examples
  • check out the org.eclipse.epsilon.examples.standalone project

Once you have checked out/imported the code, to run the example you need to go through the following steps:

  1. register any .ecore metamodels in the org.eclipse.epsilon.examples.standalone project
  2. right click the .launch file in the org.eclipse.epsilon.examples.standalone project
  3. select Run as... and click the first item in the menu that pops up

What's this?

In this example, we demonstrate how Epsilon languages can be used in standalone, non-Eclipse-based Java applications.

What are .emf files?

.emf files are Ecore metamodels expressed using the Emfatic textual syntax.

More examples...

Epsilon Object Language
Epsilon Transformation Language
Epsilon Generation Language
Epsilon Validation Language
Epsilon Merging Language
Epsilon Flock
Combining the Epsilon Languages
EuGENia
EUnit

Even more examples...

More examples are available in the examples folder of the SVN repository.