tom-2.8:Playing with EMF
From Tom
| Tom-2.8 : Tutorial |
Level 1 - Introduction > Level 2 - List matching > Level 3 - Strategies > Advanced - Mappings > Writing a (small) Parser/Compiler/Interpreter > XML > Playing with EMF > EMF use case: SimplePDLToPetrinet |
This section deals with using Tom to handle EMF models.
Contents |
Generating model code
If you don't know how to generate model code using EMF, you should have a look at this tutorial.
We will use this example taken from the EMF documentation :
Generating EMF mappings
To generate EMF mappings, you will have to compile the EMF mapping generator.
Add needed EMF jars to CLASSPATH :
export CLASSPATH=`echo ${TOM_HOME}/lib/tools/org.eclipse.emf*.jar | tr ' ' ':'`:${CLASSPATH}
This generator takes the generated EPackage class as argument. For example :
emf-generate-mappings org.eclipse.example.library.LibraryPackage
|
Note: LibraryPackage have to be in classpath, you can use -cp java command option. For example : java -cp "$ECLIPSE_WORKSPACE/library/bin:$CLASSPATH" TomMappingFromEcore org.eclipse.example.library.LibraryPackage |
To redirect result into a file, use the following command :
emf-generate-mappings org.eclipse.example.library.LibraryPackage > librarymapping.tom
You can generate Ecore metamodel mapping thanks to the following line :
emf-generate-mappings org.eclipse.emf.ecore.EcorePackage
Using generated mappings
Generated mappings can be used as follows :
import org.eclipse.emf.common.util.*; import org.eclipse.emf.ecore.*; import org.eclipse.example.library.*; import tom.library.sl.*; class ClassUsingLibraryMapping { %include{ librarymapping.tom } %include { sl.tom } %strategy stratPrintBook() extends Fail() { visit Book { Book(name, _, _, _) -> { System.out.println(`name); } } } public static void main(String[] args) throws VisitFailure { Book b1=`Book("Book 1", 420, ScienceFiction(), null); Book b2=`Book("Book 2", 300, Mystery(), null); Book b3=`Book("Book 3", 500, Biography(), null); Writer w1=`Writer("Writer 1", BookEList(b1,b2)); Writer w2=`Writer("Writer 2", BookEList(b3)); b1.setAuthor(w1); b2.setAuthor(w1); b3.setAuthor(w2); Library l = `Library("Library name", WriterEList(w1,w2), BookEList(b1,b2,b3)); // print all book names from library l System.out.println("%match :"); %match (l) { Library(_, _, BookEList(_*, Book(name, _, _, _), _*)) -> { System.out.println(`name); } } // same thing using strategies and a containment instrospector System.out.println("strategies :"); `TopDown(Try(stratPrintBook())).visitLight(l, new EcoreContainmentIntrospector()); } }
|
Note: EcoreContainmentIntrospector only follows containment features avoiding looping problems |
Compile it with :
tom -p ClassUsingLibraryMapping.t javac -sourcepath . ClassUsingLibraryMapping.java
Use it with :
java ClassUsingLibraryMapping
|
Note: Don't forget to add library classes to classpath |
Using XMI generator
XMI generator uses Ecore metadata mappings to generate an XMI file from a package class.
Generate library XMI file as follow :
emf-generate-xmi org.eclipse.example.library.LibraryPackage > library.xmi
Generate Ecore metadata XMI file as follow :
emf-generate-xmi org.eclipse.emf.ecore.EcorePackage > ecore.xmi
| Tutorial |
|
Level 1 - Introduction > Level 2 - List matching > Level 3 - Strategies > Advanced - Mappings > Writing a (small) Parser/Compiler/Interpreter > XML > Playing with EMF > EMF use case: SimplePDLToPetrinet |
| Tom-2.8 Documentation |
| Guided Tour :: Tutorial :: Language Reference :: Tools |


