This lib, pyswarmU2L (U2L = abbr., UML 2.x Library), will be able to import an UML model that is serialized in any supported XMI format, to create a new UML model or to manipulate an existing UML model, and to export an UML model to any supported XMI format.
Each element in an UML model is an instance of a metaclass specified in the UML meta-model. The meta-model used by this lib is based on specifications published by the Object Management Group (OMG), particularly the OMG Unified Modeling Language (OMG UML), Superstructure, V2.1.2 (PDF, 5.8MB). Please note that no support of diagram exchange is planned yet for this lib.
Creating UML model elements
For each metaclass the lib will provide a method to create a new instance of this metaclass. So any client can create its own new UML model from the scratch.
Where MetaclassName is a placeholder for the class of the UML metaclass. As parameters the client can use name-value pairs for properties that the meta model specifies for this UML metaclass.
BTW, whenever I use the term "metaclass" in this post, I refer to metaclass as used in the UML meta-model, not as used for the metaclass concept of Python. I don't think that Python's metaclass concept will be used in the implementation of this lib.
import pyswarmU2LSince the UML metaclasses Model and Package are subclasses of the abstract UML metaclass NamedElement, there is a property name with the type String inherited.
# create new Model instance and new Package instance:
sales = pyswarmU2L.newModel(name='SalesApp')
# is instance of pyswarmU2L.meta.AuxiliaryConstructs.Models.Model
customers = pyswarmU2L.newPackage(name='CustomerManagement')
# is instance of pyswarmU2L.meta.Classes.Kernel.Package
If shown in a diagram, the two instances in the UML model would look like this.
On the left side you see the Model instance with the property name="SalesApp", and on the right the Package instance with the property name="CustomerManagement".
Manipulating an UML model
Next, we want that CustomerManagement package is owned by the SalesApp model, so it will become part of the entire UML model. The meta-model defines that any Package instance can own other Package instances. The property name for this owned elements is "Package::nestedPackage"). Since Model is a subclass of Package, the SalesApp model can own the CustomerManagement package:
sales.addNestedPackage(customers)Alternatively, we could instead use the exactly opposite role of this ownership relation, where the property name is "Package::nestingPackage". Since there must not be more than one owner for an Element, the setter method name on this direction of the relationship is beginning with "set".
customers.setNestingPackage(sales)Okay, both ways have the same outcome, that the sales model owns the customers package. Visualized in UML diagram:
You also can get property values of any instance. In the case the property has a primitive type, you can get the value of the property by using:
Where ModelObject is an instance of a metaclass and PropertyName is the name of the property of which you want to get the value. Of course you can only query values of properties that are available for the instantiated metaclass.
>>>print customers.getName()If the type of the property is a metaclass with a multiplicity of 0..1 or 1..1, you will get one instance of the defined metaclass as return value or None if there is no instance.
>>>owner = customers.getNestingPackage()If the multiplicity of the property exceeds an upper limit of 1 -- in these cases the UML meta-model usually defines 0..*-- the getter method for this property will return a collection of metaclass instances. Of course the collection can be empty if the lower limit is 0.
>>>print owner.getName() 'SalesApp'
To exemplify collections, we add another package to the sales object, before iterating over the collection.
>>>orders = pyswarmU2L.newPackage()Visualized in a diagram our UML model would look like this now:
>>>for p in sales.getNestedPackages():
... print p.getName()
Importing and exporting XMI
Following some examples how this UML model could be extended with elements from an existing XMI file, and how the entire UML model could be serialized into an XMI file.
Import an XMI file, either a string with the XML or an URL providing the location of the file, as following:
xmiData = '''After importing an XMI file the data is available as UML model, according to the meta-model, and the client can work with it:
.....many more data here!..... ''' billingModel = pyswarmU2L.import(xmiData)
>>>print billingModelAfter nesting the imported model into our existing model, the diagram would look like this:
Finally, we can convert the entire UML model to any XMI format that is supported by the lib:
newXmiData = sales.export(extender='FooUML', version='1.3')By providing extender and version values, the lib is told which XMI format exactly to use. The export() method returns the corresponding XMI data as string and the client can do further processing of the string, i.e. write it to the file-system.
So far these are my considerations regarding a Python library for UML models. There are still some open issues. Especially the XMI filters will be tricky, since the differences between the formats and the meta model can be rather big.
Any comments or questions are welcome.