我试图通过定义 DSL 并执行 M2M 以及 M2T(代码生成)来实现模型驱动的开发。对于代码生成,我选择使用 XPand 和模板定义。您能否提供示例代码或文档链接来解释如何从 Java 代码调用模板扩展?
PS 我发现在 Eclipse 之外以独立模式运行这些东西非常困难,所以我从编写简单的 Java 实用程序开始。
这是任何有兴趣的人的代码:
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.");