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

Fixes Bug 487483 - Mapping-rules of events are ignored when generating code #45

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 Down Expand Up @@ -51,12 +52,13 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
}

private void evaluate(String generatorName) {
final IModelProject selectedProject = ModelProjectServiceFactory.getDefault().getProjectFromSelection();

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

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

final IConfigurationElement[] configElements = getUserSelectedGenerators(generatorName);
IModelProject selectedProject = ModelProjectServiceFactory.getDefault().getProjectFromSelection();

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

for (IConfigurationElement e : configElements) {
try {
final Object codeGenerator = e.createExecutableExtension("class");
Expand All @@ -67,7 +69,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);
Expand All @@ -76,15 +78,22 @@ 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) {

IConfigurationElement[] configurationElements;
ConfigurationElementLookup elementLookup = ConfigurationElementLookup.getDefault();
configurationElements = elementLookup.getSelectedConfigurationElementFor(GENERATOR_ID, generatorIdentifier);

return configurationElements;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public IConfigurationElement[] getSelectedConfigurationElementFor(
id);
return extension.getConfigurationElements();
}

public String getExtensionSimpleIdentifier(String extensionPtId, String id) {
IExtension extension = EXTENSION_REGISTRY.getExtension(extensionPtId,
id);
if (extension != null) {
return extension.getSimpleIdentifier();
}

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 @@ -51,8 +51,6 @@ public void addMappingModel(MappingModel mappingModel) {
this.allMappingModels.add(mappingModel);
}



@Override
public List<MappingRule> getMappingRulesByOperation(Operation operation) {
List<MappingRule> mappingRules = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -34,6 +36,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 +141,12 @@ public void refresh(IProgressMonitor monitor) {
}

@Override
public IMapping getMapping(String targetPlatform){
return MappingResourceFactory.getInstance().createMapping(this, targetPlatform);
public Collection<MappingModel> getMapping(String targetPlatform){
if (targetPlatform == null) {
return Collections.emptyList();
}

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,13 @@ public IMapping createMapping(IModelElement ownerModelElement, String targetPlat
List<IMapping> referenceMappings = this.getReferenceMappings(ownerModelElement, targetPlatform);
return createMapping(mappingModel, referenceMappings);
}

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 @@ -148,6 +155,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>();
for (IModelElement referenceModelElement : ownerModelElement.getReferences()) {
Expand Down