From d5a3254fd1a2f66c1c2ad88ae14bd68737470c27 Mon Sep 17 00:00:00 2001 From: Erle Czar Mantos Date: Mon, 21 Mar 2016 13:36:03 +0800 Subject: [PATCH] Fix for https://github.com/eclipse/vorto/issues/63 - Mapping-rules of events are ignored when generating code Signed-off-by: Erle Czar Mantos --- .../CodeGeneratorInvocationHandler.java | 20 +++++++++++++----- .../handler/ConfigurationElementLookup.java | 15 +++++++++++++ .../core/model/AbstractModelProject.java | 6 ++++-- .../vorto/core/model/IModelProject.java | 5 ++++- .../core/model/MappingResourceFactory.java | 21 +++++++++++++++++++ 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/bundles/org.eclipse.vorto.codegen.ui/src/org/eclipse/vorto/codegen/ui/handler/CodeGeneratorInvocationHandler.java b/bundles/org.eclipse.vorto.codegen.ui/src/org/eclipse/vorto/codegen/ui/handler/CodeGeneratorInvocationHandler.java index 15cd723c6f..75e9a84948 100644 --- a/bundles/org.eclipse.vorto.codegen.ui/src/org/eclipse/vorto/codegen/ui/handler/CodeGeneratorInvocationHandler.java +++ b/bundles/org.eclipse.vorto.codegen.ui/src/org/eclipse/vorto/codegen/ui/handler/CodeGeneratorInvocationHandler.java @@ -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; @@ -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 @@ -56,10 +58,12 @@ 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 @@ -67,7 +71,7 @@ private void evaluate(String generatorName) { IVortoCodeGenerator informationModelCodeGenerator = (IVortoCodeGenerator) codeGenerator; - CodeGeneratorTaskExecutor.execute(informationModel, informationModelCodeGenerator, createMappingContext()); + CodeGeneratorTaskExecutor.execute(informationModel, informationModelCodeGenerator, createMappingContext(selectedProject, targetPlatform)); } catch (Exception e1) { MessageDisplayFactory.getMessageDisplay().displayError(e1); @@ -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) { diff --git a/bundles/org.eclipse.vorto.codegen.ui/src/org/eclipse/vorto/codegen/ui/handler/ConfigurationElementLookup.java b/bundles/org.eclipse.vorto.codegen.ui/src/org/eclipse/vorto/codegen/ui/handler/ConfigurationElementLookup.java index a442936758..ff181dabab 100644 --- a/bundles/org.eclipse.vorto.codegen.ui/src/org/eclipse/vorto/codegen/ui/handler/ConfigurationElementLookup.java +++ b/bundles/org.eclipse.vorto.codegen.ui/src/org/eclipse/vorto/codegen/ui/handler/ConfigurationElementLookup.java @@ -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; @@ -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; diff --git a/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/AbstractModelProject.java b/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/AbstractModelProject.java index d2384739ec..d4039c26c6 100644 --- a/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/AbstractModelProject.java +++ b/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/AbstractModelProject.java @@ -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; @@ -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; @@ -138,8 +140,8 @@ public void refresh(IProgressMonitor monitor) { } @Override - public IMapping getMapping(String targetPlatform){ - return MappingResourceFactory.getInstance().createMapping(this, targetPlatform); + public Collection getMapping(String targetPlatform){ + return MappingResourceFactory.getInstance().getMappingModels(this, targetPlatform); } protected IModelElementResolver[] getResolvers() { diff --git a/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/IModelProject.java b/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/IModelProject.java index 464d51b040..23b4a3610f 100644 --- a/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/IModelProject.java +++ b/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/IModelProject.java @@ -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; /** @@ -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 getMapping(String targetPlatform); /** * Saves the actual model project, after it has been modified, e.g. after diff --git a/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/MappingResourceFactory.java b/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/MappingResourceFactory.java index a2e8c38ba8..4ea5a2a296 100644 --- a/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/MappingResourceFactory.java +++ b/bundles/org.eclipse.vorto.core.ui/src/org/eclipse/vorto/core/model/MappingResourceFactory.java @@ -81,6 +81,19 @@ public IMapping createMapping(IModelElement ownerModelElement, String targetPlat List 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 getMappingModels(IModelElement ownerModelElement, String targetPlatform) { + List mappingModels = new ArrayList(); + mappingModels.add(getMappingModel(ownerModelElement, targetPlatform)); + mappingModels.addAll(getReferenceMappingModels(ownerModelElement, targetPlatform)); + return mappingModels; + } /** * Create IMapping instance based on given Mapping Model and child IMapping @@ -147,6 +160,14 @@ private MappingModel getMappingModel(IModelElement ownerModelElement, String tar return mappingModel; } + + private List getReferenceMappingModels(IModelElement ownerModelElement, String targetPlatform) { + List referenceMappings = new ArrayList(); + for (IModelElement referenceModelElement : ownerModelElement.getReferences()) { + referenceMappings.add(getMappingModel(referenceModelElement, targetPlatform)); + } + return referenceMappings; + } private List getReferenceMappings(IModelElement ownerModelElement, String targetPlatform) { List referenceMappings = new ArrayList();