NEW - bug 403048: Basic Generation of BPMN2 instances from Mangrove Core 
https://bugs.eclipse.org/bugs/show_bug.cgi?id=403048
diff --git a/org.eclipse.soa.mangrove.branches/trunk/.classpath b/org.eclipse.soa.mangrove.branches/trunk/.classpath
new file mode 100644
index 0000000..8a8f166
--- /dev/null
+++ b/org.eclipse.soa.mangrove.branches/trunk/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>

+<classpath>

+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>

+	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>

+	<classpathentry kind="src" path="src"/>

+	<classpathentry kind="output" path="bin"/>

+</classpath>

diff --git a/org.eclipse.soa.mangrove.branches/trunk/.project b/org.eclipse.soa.mangrove.branches/trunk/.project
new file mode 100644
index 0000000..34c92a1
--- /dev/null
+++ b/org.eclipse.soa.mangrove.branches/trunk/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>

+<projectDescription>

+	<name>org.eclipse.soa.mangrove.out.bpmn2</name>

+	<comment></comment>

+	<projects>

+	</projects>

+	<buildSpec>

+		<buildCommand>

+			<name>org.eclipse.jdt.core.javabuilder</name>

+			<arguments>

+			</arguments>

+		</buildCommand>

+		<buildCommand>

+			<name>org.eclipse.pde.ManifestBuilder</name>

+			<arguments>

+			</arguments>

+		</buildCommand>

+		<buildCommand>

+			<name>org.eclipse.pde.SchemaBuilder</name>

+			<arguments>

+			</arguments>

+		</buildCommand>

+	</buildSpec>

+	<natures>

+		<nature>org.eclipse.pde.PluginNature</nature>

+		<nature>org.eclipse.jdt.core.javanature</nature>

+	</natures>

+</projectDescription>

diff --git a/org.eclipse.soa.mangrove.branches/trunk/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.soa.mangrove.branches/trunk/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..f287d53
--- /dev/null
+++ b/org.eclipse.soa.mangrove.branches/trunk/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1

+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled

+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6

+org.eclipse.jdt.core.compiler.compliance=1.6

+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error

+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error

+org.eclipse.jdt.core.compiler.source=1.6

diff --git a/org.eclipse.soa.mangrove.branches/trunk/META-INF/MANIFEST.MF b/org.eclipse.soa.mangrove.branches/trunk/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..8463e1a
--- /dev/null
+++ b/org.eclipse.soa.mangrove.branches/trunk/META-INF/MANIFEST.MF
@@ -0,0 +1,15 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Mangrove to BPMN2 Transformation
+Bundle-SymbolicName: org.eclipse.soa.mangrove.out.bpmn2; singleton:=true
+Bundle-Version: 0.0.1.qualifier
+Bundle-Activator: org.eclipse.soa.mangrove.out.bpmn2.Activator
+Bundle-Vendor: Xerox Research
+Require-Bundle: org.eclipse.ui,
+ org.eclipse.core.runtime,
+ org.eclipse.soa.mangrove;bundle-version="1.0.0",
+ org.eclipse.soa.mangrove.branches.common;bundle-version="0.5.0",
+ org.eclipse.bpmn2;bundle-version="0.7.0"
+Bundle-RequiredExecutionEnvironment: JavaSE-1.6
+Bundle-ActivationPolicy: lazy
+Import-Package: org.eclipse.core.resources
diff --git a/org.eclipse.soa.mangrove.branches/trunk/build.properties b/org.eclipse.soa.mangrove.branches/trunk/build.properties
new file mode 100644
index 0000000..6f20375
--- /dev/null
+++ b/org.eclipse.soa.mangrove.branches/trunk/build.properties
@@ -0,0 +1,5 @@
+source.. = src/

+output.. = bin/

+bin.includes = META-INF/,\

+               .,\

+               plugin.xml

diff --git a/org.eclipse.soa.mangrove.branches/trunk/plugin.xml b/org.eclipse.soa.mangrove.branches/trunk/plugin.xml
new file mode 100644
index 0000000..a5cc408
--- /dev/null
+++ b/org.eclipse.soa.mangrove.branches/trunk/plugin.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>

+<?eclipse version="3.4"?>

+<plugin>

+   <extension

+         point="org.eclipse.ui.popupMenus">

+      <objectContribution

+            id="org.eclipse.soa.mangrove.out.bpmn2.contribution"

+            nameFilter="*.mangrove"

+            objectClass="org.eclipse.core.resources.IFile">

+         <action

+               label="Generate BPMN2"

+               class="org.eclipse.soa.mangrove.out.bpmn2.popup.actions.GenerateBPMN2Action"

+               menubarPath="org.eclipse.soa.mangrove.branches.common.mainMenu/mainGroup"

