Skip to content

Instantly share code, notes, and snippets.

@louismrose
Created March 2, 2013 11:03
Show Gist options
  • Save louismrose/5070532 to your computer and use it in GitHub Desktop.
Save louismrose/5070532 to your computer and use it in GitHub Desktop.
Code for converting an EMF file containing ID-based references to EMF fragment path style references. Models using the latter format appear to be quicker to load than equivalent models using the former.
/**
* <copyright>
* </copyright>
*
* $Id$
*/
package PetriNet.tests;
import PetriNet.Net;
import PetriNet.PetriNetFactory;
import PetriNet.PetriNetPackage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.emf.common.util.Diagnostic;
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.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
/**
* <!-- begin-user-doc -->
* A sample utility for the '<em><b>PetriNet</b></em>' package.
* <!-- end-user-doc -->
* @generated
*/
public class PetriNetConvert
{
/**
* <!-- begin-user-doc -->
* Load all the argument file paths or URIs as instances of the model.
* <!-- end-user-doc -->
* @param args the file paths or URIs.
* @generated
*/
public static void main(String[] args)
{
printTime();
// Create a resource set to hold the resources.
//
ResourceSet resourceSet = new ResourceSetImpl();
// Register the appropriate resource factory to handle all file extensions.
//
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
(Resource.Factory.Registry.DEFAULT_EXTENSION,
new XMIResourceFactoryImpl());
// Register the package to ensure it is available during loading.
//
resourceSet.getPackageRegistry().put
(PetriNetPackage.eNS_URI,
PetriNetPackage.eINSTANCE);
// If there are no arguments, emit an appropriate usage message.
//
if (args.length == 0)
{
System.out.println("Enter a list of file paths or URIs that have content like this:");
try
{
Resource resource = resourceSet.createResource(URI.createURI("http:///My.petrinet"));
Net root = PetriNetFactory.eINSTANCE.createNet();
resource.getContents().add(root);
resource.save(System.out, null);
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
else
{
// Iterate over all the arguments.
//
for (int i = 0; i < args.length; ++i)
{
// Construct the URI for the instance file.
// The argument is treated as a file path only if it denotes an existing file.
// Otherwise, it's directly treated as a URL.
//
File file = new File(args[i]);
URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()): URI.createURI(args[i]);
try
{
// Demand load resource for this file.
//
Resource resource = resourceSet.getResource(uri, true);
System.out.println("Loaded " + uri);
resource.setURI(uri.appendFileExtension("converted"));
for (Iterator<EObject> it = resource.getAllContents(); it.hasNext();) {
EObject next = it.next();
((XMIResource)(next.eResource())).setID(next, null);
}
System.out.println("Converted " + uri);
resource.save(null);
System.out.println("Saved " + uri);
}
catch (Exception exception)
{
System.out.println("Problem loading " + uri);
exception.printStackTrace();
}
}
}
printTime();
}
/**
* <!-- begin-user-doc -->
* Prints diagnostics with indentation.
* <!-- end-user-doc -->
* @param diagnostic the diagnostic to print.
* @param indent the indentation for printing.
* @generated
*/
protected static void printDiagnostic(Diagnostic diagnostic, String indent)
{
System.out.print(indent);
System.out.println(diagnostic.getMessage());
for (Diagnostic child : diagnostic.getChildren())
{
printDiagnostic(child, indent + " ");
}
}
private static void printTime() {
Calendar cal = Calendar.getInstance();
cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println( sdf.format(cal.getTime()) );
}
} //PetriNetExample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment