Source code for pyuseocl.printer

# coding=utf-8

Generate a USE OCL specification from a modeL.
This is currently only a preliminary version.

#TODO: to be continued

import logging

log = logging.getLogger('test.' + __name__)

[docs]class UseOCLPrinter(object): def __init__(self, model): self.theModel = model self.output = ''
[docs] def do(self): self.output = '' self.model(self.theModel) return self.output
[docs] def out(self, s): self.output += s
[docs] def model(self, model): self.out('model %s\n\n' % for e in model.enumerations.values(): self.enumeration(e) for c in model.classes.values(): self.class_(c) for a in model.associations.values(): self.association(a) for ac in model.associationClasses.values(): self.associationClass(ac)
[docs] def enumeration(self, enumeration): literals = ','.join(enumeration.literals) self.out( 'enum { %s };\n' % literals )
[docs] def class_(self, class_): if class_.superclasses: sc = '< '+','.join(map(lambda, class_.superclasses)) else: sc = '' self.out("class %s %s\n" % (, sc)) if class_.attributes: self.out('attributes\n') for attribute in class_.attributes.values(): self.attribute(attribute) if class_.operations: self.out('operations\n') for operation in class_.operations.values(): self.operation(operation) if class_.invariants: for invariant in class_.invariants.values(): self.invariant(invariant) self.out('end\n\n')
[docs] def association(self, association): self.out('%s %s between\n' % (association.kind, for role in association.roles.values(): self.role(role) self.out('end\n\n')
[docs] def associationClass(self, associationClass): if associationClass.superclasses: sc = ' < ' + ','.join(associationClass.superclasses.values()) else: sc = '' self.out('associationclass %s%s between\n' % (, sc)) for role in associationClass.roles.values(): self.role(role) if associationClass.attributes: self.out('attributes\n') for attribute in associationClass.attributes.values(): self.attribute(attribute) if associationClass.operations: self.out('operations\n') for operation in associationClass.operations.values(): self.operation(operation) self.out('end\n\n')
[docs] def attribute(self, attribute): self.out(' %s : %s\n' % (, attribute.type))
[docs] def operation(self, operation): self.out(' %s\n' % operation.signature)
# TODO - to be continued
[docs] def invariant(self, invariant): pass
[docs] def role(self, role): if rn = 'role ' else: rn = '' max = '*' if role.cardinalityMax is None else role.cardinalityMax self.out(' %s[%s..%s] %s\n' % (, role.cardinalityMin, max, rn ))