0

DSL を定義し、M2M と M2T (コード生成) を実行することで、モデル駆動型開発を実現しようとしています。コード生成では、XPand とテンプレート定義を使用することにしました。Java コードからテンプレート展開を呼び出す方法を説明するサンプル コードまたはドキュメントへのリンクを提供していただけますか?

PS Eclipse の外でスタンドアロン モードでこのようなものを実行するのは非常に難しいと思うので、単純な Java ユーティリティを作成することから始めました。

4

1 に答える 1

2

興味のある人のためのコードは次のとおりです。

    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.");
于 2013-03-28T22:15:49.077 に答える