Login | Register
My pages Projects Community openCollabNet

argouml
Wiki: Code example for model subsystem usage

Edit this page | Links to this page | Page information | Attachments | Refresh page

 

The required libs (from ArgoUML 26.2 installation):

  • argouml-mdr.jar
  • argouml-model.jar
  • argouml.jar
  • java-interfaces.jar
  • jmi.jar
  • jmiutils.jar
  • log4j-1.2.6.jar
  • mdrapi.jar
  • mof.jar
  • nbmdr.jar
  • openide-util.jar

Note: only the first two jar files need to be in classpath when compiling the code below; the other jar files are needed at runtime only.

The code:

import java.io.File;
import java.io.FileOutputStream;

import org.argouml.model.Model;
import org.argouml.model.XmiWriter;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // initialize model subsystem
        Throwable error = Model
                .initialise("org.argouml.model.mdr.MDRModelImplementation");
        if (error != null) {
            // need to be handled
            System.out.println(error.toString());
            return;
        }

        // create a model
        Object model = Model.getModelManagementFactory().createModel();
        Model.getCoreHelper().setName(model, "myModel");

        // create a class in the model
        Object mClass = Model.getCoreFactory().buildClass("MyClass", model);

        // create an attribute in the class
        Object intType = Model.getCoreFactory().buildDataType("int", model);
        Object mAttr = Model.getCoreFactory().buildAttribute2(mClass, intType);
        Model.getCoreHelper().setName(mAttr, "myAttr");

        // create an operation in the class
        Object voidType = Model.getCoreFactory().buildDataType("void", model);
        Model.getCoreFactory().buildOperation2(mClass, voidType, "myOp");

        // export to XMI
        try {
            File file = new File("myXMI.xmi");
            FileOutputStream stream = new FileOutputStream(file);
            XmiWriter xmiWriter = Model
                    .getXmiWriter(model, stream, "myVersion");
            xmiWriter.write();
            stream.flush();
            stream.close();
            System.out.println("Wrote file: " + file.getAbsolutePath());
        } catch (Exception e) {
            // need to be handled
        }
    }
}

Code example for model subsystem usage (last edited 2009-02-23 21:40:48 -0800 by thn)