Example: Use Epsilon in standalone Java applications


  • EolStandaloneExample.java
  • EpsilonStandaloneExample.java
  • Get it!
/*******************************************************************************
 * 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.commons.parse.problem.ParseProblem;
import org.eclipse.epsilon.commons.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_METAMODEL_FILE, getFile(metamodel).getAbsolutePath());
    properties.put(EmfModel.PROPERTY_MODEL_FILE, "file:/" + getFile(model).getAbsolutePath());
    properties.put(EmfModel.PROPERTY_IS_METAMODEL_FILE_BASED, "true");
    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_FILE, "file:/" + getFile(model).getAbsolutePath());
    properties.put(EmfModel.PROPERTY_IS_METAMODEL_FILE_BASED, "false");
    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);
  }
  
}

There are two ways to get the code of this example:

  1. download the following zip archive(s), extract them and import them as new Eclipse projects
  2. or 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. right click the .launch file in the org.eclipse.epsilon.examples.standalone project
  2. select Run as... and click the first item in the menu that pops up