+               enablesFor="1"

+               id="org.eclipse.soa.mangrove.out.bpmn2.generateAction">

+         </action>

+      </objectContribution>

+   </extension>

+</plugin>

diff --git a/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/Activator.java b/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/Activator.java
new file mode 100644
index 0000000..359dde5
--- /dev/null
+++ b/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/Activator.java
@@ -0,0 +1,50 @@
+package org.eclipse.soa.mangrove.out.bpmn2;

+

+import org.eclipse.ui.plugin.AbstractUIPlugin;

+import org.osgi.framework.BundleContext;

+

+/**

+ * The activator class controls the plug-in life cycle

+ */

+public class Activator extends AbstractUIPlugin {

+

+	// The plug-in ID

+	public static final String PLUGIN_ID = "org.eclipse.soa.mangrove.out.bpmn2"; //$NON-NLS-1$

+

+	// The shared instance

+	private static Activator plugin;

+	

+	/**

+	 * The constructor

+	 */

+	public Activator() {

+	}

+

+	/*

+	 * (non-Javadoc)

+	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)

+	 */

+	public void start(BundleContext context) throws Exception {

+		super.start(context);

+		plugin = this;

+	}

+

+	/*

+	 * (non-Javadoc)

+	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)

+	 */

+	public void stop(BundleContext context) throws Exception {

+		plugin = null;

+		super.stop(context);

+	}

+

+	/**

+	 * Returns the shared instance

+	 *

+	 * @return the shared instance

+	 */

+	public static Activator getDefault() {

+		return plugin;

+	}

+

+}

diff --git a/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/popup/actions/GenerateBPMN2Action.java b/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/popup/actions/GenerateBPMN2Action.java
new file mode 100644
index 0000000..63b2e96
--- /dev/null
+++ b/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/popup/actions/GenerateBPMN2Action.java
@@ -0,0 +1,65 @@
+/*******************************************************************************
+ * Copyright (c) {2013} Xerox Corp.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors: Adrian Mos (Xerox Corp.) - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.soa.mangrove.out.bpmn2.popup.actions;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.soa.mangrove.out.bpmn2.transform.Mangrove2BPMN2Transformer;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IActionDelegate;
+import org.eclipse.ui.IObjectActionDelegate;
+import org.eclipse.ui.IWorkbenchPart;
+
+public class GenerateBPMN2Action implements IObjectActionDelegate {
+
+    private IFile selectedIMFile = null;
+    private Mangrove2BPMN2Transformer bpmn2Transformer;
+    private Shell shell;
+
+	public GenerateBPMN2Action() {
+		super();
+        shell = new Shell();
+	}
+
+	/**
+	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
+	 */
+	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
+	}
+
+	/**
+	 * @see IActionDelegate#run(IAction)
+	 */
+	public void run(IAction action) {
+        if (null != this.selectedIMFile) {
+            //load the IM from the selected file (full path includes the workspace location)
+            IPath location = Platform.getLocation();
+            //System.out.println("Workspace Location: " + location.toFile().getAbsolutePath());
+            URI uri = URI.createFileURI(location.toString() + this.selectedIMFile.getFullPath().toString());
+            IContainer container = this.selectedIMFile.getParent();
+            this.bpmn2Transformer = new Mangrove2BPMN2Transformer(shell, uri);
+            this.bpmn2Transformer.createBPMN2fromCore(container);
+        }
+	}
+
+    /**
+     * @see IActionDelegate#selectionChanged(IAction, ISelection)
+     */
+    public void selectionChanged(IAction action, ISelection selection) {
+        this.selectedIMFile = ((IFile) ((StructuredSelection) selection).getFirstElement());
+    }
+
+}
diff --git a/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/transform/BPMN2Handler.java b/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/transform/BPMN2Handler.java
new file mode 100644
index 0000000..f5f08fe
--- /dev/null
+++ b/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/transform/BPMN2Handler.java
@@ -0,0 +1,131 @@
+/*******************************************************************************

+ * Copyright (c) {2013} Xerox Corp.

+ * All rights reserved. This program and the accompanying materials

+ * are made available under the terms of the Eclipse Public License v1.0

+ * which accompanies this distribution, and is available at

+ * http://www.eclipse.org/legal/epl-v10.html

+ *

+ * Contributors: Adrian Mos (Xerox Corp.) - initial API and implementation

+ *******************************************************************************/

+

+package org.eclipse.soa.mangrove.out.bpmn2.transform;

+

+import java.io.IOException;

+import java.util.Collections;

+

+import org.eclipse.bpmn2.*;

+import org.eclipse.bpmn2.Process;

+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.resource.impl.ResourceSetImpl;

