Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use EcoreImporter and GenModel to generate Code #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.codegen.ecore.generator.Generator;
Expand Down Expand Up @@ -58,73 +62,68 @@ public void loadDefaultSettings() {
}

protected void generateMetaModelCode(URI base, String metaModelLocation, String genModelLocation,
EPackage metaModel) {
EPackage metaModel) {
String pluginID = options.project.name();

URI metaModelUri = URI.createURI(metaModelLocation);
metaModelUri = metaModelUri.resolve(base);

Monitor monitor = BasicMonitor.toMonitor(new NullProgressMonitor());
BasicMonitor monitor = new BasicMonitor.Printing(System.out);
try {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IFile metaModelFile = root.getFile(new Path(metaModelLocation));
IProject project = metaModelFile.getProject();
IFile pluginXml = project.getFile("plugin.xml");
if (pluginXml.exists()) {
// TODO: Check if genmodels are registered at the extension-point
// "org.eclipse.emf.ecore.generated_package" that do not exist
}

EcoreImporter importer = new EcoreImporter();
importer.setModelLocation(metaModelUri.toString());
importer.setModelFile(metaModelFile);
IFile genModelFile = root.getFile(new Path(genModelLocation));
if (!genModelFile.exists()) {
// Generate a dummy GenModel in case it is already registered at "org.eclipse.emf.ecore.generated_package"
Resource res = new ResourceSetImpl()
.createResource(URI.createPlatformResourceURI(genModelFile.getFullPath().toString(), true));
GenModel dummyGenModel = GenModelFactory.eINSTANCE.createGenModel();
res.getContents().add(dummyGenModel);
res.save(Collections.emptyMap());
}
importer.computeEPackages(monitor);
importer.adjustEPackages(monitor);

for(EPackage ePackage : importer.getEPackages()) {
if(ePackage.getName().equals(metaModel.getName())) {

Set<EPackage> importedEPackages = new HashSet<>();
for (GenModel referencedGen : importer.getExternalGenModels()) {
for (GenPackage genPackage : referencedGen.getGenPackages()) {
EPackage ePackage = importer.getReferredEPackage(genPackage);
if (ePackage != null && !metaModelUri.toString().equals(ePackage.getNsURI())) {
importer.getReferencedGenPackages().add(genPackage);
importer.getReferenceGenPackageConvertInfo(genPackage).setValidReference(true);
importer.getEPackageConvertInfo(ePackage).setConvert(false);
importedEPackages.add(ePackage);
}
}
}
for (EPackage ePackage : importer.getEPackages()) {
if (!importedEPackages.contains(ePackage)) {
importer.getEPackageConvertInfo(ePackage).setConvert(true);
}else {
importer.getEPackageConvertInfo(ePackage).setConvert(false);
}

}

}
importer.setGenModelContainerPath(new Path(pluginID).append("model"));
importer.setGenModelFileName(importer.computeDefaultGenModelFileName());
importer.prepareGenModelAndEPackages(monitor);

GenModel genModel = importer.getGenModel();
genModel.setModelDirectory(options.project.path() + "/gen/");

Set<GenPackage> removals = genModel.getGenPackages().stream().filter(pkg -> !pkg.getEcorePackage().getName().equals(metaModel.getName())).collect(Collectors.toSet());
removals.forEach(pkg -> genModel.getGenPackages().remove(pkg));
genModel.getUsedGenPackages().addAll(removals);

Map<String, URI> pack2genMapEnv = EcorePlugin.getEPackageNsURIToGenModelLocationMap(false);
Map<String, URI> pack2genMapTarget = EcorePlugin.getEPackageNsURIToGenModelLocationMap(true);

// create dummy genmodels or else the genpackages can not be found and thus persisted
for(GenPackage gp : removals) {
// search first in environment in case that the genmodel is exported as plugin
URI genURI = pack2genMapEnv.get(gp.getNSURI());
if(genURI == null)
genURI = pack2genMapTarget.get(gp.getNSURI());
ResourceSet rs = new ResourceSetImpl();
Resource createResource = rs.createResource(genURI);
createResource.load(null);
if(createResource.isLoaded()) {
GenModel newGen = (GenModel) createResource.getContents().get(0);
genModel.getUsedGenPackages().remove(gp);
genModel.getUsedGenPackages().add(newGen.getGenPackages().get(0));
}
else {
GenModel fakeGen = GenModelFactory.eINSTANCE.createGenModel();
fakeGen.setModelPluginID(gp.getEcorePackage().getNsPrefix());
fakeGen.getGenPackages().add(gp);
genModel.eResource().getContents().add(fakeGen);
}
}

genModel.setGenerateSchema(true);
genModel.setCanGenerate(true);
genModel.reconcile();
genModel.reconcile();

EcoreUtil.resolveAll(importer.getGenModelResourceSet());
genModel.eResource().save(Collections.emptyMap());

// importer.saveGenModelAndEPackages(monitor);
genModel.eResource().save(Collections.emptyMap());

Generator generator = GenModelUtil.createGenerator(genModel);
generator.setInput(genModel);
generator.generate(genModel, GenBaseGeneratorAdapter.MODEL_PROJECT_TYPE, monitor);
} catch (Exception e) {
System.err.println("Could not generate TGG metamodel code!");
Expand All @@ -135,8 +134,8 @@ public Collection<EPackage> getImportedPackages() {
return importedPackages;
}

public static IbexOptions registerResourceHandler(IbexOptions options, List<String> metaModelImports, boolean generateCode)
throws IOException {
public static IbexOptions registerResourceHandler(IbexOptions options, List<String> metaModelImports,
boolean generateCode) throws IOException {
HiPEBuilderUtil util = new HiPEBuilderUtil(options);
options.resourceHandler(new TGGResourceHandler() {
@Override
Expand All @@ -149,7 +148,7 @@ protected void registerUserMetamodels() throws IOException {
String genModelLocation = options.project.path() + "/model/"
+ MoflonUtil.lastCapitalizedSegmentOf(options.project.name()) + ".genmodel";
EPackage metaModel = loadAndRegisterCorrMetamodel(metaModelLocation);
if(generateCode)
if (generateCode)
util.generateMetaModelCode(base, metaModelLocation, genModelLocation, metaModel);
}

Expand All @@ -169,4 +168,4 @@ public void saveModels() throws IOException {
});
return options;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.emoflon.ibex.tgg.compiler.hipe.defaults

import java.util.Collection
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage
import org.emoflon.ibex.tgg.compiler.defaults.DefaultFilesGenerator
import org.moflon.core.utilities.MoflonUtil

class HiPEFilesGenerator extends DefaultFilesGenerator {

public static final String DEFAULT_REGISTRATION_HELPER = "_DefaultRegistrationHelper";
public static final String MODELGEN_APP = "MODELGEN_App";
public static final String MODELGEN_APP = "MODELGEN_App";
public static final String SYNC_APP = "SYNC_App";
public static final String INITIAL_FWD_APP = "INITIAL_FWD_App";
public static final String INITIAL_BWD_APP = "INITIAL_BWD_App";
Expand All @@ -15,10 +17,12 @@ class HiPEFilesGenerator extends DefaultFilesGenerator {
public static final String FWD_OPT_APP = "FWD_OPT_App";
public static final String BWD_OPT_APP = "BWD_OPT_App";
public static final String REGISTRATION_HELPER = "HiPERegistrationHelper";

def static String generateRegHelperFile(String projectName, String srcProject, String trgProject, String srcPkg, String trgPkg) {
'''
package org.emoflon.ibex.tgg.run.«MoflonUtil.lastCapitalizedSegmentOf(projectName).toLowerCase».config;

def static String generateRegHelperFie(GenPackage corrPackage, Collection<GenPackage> sourcePackages,
Collection<GenPackage> targetPackages) {
var variableNames = newHashMap
var value = '''
package org.emoflon.ibex.tgg.run.«corrPackage.packageName.toLowerCase».config;

import java.io.File;
import java.io.IOException;
Expand All @@ -27,91 +31,145 @@ class HiPEFilesGenerator extends DefaultFilesGenerator {
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.emoflon.ibex.tgg.operational.csp.constraints.factories.«MoflonUtil.lastCapitalizedSegmentOf(projectName).toLowerCase».UserDefinedRuntimeTGGAttrConstraintFactory;
import org.emoflon.ibex.tgg.operational.csp.constraints.factories.«corrPackage.packageName.toLowerCase».UserDefinedRuntimeTGGAttrConstraintFactory;
import org.emoflon.ibex.tgg.operational.defaults.IbexOptions;
import org.emoflon.ibex.tgg.operational.strategies.modules.IbexExecutable;
import org.emoflon.ibex.tgg.operational.strategies.opt.BWD_OPT;
import org.emoflon.ibex.tgg.operational.strategies.opt.FWD_OPT;
import org.emoflon.ibex.tgg.runtime.hipe.HiPETGGEngine;
import org.emoflon.ibex.tgg.compiler.defaults.IRegistrationHelper;

import «projectName».«projectName»Package;
import «projectName».impl.«projectName»PackageImpl;
import «srcPkg».impl.«srcPkg.toFirstUpper»PackageImpl;
import «trgPkg».impl.«trgPkg.toFirstUpper»PackageImpl;

public class «REGISTRATION_HELPER» implements IRegistrationHelper {

/** Create default options **/
public final void setWorkspaceRootDirectory(ResourceSet resourceSet) throws IOException {
final String root = "../";
URI key = URI.createPlatformResourceURI("/", true);
URI value = URI.createFileURI(new File(root).getCanonicalPath() + File.separatorChar);
resourceSet.getURIConverter().getURIMap().put(key, value);
}

/** Load and register source and target metamodels */
public void registerMetamodels(ResourceSet rs, IbexExecutable executable) throws IOException {

// Set correct workspace root
setWorkspaceRootDirectory(rs);
public class «REGISTRATION_HELPER» implements IRegistrationHelper {

// Load and register source and target metamodels
EPackage «srcProject.toLowerCase»Pack = null;
EPackage «trgProject.toLowerCase»Pack = null;
EPackage «projectName.toLowerCase»Pack = null;

if(executable instanceof FWD_OPT) {
Resource res = executable.getResourceHandler().loadResource("platform:/resource/«trgProject»/model/«trgProject».ecore");
«trgProject.toLowerCase»Pack = (EPackage) res.getContents().get(0);
rs.getResources().remove(res);
/** Create default options **/
public final void setWorkspaceRootDirectory(ResourceSet resourceSet) throws IOException {
final String root = "../";
URI key = URI.createPlatformResourceURI("/", true);
URI value = URI.createFileURI(new File(root).getCanonicalPath() + File.separatorChar);
resourceSet.getURIConverter().getURIMap().put(key, value);
}

/** Load and register source and target metamodels */
public void registerMetamodels(ResourceSet rs, IbexExecutable executable) throws IOException {

// Set correct workspace root
setWorkspaceRootDirectory(rs);

res = executable.getResourceHandler().loadResource("platform:/resource/«projectName»/model/«projectName».ecore");
«projectName.toLowerCase»Pack = (EPackage) res.getContents().get(0);
EPackage «corrPackage.packageInterfaceName.toFirstLower» = null;
// Load and register source metamodels
'''
variableNames.put(corrPackage, corrPackage.packageInterfaceName.toFirstLower)

for (genPackage : sourcePackages) {
var name = genPackage.packageInterfaceName.toFirstLower
var i = 1;
while (variableNames.containsValue(name)){
name = genPackage.packageInterfaceName.toFirstLower + i++
}
variableNames.put(genPackage, name)
value +=
''' EPackage «name» = null;
'''
}

value +=
''' // Load and register target metamodels
'''

for (genPackage : targetPackages) {
var name = genPackage.packageInterfaceName.toFirstLower
var i = 1;
while (variableNames.containsValue(name)){
name = genPackage.packageInterfaceName.toFirstLower + i++
}
variableNames.put(genPackage, name)
value +=
''' EPackage «name» = null;
'''
}

value +=
''' if(executable instanceof FWD_OPT) {
Resource res = null;
'''
for (i : sourcePackages) {
value +=
''' res = executable.getResourceHandler().loadResource("«i.getEcorePackage().nsURI»");
«variableNames.get(i)» = (EPackage) res.getContents().get(0);
rs.getResources().remove(res);
'''
}
value +=
''' res = executable.getResourceHandler().loadResource("platform:/resource/«corrPackage.genModel.modelPluginID»/model/«corrPackage.NSName.toFirstUpper».ecore");
«variableNames.get(corrPackage)» = (EPackage) res.getContents().get(0);
rs.getResources().remove(res);
}

if(executable instanceof BWD_OPT) {
Resource res = executable.getResourceHandler().loadResource("platform:/resource/«srcProject»/model/«srcProject».ecore");
«srcProject.toLowerCase»Pack = (EPackage) res.getContents().get(0);
Resource res = null;
'''
for (i : targetPackages) {
value +=
''' res = executable.getResourceHandler().loadResource("«i.getEcorePackage().nsURI»");
«variableNames.get(i)» = (EPackage) res.getContents().get(0);
rs.getResources().remove(res);

res = executable.getResourceHandler().loadResource("platform:/resource/«projectName»/model/«projectName».ecore");
«projectName.toLowerCase»Pack = (EPackage) res.getContents().get(0);
'''
}
value +=
''' res = executable.getResourceHandler().loadResource("platform:/resource/«corrPackage.genModel.modelPluginID»/model/«corrPackage.NSName.toFirstUpper».ecore");
«variableNames.get(corrPackage)» = (EPackage) res.getContents().get(0);
rs.getResources().remove(res);
}
}
'''

if(«srcProject.toLowerCase»Pack == null)
«srcProject.toLowerCase»Pack = «srcPkg.toFirstUpper»PackageImpl.init();

if(«trgProject.toLowerCase»Pack == null)
«trgProject.toLowerCase»Pack = «trgPkg.toFirstUpper»PackageImpl.init();

if(«projectName.toLowerCase»Pack == null) {
«projectName.toLowerCase»Pack = «projectName»PackageImpl.init();
rs.getPackageRegistry().put("platform:/resource/«projectName»/model/«projectName».ecore", «projectName»Package.eINSTANCE);
rs.getPackageRegistry().put("platform:/plugin/«projectName»/model/«projectName».ecore", «projectName»Package.eINSTANCE);
for (i : sourcePackages) {
value +=
''' if(«variableNames.get(i)» == null) {
«variableNames.get(i)» = «i.qualifiedPackageInterfaceName».eINSTANCE;
}
'''
}
for (i : targetPackages) {
value +=
''' if(«variableNames.get(i)» == null) {
«variableNames.get(i)» = «i.qualifiedPackageInterfaceName».eINSTANCE;
}
'''
}

value +=
''' if(«variableNames.get(corrPackage)» == null) {
«variableNames.get(corrPackage)» = «corrPackage.qualifiedPackageInterfaceName».eINSTANCE;
rs.getPackageRegistry().put("platform:/resource/«corrPackage.genModel.modelPluginID»/model/«corrPackage.packageName.toFirstUpper».ecore", «corrPackage.qualifiedPackageInterfaceName».eINSTANCE);
rs.getPackageRegistry().put("platform:/plugin/«corrPackage.genModel.modelPluginID»/model/«corrPackage.packageName.toFirstUpper».ecore", «corrPackage.qualifiedPackageInterfaceName».eINSTANCE);
}

rs.getPackageRegistry().put("platform:/resource/«srcProject»/model/«srcProject».ecore", «srcProject.toLowerCase»Pack);
rs.getPackageRegistry().put("platform:/plugin/«srcProject»/model/«srcProject».ecore", «srcProject.toLowerCase»Pack);

rs.getPackageRegistry().put("platform:/resource/«trgProject»/model/«trgProject».ecore", «trgProject.toLowerCase»Pack);
rs.getPackageRegistry().put("platform:/plugin/«trgProject»/model/«trgProject».ecore", «trgProject.toLowerCase»Pack);
}
'''
for (i : sourcePackages) {
value +=
''' rs.getPackageRegistry().put("«i.getEcorePackage.nsURI»",«i.qualifiedPackageInterfaceName».eINSTANCE);
'''
}
for (i : targetPackages) {
value +=
''' rs.getPackageRegistry().put("«i.getEcorePackage.nsURI»",«i.qualifiedPackageInterfaceName».eINSTANCE);
'''
}
value +=
''' }

/** Create default options **/
public IbexOptions createIbexOptions() {
IbexOptions options = new IbexOptions();
options.blackInterpreter(new HiPETGGEngine());
options.project.name("«MoflonUtil.lastCapitalizedSegmentOf(projectName)»");
options.project.path("«projectName»");
options.project.name("«MoflonUtil.lastCapitalizedSegmentOf(corrPackage.genModel.modelPluginID)»");
options.project.path("«MoflonUtil.lastSegmentOf(corrPackage.genModel.modelPluginID)»");
options.debug.ibexDebug(false);
options.csp.userDefinedConstraints(new UserDefinedRuntimeTGGAttrConstraintFactory());
options.registrationHelper(this);
return options;
}
}
'''
return value
}
}
}
Loading