|
|
|
|
|
|
|
|
Re: Create Ecore Modelling Project programmatically [message #1860946 is a reply to message #1860909] |
Mon, 11 September 2023 11:42 |
John Henbergs Messages: 239 Registered: October 2020 |
Senior Member |
|
|
I was able to fix it by locating the issues via comparison. In general, I had issues with the dir and pluginIds. However if anyone wants to do something similar in the future, the code that worked for me is in the following:
public class GenModelGenerator {
public void generateGenModel(String ecoreFilePath, String genModelFilePath, String genModelName) {
// Initialize the EMF resource set
ResourceSet resourceSet = new ResourceSetImpl();
// Load the Ecore model from the file
URI ecoreURI = URI.createURI(ecoreFilePath);
Resource ecoreResource = resourceSet.getResource(ecoreURI, true);
EPackage ePackage = (EPackage) ecoreResource.getContents().get(0);
// Create a new GenModel instance
GenModel genModel = GenModelFactory.eINSTANCE.createGenModel();
genModel.setModelName(genModelName);
genModel.setDynamicTemplates(true);
// Configure the GenModel as needed
genModel.setModelDirectory("/" +genModelName+ "/src");
genModel.setModelPluginID(genModelName);
// Enable code generation options for edit code, editor code, and test code
genModel.setEditDirectory("/" +genModelName+ ".edit/src");
genModel.setEditPluginID(genModelName+ ".edit");
genModel.setEditorDirectory("/" +genModelName+ ".editor/src");
genModel.setEditorPluginID(genModelName+".editor");
genModel.setTestsDirectory("/" +genModelName+ ".tests/src");
genModel.setTestsPluginID(genModelName+".tests");
genModel.getGenPackages().add(createGenPackage(ePackage));
// Save the GenModel to a file
URI genModelURI = URI.createURI(genModelFilePath);
Resource genModelResource = resourceSet.createResource(genModelURI);
genModelResource.getContents().add(genModel);
try {
genModelResource.save(null);
System.out.println("GenModel generated successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
private GenPackage createGenPackage(EPackage ePackage) {
GenPackage genPackage = GenModelFactory.eINSTANCE.createGenPackage();
genPackage.setEcorePackage(ePackage);
// Configure other GenPackage settings as needed
return genPackage;
}
public static void generate(GenModel genModel) {
genModel.reconcile();
genModel.setCanGenerate(true);
genModel.setValidateModel(true);
Generator generator = new Generator();
generator.run(genModel);
}
}
[Updated on: Mon, 11 September 2023 11:43] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03874 seconds