[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[stp-commits] r2959 - org.eclipse.stp.model/trunk/org.eclipse.stp.im/src/org/eclipse/stp/im/util

Author: amos
Date: 2009-08-27 04:48:14 -0400 (Thu, 27 Aug 2009)
New Revision: 2959

Added:
   org.eclipse.stp.model/trunk/org.eclipse.stp.im/src/org/eclipse/stp/im/util/IMHandler.java
Log:
Convenience helper for common operations with the IM

Added: org.eclipse.stp.model/trunk/org.eclipse.stp.im/src/org/eclipse/stp/im/util/IMHandler.java
===================================================================
--- org.eclipse.stp.model/trunk/org.eclipse.stp.im/src/org/eclipse/stp/im/util/IMHandler.java	                        (rev 0)
+++ org.eclipse.stp.model/trunk/org.eclipse.stp.im/src/org/eclipse/stp/im/util/IMHandler.java	2009-08-27 08:48:14 UTC (rev 2959)
@@ -0,0 +1,119 @@
+/*******************************************************************************
+ * Copyright (c) {2008} INRIA
+ * 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 (INRIA) - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.stp.im.util;
+
+import java.io.IOException;
+import java.util.Collections;
+
+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.stp.im.ImFactory;
+import org.eclipse.stp.im.Process;
+import org.eclipse.stp.im.ProcessCollection;
+import org.eclipse.stp.im.Service;
+import org.eclipse.stp.im.ServiceCollection;
+import org.eclipse.stp.im.Step;
+import org.eclipse.stp.im.StpIntermediateModel;
+
+/**
+ * Contains operations for creating elements in the Intermediate Model and writing it to disk
+ * It is oriented towards SCA-related elements
+ * An instance of this class corresponds to an instance of the IM
+ * @author Adrian Mos
+ */
+public class IMHandler {
+    private StpIntermediateModel stpIM = null; //the top level element of the Intermediate Model EMF instance
+    private ProcessCollection processCollection;
+    private ServiceCollection serviceCollection;
+    private ImFactory imFactory;
+
+    /**
+     * Instantiates the STP-IM and performs other set-up operations  
+     */
+    public IMHandler() {
+        imFactory = ImFactory.eINSTANCE;
+        stpIM = imFactory.createStpIntermediateModel();
+        //create the IM collections (for processes and services)
+        //each process or service created in the future will be added to these accordingly
+        processCollection = imFactory.createProcessCollection();
+        serviceCollection = imFactory.createServiceCollection();
+        stpIM.setProcessCollection(processCollection);
+        stpIM.setServiceCollection(serviceCollection);
+    }
+
+    /**
+     * Instantiates the STP-IM from a .im file  
+     * @param uri the file to load the IM from
+     */
+    public IMHandler(URI uri){
+        
+    }
+    
+    public StpIntermediateModel getStpIM() {
+        return stpIM;
+    }
+    
+    /**
+     * creates and returns a Process element in the IM
+     * @param name the process name
+     * @return the Process EMF element
+     */
+    public Process createProcess(String name){
+        Process p = imFactory.createProcess();
+        p.setName(name);
+        processCollection.getProcesses().add(p); //add it to the IM collection
+        
+        return p;
+    }
+    
+    /**
+     * creates and returns a Service element in the IM
+     * @param name the service name
+     * @return the Service EMF element
+     */
+    public Service createService(String name){
+        Service s = imFactory.createService();
+        s.setServiceName(name);
+        serviceCollection.getServices().add(s); //add it to the IM collection
+        return s;
+    }
+
+    /**
+     * creates a process step and registers it with the given process
+     * @param stepName the name of the step to create
+     * @param parent the parent to attach the step to
+     * @return the newly created step
+     */
+    public Step createStep(String stepName, Process parent){
+        Step s = imFactory.createStep();
+        s.setName(stepName);
+        parent.getSteps().add(s);
+        return s;
+    }
+    
+    /**
+     * will create a file and save the IM instance in it
+     * @param uri the URI of the file to save to
+     * @throws IOException if the save operation does not succeed
+     */
+    public void persistIM(URI uri) throws IOException {
+        //System.out.println("Attempting to persist the IM in " + uri);
+
+        ResourceSet rs = new ResourceSetImpl();
+
+        Resource resource = rs.createResource(uri);
+        resource.getContents().add(this.stpIM);
+        
+        resource.save(Collections.EMPTY_MAP);
+    }
+    
+}