comments added BLD200408011800
authormmatula@netbeans.org
Wed, 28 Jul 2004 19:09:11 +0000
changeset 155592efa2ebe756
parent 1554 ac318f92f508
child 1556 63d4aebf4366
comments added
mdr/extras/uml2mof/src/org/netbeans/lib/jmi/uml2mof/Main.java
     1.1 --- a/mdr/extras/uml2mof/src/org/netbeans/lib/jmi/uml2mof/Main.java	Wed Jul 28 12:41:12 2004 +0000
     1.2 +++ b/mdr/extras/uml2mof/src/org/netbeans/lib/jmi/uml2mof/Main.java	Wed Jul 28 19:09:11 2004 +0000
     1.3 @@ -27,37 +27,61 @@
     1.4  
     1.5  /**
     1.6   *
     1.7 - * @author  mm109185
     1.8 + * @author  Martin Matula
     1.9   */
    1.10  public class Main {
    1.11 +    // name of a MOF extent that will serve as a target extent for the UML2MOF transformation
    1.12      private static final String MOF_INSTANCE = "MOFInstance";
    1.13 +    // name of a UML extent (instance of UML metamodel) that the UML models will be loaded into
    1.14      private static final String UML_INSTANCE = "UMLInstance";
    1.15 +    // name of a MOF extent that will contain definition of UML metamodel
    1.16      private static final String UML_MM = "UML";
    1.17 +
    1.18 +    // repository
    1.19 +    private static MDRepository rep;
    1.20 +    // UML extent
    1.21 +    private static UmlPackage uml;
    1.22 +    // MOF extent
    1.23 +    private static ModelPackage mof;
    1.24 +    // XMI reader
    1.25 +    private static XmiReader reader;
    1.26      
    1.27 -    public static MDRepository rep;
    1.28 -    public static UmlPackage uml;
    1.29 -    public static ModelPackage mof;
    1.30 -    public static XmiReader reader;
    1.31 -    
    1.32 -    /** Creates a new instance of Main */
    1.33 -    public Main() {
    1.34 -    }
    1.35 - 
    1.36      public static void main(String args[]) {
    1.37          try {
    1.38 +            // get the default repository from the MDR manager
    1.39              rep = MDRManager.getDefault().getDefaultRepository();
    1.40 +            // get's the URL of the file passed as the first commandline parameter
    1.41 +            // (file containing the UML model to be transformed to MOF)
    1.42              String uri = new File(args[0]).toURL().toString();
    1.43 +            // opens an output stream for the file name passed as the second commandline parameter
    1.44 +            // (name of file to be used to save the resulting MOF metamodel)
    1.45              FileOutputStream out = new FileOutputStream(args[1]);
    1.46 +            // look up an implementation of XmiReader interface
    1.47              reader = (XmiReader) Lookup.getDefault().lookup(XmiReader.class);
    1.48 +            // look up an implementation of XmiWriter interface
    1.49              XmiWriter writer = (XmiWriter) Lookup.getDefault().lookup(XmiWriter.class);
    1.50 +            
    1.51 +            // initialize the repository (make sure the UML metamodel is loaded and both
    1.52 +            // UML and MOF metamodels are instantiated)
    1.53              init();
    1.54 +            
    1.55 +            // start a write transaction
    1.56              rep.beginTrans(true);
    1.57              try {
    1.58 +                // read the UML model into the UML extent
    1.59                  reader.read(uri, uml);
    1.60 +                // transform the UML model in UML extent into a MOF metamodel (which will reside in MOF extent)
    1.61                  Transformer.execute(uml, mof);
    1.62 +                // write the content of the MOF extent to the XMI (i.e. write the resulting MOF metamodel)
    1.63                  writer.write(out, mof, null);
    1.64              } finally {
    1.65 +                // rollback the write transaction
    1.66 +                // (this is to make sure the transformed models are not kept in the storage - they
    1.67 +                // will probably not be needed anymore - another alternative to this would be to
    1.68 +                // remove both UML model and the resulting MOF metamodel and do commit, but in this
    1.69 +                // case doing rollback is simpler)
    1.70                  rep.endTrans(true);
    1.71 +                // shutdown the repository to make sure all caches are flushed to disk
    1.72                  MDRManager.getDefault().shutdownAll();
    1.73                  out.close();
    1.74              }
    1.75 @@ -66,37 +90,61 @@
    1.76          }
    1.77      }
    1.78      
    1.79 +    /** Makes sure UML and MOF extents are created. */
    1.80      private static void init() throws Exception {
    1.81 +        // try to retrieve MOF and UML extents
    1.82          mof = (ModelPackage) rep.getExtent(MOF_INSTANCE);
    1.83          uml = (UmlPackage) rep.getExtent(UML_INSTANCE);
    1.84 +        // check whether both extents exist (they do not exist if this is the first time
    1.85 +        // the UML2MOF tool is run or the storage files created by previous runs
    1.86 +        // were deleted)
    1.87          if (mof == null) {
    1.88 +            // MOF extent does not exist -> create it
    1.89              mof = (ModelPackage) rep.createExtent(MOF_INSTANCE);
    1.90          }
    1.91          if (uml == null) {
    1.92 +            // UML extent does not exist -> create it (note that in case one want's to instantiate
    1.93 +            // a metamodel other than MOF, they need to provide the second parameter of the createExtent
    1.94 +            // method which indicates the metamodel package that should be instantiated)
    1.95              uml = (UmlPackage) rep.createExtent(UML_INSTANCE, getUmlPackage());
    1.96          }
    1.97      }
    1.98      
    1.99 +    /** Finds "UML" package -> this is the topmost package of UML metamodel - that's the
   1.100 +     * package that needs to be instantiated in order to create a UML extent
   1.101 +     */
   1.102      private static MofPackage getUmlPackage() throws Exception {
   1.103 +        // get the MOF extent containing definition of UML metamodel
   1.104          ModelPackage umlMM = (ModelPackage) rep.getExtent(UML_MM);
   1.105          if (umlMM == null) {
   1.106 +            // it is not present -> create it
   1.107              umlMM = (ModelPackage) rep.createExtent(UML_MM);
   1.108          }
   1.109 +        // find package named "UML" in this extent
   1.110          MofPackage result = getUmlPackage(umlMM);
   1.111          if (result == null) {
   1.112 +            // it cannot be found -> UML metamodel is not loaded -> load it from XMI
   1.113              reader.read(UmlPackage.class.getResource("resources/01-02-15_Diff.xml").toString(), umlMM);
   1.114 +            // try to find the "UML" package again
   1.115 +            result = getUmlPackage(umlMM);
   1.116          }
   1.117 -        result = getUmlPackage(umlMM);
   1.118          return result;
   1.119      }
   1.120      
   1.121 +    /** Finds "UML" package in a given extent
   1.122 +     * @param umlMM MOF extent that should be searched for "UML" package.
   1.123 +     */
   1.124      private static MofPackage getUmlPackage(ModelPackage umlMM) {
   1.125 +        // iterate through all instances of package
   1.126          for (Iterator it = umlMM.getMofPackage().refAllOfClass().iterator(); it.hasNext();) {
   1.127              MofPackage pkg = (MofPackage) it.next();
   1.128 +            // is the package topmost and is it named "UML"?
   1.129              if (pkg.getContainer() == null && "UML".equals(pkg.getName())) {
   1.130 +                // yes -> return it
   1.131                  return pkg;
   1.132              }
   1.133          }
   1.134 +        // a topmost package named "UML" could not be found
   1.135          return null;
   1.136      }
   1.137  }