Jump to: navigation, search

Texo/Convert to and from EMF

< Texo
Revision as of 23:46, 6 May 2012 by Mtaal.elver.org (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Introduction

The EMF landscape consists of many projects which can be useful for different situations. To make it possible to use other EMF components with Texo, Texo provides conversion logic which can convert Texo generated pojo's to EMF EObjects and back.

Texo itself makes use of the EMF conversion logic in 2 places:

  • to support XML/XMI serialization: Texo converts pojo's to EObject which are then (de-)serialized through standard EMF XML/XMI Resources.
  • Texo uses the EMF converters in its test cases to integrate with EMF Compare. Test data is converted from Texo pojos to XML and back. The start object set is then compared with the object set computed at the end of the test.

Description

The EMF converters are located in the org.eclipse.emf.texo plugin. There are two classes which take care of converting from and to EMF:

  • EMFModelConverter: converts from EObjects to Texo generated pojos.
  • ModelEMFConverter: converts Texo generated pojos to EObjects.

The classes need to be instantiated (new EMFModelConverter() for example) to be used. Both classes have a convert method which executes the conversion logic. The classes both have javadoc available.

Usage

A small code snippet shows how a list of pojos can be converted to EObject. This code snippet converts a Library pojo (from the common EMF Library example) to an EObject:

// code truncated to be more brief
final Library library = factory.createLibrary();
library.setName("name");
library.getWriters().add(writer);
library.getBooks().add(book);
 
// convert to eobjects
// create a list
final List<Object> modelObjects = Collections.singletonList((Object) library);
// and convert to create the EObjects
final List<EObject> eObjects = new ModelEMFConverter().convert(modelObjects);

The usage shows that the ModelEMFConverter is created and then discarded right away. The ModelEMFConverter keeps an internal mapping from pojos to created EObjects. So it can sometimes make sense to keep the ModelEMFConverter and use one instance multiple times so that object references are resolved correctly.

The conversion back is very similar:

final List<Object> convertedModelObjects = new EMFModelConverter().convert(eObjects);

The returned list contains a list of Texo created pojo objects.