📄 marte2cheddarxml.java
字号:
package com.thalesgroup.cheddar.MARTE2cheddar;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.atl.engine.repositories.emf4atl.ASMEMFModel;
import org.atl.engine.repositories.emf4atl.ASMEMFModelElement;
import org.atl.engine.vm.StackFrame;
import org.atl.engine.vm.nativelib.ASMString;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.NamedElement;
import com.thalesgroup.atl.ExtendedATLProblemsManager;
import com.thalesgroup.atl.ExtendedATLTransform;
import com.thalesgroup.atl.exception.ATLException;
import com.thalesgroup.atl.marte.rsa.MARTEImplementation_RSA;
import com.thalesgroup.java.log.Log;
/**
* This class provides all the tools to :
* - check compatibility of a MARTE model for MARTE to Cheddar transformation
* - run the transformations :
* - MARTE to Cheddar XML (full transform)
* - MARTE to Cheddar (first step)
* - Cheddar to CheddarXML (second step)
* This transformation can handle multiple input analysis contexts.
* (design pattern : Singleton)
*
* <copyright>
* Thales MARTE to Cheddar (Copyright (c) THALES 2007 All rights reserved) is free software; you can redistribute itand/or modify
* it under the terms of the Eclipse Public License as published in http://www.eclipse.org/legal/epl-v10.html
*
* Thales MARTE to Cheddar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Eclipse Public License for more details.
* </copyright>
*
* @author Nicolas Vienne
* @version 0.0.3, 09/2007
*
*/
public class MARTE2CheddarXML extends ExtendedATLTransform implements IRunnableWithProgress {
public static final String COPYRIGHT = "Copyright 2007 Thales Research and Technology";
// Paths
protected static final String BASE_PATH = "/ATL/";
protected static final String DTD_PATH = BASE_PATH+"cheddardtdheader.xml";
protected static final String C2X_PATH = BASE_PATH+"cheddar2XML.asm";
protected static final String M2C_PATH = BASE_PATH+"MARTE2cheddar.asm";
protected static final String CHECK_PATH = BASE_PATH+"CheckMARTE4cheddar.asm";
protected static final URL transfo_m2c = MARTE2CheddarXML.class.getResource(M2C_PATH);
protected static final URL transfo_c2x = MARTE2CheddarXML.class.getResource(C2X_PATH);
protected static final URL transfo_check = MARTE2CheddarXML.class.getResource(CHECK_PATH);
protected ExtendedATLProblemsManager apm = new ExtendedATLProblemsManager();
protected static MARTE2CheddarXML instance = null;
protected boolean export = true;
protected IFile inputFile;
protected String qualifiedName;
protected IFile outputFile;
/**
* Retrieves the singleton instance.
* @return the instance of MARTE2CheddarXML
*/
public static MARTE2CheddarXML getInstance() {
if(instance != null) {
return instance;
} else {
try {
instance = new MARTE2CheddarXML();
} catch (Exception exception) {
Log.errorMessage(Activator.PLUGIN_ID, "Error while creating MARTE2CheddarXML", exception);
}
return instance;
}
}
/**
* Constructeur.
* (Singleton - for internal use only).
* @throws IOException
* @throws ATLException
*/
protected MARTE2CheddarXML() throws IOException, ATLException {
super(null);
enableLOG();
enableUML();
enableMARTE(new MARTEImplementation_RSA()); // should enable UML in the same movement
enablePROBLEM();
enableXMLParameters(); // should enable XML in the same movement
addMetamodelByURI("CHEDDAR", "http:///cheddar.ecore");
addLibrary("MARTE4CHEDDAR", MARTE2CheddarXML.class.getResource(BASE_PATH+"MARTE4CHEDDAR.asm"));
addCustomOperationToVM(MARTE2CheddarXML.class, "getQName");
}
private static Map<EObject, String> object_names = new HashMap<EObject, String>();
private static Map<String, Integer> next_name = new HashMap<String, Integer>();
public static void reset_names() {
object_names.clear();
next_name.clear();
}
public static ASMString getQName(StackFrame frame, ASMEMFModelElement self) {
EObject s = self.getObject();
if(object_names.containsKey(s)) {
return new ASMString(object_names.get(s));
} else {
if(s instanceof NamedElement) {
NamedElement ne = (NamedElement) s;
String name = ne.getName();
String r = name;
if(next_name.containsKey(name)) {
int current_id = next_name.get(name);
r += "_"+current_id;
next_name.put(name, current_id+1);
} else {
next_name.put(name, 2); // next id
}
object_names.put(s, r);
return new ASMString(r);
}
else {
return new ASMString("");
}
}
}
/*
* MARTE2Cheddar tranformation interface
* WARNING : 1 MARTE model to * CHEDDAR model(s)
*/
/**
* Inserts the Cheddar DTD in a XML String model.
* @return XML String model with the cheddar DTD inserted after the first ligne
* @param xmlstr Model as XML string
* @throws IOException
*/
protected String AddCheddarDTD(String xmlstr){
String cheddarxmldtd = "";
URL dtd_url = MARTE2CheddarXML.class.getResource(DTD_PATH);
FileInputStream fis = null;
try {
fis = (FileInputStream) dtd_url.openStream();
int c;
do {
c = fis.read();
if(c != -1) {
//if(c != 1) {
cheddarxmldtd += (char)c;
}
} while( c != -1);
//cheddarxmldtd = cheddarxmldtd.substring(0, cheddarxmldtd.length()-1);
} catch (Exception exception) {
Log.errorMessage(Activator.PLUGIN_ID,exception.getMessage(),exception);
}
int split = xmlstr.indexOf("\n");
String cheddarxmlheader = xmlstr.substring(0, split);
String cheddarxmlmodel = xmlstr.substring(split);
String xmlcopy = "\n\n<!-- " + COPYRIGHT + "-->\n";
String xmlgenerationinfo = "<!-- Generated " + new Date().toString() + "-->\n";
String result = cheddarxmlheader.concat(xmlcopy).concat(xmlgenerationinfo).concat(cheddarxmldtd).concat(cheddarxmlmodel);
return result;
}
protected List<String> getAnalysisContexts(IFile infile) {
List<String> AnaCont = new ArrayList<String>();
ResourceSet rs = ASMEMFModel.getResourceSet();
Resource r = rs.getResource(URI.createPlatformResourceURI(infile.getFullPath().toString()), true);
EList ol = r.getContents();
for(Object o : ol ) {
if (o instanceof Model) {
Model m = (Model) o;
EList moel = m.allOwnedElements();
for(Object obj : moel) {
if (obj instanceof NamedElement) {
NamedElement elem = (NamedElement) obj;
if(elem.getAppliedStereotype("GQAM::GaAnalysisContext") != null ||
elem.getAppliedStereotype("SAM::SaAnalysisContext") != null) {
AnaCont.add(elem.getQualifiedName());
}
}
}
}
}
return AnaCont;
}
/**
* Runs the MARTE 2 Cheddar transformation and export the result as an IFile
* @param infile IFile containing the input model
* @param outfile Destination IFile
* @return true if only one context was exported, false otherwise
* @throws IOException
* @throws URISyntaxException
* @throws CoreException
* @throws ATLTransformException
*/
public int MARTE2Cheddar(IFile infile) throws IOException, CoreException, ATLTransformException{
List<String> ac = getAnalysisContexts(infile);
for(String s : ac) {
_MARTE2Cheddar(infile, s);
String[] p = s.split("::");
IFile outfile = MyEclipseSwissTools.ChangeFileExtension(infile, p[p.length-1]+".cheddar");
exportModel("OUT", outfile);
removeModel("OUT");
}
return ac.size();
}
/**
* Runs the MARTE 2 Cheddar transformation
* @param infile IFile containing the input model
* @return the number of resulting Cheddar models
* @throws IOException
* @throws CoreException
* @throws ATLTransformException
*/
protected void _MARTE2Cheddar(IFile infile, String qualifiedname) throws IOException, CoreException, ATLTransformException{
addUMLInputModel("IN", infile);
createModel("OUT", "CHEDDAR");
reset_names();
addXMLParameter("targetElement", qualifiedname);
//addXMLParameter("enableVERBOSE", "true");
//addXMLParameter("enableDEBUG", "true");
run(transfo_m2c);
removeModel("IN");
clearXMLParameters();
// The result is left in the model OUT which MUST be handled
}
/*
* Cheddar2XML
* 1 Cheddar model to 1 Cheddar XML model
*/
/**
* Runs the Cheddar 2 CheddarXML transformation and export the result as an IFile
* @param infile Source IFile
* @param outfile Destination IFile
*/
public void Cheddar2XML(IFile infile, IFile outfile) throws IOException, CoreException, ATLTransformException {
addInputModel("IN", "CHEDDAR", infile);
Cheddar2XML(outfile);
}
/**
* Runs the Cheddar 2 CheddarXML transformation on an existing "IN" model
* @param outfile Destination IFile
* @throws IOException
* @throws CoreException
* @throws ATLTransformException
*/
protected void Cheddar2XML(IFile outfile) throws IOException, CoreException, ATLTransformException {
if(!models.containsKey("IN")) throw new UndefinedException("IN");
createXMLModel("OUT");
run(transfo_c2x);
String xmlxmlstr = exportModelAsXMLToString("OUT");
removeModel("OUT");
removeModel("IN");
String xmlstr = AddCheddarDTD(xmlxmlstr);
exportStringToIFile(xmlstr, outfile);
}
protected void init(IFile infile, boolean export) {
this.export = export;
this.inputFile = infile;
}
protected void init(IFile infile, String qualifiedName, boolean export) {
this.export = export;
this.inputFile = infile;
this.qualifiedName = qualifiedName;
}
/**
* Initialisation for Exporting the infile
* @param infile
*/
public void initExport(IFile infile) {
init(infile,true);
}
/**
* Initialisation for Exporting the qualifiedName Analysis Context of infile
* @param infile
* @param qualifiedName
*/
public void initExport(IFile infile, String qualifiedName) {
init(infile,qualifiedName,true);
}
/**
* Initialisation for Checking the infile
* @param infile
*/
public void initCheck(IFile infile) {
init(infile,false);
}
/**
* Initialisation for Exporting the qualifiedName Analysis Context of infile
* @param infile
* @param qualifiedName
*/
public void initCheck(IFile infile, String qualifiedName) {
init(infile,qualifiedName,false);
}
/**
* Get back the generated output file
* @return
*/
public IFile result() {
return this.outputFile;
}
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
String message;
if (export) {
message = "Transformation of a UML-MARTE model in cheddar XML format files";
} else {
message = "UML-MARTE Model checking for an export to cheddar XML format";
}
monitor.beginTask(message, IProgressMonitor.UNKNOWN);
this.outputFile = null;
if (inputFile!=null) {
// Export
if (export) {
if (qualifiedName==null) {
this.outputFile=run(this.inputFile);
} else {
this.outputFile=run(this.inputFile, this.qualifiedName);
}
}
// Check
else {
if (qualifiedName==null) {
check(this.inputFile);
} else {
check(this.inputFile, this.qualifiedName);
}
}
}
this.inputFile = null;
this.qualifiedName = null;
monitor.done();
if (monitor.isCanceled())
throw new InterruptedException("Cancelled");
}
catch (Exception exception) {
String message = "Error during the transformation process";
Log.errorMessage(Activator.PLUGIN_ID, message, exception);
}
}
/*
* MARTE2CheddarXML
*/
public IFile run(IFile infile) throws IOException, CoreException, ATLTransformException {
List<String> ac = getAnalysisContexts(infile);
IFile outfile = null;
for(String s : ac) {
outfile = run(infile, s);
}
if(ac.size() == 1) return outfile;
else return null;
}
public IFile run(IFile infile, String qualifiedname) throws IOException, CoreException, ATLTransformException {
String[] p = qualifiedname.split("::");
IFile outfile = MyEclipseSwissTools.ChangeFileExtension(infile, p[p.length-1]+".xml");
_MARTE2Cheddar(infile, qualifiedname);
Resource res = getResourceModel("OUT");
CheddarImprover.improve(res);
renameModel("OUT", "IN");
Cheddar2XML(outfile);
return outfile;
}
/*
* Check
*/
/**
* Clean the error markers attached to the input IFile resource
* @param infile IFile
*/
public void cleanErrors(IFile infile) throws IOException, CoreException, ATLTransformException {
ExtendedATLProblemsManager.cleanProblems(infile, true);
}
/**
* Check is the input model is valid for MARTE 2 Cheddar transformation
* @param file IFile containing the input model
* @throws IOException
* @throws CoreException
* @throws ATLTransformException
*/
public void check(IFile file) throws IOException, CoreException, ATLTransformException {
apm.cleanProblems(file);
for(String s : getAnalysisContexts(file)) {
Log.verboseMessage(Activator.PLUGIN_ID,"Analyzing : " + s);
_check(file, s);
}
}
public void check(IFile file, String qualifiedname) throws IOException, CoreException, ATLTransformException {
apm.cleanProblems(file);
_check(file, qualifiedname);
}
protected void _check(IFile file, String qualifiedname) throws IOException, CoreException, ATLTransformException {
addUMLInputModel("IN", file);
createProblemModel("OUT");
reset_names();
addXMLParameter("targetElement", qualifiedname);
//addXMLParameter("enableVERBOSE", "true");
//addXMLParameter("enableDEBUG", "true");
run(transfo_check);
clearXMLParameters();
apm.importModel(getResourceModel("OUT"));
Log.verboseMessage(Activator.PLUGIN_ID, "[" + new Date().toString() + "] " + file.getFullPath().toString() + " :");
Log.verboseMessage(Activator.PLUGIN_ID, (apm.getCritic_count()+ apm.getError_count()) + " error(s)"
+ " and " + apm.getWarning_count() + "warning(s)/info(s)");
apm.signalProblems(file);
removeModel("IN");
removeModel("OUT");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -