コード例 #1
0
 def setUp(self):
     self.addTypeEqualityFunc(Material,
                              partial(compare_material, testcase=self))
     self.materials = CUDS()
     self.example_materials = []
     for i in xrange(5):
         m = Material(description="Material {}".format(i))
         m.name = ""
         self.example_materials.append(m)
コード例 #2
0
 def setUp(self):
     self.addTypeEqualityFunc(Material,
                              partial(compare_material, testcase=self))
     self.materials = CUDS()
     self.example_materials = []
     for i in xrange(5):
         m = Material(description="Material {}".format(i))
         m.name = ""
         self.example_materials.append(m)
コード例 #3
0
    def test_save_CUDS_description(self):
        description = 'some very long description'
        filename = os.path.join(self.temp_dir, 'test_description.yml')
        C = CUDS(description=description)
        with closing(open(filename, 'w')) as handle:
            save_CUDS(handle, C)

        with closing(open(filename, 'r')) as handle:
            CC = load_CUDS(handle)
        self.assertEqual(CC.description, description)
コード例 #4
0
    def test_save_CUDS_name(self):
        name = 'somename'
        filename = os.path.join(self.temp_dir, 'test_named.yml')
        C = CUDS(name=name)
        with closing(open(filename, 'w')) as handle:
            save_CUDS(handle, C)

        with closing(open(filename, 'r')) as handle:
            CC = load_CUDS(handle)
        self.assertEqual(CC.name, name)
コード例 #5
0
def load_CUDS(handle):
    """ Load CUDS model from a Yaml file

    Parameters
    ----------
    handle: file handle
        yaml file containing CUDSComponents

    Raises
    ------
    FileError
        if yaml file contains errors

    Returns
    -------
    model: CUDS
        computational model

    """

    cuds_components = {}
    name = None
    desc = None
    for data in yaml.safe_load_all(handle):
        # Go through the dictionaries constructed from the Yaml script
        for dict_cuds in data:
            cubatype = dict_cuds.keys()[0]
            if cubatype == 'NAME':
                name = dict_cuds['NAME']
            elif cubatype == 'DESCRIPTION':
                desc = dict_cuds['DESCRIPTION']
            else:
                _dict_to_CUDSComponent(cubatype, dict_cuds[cubatype],
                                       cuds_components)

    model = CUDS(name=name or '', description=desc or '')
    for comp in cuds_components.values():
        model.add([comp])

    return model
コード例 #6
0
    def test_save_CUDS_complicated_data(self):
        filename = os.path.join(self.temp_dir, 'test_full.yml')
        cuds = CUDS(name='full',
                    description='model with crossreferenced components')

        M1 = Material(name='steel', description='FCC steel sphere structure')
        M2 = Material(name='epoxy', description='')
        M3 = Material(name='iron', description='sheet metal container')
        MR1 = MaterialRelation(name='steel spheres in epoxy',
                               material=[M1, M2])
        MR2 = MaterialRelation(name='epoxy in sheet metal container',
                               material=[M2, M3])
        # M1 is not added
        cuds.add([M2])
        cuds.add([M3])
        cuds.add([MR1])
        cuds.add([MR2])

        with closing(open(filename, 'w')) as handle:
            save_CUDS(handle, cuds)
            print("cuds data", cuds.data)

        with closing(open(filename, 'r')) as handle:
            loaded_cuds = load_CUDS(handle)
            print("loaded_cuds data", loaded_cuds.data)

        self.assertEqual(loaded_cuds.name, cuds.name)
        self.assertEqual(loaded_cuds.description, cuds.description)

        for cuds_item in cuds.iter(item_type=CUBA.CUDS_COMPONENT):
            print('item original', cuds_item)

        for cuds_item in loaded_cuds.iter(item_type=CUBA.CUDS_COMPONENT):
            print('item loaded', cuds_item)

        # Iterate over components in the original model and check
        # that they are present in the loaded model. Loaded model
        # has additionally material 'M1' included.
        for cuds_item in cuds.iter(item_type=CUBA.CUDS_COMPONENT):
            # Check items that have name parameter defined
            print("cuds_item", cuds_item)
            if cuds_item.name is not None:
                loaded_item = loaded_cuds.get(cuds_item.uid)
                print("loaded_item", loaded_item)
                for key in cuds_item.data.keys():
                    ci = cuds_item.data[key]
                    li = loaded_item.data[key]
                    _compare_components(ci, li, testcase=self)
