Question

I am trying to achieve model driven development by defining a DSL and performing M2M as well as M2T (code generation). For the code generation I chose to work with XPand and template definitions. Could you provide sample code or link to documentation that explains how to invoke the template expansion from Java code?

P.S. I find it very hard to run such things in standalone mode outside of Eclipse so I started by writing simple Java utilities.

Was it helpful?

Solution

Here is the code for anyone interested:

    Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    Map<String, Object> factoryMap = reg.getExtensionToFactoryMap();
    factoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());

    ResourceSet resourceSet = new ResourceSetImpl();
    EPackage PSM_Pkg = MyDSLPackage.eINSTANCE;
    resourceSet.getPackageRegistry().put(PSM_Pkg.getNsURI(), PSM_Pkg);

    Resource resource = resourceSet.getResource(Constants.PSM_URI, true);
    EList<EObject> inObjects = resource.getContents();

    // Xpand
    URI outURI = URI.createURI("file:///C:/Users/...");
    Output out = new OutputImpl();
    Outlet outlet = new Outlet(outURI.toFileString());
    out.addOutlet(outlet);
    XpandExecutionContextImpl executionContext = new XpandExecutionContextImpl(out, null);

    // Configure the metamodels
    EmfMetaModel emfMetaModel = new EmfMetaModel();
    emfMetaModel.setMetaModelPackage(MyDSLlPackage.class.getName());
    executionContext.registerMetaModel(emfMetaModel);
    XpandFacade xpandFacade = XpandFacade.create(executionContext);
    Object[] params = null;
    System.out.println(inObjects.get(0));
    xpandFacade.evaluate("template::Template::main", inObjects.get(0), params);
    System.out.println("Code generated.");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top