fixed checkmetamodel plugin

git-svn-id: file:///Users/arjan/backup/gaphor/gaphor/trunk@1357 a8418922-720d-0410-834f-a69b97ada669
This commit is contained in:
Arjan Molenaar 2007-06-11 12:34:44 +00:00
parent de3117acc3
commit 3fa88ccb63
4 changed files with 83 additions and 54 deletions

View File

@ -1,15 +1,3 @@
# vim:sw=4:et
import gaphor.plugin
from checkmodelgui import CheckModelWindow
from checkmodel import check_associations, check_attributes
class CheckMetamodelAction(gaphor.plugin.Action):
def execute(self):
print 'CheckMetamodelAction'
# TODO: override checkmodel.report
cw = CheckModelWindow()
cw.construct()
self.window.add_transient_window(cw)
cw.run()

View File

@ -1,8 +1,5 @@
# vim:sw=4:et
from gaphor import storage
from gaphor import UML
from gaphor.core import inject
from os import path
def report(element, message):
@ -29,18 +26,18 @@ def get_superclasses(class_):
gen = 1
def check_classes():
classes = UML.select(lambda e: e.isKindOf(UML.Class))
def check_classes(element_factory):
classes = element_factory.select(lambda e: e.isKindOf(UML.Class))
names = [ c.name for c in classes ]
for c in classes:
if names.count(c.name) > 1:
report(c, 'Class name %s used more than once' % c.name)
def check_association_end_subsets(end):
subsets = get_subsets(end.taggedValue and end.taggedValue.value or '')
opposite_subsets = get_subsets(end.opposite.taggedValue and end.opposite.taggedValue.value or '')
subset_properties = UML.select(lambda e: e.isKindOf(UML.Property) and e.name in subsets)
def check_association_end_subsets(element_factory, end):
subsets = get_subsets(end.taggedValue and end.taggedValue[0].value or '')
opposite_subsets = get_subsets(end.opposite.taggedValue and end.opposite.taggedValue[0].value or '')
subset_properties = element_factory.select(lambda e: e.isKindOf(UML.Property) and e.name in subsets)
# TODO: check if properties belong to a superclass of the end's class
@ -61,23 +58,23 @@ def check_association_end_subsets(end):
elif p.upperValue.value < end.upperValue.value:
report(end, 'Association end %s has has a bigger upper value than subse %s' % (end.name, p.name))
def check_association_end(end):
check_association_end_subsets(end)
def check_association_end(element_factory, end):
check_association_end_subsets(element_factory, end)
def check_associations():
for a in UML.select(lambda e: e.isKindOf(UML.Association)):
def check_associations(element_factory):
for a in element_factory.select(lambda e: e.isKindOf(UML.Association)):
assert len(a.memberEnd) == 2
head = a.memberEnd[0]
tail = a.memberEnd[1]
check_association_end(head)
check_association_end(tail)
check_association_end(element_factory, head)
check_association_end(element_factory, tail)
def check_attributes():
for a in UML.select(lambda e: e.isKindOf(UML.Property) and not e.association):
def check_attributes(element_factory):
for a in element_factory.select(lambda e: e.isKindOf(UML.Property) and not e.association):
if not a.typeValue or not a.typeValue.value:
report(a,'Attribute has no type: %s' % a.name)
elif a.typeValue.value.lower() not in ('string', 'boolean', 'integer', 'unlimitednatural'):
report(a, 'Invalid attribute type: %s' % a.taggedValue.value)
report(a, 'Invalid attribute type: %s' % a.typeValue.value)
# TODO: Check the sanity of the generated data model.
def check_UML_module():
@ -88,7 +85,13 @@ def check_UML_module():
# TODO: check derived unions.
if __name__ == '__main__':
storage.load(path.join('gaphor', 'UML', 'uml2.gaphor'))
check_associations()
check_attributes()
from gaphor.UML import ElementFactory
from gaphor import storage
element_factory = ElementFactory()
storage.load(path.join('gaphor', 'UML', 'uml2.gaphor'), factory=element_factory)
check_associations(element_factory)
check_attributes(element_factory)
# vim:sw=4:et