コード例 #7
0
    def test_save_CUDS_empty(self):
        filename = os.path.join(self.temp_dir, 'test_empty.yml')
        C = CUDS(name='empty', description='just an empty model')
        with closing(open(filename, 'w')) as handle:
            save_CUDS(handle, C)

        with closing(open(filename, 'r')) as handle:
            CC = load_CUDS(handle)

        self.assertEqual(CC.name, C.name)
        self.assertEqual(CC.description, C.description)

        for item in CC.iter(item_type=CUBA.CUDS_COMPONENT):
            self.assertEqual(item, None)
コード例 #8
0
    def test_save_CUDS_complicated_data(self):
        filename = os.path.join(self.temp_dir, 'test_full.yml')
        cuds = CUDS(name='full',
                    description='model with crossreferenced components')

        M1 = Material(name='steel',
                      description='FCC steel sphere structure')
        M2 = Material(name='epoxy', description='')
        M3 = Material(name='iron', description='sheet metal container')
        MR1 = MaterialRelation(name='steel spheres in epoxy',
                               material=[M1, M2])
        MR2 = MaterialRelation(name='epoxy in sheet metal container',
                               material=[M2, M3])
        # M1 is not added
        cuds.add([M2])
        cuds.add([M3])
        cuds.add([MR1])
        cuds.add([MR2])

        with closing(open(filename, 'w')) as handle:
            save_CUDS(handle, cuds)
            print("cuds data", cuds.data)

        with closing(open(filename, 'r')) as handle:
            loaded_cuds = load_CUDS(handle)
            print("loaded_cuds data", loaded_cuds.data)

        self.assertEqual(loaded_cuds.name, cuds.name)
        self.assertEqual(loaded_cuds.description, cuds.description)

        for cuds_item in cuds.iter(item_type=CUBA.CUDS_COMPONENT):
            print('item original', cuds_item)

        for cuds_item in loaded_cuds.iter(item_type=CUBA.CUDS_COMPONENT):
            print('item loaded', cuds_item)

        # Iterate over components in the original model and check
        # that they are present in the loaded model. Loaded model
        # has additionally material 'M1' included.
        for cuds_item in cuds.iter(item_type=CUBA.CUDS_COMPONENT):
            # Check items that have name parameter defined
            print("cuds_item", cuds_item)
            if cuds_item.name is not None:
                loaded_item = loaded_cuds.get(cuds_item.uid)
                print("loaded_item", loaded_item)
                for key in cuds_item.data.keys():
                    ci = cuds_item.data[key]
                    li = loaded_item.data[key]
                    _compare_components(ci, li, testcase=self)
コード例 #9
0
    def test_create_wrapper(self):
        cuds = CUDS()
        example1 = \
            self.manager.create_wrapper(cuds,
                                        'EXAMPLE1',
                                        EngineInterface.Internal)
        self.assertIsInstance(example1, DummyEngine1)
        self.assertEqual(cuds, example1.get_cuds())

        # Explicitly add example2 engine
        cls = get_example_engine_extension()
        self.manager.register_extension(cls)
        example2 = \
            self.manager.create_wrapper(cuds,
                                        'EXAMPLE2',
                                        EngineInterface.Internal)
        self.assertIsInstance(example2, DummyEngine2)
        self.assertEqual(cuds, example2.get_cuds())
コード例 #10
0
class TestMaterials(unittest.TestCase):
    """Test case for Materials class."""

    def setUp(self):
        self.addTypeEqualityFunc(Material,
                                 partial(compare_material, testcase=self))
        self.materials = CUDS()
        self.example_materials = []
        for i in xrange(5):
            m = Material(description="Material {}".format(i))
            m.name = ""
            self.example_materials.append(m)

    def test_add_get_material(self):
        self.materials.add([self.example_materials[0]])
        self.assertEqual(self.materials.get(
            self.example_materials[0].uid),
            self.example_materials[0])

    def test_add_existing_material(self):
        # Adding the same material has no effect
        self.materials.add([self.example_materials[0]])
        self.materials.add([self.example_materials[0]])

        self.assertEqual(self.materials.count_of(CUBA.MATERIAL), 1)

    def test_get_missing_material(self):
        self.assertRaises(KeyError, self.materials.get, uuid.uuid4())

    def test_remove_missing_material(self):
        with self.assertRaises(KeyError):
            self.materials.remove([uuid.uuid4()])

    def test_iter_all_materials_with_ids(self):
        # given
        for material in self.example_materials:
            self.materials.add([material])

        # when
        iterated_all_materials =\
            {material.uid: material for material
             in self.materials.iter(item_type=CUBA.MATERIAL)}

        # then
        self.assertEqual(len(iterated_all_materials),
                         len(self.example_materials))
        for material in self.example_materials:
            self.assertEqual(material, iterated_all_materials[material.uid])

    def test_iter_subset_of_materials_with_ids(self):
        # given
        material_subset = [material for material in self.example_materials[:2]]

        subset_ids = [material.uid for material in material_subset]
        for material in self.example_materials:
            self.materials.add([material])

        # when
        iterated_materials = {material.uid: material for material
                              in self.materials.iter(item_type=CUBA.MATERIAL)
                              if material.uid in subset_ids}

        # then
        self.assertEqual(len(iterated_materials),
                         len(material_subset))
        for material in material_subset:
            self.assertEqual(material, iterated_materials[material.uid])
コード例 #11
0
class TestMaterials(unittest.TestCase):
    """Test case for Materials class."""
    def setUp(self):
        self.addTypeEqualityFunc(Material,
                                 partial(compare_material, testcase=self))
        self.materials = CUDS()
        self.example_materials = []
        for i in xrange(5):
            m = Material(description="Material {}".format(i))
            m.name = ""
            self.example_materials.append(m)

    def test_add_get_material(self):
        self.materials.add([self.example_materials[0]])
        self.assertEqual(self.materials.get(self.example_materials[0].uid),
                         self.example_materials[0])

    def test_add_existing_material(self):
        # Adding the same material has no effect
        self.materials.add([self.example_materials[0]])
        self.materials.add([self.example_materials[0]])

        self.assertEqual(self.materials.count_of(CUBA.MATERIAL), 1)

    def test_get_missing_material(self):
        self.assertRaises(KeyError, self.materials.get, uuid.uuid4())

    def test_remove_missing_material(self):
        with self.assertRaises(KeyError):
            self.materials.remove([uuid.uuid4()])

    def test_iter_all_materials_with_ids(self):
        # given
        for material in self.example_materials:
            self.materials.add([material])

        # when
        iterated_all_materials =\
            {material.uid: material for material
             in self.materials.iter(item_type=CUBA.MATERIAL)}

        # then
        self.assertEqual(len(iterated_all_materials),
                         len(self.example_materials))
        for material in self.example_materials:
            self.assertEqual(material, iterated_all_materials[material.uid])

    def test_iter_subset_of_materials_with_ids(self):
        # given
        material_subset = [material for material in self.example_materials[:2]]

        subset_ids = [material.uid for material in material_subset]
        for material in self.example_materials:
            self.materials.add([material])

        # when
        iterated_materials = {
            material.uid: material
            for material in self.materials.iter(item_type=CUBA.MATERIAL)
            if material.uid in subset_ids
        }

        # then
        self.assertEqual(len(iterated_materials), len(material_subset))
        for material in material_subset:
            self.assertEqual(material, iterated_materials[material.uid])