def serialize_class(Cls): ''' Serialize an xtUML metamodel class. ''' metaclass = xtuml.get_metaclass(Cls) attributes = ['%s %s' % (name, ty.upper()) for name, ty in metaclass.attributes] s = 'CREATE TABLE %s (\n ' % metaclass.kind s += ',\n '.join(attributes) s += '\n);\n' return s
def pretty_unique_identifier(inst, identifier): ''' Create a human-readable representation a unique identifier. ''' values = '' prefix = '' metaclass = xtuml.get_metaclass(inst) for name, ty in metaclass.attributes: if name in metaclass.identifying_attributes: value = getattr(inst, name) value = xtuml.serialize_value(value, ty) values += '%s%s=%s' % (prefix, name, value) prefix = ', ' return '%s(%s)' % (identifier, values)
def pretty_from_link(inst, link): ''' Create a human-readable representation of a link on the 'FROM'-side ''' values = '' prefix = '' metaclass = xtuml.get_metaclass(inst) for name, ty in metaclass.attributes: if name in link.key_map: value = getattr(inst, name) value = xtuml.serialize_value(value, ty) values += '%s%s=%s' % (prefix, name, value) prefix = ', ' return '%s(%s)' % (metaclass.kind, values)
def populate_connections(self, metamodel): ''' Populate links in a *metamodel* with connections between them. ''' storage = dict() for ass in metamodel.associations: source_class = ass.source_link.to_metaclass target_class = ass.target_link.to_metaclass if target_class not in storage: storage[target_class] = dict() link_key = frozenset(ass.source_link.key_map.values()) if link_key not in storage[target_class]: storage[target_class][link_key] = dict() for other_inst in target_class.storage: inst_key = ass.source_link.compute_index_key(other_inst) if inst_key is None: continue if inst_key not in storage[target_class][link_key]: storage[target_class][link_key][ inst_key] = xtuml.OrderedSet() storage[target_class][link_key][inst_key].add(other_inst) for inst in source_class.storage: inst_key = ass.source_link.compute_lookup_key(inst) if inst_key is None: continue if inst_key not in storage[target_class][link_key]: continue for other_inst in storage[target_class][link_key][inst_key]: ass.source_link.connect(other_inst, inst, check=False) ass.target_link.connect(inst, other_inst, check=False) for inst in metamodel.instances: metaclass = xtuml.get_metaclass(inst) for attr in metaclass.referential_attributes: if attr in inst.__dict__: delattr(inst, attr)
def populate_connections(self, metamodel): ''' Populate links in a *metamodel* with connections between them. ''' storage = dict() for ass in metamodel.associations: source_class = ass.source_link.to_metaclass target_class = ass.target_link.to_metaclass if target_class not in storage: storage[target_class] = dict() link_key = frozenset(ass.source_link.key_map.values()) if link_key not in storage[target_class]: storage[target_class][link_key] = dict() for other_inst in target_class.storage: inst_key = ass.source_link.compute_index_key(other_inst) if inst_key is None: continue if inst_key not in storage[target_class][link_key]: storage[target_class][link_key][inst_key] = xtuml.OrderedSet() storage[target_class][link_key][inst_key].add(other_inst) for inst in source_class.storage: inst_key = ass.source_link.compute_lookup_key(inst) if inst_key is None: continue if inst_key not in storage[target_class][link_key]: continue for other_inst in storage[target_class][link_key][inst_key]: ass.source_link.connect(other_inst, inst, check=False) ass.target_link.connect(inst, other_inst, check=False) for inst in metamodel.instances: metaclass = xtuml.get_metaclass(inst) for attr in metaclass.referential_attributes: if attr in inst.__dict__: delattr(inst, attr)
def serialize_instance(instance): ''' Serialize an *instance* from a metamodel. ''' attr_count = 0 metaclass = xtuml.get_metaclass(instance) s = 'INSERT INTO %s VALUES (' % metaclass.kind for name, ty in metaclass.attributes: value = getattr(instance, name) s += '\n ' s += serialize_value(value, ty) attr_count += 1 if attr_count < len(metaclass.attributes): s += ', -- %s : %s' % (name, ty) else: s += ' -- %s : %s' % (name, ty) s += '\n);\n' return s