Skip to content
Gábor Bergmann edited this page Feb 22, 2023 · 18 revisions

Ecore reference card

EMF Ecore refcard

Generate model code with good package names

Let MM (metamodel) denote the properties of the second level root (package) in the created model. Let GM (genmodel) denote the properties of the second level root in the created genmodel.

  • The package name of the generated Java code will be <GM.BasePackage>.<MM.Name>
  • --> make sure MM.Name is smallcase and doesn't contain any whitespaces!
  • There is no settable postfix after MM.Name...
  • GM.Prefix will be the prefix of the Package, Factory, AdapterFactory, etc. classes (case sensitive)

Xcore and genmodels

Trying to open a genmodel file for generating code from Xcore produces the following error:

Problems encountered in file "platform:/resource/org.eclipse.emf.ecore.xcore.lib/model/XcoreLang.xcore"
Resource '/org.eclipse.emf.ecore.xcore.lib/model/XcoreLang.xcore' does not exist.

Solution: do not use agenmodel file to generate the code -- it is generated automatically in the (auto)build process.

Install EMF to the "Eclipse for Java Developers" distribution

Export ecore diagrams to a vector graphical representation

Eclipse 4.3 (Kepler)

Export to SVG and convert the SVG to PDF. Inkscape may not open the SVG files properly -- a good alternative is to use a web browser and print the SVG to PDF.

VIATRA (formerly EMF-IncQuery)

Check expressions

Problem: VIATRA/IncQuery displays the following for check expressions: Check expressions must return boolean instead of Object

Solution: Add the org.eclipse.xtext.xbase.lib bundle to the dependencies. Also check that the project is indeed a plug-in project and has an Xtext nature.

Initializing EMF models

Instead of initializing the EMF model with MyPackage.eINSTANCE.eClass(), you can invoke MyPackageImpl.init() (actually, this is what happens in the background).

Load EMF models in a dynamic manner

Similarly to the Sample Reflective Ecore Model Editor.

final ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new GenericXMLResourceFactoryImpl());

final Resource resource = resourceSet.getResource(URI.createFileURI(path), true);

final Iterator<EObject> allContents = resource.getAllContents();
while (allContents.hasNext()) {
	final EObject element = allContents.next();
	System.out.println(element);
}

Literature

On the performance of EMF: http://www.slideshare.net/kenn.hussey/performance-and-extensibility-with-emf

Pretty-print EMF models

Source: https://www.eclipse.org/forums/index.php/t/210069/

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;

public class PrettyPrinter {
	private static final String INDENT = "    "; //$NON-NLS-1$

	public static String prettyPrint(EObject object) {
		List<String> lines = prettyPrintAny(object, ""); //$NON-NLS-1$
		StringBuilder sb = new StringBuilder();
		for (String line : lines) {
			sb.append(line).append('\n');
		}
		return sb.toString();
	}

	private static List<String> prettyPrintAny(Object o, String indent) {
		List<String> lines = new ArrayList<>();
		if (o instanceof EObject) {
			EObject object = (EObject) o;
			EClass eClass = object.eClass();
			lines.add(eClass.getName() + " [" + o.getClass().getCanonicalName() + "] {"); //$NON-NLS-1$ //$NON-NLS-2$
			lines.addAll(prettyPrintRecursive(object, INDENT));
			lines.add("}"); //$NON-NLS-1$
		} else if (o instanceof Iterable) {
			lines.add("["); //$NON-NLS-1$
			for (Object obj : (Iterable<?>) o) {
				lines.addAll(prettyPrintAny(obj, INDENT));
			}
			lines.add("]"); //$NON-NLS-1$
		} else {
			String line = String.valueOf(o) + ' ';
			if (o != null) {
				line += '[' + o.getClass().getCanonicalName() + ']';
			}
			lines.add(line);
		}
		return indentLines(lines, indent);
	}

	private static List<String> indentLines(List<String> lines, String indent) {
		List<String> result = new ArrayList<>();
		for (String l : lines) {
			result.add(indent + l);
		}
		return result;
	}

	private static List<String> prettyPrintRecursive(EObject o, String indent) {
		EClass eClass = o.eClass();
		List<String> result = new ArrayList<>();
		for (EStructuralFeature feature : eClass.getEAllStructuralFeatures()) {
			Object value = o.eGet(feature);
			String line = feature.getName() + " = "; //$NON-NLS-1$
			List<String> list = prettyPrintAny(value, INDENT);
			list.set(0, list.get(0).trim());
			result.add(line + list.get(0));
			list.remove(0);
			result.addAll(list);
		}
		return indentLines(result, indent);
	}
}
Clone this wiki locally