⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 marte2cheddarxml.java

📁 papyrus插件MARTE例子,uml建模
💻 JAVA
字号:
package com.thalesgroup.cheddar.MARTE2cheddar.tools;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;

import org.atl.engine.repositories.emf4atl.ASMEMFModel;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;

import com.thalesgroup.atl.ExtendedATLProblemsManager;
import com.thalesgroup.atl.ExtendedATLTransform;
import com.thalesgroup.cheddar.MARTE2cheddar.Activator;
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.2, 13/09/2007
 *
 */
public class MARTE2CheddarXML extends ExtendedATLTransform {

	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;
	
	/**
	 * Retrieves the singleton instance.
	 * @return the instance of MARTE2CheddarXML
	 */
	public static MARTE2CheddarXML getInstance() {
		if(instance != null) {
			return instance;
		} else {
			try {
				instance = new MARTE2CheddarXML();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return instance;
		}
	}
	/**
	 * Constructeur.
	 * (Singleton - for internal use only).
	 * @throws IOException
	 */
	protected MARTE2CheddarXML() throws IOException {
		super(null);
		try {
			enableLOG();
			enableMARTE(); // should enable UML in the same movement
			enableXML();
			enablePROBLEM();
			enableXMLParameters();
			addMetamodelByURI("CHEDDAR", "http:///cheddar.ecore");
			addLibrary("MARTE4CHEDDAR", MARTE2CheddarXML.class.getResource(BASE_PATH+"MARTE4CHEDDAR.asm"));
		}
		catch (Exception exception) {
			Log.errorMessage(Activator.PLUGIN_ID,"Error in MARTE2Cheddar transformation",exception);
		}
	}
	

	/*
	 * 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) {
					cheddarxmldtd += (char)c;
				}
			} while( c != -1);
			
			cheddarxmldtd = cheddarxmldtd.substring(0, cheddarxmldtd.length()-1);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}

		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;
	}
	
	/**
	 * Select the target element of the transformation if there is more than one Analysis Context.
	 * @param qualifiedname Qualified name of the Analysis Context that will be the transformation target
	 * @throws ATLTransformException
	 * @throws IOException
	 */
	public void setTargetElement(String qualifiedname) throws ATLTransformException, IOException{
		addXMLParameter("targetElement", qualifiedname);
	}
	
	/**
	 * Splits the input model into as many models as needed to
	 * have one RootImpl in each one.
	 * The input model is destroyed after the "basename%d"%i
	 * (with i going from 1 to the count of submodels)
	 * models have been created.
	 * @param basename Input model to be splitted
	 * @return the number of models created
	 */
	@SuppressWarnings("unchecked")
	protected int splitCheddarModel(String basename) {
		
		int model_count = 0;
		
        try {
        	
        	ResourceSet resourceSet = ASMEMFModel.getResourceSet();
    		Resource resource = resourceSet.getResource(URI.createURI(basename), false);;
    		
    		EList sub_models = resource.getContents();
    		model_count = sub_models.size();
    		
    		for(int i = 0; i < model_count; i++) {
    
    			XMIResourceImpl t = new XMIResourceImpl();
    			
    			t.getContents().add(sub_models.get(0));
    			
    			ByteArrayOutputStream baos = new ByteArrayOutputStream();
    			t.save(baos, null);
    			baos.flush();
    			
    			addInputModel(basename+(i+1), "CHEDDAR", new ByteArrayInputStream(baos.toByteArray()));
    		}
    		
    		removeModel(basename);
 
    		
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		return model_count;

	}
	
	/**
	 * 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
	 */
	/*
	 * WARNING : transforming a model with multiple Analysis Context without targetting
	 * one element can lead to name collision and invalid results 
	 */ 
	public boolean MARTE2Cheddar(IFile infile, IFile outfile) throws IOException, URISyntaxException, CoreException, ATLTransformException{
		int model_count = MARTE2Cheddar(infile);
		if(model_count > 1) {
			int model_count2 = splitCheddarModel("OUT");
			
			if(model_count == model_count2) {
				for(int i = 1; i <= model_count; i++){
					String submodelname = "OUT"+i;
					IFile moutfile = MyEclipseSwissTools.ChangeFileExtension(outfile, i+".cheddar");
					exportModel(submodelname, moutfile);
					removeModel(submodelname);
				}
			} else {
				System.out.println("Some errors occured while splitting the model ...");
			}
		} else {
			exportModel("OUT", outfile);
			removeModel("OUT");
		}
		return (model_count==1);
	}

	/**
	 * 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
	 */
	/*
	 * WARNING : transforming a model with multiple Analysis Context without targetting
	 * one element can lead to name collision and invalid results 
	 */ 
	// TODO fix the name collision potential problem
	protected int MARTE2Cheddar(IFile infile) throws IOException, CoreException, ATLTransformException{
		// Runs the transformation
		addUMLInputModel("IN", infile);
		createModel("OUT", "CHEDDAR");
		run(transfo_m2c);
		removeModel("IN");
		
		// The number of resulting models is the count of RootImpl object in the resource
		Resource resource = getResourceModel("OUT");
		return resource.getContents().size();
	}
	
	
	
	/*
	 * 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);
	}
	
	/*
	 * MARTE2CheddarXML
	 */

	public boolean run(IFile infile, IFile outfile) throws IOException, CoreException, ATLTransformException {
		int model_count = MARTE2Cheddar(infile);
		if(model_count > 1) {
			int model_count2 = splitCheddarModel("OUT");
			
			if(model_count == model_count2) {
				for(int i = 1; i <= model_count; i++){
					String submodelname = "OUT"+i;
					Resource res = getResourceModel(submodelname);
					CheddarImprover.improve(res);
					renameModel(submodelname, "IN");
					IFile moutfile = MyEclipseSwissTools.ChangeFileExtension(outfile, i+".xml");
					Cheddar2XML(moutfile);
				}
			} else {
				System.out.println("Some errors occured while splitting the model ...");
			}
		} else {
			Resource res = getResourceModel("OUT");
			CheddarImprover.improve(res);
			renameModel("OUT", "IN");
			Cheddar2XML(outfile);
		}
		return (model_count==1);
	}
	
	/*
	 * 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 {
		addUMLInputModel("IN", file);
		createProblemModel("OUT");
		
		run(transfo_check);
		
		apm.importModel(getResourceModel("OUT"));
		
		System.out.print("[" + new Date().toString() + "] " + file.getFullPath().toString() + " :");
		
		System.out.println((apm.getCritic_count()+ apm.getError_count()) + " error(s)"
				+ " and " + apm.getWarning_count() + "warning(s)/info(s)");
		
		apm.cleanProblems(file);
		apm.signalProblems(file);
		
		removeModel("IN");
		removeModel("OUT");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -