JaMoPP Basic Tutorial3

Ken Webb 2010-04-16T16:56:44Z

In this part of the tutorial, we will modify the contents of the model before writing it out as Java source code. The Elephant class will now extend the Mammal class.

public class Elephant extends Mammal {

The model can be modified manually by directly editing the Elephant.java.xmi file. The following XML subtree can be added as a child of the <classifiers> node.

<extends xsi:type="types:NamespaceClassifierReference">
 <classifierReferences>
   <target xsi:type="classifiers:Class" href="Mammal.java.xmi#//@classifiers.0"/>
 </classifierReferences>
</extends>

The model could also be modified manually, by right-clicking Elephant.java.xmi in the Eclipse Package Explorer, and selecting the Sample Reflective Ecore Model Editor. Add a New Child of the Class Elepahnt node, and then add a New Child of that new node. Here's the result of adding these nodes either through the Model Editor or by directly typing the XML.

What I really want to do is to add this programmatically, but I don't know yet how to do this in the JaMoPP/EMFText way. The best guess that I can come up with so far is the following Java code, but at least one line won't compile, and I don't know if I'm doing this in the right way.

protected static void modifiedModel2java() {
    // setup (from original JaMoPP.java)
    setUp();
    
    // get the model
    Resource xmiResource = rs.getResource(URI.createFileURI("src/test/Elephant.java.xmi"), true);
    
    // modify the model
    TypesFactory typesFactory = new org.emftext.language.java.types.impl.TypesFactoryImpl();
    // the following line doesn't compile; create() needs an EClass
    xmiResource.getContents().add(typesFactory
            .create(org.emftext.language.java.types.impl.NamespaceClassifierReferenceImpl));
    
    // transform the model to a Java class file
    Resource javaResource = rs.getResource(URI.createFileURI("Elephant.java"), true);
    javaResource.getContents().addAll(xmiResource.getContents());
    try {
        javaResource.save(null);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

return to main page