Skip to content

Commit

Permalink
Fix for eclipse-vorto#63 - Mapping-rules of events are ignored when g…
Browse files Browse the repository at this point in the history
…enerating code

Signed-off-by: Erle Czar Mantos <[email protected]>
  • Loading branch information
Erle Czar Mantos committed Mar 21, 2016
1 parent 55c56a9 commit d5a3254
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.vorto.codegen.ui.display.MessageDisplayFactory;
import org.eclipse.vorto.codegen.ui.utils.PlatformUtils;
import org.eclipse.vorto.core.api.model.informationmodel.InformationModel;
import org.eclipse.vorto.core.api.model.mapping.MappingModel;
import org.eclipse.vorto.core.model.IModelProject;
import org.eclipse.vorto.core.service.ModelProjectServiceFactory;

Expand All @@ -35,6 +36,7 @@
*/
public class CodeGeneratorInvocationHandler extends AbstractHandler {

private static final String CLASS = "class";
private static final String GENERATOR_ID = IVortoCodeGenerator.GENERATOR_ID;

@Override
Expand All @@ -56,18 +58,20 @@ private void evaluate(String generatorName) {
IModelProject selectedProject = ModelProjectServiceFactory.getDefault().getProjectFromSelection();

InformationModel informationModel = (InformationModel) selectedProject.getModel();


final String targetPlatform = ConfigurationElementLookup.getDefault().getExtensionSimpleIdentifier(GENERATOR_ID, generatorName);

for (IConfigurationElement e : configElements) {
try {
final Object codeGenerator = e.createExecutableExtension("class");
final Object codeGenerator = e.createExecutableExtension(CLASS);

if (!(codeGenerator instanceof IVortoCodeGenerator)) {
continue; // interested only in code generators
}

IVortoCodeGenerator informationModelCodeGenerator = (IVortoCodeGenerator) codeGenerator;

CodeGeneratorTaskExecutor.execute(informationModel, informationModelCodeGenerator, createMappingContext());
CodeGeneratorTaskExecutor.execute(informationModel, informationModelCodeGenerator, createMappingContext(selectedProject, targetPlatform));

} catch (Exception e1) {
MessageDisplayFactory.getMessageDisplay().displayError(e1);
Expand All @@ -76,8 +80,14 @@ private void evaluate(String generatorName) {
}
}

private IMappingContext createMappingContext() {
return new DefaultMappingContext();
private IMappingContext createMappingContext(IModelProject project, String targetPlatform) {
DefaultMappingContext mappingContext = new DefaultMappingContext();

for(MappingModel mappingModel : project.getMapping(targetPlatform)) {
mappingContext.addMappingModel(mappingModel);
}

return mappingContext;
}

private IConfigurationElement[] getUserSelectedGenerators(String generatorIdentifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;

public class ConfigurationElementLookup {

private static final String BUNDLE_NAME = "Bundle-Name";
private IExtensionRegistry EXTENSION_REGISTRY = Platform
.getExtensionRegistry();
private static ConfigurationElementLookup instance;
Expand All @@ -46,6 +48,19 @@ public IConfigurationElement[] getSelectedConfigurationElementFor(
id);
return extension.getConfigurationElements();
}

public String getExtensionSimpleIdentifier(String extensionPtId, String id) {
IExtension extension = EXTENSION_REGISTRY.getExtension(extensionPtId,
id);
if (extension != null && extension.getContributor() != null) {

Bundle bundle = Platform.getBundle(extension.getContributor().getName());

return bundle.getHeaders().get(BUNDLE_NAME);
}

return null;
}

public void setExtensionRegistry(IExtensionRegistry reg) {
this.EXTENSION_REGISTRY = reg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -34,6 +35,7 @@
import org.eclipse.vorto.core.api.model.datatype.Type;
import org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel;
import org.eclipse.vorto.core.api.model.informationmodel.InformationModel;
import org.eclipse.vorto.core.api.model.mapping.MappingModel;
import org.eclipse.vorto.core.api.model.model.Model;
import org.eclipse.vorto.core.api.model.model.ModelId;
import org.eclipse.vorto.core.api.model.model.ModelReference;
Expand Down Expand Up @@ -138,8 +140,8 @@ public void refresh(IProgressMonitor monitor) {
}

@Override
public IMapping getMapping(String targetPlatform){
return MappingResourceFactory.getInstance().createMapping(this, targetPlatform);
public Collection<MappingModel> getMapping(String targetPlatform){
return MappingResourceFactory.getInstance().getMappingModels(this, targetPlatform);
}

protected IModelElementResolver[] getResolvers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
*******************************************************************************/
package org.eclipse.vorto.core.model;

import java.util.Collection;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.vorto.core.api.model.mapping.MappingModel;
import org.eclipse.vorto.core.api.model.model.ModelId;

/**
Expand Down Expand Up @@ -60,7 +63,7 @@ public interface IModelProject extends IModelElement {
* @param targetPlatform: Target platform name the mapping designed for
* @return instance if IMapping
*/
IMapping getMapping(String targetPlatform);
Collection<MappingModel> getMapping(String targetPlatform);

/**
* Saves the actual model project, after it has been modified, e.g. after
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ public IMapping createMapping(IModelElement ownerModelElement, String targetPlat
List<IMapping> referenceMappings = this.getReferenceMappings(ownerModelElement, targetPlatform);
return createMapping(mappingModel, referenceMappings);
}

/**
* Returns all the mapping models of a particular project
* @param ownerModelElement
* @param targetPlatform
* @return
*/
public List<MappingModel> getMappingModels(IModelElement ownerModelElement, String targetPlatform) {
List<MappingModel> mappingModels = new ArrayList<MappingModel>();
mappingModels.add(getMappingModel(ownerModelElement, targetPlatform));
mappingModels.addAll(getReferenceMappingModels(ownerModelElement, targetPlatform));
return mappingModels;
}

/**
* Create IMapping instance based on given Mapping Model and child IMapping
Expand Down Expand Up @@ -147,6 +160,14 @@ private MappingModel getMappingModel(IModelElement ownerModelElement, String tar

return mappingModel;
}

private List<MappingModel> getReferenceMappingModels(IModelElement ownerModelElement, String targetPlatform) {
List<MappingModel> referenceMappings = new ArrayList<MappingModel>();
for (IModelElement referenceModelElement : ownerModelElement.getReferences()) {
referenceMappings.add(getMappingModel(referenceModelElement, targetPlatform));
}
return referenceMappings;
}

private List<IMapping> getReferenceMappings(IModelElement ownerModelElement, String targetPlatform) {
List<IMapping> referenceMappings = new ArrayList<IMapping>();
Expand Down

0 comments on commit d5a3254

Please sign in to comment.