def test_string_to_multiplicity(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) self.assertEqual(assoc.string_to_multiplicity("0..1"), ("0", "1")) self.assertEqual(assoc.string_to_multiplicity("0..*"), ("0", "*")) self.assertEqual(assoc.string_to_multiplicity("*..1"), ("*", "1"))
def association_parse(package, source_element, dest_element, source, dest): id = source_element.get('{%s}id' % ns['xmi']) association = UMLAssociation(package, source, dest, id) assoc_type = source_element.get("aggregation") if assoc_type == "composite": association.association_type = UMLAssociationType.COMPOSITION elif assoc_type == "shared": association.association_type = UMLAssociationType.AGGREGATION # Extract multiplicity for source source_lower = source_element.find('lowerValue') if source_lower is not None: source_lower = source_lower.get('value') if source_lower == '-1': source_lower = '*' source_upper = source_element.find('upperValue').get('value') if source_upper == '-1': source_upper = '*' association.source_multiplicity = (source_lower, source_upper) # Extract multiplicity for dest dest_lower = dest_element.find('lowerValue') if dest_lower is not None: dest_lower = dest_lower.get('value') if dest_lower == '-1': dest_lower = '*' dest_upper = dest_element.find('upperValue').get('value') if dest_upper == '-1': dest_upper = '*' association.destination_multiplicity = (dest_lower, dest_upper) # print('{} {} to {}'.format(association.source.name, association.association_type, association.destination.name)) # If it's an association to or from a multiple then pluralize the name # TODO: Allow pluralized name to be specified in UML if dest_element.get('name') is not None: association.source_name = dest_element.get('name') else: # Use opposing ends class name as attribute name for association association.destination_name = association.destination.name.lower() if association.destination_multiplicity[1] == '*': association.destination_name += 's' if source_element.get('name') is not None: association.destination_name = source_element.get('name') else: # Use opposing ends class name as attribute name for association association.source_name = association.source.name.lower() if association.source_multiplicity[1] == '*': association.source_name += 's' # print('Assoc in {}: {} to {}: type = {}'.format(self.source.name, self.source_name, self.destination_name, # self.association_type) ) return association
def association_parse(package: UMLPackage, element, root): id = element.get("id") cell = element.find("mxCell") source: UMLClass = package.find_by_id(cell.get("source")) target: UMLClass = package.find_by_id(cell.get("target")) element_type = element.get("UMLType") association = UMLAssociation(package, source, target, id, UMLAssociationType[element_type.upper()]) # Extract multiplicities ret = root.findall('.//object[@UMLType="DestinationMultiplicity"]') for dest in ret: dm = dest.find('mxCell[@parent="{}"]'.format(association.id)) if dm is not None: label = dest.get("label").strip('<div>').strip('</div>') association.destination_multiplicity = association.string_to_multiplicity( label) break ret = root.findall('.//object[@UMLType="SourceMultiplicity"]') for dest in ret: dm = dest.find('mxCell[@parent="{}"]'.format(association.id)) if dm is not None: label = dest.get("label").strip('<div>').strip('</div>') association.source_multiplicity = association.string_to_multiplicity( label) break # Set association destination name destination_name = element.get("destination_name") if destination_name is not None: association.destination_name = destination_name else: association.destination_name = target.name if association.source_multiplicity[ 1] == '*' and association.association_type == UMLAssociationType.ASSOCIATION: association.destination_name += 's' elif association.destination_multiplicity[ 1] == '*' and association.association_type == UMLAssociationType.COMPOSITION: association.destination_name += 's' # Set association source name source_name = element.get("source_name") if source_name is not None: association.source_name = source_name else: association.source_name = source.name if association.destination_multiplicity[ 1] == '*' and association.association_type == UMLAssociationType.ASSOCIATION: association.source_name += 's' if association.source_multiplicity[ 1] == '*' and association.association_type == UMLAssociationType.COMPOSITION: association.source_name += 's'
def test_association_type(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) self.assertEqual(assoc.association_type, UMLAssociationType.ASSOCIATION) assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1, UMLAssociationType.COMPOSITION) self.assertEqual(assoc.association_type, UMLAssociationType.COMPOSITION)
def test_association_cardinality(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) assoc.source_multiplicity = ("0", "1") assoc.destination_multiplicity = ("0", "*") self.assertEqual(assoc.cardinality, Cardinality.ONE_TO_MANY) assoc.source_multiplicity = ("0", "1") assoc.destination_multiplicity = ("0", "1") self.assertEqual(assoc.cardinality, Cardinality.ONE_TO_ONE) assoc.source_multiplicity = ("0", "*") assoc.destination_multiplicity = ("0", "1") self.assertEqual(assoc.cardinality, Cardinality.MANY_TO_ONE) assoc.source_multiplicity = ("0", "*") assoc.destination_multiplicity = ("0", "*") self.assertEqual(assoc.cardinality, Cardinality.MANY_TO_MANY)
def test_association_type(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) assoc.source_multiplicity = ("0", "1") assoc.destination_multiplicity = ("0", "*") self.assertEqual(assoc.association_type, "OneToMany") assoc.source_multiplicity = ("0", "1") assoc.destination_multiplicity = ("0", "1") self.assertEqual(assoc.association_type, "OneToOne") assoc.source_multiplicity = ("0", "*") assoc.destination_multiplicity = ("0", "1") self.assertEqual(assoc.association_type, "ManyToOne")