blob: 37452d295d4468aba332b536650c3da3fec1096e [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2017, MDH
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Faiz Ul Muram <faiz.ul.muram@mdh.se>
* Initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.opencert.process2assurproj.assuranceproject.etl;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.epsilon.common.parse.problem.ParseProblem;
import org.eclipse.epsilon.common.util.StringProperties;
import org.eclipse.epsilon.emc.emf.EmfModel;
import org.eclipse.epsilon.emc.emf.xml.XmlModel;
import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
import org.eclipse.epsilon.eol.exceptions.models.EolModelLoadingException;
import org.eclipse.epsilon.eol.models.IRelativePathResolver;
import org.eclipse.epsilon.etl.EtlModule;
import org.eclipse.epsilon.etl.IEtlModule;
import org.eclipse.epsilon.etl.dom.TransformationRule;
import org.eclipse.epsilon.etl.trace.Transformation;
import org.eclipse.opencert.process2assurproj.assuranceproject.provider.AssuranceprojectEditPlugin;
public class EPF2ARGTransformation {
private static final String RESOURCE_PATH_PREFIX = "/";
private static final String UTF_8 = "UTF-8";
private String pluginID;
private String etlTransform;
//MCP
private Object result;
// Transformation params
//MCP
private String libName;
private String libModelFilePath;
private boolean libReadOnLoad;
private boolean libStoreOnDisposal;
private String epfMetamodelURI;
private String epfMetamodelFilePath;
// Paremeters for ouput models
// Argument
private String argName;
private String argMetamodelURI;
//CRF problem
private String argMetaModelFilePath;
//CRF problem
private String argModelFilePath;
private boolean argReadOnLoad;
private boolean argStoreOnDisposal;
public void execute() throws Exception {
URL resolvedTransformationURL = null;
URI transformURI = null;
String file = null;
try{
String spluginPath = AssuranceprojectEditPlugin.getPlugin().getBundle().getLocation();
RedirectSystemErr.out("spluginPath:= ." + spluginPath + ".");
if(spluginPath.contains("plugins")) // Yes, in jar mode.
{
String pluginPath = "configuration";
URL transformationEntry = AssuranceprojectEditPlugin.getPlugin().getBundle().getEntry(
etlTransform);
resolvedTransformationURL = FileLocator
.resolve(transformationEntry);
// MCP
file = resolvedTransformationURL.getFile();
file = file.replace(" ", "%20");// replace white character
String wrong = file.substring(file.indexOf("plugins"), file.indexOf("/"+etlTransform));
String file2 = file.replace(wrong, pluginPath);
file = file2;
}
else
{
URL transformationEntry = AssuranceprojectEditPlugin.getPlugin().getBundle().getEntry(
etlTransform);
resolvedTransformationURL = FileLocator
.resolve(transformationEntry);
// MCP
file = "file:" + resolvedTransformationURL.getFile();
file = file.replace(" ", "%20");// replace white character
}
RedirectSystemErr.out("rules file:= ." + file + ".");
//transformURI = resolvedTransformationURL.toURI();
transformURI = new URI(file);
// MCP
}catch(IOException e){
e.printStackTrace();RedirectSystemErr.out("execute exception 1: "+e.toString());
} catch (URISyntaxException e) {
e.printStackTrace();
RedirectSystemErr.out("execute exception 2: "+e.toString());
}
// MCP
//IEolExecutableModule etlModule = new EtlModule();
IEtlModule etlModule = new EtlModule();
// MCP
try {
etlModule.parse(transformURI);
} catch (Exception e) {
e.printStackTrace();
RedirectSystemErr.out("execute exception 3: "+e.toString());
}
if (etlModule.getParseProblems().size() > 0) {
System.err.println("Parse errors occured...");
for (ParseProblem problem : etlModule.getParseProblems()) {
System.err.println(problem.toString());
RedirectSystemErr.out("encontrado "+problem.toString());
}
}
for ( TransformationRule rule: etlModule.getTransformationRules())
{
RedirectSystemErr.out("rule = ."+ rule.toString() + ".");
}
// Loading of the models used in the transformation.
XmlModel libModel=createSourceXmlModel(libName, epfMetamodelFilePath, libModelFilePath, libReadOnLoad, libStoreOnDisposal);
//
EmfModel argModel=createTargetEmFModel(argName, argMetamodelURI, argMetaModelFilePath, argModelFilePath, argReadOnLoad, argStoreOnDisposal);
if(libModel == null)
{
RedirectSystemErr.out("libModel is null");
}
if(argModel == null)
{
RedirectSystemErr.out("argModel is null");
}
etlModule.getContext().getModelRepository().addModel(libModel);
etlModule.getContext().getModelRepository().addModel(argModel);
// Running the transformation
try {
result = etlModule.execute();
} catch (EolRuntimeException e) {
e.printStackTrace();
RedirectSystemErr.out("execute exception 4: "+e.toString());
}
RedirectSystemErr.out("execute voy a leer reglas disparadas ");
for(Transformation elem : etlModule.getContext().getTransformationTrace().getTransformations())
{
TransformationRule rule = elem.getRule();
Object source = elem.getSource();
Collection targets = elem.getTargets();
System.out.println("Fired rule =" + rule.getName() + ": source =[" + source.toString() + "] & tragets =[" + targets.toString() + "]");
}
libModel.dispose();
argModel.dispose();
etlModule.getContext().getModelRepository().dispose();
}
private org.eclipse.emf.common.util.URI getResourceEmfURI(String resourcePath) {
org.eclipse.emf.common.util.URI res=org.eclipse.emf.common.util.URI.createURI(getClass().getResource(
RESOURCE_PATH_PREFIX + resourcePath).toExternalForm());
System.out.println(res.toFileString());
return res;
}
protected XmlModel createSourceXmlModel(String name, String sourceMetaModelFilePath,
String sourceModelFilePath, Boolean sourceReadOnLoad, Boolean sourceStoreOnDisposal){
XmlModel xmlModel=new XmlModel();
try{
StringProperties sp = new StringProperties();
sp.put(XmlModel.PROPERTY_NAME, name);
sp.put(XmlModel.PROPERTY_READONLOAD, sourceReadOnLoad);
sp.put(XmlModel.PROPERTY_STOREONDISPOSAL, sourceStoreOnDisposal);
sp.put(XmlModel.PROPERTY_XSD_FILE, sourceMetaModelFilePath);
sp.put(XmlModel.PROPERTY_MODEL_FILE,sourceModelFilePath);
xmlModel.load(sp, new IRelativePathResolver() {
@Override
public String resolve(String relativePath) {
return relativePath;
}
});
//return model;
}catch(Exception e){
e.printStackTrace();
}
return xmlModel;
}
protected EmfModel createTargetEmFModel(String name, String targetMetaModelURI, String targetMetaModelFilePath,
String targetModelFilePath, Boolean targetReadOnLoad, Boolean targetStoreOnDisposal){
EmfModel emfModel= new EmfModel();
emfModel.setName(name);
if(targetMetaModelURI != null && !targetMetaModelURI.isEmpty())
{
emfModel.setMetamodelFileBased(false);
if(targetMetaModelURI.contains(","))
{
String[] metaModelURIs = targetMetaModelURI.split(",");
List<String> uris =new ArrayList<String>(metaModelURIs.length);
for(int i=0;i<metaModelURIs.length; i++)
{
uris.add(metaModelURIs[i].trim());
};
RedirectSystemErr.out("target uris:= ." + uris.toString() + ".");
emfModel.setMetamodelUris(uris);
}
else
{
RedirectSystemErr.out("targetMetaModelURI:= ." + targetMetaModelURI + ".");
emfModel.setMetamodelUri(targetMetaModelURI);
}
}
if(targetMetaModelFilePath != null && !targetMetaModelFilePath.isEmpty())
{
emfModel.setMetamodelFileBased(true);
if(targetMetaModelFilePath.contains(","))
{
String[] metaModelURIs = targetMetaModelFilePath.split(",");
List<String> files = new ArrayList<String>(metaModelURIs.length);
List<org.eclipse.emf.common.util.URI> fileUris =new ArrayList<org.eclipse.emf.common.util.URI>(metaModelURIs.length);
for(int i=0;i<metaModelURIs.length; i++)
{
files.add(metaModelURIs[i].trim());
};
RedirectSystemErr.out("target files:= ." + files.toString() + ".");
emfModel.setMetamodelFiles(files);
}
else
{
RedirectSystemErr.out("targetMetaModelFilePath:= ." + targetMetaModelFilePath + ".");
emfModel.setMetamodelFile(targetMetaModelFilePath);
}
}
//CRF problem
RedirectSystemErr.out("targetModelFilePath:= ." + targetModelFilePath + ".");
emfModel.setModelFile(targetModelFilePath);
emfModel.setReadOnLoad(targetReadOnLoad);
emfModel.setStoredOnDisposal(targetStoreOnDisposal);
// MCP
emfModel.setCachingEnabled(true);
emfModel.setExpand(true);
// MCP
try {
emfModel.load();
} catch (EolModelLoadingException e) {
e.printStackTrace();
}
return emfModel;
}
public void createJob(EPF2ARGTransParameters parameters) {
this.pluginID = parameters.getPluginID();
this.etlTransform = parameters.getEtlTransform();
//EPF process model
libName=parameters.getLibName();
libModelFilePath=parameters.getLibModelFilePath();
libReadOnLoad=parameters.isLibReadOnLoad();
libStoreOnDisposal=parameters.isLibStoreOnDisposal();
// EPF metamodel (common to library and configuration)
epfMetamodelURI=parameters.getEPFMetaModelURI();
epfMetamodelFilePath=parameters.getEPFMetaModelFilePath();
// Argument model
argName=parameters.getArgName(); //ARG
argMetamodelURI=parameters.getArgMetamodelURI();
argMetaModelFilePath=parameters.getArgMetamodelFilePath();
argModelFilePath=parameters.getArgModelFilePath();
argReadOnLoad=parameters.isArgReadOnLoad();
argStoreOnDisposal=parameters.isArgStoreOnDisposal();
}
public Object getResult()
{
return result;
}
}