Monday, March 9, 2009

Gaphor element factory

The Gaphor UML modeling tool, written in Python, defines a UML element factory. The factory, given an instance of the Gaphor Application class, can construct UML element instances as Python objects. These Gaphor components may be used from within Gaphor plugins or independently of Gaphor entirely.

Consider the following example.
#Example; Gaphor factory.

from gaphor.application import Application
from gaphor.UML.elementfactory import ElementFactory
from gaphor.UML.uml2 import Class

if __name__=="__main__":
factory_obj=ElementFactory()
factory_obj.init(Application)
class_obj=factory_obj.create(Class)
class_obj.name="MyClass"
print class_obj.name
print class_obj.id
class_obj.unlink()
factory_obj.shutdown()
Here, we construct an element factory and pass it the Gaphor Application instance. Next, we create a Class UML element. Finally, we set an attribute of the element, destroy the element, and shutdown the factory.

We can construct any UML element from the Gaphor uml2 module which is automatically generated. Very neat stuff.