Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » M2M (model-to-model transformation) » [ATL] A problem in a standlone model transformation.
[ATL] A problem in a standlone model transformation. [message #101012] Wed, 04 March 2009 13:06
Mikai Yang is currently offline Mikai YangFriend
Messages: 149
Registered: July 2009
Senior Member
Hi,
Would u please debug the following standalone transformation?
I follow Milan Milanovic's ATLCommandLine to develop the
transformation. It seems the transformation is ok. However there is a
fatal run-time error while extracting the resulting mdoels into files.

The followign is some information about this transformation.

1) I use the EMF model handler, using no MDR handler.
2) I am sure the ATL transformation file is absolutely ok, because
it can be used without problem in Eclipse environemtn.
3) For you information, he following head is in the ATL
tranformation file:
module AL2G; -- Module Template
create acg : ALG from ac : AL;
uses Lib4This;
4) The input model is well-formed.


So, do you have any idea of where the problem comes from? You will be
highly appreciated.

Cheers.


Michael.





/*
* Created on 3 March 2008. - last revision: 8.5.2008.
*/
package atl;

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.reflect.*;
import javax.jmi.reflect.*;


import org.xml.sax.Locator;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import org.eclipse.core.runtime.*;
import org.atl.engine.repositories.mdr4atl.*;
import org.atl.engine.vm.*;
import org.atl.eclipse.engine.*;
import org.atl.engine.extractors.*;
import org.atl.engine.extractors.ebnf.*;
import org.atl.engine.injectors.ebnf.*;
import org.atl.engine.extractors.xml.*;
import org.atl.engine.injectors.xml.*;
import org.atl.engine.repositories.emf4atl.*;
import org.atl.engine.vm.nativelib.*;




