diff --git a/doc/source/api/cuds.rst b/doc/source/api/cuds.rst index 6afc309f..a53b85b5 100644 --- a/doc/source/api/cuds.rst +++ b/doc/source/api/cuds.rst @@ -42,12 +42,12 @@ CUDS Computational Model .. autosummary:: - ~model.CUDS + ~cuds.CUDS ~simulation.Simulation .. rubric:: Description -.. automodule:: simphony.cuds.model +.. automodule:: simphony.cuds.cuds :members: :undoc-members: diff --git a/simphony/core/__init__.py b/simphony/core/__init__.py index 3ed2f9d7..adacb1cc 100644 --- a/simphony/core/__init__.py +++ b/simphony/core/__init__.py @@ -6,4 +6,5 @@ class Default(): pass + __all__ = ['CUBA', 'DataContainer'] diff --git a/simphony/cuds/__init__.py b/simphony/cuds/__init__.py index 84c6df57..515d96dc 100644 --- a/simphony/cuds/__init__.py +++ b/simphony/cuds/__init__.py @@ -7,7 +7,7 @@ from .lattice_items import LatticeNode from .particles import Particles from .particles_items import Particle, Bond -from .model import CUDS +from .cuds import CUDS from .simulation import Simulation from .meta import api diff --git a/simphony/cuds/model.py b/simphony/cuds/cuds.py similarity index 100% rename from simphony/cuds/model.py rename to simphony/cuds/cuds.py diff --git a/simphony/cuds/tests/test_materials.py b/simphony/cuds/tests/test_materials.py index b59dce4c..9524c020 100644 --- a/simphony/cuds/tests/test_materials.py +++ b/simphony/cuds/tests/test_materials.py @@ -2,7 +2,7 @@ import uuid from functools import partial -from simphony.cuds.model import CUDS +from simphony.cuds import CUDS from simphony.cuds.meta.api import Material from simphony.testing.utils import compare_material diff --git a/simphony/io/serialisation.py b/simphony/io/serialisation.py index 92d7a84a..60088079 100644 --- a/simphony/io/serialisation.py +++ b/simphony/io/serialisation.py @@ -1,13 +1,13 @@ import yaml import numpy - -from simphony.cuds.model import CUDS -from simphony.cuds.meta.cuds_component import CUDSComponent -from simphony.core.keywords import KEYWORDS -# from simphony.cuds.meta import * -from simphony.core.cuba import CUBA +import uuid from collections import OrderedDict from importlib import import_module + +from simphony.core import CUBA +from simphony.core.keywords import KEYWORDS +from simphony.cuds import CUDS +from simphony.cuds.meta.api import CUDSComponent from simphony.cuds.meta.validation import to_camel_case _CUDSREFMAP = OrderedDict() @@ -91,7 +91,7 @@ def load_CUDS(handle): """ - comp_dict = {} + cuds_components = {} name = None desc = None for data in yaml.safe_load_all(handle): @@ -104,11 +104,11 @@ def load_CUDS(handle): desc = dict_cuds['DESCRIPTION'] else: _dict_to_CUDSComponent(cubatype, dict_cuds[cubatype], - comp_dict) + cuds_components) model = CUDS(name=name, description=desc) - for compid in comp_dict.keys(): - model.add(comp_dict[compid]) + for comp in cuds_components.values(): + model.add(comp) return model @@ -147,11 +147,8 @@ def _CUDSComponent_to_yaml(comp, stream): # Go through the keys in the component cdata = comp._data for key in cdata.keys(): - # Skip the UID key, as it's not accepted by the deserializer - if key == CUBA.UID: - continue # Check if the data is a list or numpy types or CUDSComponents - elif type(cdata[key]) == list: + if type(cdata[key]) == list: # List of simple types? if KEYWORDS[key._name_].dtype in [numpy.float64, numpy.int32, bool]: @@ -210,7 +207,7 @@ def _CUDSComponent_to_yaml(comp, stream): return stream -def _dict_to_CUDSComponent(cubatype, comp, comp_dict={}): +def _dict_to_CUDSComponent(cubatype, comp, comp_dict=None): """ Generate a CUDSComponent on the basis of a dictionary provided by PyYaml library. @@ -226,12 +223,16 @@ def _dict_to_CUDSComponent(cubatype, comp, comp_dict={}): id(CUDSComponent): CUDSComponent """ - + if comp_dict is None: + comp_dict = {} # Check that comp does not refer to one of the components that # have been already constructed if id(comp) in comp_dict.keys(): return + # Pop out uid, since we need to set it differently. + uid = comp.pop('UID') + if cubatype in KEYWORDS.keys(): # Find corresponding module and instantiate the class mod = import_module('simphony.cuds.meta.%s' % cubatype.lower()) @@ -245,8 +246,7 @@ def _dict_to_CUDSComponent(cubatype, comp, comp_dict={}): system_managed_keys = {} supp_params = [str(e).replace('CUBA.', '') for e in comp_class.supported_parameters()] - # Don't accept UUID entry from the yaml file - supp_params.remove('UID') + for key in comp.keys(): # Check if the key is supported if key in supp_params: @@ -290,6 +290,9 @@ def _dict_to_CUDSComponent(cubatype, comp, comp_dict={}): # Add system managed components by updating the DataContainer data = comp_inst.data data.update(system_managed_keys) + + # Set the initial uid + data[CUBA.UID] = uuid.UUID(uid) comp_inst.data = data else: message = 'Unknown CUDSComponent "{}"' @@ -298,3 +301,5 @@ def _dict_to_CUDSComponent(cubatype, comp, comp_dict={}): # Store component under the key representing it's memory location # so that correct object references from the yaml file are preserved comp_dict[id(comp)] = comp_inst + + return comp_dict diff --git a/simphony/io/tests/test_serialisation.py b/simphony/io/tests/test_serialisation.py index 8092ef51..a239ef30 100644 --- a/simphony/io/tests/test_serialisation.py +++ b/simphony/io/tests/test_serialisation.py @@ -8,10 +8,10 @@ import tempfile import uuid -from simphony.cuds.meta.cuds_component import CUDSComponent -from simphony.cuds.meta.material import Material -from simphony.cuds.meta.material_relation import MaterialRelation -from simphony.cuds.model import CUDS +from simphony.cuds.meta.api import CUDSComponent +from simphony.cuds.meta.api import Material +from simphony.cuds.meta.api import MaterialRelation +from simphony.cuds import CUDS from simphony.io.serialisation import save_CUDS, load_CUDS @@ -62,8 +62,8 @@ def test_save_CUDS_empty(self): def test_save_CUDS_complicated_data(self): filename = os.path.join(self.temp_dir, 'test_full.yml') - C = CUDS(name='full', - description='model with crossreferenced components') + cuds = CUDS(name='full', + description='model with crossreferenced components') M1 = Material(name='steel', description='FCC steel sphere structure') @@ -74,31 +74,42 @@ def test_save_CUDS_complicated_data(self): MR2 = MaterialRelation(name='epoxy in sheet metal container', material=[M2, M3]) # M1 is not added - C.add(M2) - C.add(M3) - C.add(MR1) - C.add(MR2) + cuds.add(M2) + cuds.add(M3) + cuds.add(MR1) + cuds.add(MR2) with closing(open(filename, 'w')) as handle: - save_CUDS(handle, C) + save_CUDS(handle, cuds) + print("cuds data", cuds.data) with closing(open(filename, 'r')) as handle: - CC = load_CUDS(handle) + loaded_cuds = load_CUDS(handle) + print("loaded_cuds data", loaded_cuds.data) - self.assertEqual(CC.name, C.name) - self.assertEqual(CC.description, C.description) + self.assertEqual(loaded_cuds.name, cuds.name) + self.assertEqual(loaded_cuds.description, cuds.description) + + for cuds_item in cuds.iter(CUDSComponent): + print('item original', cuds_item) + + for cuds_item in loaded_cuds.iter(CUDSComponent): + 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 Citem in C.iter(CUDSComponent): + for cuds_item in cuds.iter(CUDSComponent): # Check items that have name parameter defined - if Citem.name is not None: - CCitem = CC.get(Citem.name) - for key in Citem.data.keys(): - Ci = Citem.data[key] - CCi = CCitem.data[key] - _compare_components(Ci, CCi, testcase=self) + print("cuds_item", cuds_item) + if cuds_item.name is not None: + loaded_item = loaded_cuds.get_by_uid(cuds_item.uid) + print("loaded_item", loaded_item) + for key in cuds_item.data.keys(): + print(key) + ci = cuds_item.data[key] + li = loaded_item.data[key] + _compare_components(ci, li, testcase=self) def _compare_components(comp1, comp2, testcase):