View File

@ -1,28 +1,50 @@
# vim:sw=4:et
"""A GUI for the checkmodel plugin.
"""
A GUI for the checkmodel plugin.
"""
import sys
import gobject
import gtk
import gaphor
from gaphor.ui.abstractwindow import AbstractWindow
from gaphor.plugin import Application
from zope import interface, component
from gaphor.core import _, inject, action, build_action_group
from gaphor.interfaces import IService, IActionProvider
import checkmodel
PYELEMENT_COLUMN = 0
ELEMENT_COLUMN = 1
REASON_COLUMN = 2
class CheckModelWindow(AbstractWindow):
class CheckModelWindow(object):
menu = ('_File', ('FileClose',))
interface.implements(IService, IActionProvider)
element_factory = inject('element_factory')
gui_manager = inject('gui_manager')
menu_xml = """
<ui>
<menubar action="mainwindow">
<menu action="tools">
<menuitem action="tools-open-check-model" />
</menu>
</menubar>
</ui>"""
def __init__(self):
AbstractWindow.__init__(self)
# Override the report method
checkmodel.report = self.on_report
self.action_group = build_action_group(self)
def init(self, app):
pass
def shutdown(self):
pass
@action(name='tools-open-check-model', label='Check UML model')
def open(self):
self.construct()
self.run()
def construct(self):
model = gtk.ListStore(gobject.TYPE_PYOBJECT,
@ -50,18 +72,26 @@ class CheckModelWindow(AbstractWindow):
treeview.append_column(column)
treeview.show()
self._construct_window(name='checkmodel',
title='Check Model',
size=(400, 400),
contents=scrolled_window)
#self._construct_window(name='checkmodel',
# title='Check Model',
# size=(400, 400),
# contents=scrolled_window)
self.model = model
self.treeview = treeview
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect('destroy', self.on_destroy)
self.window.set_title('Gaphor - Check Model')
self.window.add(scrolled_window)
self.window.set_size_request(400, 400)
self.window.show()
def run(self):
# TODO: Let this run in a Thread(?)
checkmodel.check_classes()
checkmodel.check_attributes()
checkmodel.check_associations()
checkmodel.check_classes(self.element_factory)
checkmodel.check_attributes(self.element_factory)
checkmodel.check_associations(self.element_factory)
def on_report(self, element, message):
log.info('%s: %s' % (type(element).__name__, message))
@ -78,11 +108,18 @@ class CheckModelWindow(AbstractWindow):
element = self.model.get_value(iter, PYELEMENT_COLUMN)
print 'Looking for element', element
if element.presentation:
main_window = Application.get_service('gui_manager').main_window
main_window = self.gui_manager.main_window
presentation = element.presentation[0]
diagram = presentation.canvas.diagram
try:
diagram = presentation.canvas.diagram
except AttributeError:
presentation = element.namespace.presentation[0]
diagram = presentation.canvas.diagram
diagram_tab = main_window.show_diagram(diagram)
view = diagram_tab.get_view()
view_item = view.find_view_item(presentation)
diagram_tab.get_view().focus(view_item)
diagram_tab.view.focused_item = presentation
def on_destroy(self, window):
self.window = None
self.treeview = None
# vim:sw=4:et

View File

@ -130,6 +130,7 @@ setup(
'xmi_export = gaphor.plugins.xmiexport:XMIExport',
'diagram_layout = gaphor.plugins.diagramlayout:DiagramLayout',
'pynsource = gaphor.plugins.pynsource:PyNSource',
'check_metamodel = gaphor.plugins.checkmetamodel:CheckModelWindow',
],
'gaphor.uicomponents': [
'mainwindow = gaphor.ui.mainwindow:MainWindow',