public class Xuml2JavaTranslator {


// Relative location of transformations
String atlFilesLoc = "..\\..\\transformations\\";
// Relative location of metamodels
String mmsLoc = "..\\..\\metamodels\\";

// Obtain URLs of transformation files.
private URL OneMM2OtherMMurl =
Xuml2JavaTranslator.class.getResource(atlFilesLoc + "AL2G.asm");
private URL Lib4Thisurl =
Xuml2JavaTranslator.class.getResource(atlFilesLoc + "AL2G_LIB.asm");

// EMF model handler
private AtlModelHandler emfamh = null;

// For keeping metamodel names
Map MDRMetaModels = new HashMap();

// Meta-models in EMF.
private ASMModel oneMM = null;
private ASMModel otherMM = null;
private ASMModel xmlEMFmm = null;

// Singleton ATLTransformation.
private static Xuml2JavaTranslator translator = null;

// General meta-models
private ASMModel pbmm = null;
// Markers for Problem metamodel
private MarkerMaker markerMaker;

// Constructor
public Xuml2JavaTranslator() {
//Initialize EMF based metamodels
initEMF();
}

/**
* Initialize EMF model handler and
* Ecore based metamodels
*/
private void initEMF() {

if(emfamh == null) { // if EMF is not initialized

// Initialize EMF model handler
emfamh = AtlModelHandler.getDefault(AtlModelHandler.AMH_EMF);

// URL's to Ecore (XMI) metamodels
URL onemmurl = Xuml2JavaTranslator.class.getResource(mmsLoc +
"OneMM\\AL_Simplified.ecore");
URL xmlmmurl = Xuml2JavaTranslator.class.getResource(mmsLoc +
"xml\\xml.ecore");
URL othermmurl = Xuml2JavaTranslator.class.getResource(mmsLoc +
"OtherMM\\AL_Simplified_Gen.ecore");

try {
// Load metamodels from persistence.
oneMM = emfamh.loadModel("AL", emfamh.getMof(), onemmurl.openStream());
xmlEMFmm = emfamh.loadModel("XML", emfamh.getMof(),
xmlmmurl.openStream());
otherMM = emfamh.loadModel("ALG", emfamh.getMof(),
othermmurl.openStream());
} catch (IOException e) {
e.printStackTrace();
}
}
pbmm = emfamh.getBuiltInMetaModel("Problem");
markerMaker = new MarkerMaker();
} // -- end of initEMF

/**
* Return a singleton translator.
* @return default repository for input (EMF or MDR)
*/
public static Xuml2JavaTranslator getTranslator() {
if(translator == null)
translator = new Xuml2JavaTranslator();

return translator;
}

/**
* A helper method.
* Validate the well-formedness of input XML file
* @param file path to input XML file or XML code
* @param isFile if it is true then it is path to file, otherwise
it is String that contains XML code
* @return true if input XML file is well formed, otherwise is false
*/
private boolean checkForWellFormedness(String file, boolean isFile)
{
SAXParser saxParser = null;
DefaultHandler dh = null;
InputStream in = null;

// init parser
try {
SAXParserFactory spfactory = SAXParserFactory.newInstance();
saxParser = spfactory.newSAXParser();
dh = new DefaultHandler();
}
catch(Exception e) {
System.out.println("Initialize SAX parser fails.");
e.printStackTrace();
return false;
}

// parse the XML document using SAX parser
try {
if (isFile == true) {
in = new FileInputStream(file);
}
else {
byte currentXMLBytes[] = file.getBytes(); // Get bytes from input String
in = new ByteArrayInputStream(currentXMLBytes);
}
saxParser.parse(in, dh);
}
catch(SAXParseException spe) {
System.out.println("File is not well-formed.");
return false;
}
catch(SAXException se) { // (*)
System.out.println("Error when parsing input XML file: " + file);
se.printStackTrace();
return false;
}
catch(FileNotFoundException f) {
System.out.println("Error: File is not found");
return false;
}
catch(IOException ioe) {
System.out.println("Cannot read file.");
return false;
}
return true;
}

/**
* Inject input XML file to XML model (instance of XML metamodel -
MOF 1.4)
*/
public ASMModel injectXMLModelFromFile(String file) {
initEMF();

ASMModel ret = emfamh.newModel("IN", xmlEMFmm);

XMLInjector xmli = new XMLInjector();

Map parameters = Collections.EMPTY_MAP;

InputStream in = null;

try {
in = new FileInputStream(file);

xmli.inject(ret, in, parameters);

} catch(FileNotFoundException f) {
System.out.println("Error: File is not found");
} catch(IOException io) {
System.out.println("Error: in injection of XML file");
}

return ret;
} // -- end of injectXMLModelFromFile


/**
* Extract input XML model (instance of XML metamodel - MOF 1.4) to
XML file
* @param model input XML model which will be extracted to XML file
* @param file output XML file in which input XML model will be
extracted
*/
public void extractXMLModelToFile(ASMModel model, String file) {
initEMF();

OutputStream out = null;

Map parameters = Collections.EMPTY_MAP;

XMLExtractor xmle = new XMLExtractor();

try {
out = new FileOutputStream(file);
if (out == null) System.out.println("The out is null");
if (xmle == null) System.out.println("xmle is null");
if (model == null) System.out.println("model is null");
xmle.extract(model, out, parameters); // Do extraction !
out.flush(); out.close(); // close stream
} catch(FileNotFoundException f) {
System.out.println("11Error: File is not found");
} catch(IOException io) {
System.out.println("Error: in extraction of XML model to XML file");
}
}

/**
* Extract input MM model (instance of some MM metamodel - MOF 1.4)
to File
* @param MMModel input MM model which will be extracted to File
* @param file path to the file in which will be extracted input MM model
*/
public void saveMMModelToFile(ASMModel MMModel, String file) {
initEMF();

OutputStream out = null;

Extractor ext = new EBNFExtractor();
Map params = new HashMap();
params.put("format", MDRMetaModels.get("MM-TCS"));
params.put("indentString", "\t");

try {
out = new FileOutputStream(file);
ext.extract(MMModel, out, params); // do it!

} catch(Exception e) {
e.printStackTrace();
}
} // -- end of saveMMModelToFile


/**
* Launch ATL transformation
*/
public ASMModel runATLTransformation(AtlModelHandler modelHandler, URL
transformation, ASMModel inputModel,
ASMModel inputMetamodel, ASMModel outputMetamodel, Map
inParams, Map inLibs) {
initEMF(); // Initialize MDR model handler

ASMModel ret = null; // return model

// Set launch configuration
Map models = new HashMap();
System.out.println("The name of inputMetamodel: " +
inputMetamodel.getName());
System.out.println("The name of inputMetamodel: " +
outputMetamodel.getName());
models.put(inputMetamodel.getName(), inputMetamodel); // input metamodel
models.put(outputMetamodel.getName(), outputMetamodel); // output
metamodel
models.put("ac", inputModel); // input model
ret = modelHandler.newModel("acg", outputMetamodel); // output model
models.put("acg", ret);

Map params = inParams; // Parameters
Map libs = inLibs; // Libraries

// Launch ATL transformation
if(transformation == null)System.out.println("transformation is null");
if(libs == null)System.out.println("libs is null");
if(models == null)System.out.println("models is null");
if(params == null)System.out.println("params is null");
AtlLauncher.getDefault().launch(transformation, libs, models, params);

return ret;
} // -- end of runATLTransformation


/**
* Run OneMM to OtherMM (example) ATL transformation (OneMM2OtherMM).
*/
public ASMModel getOtherMMFromOneMM(ASMModel oneModel) {

// Run transformation and return output model
if(emfamh == null )System.out.println("emfamh is null. ");
if(oneModel == null )System.out.println("oneModel is null. ");
if(oneMM == null )System.out.println("oneMM_EMF is null. ");
if(otherMM == null )System.out.println("emfamh is null. ");

//A library is used.
Map libs = new HashMap();
libs.put("Lib4This", Lib4Thisurl);

return runATLTransformation(emfamh, OneMM2OtherMMurl, oneModel, oneMM,
otherMM, Collections.EMPTY_MAP, libs);
}

/**
* Transform input One (XML-based) file to output Other (XML-based)
file - an example with injection/extraction
*/
public String transformOnetoOther(String InputOneFile, String
OutputOtherFile) {
// Check is input String well-formed (XML)
if (!checkForWellFormedness(InputOneFile, true)) return new
String("Document is not well-formed.");

ASMModel xmlModel = injectXMLModelFromFile(InputOneFile);
ASMModel otherModel = getOtherMMFromOneMM(xmlModel);
extractXMLModelToFile(otherModel, OutputOtherFile);

return new String("Translation is completed successfully.");
}

public static void main(String [] arguments) {

// Create new instance of this class
Xuml2JavaTranslator transformation = Xuml2JavaTranslator.getTranslator();

String inputModel = "models\\one\\AClass.xmi";
String outputModel = "d:\\Other.xml";

// Transform input file to output file
String message = transformation.transformOnetoOther(inputModel,
outputModel);
System.out.println(message);
}

}
Previous Topic:[ATL] about the standalone model transforamtion.
Next Topic:[ATL] inject ATL file to ATL model with TCS?
Goto Forum:
  


Current Time: Thu Apr 25 13:46:21 GMT 2024

Powered by FUDForum. Page generated in 0.03440 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top