Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EMF Compare FAQ"

m (My model is compared as a text-file, how can EMF compare know what file it should handle ?)
(Is EMF Compare, as EMF, able to run outside of Eclispe ?)
Line 28: Line 28:
 
'''A''': Yes, here is a snippet doing that:
 
'''A''': Yes, here is a snippet doing that:
 
<pre>
 
<pre>
  /**
+
  /**
   * This application will try and launch an headless model comparison.
+
* This application will try and launch an headless model comparison.
   *  
+
*  
   * @author Cedric Brun <a href="mailto:cedric.brun@obeo.fr">cedric.brun@obeo.fr</a>
+
* @author Cedric Brun <a href="mailto:cedric.brun@obeo.fr">cedric.brun@obeo.fr</a>
   */
+
*/
  public final class ExampleLauncher {
+
public final class ExampleLauncher {
        private ExampleLauncher() {
+
private ExampleLauncher() {
                // prevents instantiation
+
// prevents instantiation
        }
+
}
  
        /**
+
/**
         * Loads a model from an {@link org.eclipse.emf.common.util.URI URI} in a given {@link ResourceSet}.
+
* Loads a model from an {@link org.eclipse.emf.common.util.URI URI} in a given {@link ResourceSet}.
         *  
+
*  
         * @param file
+
* @param file
         *            {@link java.io.File File} containing the model to be loaded.
+
*           {@link java.io.File File} containing the model to be loaded.
         * @param resourceSet
+
* @param resourceSet
         *            The {@link ResourceSet} to load the model in.
+
*           The {@link ResourceSet} to load the model in.
         * @return The model loaded from the file.
+
* @return The model loaded from the file.
         * @throws IOException
+
* @throws IOException
         *             If the given file does not exist.
+
*             If the given file does not exist.
         */
+
*/
        @SuppressWarnings("unchecked")
+
@SuppressWarnings("unchecked")
        public static EObject load(File file, ResourceSet resourceSet) throws IOException {
+
public static EObject load(File file, ResourceSet resourceSet) throws IOException {
                final URI modelURI = URI.createFileURI(file.getPath());
+
final URI modelURI = URI.createFileURI(file.getPath());
                EObject result = null;
+
EObject result = null;
  
                String fileExtension = modelURI.fileExtension();
+
String fileExtension = modelURI.fileExtension();
                if (fileExtension == null || fileExtension.length() == 0) {
+
if (fileExtension == null || fileExtension.length() == 0) {
                        fileExtension = Resource.Factory.Registry.DEFAULT_EXTENSION;
+
fileExtension = Resource.Factory.Registry.DEFAULT_EXTENSION;
                }
+
}
  
                final Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
+
final Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
                final Object resourceFactory = reg.getExtensionToFactoryMap().get(fileExtension);
+
final Object resourceFactory = reg.getExtensionToFactoryMap().get(fileExtension);
                if (resourceFactory != null) {
+
if (resourceFactory != null) {
                        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(fileExtension, resourceFactory);
+
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(fileExtension, resourceFactory);
                } else {
+
} else {
                        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(fileExtension, new XMIResourceFactoryImpl());
+
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(fileExtension, new XMIResourceFactoryImpl());
                }
+
}
  
                final Resource modelResource = resourceSet.createResource(modelURI);
+
final Resource modelResource = resourceSet.createResource(modelURI);
                modelResource.load(Collections.EMPTY_MAP);
+
modelResource.load(Collections.EMPTY_MAP);
                if (modelResource.getContents().size() > 0)
+
if (modelResource.getContents().size() > 0)
                        result = (EObject)modelResource.getContents().get(0);
+
result = (EObject)modelResource.getContents().get(0);
                return result;
+
return result;
        }
+
}
  
        /**
+
/**
         * Launcher of this application.
+
* Launcher of this application.
         *  
+
*  
         * @param args
+
* @param args
         *            Arguments of the launch.
+
*           Arguments of the launch.
         */
+
*/
        public static void main(String[] args) {
+
public static void main(String[] args) {
                if (args.length == 2 && new File(args[0]).canRead() && new File(args[1]).canRead()) {
+
if (args.length == 2 && new File(args[0]).canRead() && new File(args[1]).canRead()) {
                        final ResourceSet resourceSet = new ResourceSetImpl();
+
final ResourceSet resourceSet = new ResourceSetImpl();
                        try {
+
try {
                                final EObject model1 = load(new File(args[0]), resourceSet);
+
final EObject model1 = load(new File(args[0]), resourceSet);
                                final EObject model2 = load(new File(args[1]), resourceSet);
+
final EObject model2 = load(new File(args[1]), resourceSet);
                                final MatchModel match = new DifferencesServices().modelMatch(model1, model2, new NullProgressMonitor());
+
final MatchModel match = new DifferencesServices().modelMatch(model1, model2, new NullProgressMonitor());
                                final DiffModel diff = new DiffMaker().doDiff(match);
+
final DiffModel diff = new DiffMaker().doDiff(match, false);
                                
+
                                try {
+
try {
                                        System.out.println(ModelUtils.serialize(match));
+
System.out.println(ModelUtils.serialize(match));
                                        System.out.println(ModelUtils.serialize(diff));
+
System.out.println(ModelUtils.serialize(diff));
                                } catch (IOException e) {
+
} catch (IOException e) {
                                        e.printStackTrace();
+
e.printStackTrace();
                                }
+
}
                        } catch (IOException e) {
+
} catch (IOException e) {
                                // cannot be thrown
+
// cannot be thrown
                                e.printStackTrace();
+
e.printStackTrace();
                        } catch (InterruptedException e) {
+
} catch (InterruptedException e) {
                                e.printStackTrace();
+
e.printStackTrace();
                        }
+
}
                } else {
+
} else {
                        System.out.println("usage : Launcher <Model1> <Model2>"); //$NON-NLS-1$
+
System.out.println("usage : Launcher <Model1> <Model2>"); //$NON-NLS-1$
                }
+
}
        }
+
}
  }
+
}
 