+import org.eclipse.emf.ecore.util.EcoreUtil;

+

+/**

+ * Creates handler methods for dealing with the BPMN2 Metamodel

+ * @author Adrian Mos

+ * */

+public class BPMN2Handler {

+

+	private DocumentRoot bpmn2Root;

+	private Definitions definitions;

+	private Bpmn2Factory bpmn2Factory;

+	

+	public BPMN2Handler() {

+		bpmn2Factory = Bpmn2Factory.eINSTANCE;

+		bpmn2Root = bpmn2Factory.createDocumentRoot();

+		definitions = bpmn2Factory.createDefinitions();

+		bpmn2Root.setDefinitions(definitions);

+	}

+	

+	public Process createProcess(String name){

+		Process p = bpmn2Factory.createProcess();

+		p.setName(name);

+		p.setId(name.replaceAll("\\s", "") + "_ID"); //sets a generated ID

+		bpmn2Root.getDefinitions().getRootElements().add(p);

+		return p;

+	}

+	

+	public StartEvent createStartEvent(Process p){

+		StartEvent se = bpmn2Factory.createStartEvent();

+		se.setName("Generated Start Event");

+		p.getFlowElements().add(se);

+		return se;

+	}

+	

+	public EndEvent createEndEvent(Process p){

+		EndEvent ee = bpmn2Factory.createEndEvent();

+		ee.setName("Generated End Event");

+		p.getFlowElements().add(ee);

+		return ee;

+	}

+	

+	public SequenceFlow connectElements (Process p, FlowNode source, FlowNode target){

+		SequenceFlow flow = bpmn2Factory.createSequenceFlow();

+		p.getFlowElements().add(flow);

+		flow.setSourceRef(source);

+		flow.setTargetRef(target);

+		return flow;

+	}

+	

+	public Task createTask(Process p, String name){

+		Task task = bpmn2Factory.createTask();

+		task.setName(name);

+		task.setId(name.replaceAll("\\s", ""));

+		p.getFlowElements().add(task);

+		return task;

+	}

+	

+	public ServiceTask createServiceTask(Process p, String name, String serviceName){

+		ServiceTask task = bpmn2Factory.createServiceTask();

+		task.setName(name);

+		task.setId(name.replaceAll("\\s", "") + "_" + EcoreUtil.generateUUID());

+		task.setImplementation(serviceName);

+		p.getFlowElements().add(task);

+		return task;

+	}

+	

+	public ManualTask createManualTask(Process p, String name){

+		ManualTask task = bpmn2Factory.createManualTask();

+		task.setName(name);

+		task.setId(name.replaceAll("\\s", "") + "_" + EcoreUtil.generateUUID());

+		p.getFlowElements().add(task);

+		return task;

+	}

+	

+	public ExclusiveGateway createExGateway(Process p, String name){

+		ExclusiveGateway task = bpmn2Factory.createExclusiveGateway();

+		task.setName(name);

+		task.setId(name.replaceAll("\\s", "") + "_" + EcoreUtil.generateUUID());

+		p.getFlowElements().add(task);

+		return task;

+	}

+

+	public LaneSet createLaneSet(Process p, String name){

+		LaneSet set = bpmn2Factory.createLaneSet();

+		set.setId(name.replaceAll("\\s", ""));

+		set.setName(name);

+		p.getLaneSets().add(set);

+		return set;

+	}

+	

+	public Lane createLane(LaneSet set, String name){

+		Lane lane = bpmn2Factory.createLane();

+		lane.setId(name.replaceAll("\\s", name));

+		lane.setName(name);

+		set.getLanes().add(lane);

+		

+		return lane;

+	}

+	

+	

+	public void persistBPMN2File(URI uri) throws IOException{

+        ResourceSet rs = new ResourceSetImpl();

+

+        Resource resource = rs.createResource(uri, "org.eclipse.bpmn2.content-type.xml");

+        resource.getContents().add(this.definitions);

+        

+        resource.save(Collections.EMPTY_MAP);

+	}

+	

+}
\ No newline at end of file
diff --git a/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/transform/Mangrove2BPMN2Transformer.java b/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/transform/Mangrove2BPMN2Transformer.java
new file mode 100644
index 0000000..b8cd029
--- /dev/null
+++ b/org.eclipse.soa.mangrove.branches/trunk/src/org/eclipse/soa/mangrove/out/bpmn2/transform/Mangrove2BPMN2Transformer.java
@@ -0,0 +1,106 @@
+/*******************************************************************************

+ * Copyright (c) {2013} Xerox Corp.

+ * All rights reserved. This program and the accompanying materials

+ * are made available under the terms of the Eclipse Public License v1.0

+ * which accompanies this distribution, and is available at

+ * http://www.eclipse.org/legal/epl-v10.html

+ *

+ * Contributors: Adrian Mos (Xerox Corp.) - initial API and implementation

+ *******************************************************************************/

+package org.eclipse.soa.mangrove.out.bpmn2.transform;

+

+import java.io.IOException;

+import java.util.HashMap;

+import java.util.Iterator;

+import java.util.Set;

+

+import org.eclipse.core.resources.IContainer;

+import org.eclipse.core.runtime.CoreException;

+import org.eclipse.emf.common.util.EList;

+import org.eclipse.emf.common.util.URI;

+import org.eclipse.soa.mangrove.Process;

+import org.eclipse.soa.mangrove.Step;

+import org.eclipse.soa.mangrove.Transition;

+import org.eclipse.soa.mangrove.branches.common.Core2AnyTransformer;

+import org.eclipse.swt.widgets.Shell;

+import org.eclipse.bpmn2.*;

+

+public class Mangrove2BPMN2Transformer extends Core2AnyTransformer {

+

+	private BPMN2Handler bpmn2Handler;

+	private HashMap<Step, FlowNode> stepsToBPMN;

+	

+	public Mangrove2BPMN2Transformer(Shell shell, URI uri) {

+		super(shell, uri);

+		this.bpmn2Handler = new BPMN2Handler();

+		stepsToBPMN = new HashMap<Step, FlowNode>();

+	}

+

+	public void createBPMN2fromCore(IContainer container) {

+		

+		//this version creates a single BPMN2 Process with multiple lanes

+		org.eclipse.bpmn2.Process p = bpmn2Handler.createProcess("Generated from Mangrove");

+		StartEvent start = bpmn2Handler.createStartEvent(p);		

+		//one Lane Set to contain the several lanes

+		LaneSet laneSet = bpmn2Handler.createLaneSet(p, "Mangrove Generated LaneSet");

+		

+		//for each Mangrove process, create a new lane in BPMN2 and add it to the lane set

+		for(Process imProc : im.getProcessCollection().getProcesses()){

+			Lane lane = bpmn2Handler.createLane(laneSet, imProc.getName());

+			

+			//now add all the steps in the Mangrove process as elements of the lane

+			for(Step step : imProc.getSteps()){

+				

+				//first create the element as part of the BPMN2 process

+				FlowNode node = null;

+				if(step.getName().startsWith("IF ")) node = bpmn2Handler.createExGateway(p, step.getName());

+				else if(imProc.getName().equals("SYSTEM")) {

+					String serviceName = null;

+					if(null!=step.getServiceModel()) serviceName = step.getServiceModel().getServiceName();

+					//System.out.println("ServiceName = " + serviceName);

+					node = bpmn2Handler.createServiceTask(p, step.getName(), serviceName);

+				}

+				else node = bpmn2Handler.createManualTask(p, step.getName());

+				//now add the element to the lane

+				//System.out.println("Added node " + node.getName() + " to lane: " + lane.getName());

+				lane.getFlowNodeRefs().add(node);

+				//now save the mapping for later on when creating the sequence flows

+				stepsToBPMN.put(step, node);

+				//System.out.println("Adding mapping " + step + " - " + node);

+			}

+			

+			if(lane.getFlowNodeRefs().isEmpty()) laneSet.getLanes().remove(lane);

+		}

+		//bpmn2Handler.connectElements(p, start, end);

+

+		//create all the BPMN sequence flows

+		Iterator<Step> stepsIter = stepsToBPMN.keySet().iterator();

+		while(stepsIter.hasNext()){

+			Step s = stepsIter.next();

+			System.out.println("Processing step " + s.getName());

+			

+			EList<Transition> transitions = s.getSourceTransitions();

+			Iterator<Transition> transIter = transitions.iterator();

+			while (transIter.hasNext()){

+				Transition t = transIter.next();

+				System.out.println("Processing Transition " + t + "with source step: " + t.getSource() + " - target = " + t.getTarget());

+				FlowNode bStart = stepsToBPMN.get(s);

+				FlowNode bEnd = stepsToBPMN.get(t.getTarget());

+				bpmn2Handler.connectElements(p, bStart, bEnd);

+			}

+		}

+		

+		//to finish up, the BPMN2 file must be persisted in the workspace 

+        try {

+            URI bpmn2URI = uri.trimFileExtension().appendFileExtension("bpmn");

+            bpmn2Handler.persistBPMN2File(bpmn2URI);

+            container.refreshLocal(2, null); //refreshes the workspace so that the saved file is visible

+        } catch (IOException e) {

+            displayIMError("Could not save the BPMN2 Composite file", e);

+        } catch (CoreException e) {

+            displayIMError("Could not refresh the workspace", e);

+        }

+	

+	}

+

+}