def test_del_from_collection(): ''' Removing an item from the dictionary should drop the key that is identifier and a value that is the new object, also remove object from internal list :return: None ''' module = sys.modules[__name__] working_dict = create_some_objects() for name, value in working_dict.items(): setattr(module, name, value) tested_collection = Collection() tested_collection.add_object_to_collection(dummy_car) tested_collection.add_object_to_collection(dummy_LR_wheel) tested_collection.add_object_to_collection(dummy_LF_wheel) tested_collection.add_object_to_collection(dummy_RR_wheel) tested_collection.add_object_to_collection(dummy_RF_wheel) id_to_pull = tested_collection.object_to_id_map[hex(id(dummy_LR_wheel))] assert len(tested_collection) == 5 assert len(tested_collection.collected_objects) == 5 tested_collection.remove_object_from_collection(id_to_pull) assert len(tested_collection) == 4 assert len(tested_collection.collected_objects) == 4 assert dummy_car in tested_collection.collected_objects assert dummy_LR_wheel not in tested_collection.collected_objects
def test_del_from_collection_via_constraint(): logging.debug('Running test test_del_from_collection_via_constraint') module = sys.modules[__name__] working_dict = create_some_objects() for name, value in working_dict.items(): setattr(module, name, value) tested_collection = Collection() tested_collection.add_object_to_collection(dummy_car) tested_collection.add_object_to_collection(dummy_LR_wheel) tested_collection.add_object_to_collection(dummy_LF_wheel) tested_collection.add_object_to_collection(dummy_RR_wheel) tested_collection.add_object_to_collection(dummy_RF_wheel) id_to_pull = tested_collection.object_to_id_map[hex(id(dummy_LR_wheel))] assert len(tested_collection) == 5 assert len(tested_collection.collected_objects) == 5 tested_collection.remove_object_from_collection(id_to_pull) assert tested_collection.check_internal_constraints()
def test_add_to_collection(): ''' Adding a new item should expand the dictionary with a key that is a new unique identifier and a value that is the new object, add object to internal list :return: None ''' module = sys.modules[__name__] working_dict = create_some_objects() for name, value in working_dict.items(): setattr(module, name, value) tested_collection = Collection() tested_collection.add_object_to_collection(dummy_car) tested_collection.add_object_to_collection(dummy_LR_wheel) tested_collection.add_object_to_collection(dummy_LF_wheel) tested_collection.add_object_to_collection(dummy_RR_wheel) tested_collection.add_object_to_collection(dummy_RF_wheel) # use trick for loading up the objects assert len(tested_collection) == 5 assert len(tested_collection.collected_objects) == 5 assert dummy_car in tested_collection.collected_objects assert dummy_car in tested_collection.id_to_object_map.values() assert hex(id(dummy_car)) in tested_collection.object_to_id_map.keys()
def test_no_resolve_against_self(): logging.debug('Running test test_no_resolve_against_self') module = sys.modules[__name__] working_dict = create_some_objects_with_intersting_properties() for name, value in working_dict.items(): setattr(module, name, value) tested_collection = Collection() tested_collection.add_object_to_collection_with_id(dummy_car, 'xyz_car') tested_collection.add_object_to_collection_with_id(dummy_LR_wheel, 'xyz_LR') tested_collection.add_object_to_collection_with_id(dummy_LF_wheel, 'xyz_LF') tested_collection.add_object_to_collection_with_id(dummy_RR_wheel, 'xyz_RR') tested_collection.add_object_to_collection_with_id(dummy_RF_wheel, 'xyz_RF') tested_collection.add_object_to_collection_with_id(dummy_grocery1, 'DG1') tested_collection.add_object_to_collection_with_id(dummy_grocery2, 'DG2') dummy_car.idiot_reference = 'xyz_car' tested_collection.resolve_references('xyz_car') assert dummy_car.idiot_reference == 'xyz_car'
def main(): ''' Adding a new item should expand the dictionary with a key that is a new unique identifier and a value that is the new object, add object to internal list :return: None ''' module = sys.modules[__name__] pp = pprint.PrettyPrinter(indent=2) working_dict = create_some_objects_with_properties() for name, value in working_dict.items(): setattr(module, name, value) tested_collection = Collection() tested_collection.add_object_to_collection(dummy_car) tested_collection.add_object_to_collection(dummy_LR_wheel) tested_collection.add_object_to_collection(dummy_LF_wheel) tested_collection.add_object_to_collection(dummy_RR_wheel) tested_collection.add_object_to_collection(dummy_RF_wheel) print("\nAll objects in.\n") pp.pprint(repr(tested_collection.id_to_object_map)) pp.pprint(repr(tested_collection.object_to_id_map)) print("\nObject and properties in.\n") for prop in inspect.getmembers(dummy_car): if prop[0][0] != '_': pp.pprint(repr(prop)) print("\nObject dict with object links.\n") id_to_pull = tested_collection.object_to_id_map[hex(id(dummy_car))] pp.pprint(tested_collection.dump_obj_as_dict(id_to_pull)) print("\nObject dict with references.\n") tested_collection.dereference_links(id_to_pull) pp.pprint(tested_collection.dump_obj_as_dict(id_to_pull)) print("\nObject dict resolved again.\n") tested_collection.resolve_references(id_to_pull) pp.pprint(tested_collection.dump_obj_as_dict(id_to_pull))
def test_deref_ints_bools_nulls(): ''' Removing an item from the dictionary should drop the key that is identifier and a value that is the new object, also remove object from internal list :return: None ''' logging.debug('Running test test_deref_ints_bools_nulls') module = sys.modules[__name__] working_dict = create_some_objects_with_intersting_properties() for name, value in working_dict.items(): setattr(module, name, value) tested_collection = Collection() tested_collection.add_object_to_collection_with_id(dummy_car, 'xyz_car') tested_collection.add_object_to_collection_with_id(dummy_LR_wheel, 'xyz_LR') tested_collection.add_object_to_collection_with_id(dummy_LF_wheel, 'xyz_LF') tested_collection.add_object_to_collection_with_id(dummy_RR_wheel, 'xyz_RR') tested_collection.add_object_to_collection_with_id(dummy_RF_wheel, 'xyz_RF') tested_collection.add_object_to_collection_with_id(dummy_grocery1, 'DG1') tested_collection.add_object_to_collection_with_id(dummy_grocery2, 'DG2') print(tested_collection.dump_obj_as_dict('xyz_car')) tested_collection.resolve_references('xyz_car') print(tested_collection.dump_obj_as_dict('xyz_car')) print(tested_collection.live_link_reverse_lookup) assert len(tested_collection) == 7 assert len(tested_collection.collected_objects) == 7 tested_collection.remove_object_from_collection('xyz_LF') tested_collection.remove_object_from_collection('xyz_RF') tested_collection.remove_object_from_collection('DG1') logging.debug('Post removal state: {0}'.format(tested_collection.dump_obj_as_dict('xyz_car'))) tested_collection.dereference_links('xyz_car') assert len(tested_collection) == 4 assert len(tested_collection.collected_objects) == 4
def main(): ''' Adding a new item should expand the dictionary with a key that is a new unique identifier and a value that is the new object, add object to internal list :return: None ''' module = sys.modules[__name__] pp = pprint.PrettyPrinter(indent=2) working_dict = create_some_objects() for name, value in working_dict.items(): setattr(module, name, value) tested_collection = Collection() tested_collection.add_object_to_collection(dummy_car) tested_collection.add_object_to_collection(dummy_LR_wheel) tested_collection.add_object_to_collection(dummy_LF_wheel) tested_collection.add_object_to_collection(dummy_RR_wheel) tested_collection.add_object_to_collection(dummy_RF_wheel) print("\nAll objects in.\n") pp.pprint(repr(tested_collection.id_to_object_map)) pp.pprint(repr(tested_collection.object_to_id_map)) print("\nRemove Dummy LR Wheel\n") id_to_pull = tested_collection.object_to_id_map[hex(id(dummy_LR_wheel))] tested_collection.remove_object_from_collection(id_to_pull) pp.pprint(repr(tested_collection.id_to_object_map)) pp.pprint(repr(tested_collection.object_to_id_map))
def test_del_from_collection_fixed_id(): ''' Removing an item from the dictionary should drop the key that is identifier and a value that is the new object, also remove object from internal list :return: None ''' logging.debug('Running test test_del_from_collection_fixed_id') module = sys.modules[__name__] working_dict = create_some_objects() for name, value in working_dict.items(): setattr(module, name, value) tested_collection = Collection() tested_collection.add_object_to_collection_with_id(dummy_car, 'xyz_car') tested_collection.add_object_to_collection_with_id(dummy_LR_wheel, 'xyz_LR') tested_collection.add_object_to_collection_with_id(dummy_LF_wheel, 'xyz_LF') tested_collection.add_object_to_collection_with_id(dummy_RR_wheel, 'xyz_RR') tested_collection.add_object_to_collection_with_id(dummy_RF_wheel, 'xyz_RF') assert len(tested_collection) == 5 assert len(tested_collection.collected_objects) == 5 tested_collection.remove_object_from_collection('xyz_LF') tested_collection.remove_object_from_collection('xyz_RF') tested_collection.remove_object_from_collection('xyz_car') assert len(tested_collection) == 2 assert len(tested_collection.collected_objects) == 2 assert dummy_LR_wheel in tested_collection.collected_objects assert dummy_car not in tested_collection.collected_objects assert dummy_RF_wheel not in tested_collection.collected_objects
def load_from_file(self, file_path): ''' Process an XMI file into a set of collections (each Package becomes a collection) :param file_path: Location of XMI file :return: LXML parse root. Also alters internal state by populating self.collection_list with gathered UML Packages. ''' loaded_xmi = open(file_path, 'r') load_up = etree.parse(loaded_xmi) package_list = self.initial_parse(load_up) # transform found packages into collections # the general strategy should be to recurse into all owned meta-attributes of a given object, # live-linking the appropriate objects as Python objects and posting them to the collection # to provide id references to non-owned meta-attribute fields for package in package_list: model_collection = None for attribute in package.attrib.keys(): split_key = etree.QName(attribute) # cycle through the keys (can get more efficient once we learn how # to compose keys with namespaces if split_key.localname == 'name': model_collection = Collection() model_collection.name = package.attrib[attribute] self.collection_list.append(model_collection) for packedElement in package: split_tag = etree.QName(packedElement.tag) if split_tag.localname == 'packagedElement': thing_name = '' thing_type = '' thing_id = '' new_thing = None added_properties = [] for attribute in packedElement.attrib.keys(): split_key = etree.QName(attribute) # cycle through the keys (can get more efficient once we learn how # to compose keys with namespaces if split_key.localname == 'name': thing_name = packedElement.attrib[attribute] if split_key.localname == 'id': thing_id = packedElement.attrib[attribute] if split_key.localname == 'type': thing_type = packedElement.attrib[attribute] # add class to package to be part of collection if packedElement.attrib[attribute] == 'uml:Class': new_thing = Class() added_properties = self.collect_attributes( packedElement) for added_prop in added_properties: new_thing.owned_attribute.append( added_properties[added_prop]) elif packedElement.attrib[ attribute] == 'uml:Association': new_thing = Association() added_properties = self.collect_attributes( packedElement) # MD has an included element, Papyrus just a tag collect_ends = self.collect_ends(packedElement) for new_end in collect_ends: new_thing.member_end.append(new_end) for new_end in added_properties: new_thing.owned_end.append(new_end) elif packedElement.attrib[ attribute] == 'uml:AssociationClass': new_thing = AssociationClass() added_properties = self.collect_attributes( packedElement) # this logic needs to be recursive to handle owned elements and their specializations if new_thing is not None and thing_name is not None: new_thing.name = thing_name model_collection.add_object_to_collection_with_id( new_thing, thing_id) for new_prop in added_properties: model_collection.add_object_to_collection_with_id( added_properties[new_prop], new_prop) # Once all objects are in the collection, need to resolve their references to make # them live # Note that some references will remain unresolved because their targets are in a different collection # (package) pp = PrettyPrinter(indent=2) for package in self.collection_list: print('Object in ' + package.name) for collected in package.id_to_object_map: pp.pprint(package.dump_obj_as_dict(collected)) for package in self.collection_list: for collected in package.id_to_object_map: package.resolve_references(collected) return load_up