</pre>
 
</pre>

Revision as of 10:43, 10 August 2007

Users

My model is compared as a text-file, how can EMF compare know what file it should handle ?

Q : My model is compared as a text-file, how can EMF compare know what file it should handle ?

A : EMF compare uses a content-type to know if it should be started or not. By default this content-type is composed of *.ecore and *.uml file but you may add your own extension using the Preferences view / Global / Content-types and adding your file extension in the "Model File" content-type.


EMFComparePreferences-content-type.png

Developpers

How can I programatically add my model file extension in EMF Compare so that it is called automatically ?

Q : How can I programatically add my model file extension in EMF Compare so that it is called automatically ?

A : Using the "Model File" content-type defined with EMF Compare, here is a sample from a plugin.xml:

 <extension
        point="org.eclipse.core.contenttype.contentTypes">
     <file-association
           content-type="org.eclipse.emf.compare.ui.contenttype.ModelContentType"
           file-extensions="uml13"
           file-names="*"/>
  </extension>

Is EMF Compare, as EMF, able to run outside of Eclispe ?

Q : Is EMF Compare able to compare "in-memory" objects, and can it be run without Eclispe ?

A: Yes, here is a snippet doing that:

  /**
 * This application will try and launch an headless model comparison.
 * 
 * @author Cedric Brun <a href="mailto:cedric.brun@obeo.fr">cedric.brun@obeo.fr</a>
 */
public final class ExampleLauncher {
	private ExampleLauncher() {
		// prevents instantiation
	}

	/**
	 * Loads a model from an {@link org.eclipse.emf.common.util.URI URI} in a given {@link ResourceSet}.
	 * 
	 * @param file
	 *            {@link java.io.File File} containing the model to be loaded.
	 * @param resourceSet
	 *            The {@link ResourceSet} to load the model in.
	 * @return The model loaded from the file.
	 * @throws IOException
	 *             If the given file does not exist.
	 */
	@SuppressWarnings("unchecked")
	public static EObject load(File file, ResourceSet resourceSet) throws IOException {
		final URI modelURI = URI.createFileURI(file.getPath());
		EObject result = null;

		String fileExtension = modelURI.fileExtension();
		if (fileExtension == null || fileExtension.length() == 0) {
			fileExtension = Resource.Factory.Registry.DEFAULT_EXTENSION;
		}

		final Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
		final Object resourceFactory = reg.getExtensionToFactoryMap().get(fileExtension);
		if (resourceFactory != null) {
			resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(fileExtension, resourceFactory);
		} else {
			resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(fileExtension, new XMIResourceFactoryImpl());
		}

		final Resource modelResource = resourceSet.createResource(modelURI);
		modelResource.load(Collections.EMPTY_MAP);
		if (modelResource.getContents().size() > 0)
			result = (EObject)modelResource.getContents().get(0);
		return result;
	}

	/**
	 * Launcher of this application.
	 * 
	 * @param args
	 *            Arguments of the launch.
	 */
	public static void main(String[] args) {
		if (args.length == 2 && new File(args[0]).canRead() && new File(args[1]).canRead()) {
			final ResourceSet resourceSet = new ResourceSetImpl();
			try {
				final EObject model1 = load(new File(args[0]), resourceSet);
				final EObject model2 = load(new File(args[1]), resourceSet);
				final MatchModel match = new DifferencesServices().modelMatch(model1, model2, new NullProgressMonitor());
				final DiffModel diff = new DiffMaker().doDiff(match, false);
				
				try {
					System.out.println(ModelUtils.serialize(match));
					System.out.println(ModelUtils.serialize(diff));
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (IOException e) {
				// cannot be thrown
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("usage : Launcher <Model1> <Model2>"); //$NON-NLS-1$
		}
	}
}

Copyright © Eclipse Foundation, Inc. All Rights Reserved.