Skip to content
  Kermeta  

How to launch a Kermeta program from Java

Document Actions

Note that since May 2009 this question is answered by the "Deployment" document



// Following lines of code are valid with Kermeta version 0.4.x and 0.5.0

import fr.irisa.triskell.kermeta.interpreter.KermetaRaisedException;
import fr.irisa.triskell.kermeta.launcher.KermetaInterpreter;
import fr.irisa.triskell.kermeta.loader.KermetaUnit;
import fr.irisa.triskell.kermeta.loader.KermetaUnitFactory;
import fr.irisa.triskell.kermeta.loader.StdLibKermetaUnitHelper;
import fr.irisa.triskell.kermeta.runtime.RuntimeObject;

/**
 * This program is an example destinated to show how to run a kermeta program from a java program.
 */
public class KmtFromJava {

    /**
     * @param args
     */
    public static void main(String[] args) {
       
        StdLibKermetaUnitHelper.STD_LIB_URI = "../fr.irisa.triskell.kermeta/lib/framework.km";

        File pathWorkSpace = new File("");

        String path_kmt_program = pathWorkSpace.getAbsolutePath()+"/src/kmtFromJava.kmt";

        KermetaInterpreter interpret = typeCheckTranfo(path_kmt_program);
       

        ArrayList<RuntimeObject> params = new ArrayList<RuntimeObject>(); // Array containing the parameters
        params.add(fr.irisa.triskell.kermeta.runtime.basetypes.String.create("parameter of the method", interpret.getMemory().getROFactory()));
        interpret.setEntryParameters(params);

        RuntimeObject result = null;
        // And we launch the interpreter
        try {
            result = interpret.launch();
        } catch (KermetaRaisedException e) {
            e.printStackTrace();
        }
        System.out.println(result.getData().get("StringValue").toString());
    }

    private static KermetaInterpreter typeCheckTranfo(String transfo_abs_path_kmt) {
        KermetaUnit unit = KermetaUnitFactory.getDefaultLoader().createKermetaUnit("file:///"+transfo_abs_path_kmt);
        unit.load();
        unit.typeCheckAllUnits();
        KermetaInterpreter inter = new KermetaInterpreter(unit);
        return inter;
    }
}



// Following lines of code are valid only with the CVS version of Kermeta


import org.kermeta.io.KermetaUnit;
import org.kermeta.io.plugin.IOPlugin;

import fr.irisa.triskell.kermeta.exceptions.KermetaIOFileNotFoundException;
import fr.irisa.triskell.kermeta.exceptions.URIMalformedException;
import fr.irisa.triskell.kermeta.launcher.KermetaInterpreter;
import fr.irisa.triskell.kermeta.typechecker.KermetaTypeChecker;   


public static void main(String[] args) {
       
        KermetaUnit unit = null;
        try {
            unit = IOPlugin.getDefault().loadKermetaUnit("platform:/resource/MyProject/kmtFromJava.km");
        } catch (KermetaIOFileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URIMalformedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        KermetaTypeChecker typechecker = new KermetaTypeChecker(unit);
        typechecker.checkUnit();
        KermetaInterpreter inter = new KermetaInterpreter(unit);
        inter.setEntryPoint("hello::Main", "main");
        inter.launch();
    }