SysMLToSMACHHandler.java
Go to the documentation of this file.
00001 package org.best_of_robotics.transform.sysml_sm.to.smach.handler;
00002 
00003 import java.io.FileOutputStream;
00004 import java.io.IOException;
00005 import java.util.Collections;
00006 import java.util.HashMap;
00007 
00008 import org.best_of_robotics.transform.sysml_sm.to.smach.Activator;
00009 import org.best_of_robotics.transform.service.ITransformService;
00010 import org.best_of_robotics.transform.service.access.TransformAccessActivator;
00011 import org.best_of_robotics.transform.service.parameter.IEtlTransformParameter;
00012 import org.best_of_robotics.transform.service.parameter.TransformParameterFactory;
00013 import org.eclipse.core.commands.AbstractHandler;
00014 import org.eclipse.core.commands.ExecutionEvent;
00015 import org.eclipse.core.commands.ExecutionException;
00016 import org.eclipse.core.resources.IFile;
00017 import org.eclipse.emf.common.util.URI;
00018 import org.eclipse.emf.ecore.resource.Resource;
00019 import org.eclipse.emf.ecore.resource.ResourceSet;
00020 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
00021 import org.eclipse.emf.ecore.xmi.XMIResource;
00022 import org.eclipse.jface.dialogs.MessageDialog;
00023 import org.eclipse.jface.viewers.ISelection;
00024 import org.eclipse.jface.viewers.ITreeSelection;
00025 import org.eclipse.swt.SWT;
00026 import org.eclipse.swt.widgets.FileDialog;
00027 import org.eclipse.ui.IEditorInput;
00028 import org.eclipse.ui.IEditorPart;
00029 import org.eclipse.ui.IWorkbenchWindow;
00030 import org.eclipse.ui.PlatformUI;       
00031 import org.eclipse.ui.handlers.HandlerUtil;
00032 import org.eclipse.papyrus.infra.onefile.model.IPapyrusFile;
00033 import org.eclipse.xtext.resource.XtextResource;
00034 //import org.best.of.robotics.SmachDSLStandaloneSetup;
00035 import org.eclipse.ui.IFileEditorInput;
00036 
00037 import com.google.inject.Injector;
00038 
00039 
00040 public class SysMLToSMACHHandler extends AbstractHandler {
00041 
00042         public SysMLToSMACHHandler() {
00043         }
00044 
00045         @Override
00046         public Object execute(ExecutionEvent event) throws ExecutionException {
00047                 
00048                 IFile sourceModelFilePath = null;
00049                 IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
00050                 if (editor != null) {
00051             IEditorInput input = editor.getEditorInput();
00052             if (input instanceof IFileEditorInput) {
00053                 sourceModelFilePath = ((IFileEditorInput)input).getFile();
00054                 System.out.println("File from editor: " + sourceModelFilePath.getLocation().toOSString());
00055                 System.out.println("getFileExtension: " + sourceModelFilePath.getFileExtension().toString().compareTo("di"));
00056                 if(sourceModelFilePath.getFileExtension().toString().compareTo("di")!=0)
00057                 {
00058                         sourceModelFilePath = null;
00059                         MessageDialog
00060                                         .openError(
00061                                                         PlatformUI.getWorkbench()
00062                                                                         .getActiveWorkbenchWindow().getShell(),
00063                                                         "Error on Editor Selection",
00064                                                         "Please select the editor from which you want to generate code and execute command again.");
00065                 }
00066                 
00067             }
00068         }
00069                 if(sourceModelFilePath == null)
00070                         return null;
00071                                 
00072                 //configure new transform parameter
00073                 IEtlTransformParameter etlTransformParameter = TransformParameterFactory.createEtlTransformParameter();
00074                 etlTransformParameter.setTransformName("SYSML to SMACH");
00075                 etlTransformParameter.setEtlTransformPath("epsilon/sysml_sm_to_smach.etl");
00076                 etlTransformParameter.setPluginID(Activator.PLUGIN_ID);
00077                 etlTransformParameter.setSourceMetaModelURI("http://www.eclipse.org/papyrus/0.7.0/SysML/StateMachines");
00078                 etlTransformParameter.setSourceModelFilePath(sourceModelFilePath.getLocation().removeFileExtension().toOSString() + ".uml");
00079                 etlTransformParameter.setSourceName("Source");
00080                 etlTransformParameter.setSourceReadOnLoad(true);
00081                 etlTransformParameter.setSourceStoreOnDisposal(false);
00082                 etlTransformParameter.setTargetMetaModelURI("http://ros/1.0");
00083                 
00084                 String targetModelFilePath = sourceModelFilePath.getLocation().removeFileExtension().toOSString() + "gen.ros_coordinator";
00085                 
00086                 etlTransformParameter.setTargetModelFilePath(targetModelFilePath);
00087                 etlTransformParameter.setTargetName("Target");
00088                 etlTransformParameter.setTargetReadOnLoad(false);
00089                 etlTransformParameter.setTargetStoreOnDisposal(true);
00090                 
00091                 //get transform service
00092                 
00093                 ITransformService transformService = TransformAccessActivator.getDefault().getTransformService();
00094                 
00095                 //do transform
00096                 transformService.doTransform(etlTransformParameter);
00097                 
00098                                 
00099                 //now convert XMI to DSL file
00100                 /*Injector inj = new SmachDSLStandaloneSetup().createInjectorAndDoEMFRegistration();
00101                 ResourceSet resourceSet = inj.getInstance(ResourceSet.class);
00102                 System.out.println("loading");
00103                 XMIResource xmi_resource = (XMIResource) resourceSet.getResource(URI.createURI("file://"+targetModelFilePath), true);
00104                 System.out.println("loaded");
00105                 XtextResource mydsl_resource = (XtextResource) resourceSet.createResource(URI.createURI("file://" + sourceModelFilePath.getLocation().removeFileExtension().toOSString() + ".smach_model"));
00106                 System.out.println("created xmi");
00107                 System.out.println(xmi_resource.getContents().get(0).toString());
00108                 mydsl_resource.getContents().add(xmi_resource.getContents().get(0));
00109                 System.out.println("Converted");
00110                 System.out.println(mydsl_resource.getContents().toString());
00111                 
00112                 try {
00113                         mydsl_resource.save(Collections.EMPTY_MAP);
00114                         System.out.println("Saved to: " + "file://" + sourceModelFilePath.getLocation().removeFileExtension().toOSString() + ".smach_model" );
00115                 } catch (Exception e) {
00116                         // TODO Auto-generated catch block
00117                         System.out.println("Didnt Work");
00118                         e.printStackTrace();
00119                 }
00120                 */
00121                 //now create diagram file
00122                 
00123                 return null;
00124         }
00125 
00126 }


bride_plugin_source
Author(s): Alexander Bubeck
autogenerated on Sun Oct 5 2014 22:38:34