Commit dde5c114 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Recursive metainfo categories.

parent 195f6246
......@@ -677,6 +677,16 @@ class Definition(MObject):
""" Returns all definitions of this definition class. """
return cast(Iterable[MObjectBound], Definition.__all_definitions.get(cls, []))
@cached_property
def all_categories(self):
""" All categories of this definition and its categories. """
all_categories = list(self.categories)
for category in self.categories:
for super_category in category.all_categories:
all_categories.append(super_category)
return all_categories
class Quantity(Definition):
"""Used to define quantities that store a certain piece of (meta-)data.
......@@ -865,26 +875,17 @@ class Category(Definition):
Each definition, including categories themselves, can belong to a set of categories.
Categories therefore form a hierarchy of concepts that definitions can belong to, i.e.
they form a `is a` relationship."""
__all_instances: Dict[str, 'Category'] = {}
they form a `is a` relationship.
def __init__(self, name: str, **kwargs):
super().__init__(name=name, **kwargs)
Category.__all_instances[name] = self
In the old meta-info this was known as `abstract types`.
"""
@cached_property
def definitions(self) -> Iterable[Definition]:
""" All definitions that are directly or indirectly in this category. """
return list([
definition for definition in Definition.all_definitions()
if self in definition.categories])
# TODO this should be done differently
@classmethod
def get(cls, name: str) -> 'Category':
""" Returns the category with the given name. """
return cls.__all_instances.get(name, None)
if self in definition.all_categories])
Section.m_section = Section(repeats=True, name='Section', _bs=True)
......
......@@ -79,7 +79,7 @@ class TestPureReflection:
assert getattr(obj, 'test_quantity') == 'test_value'
Category(
material_defining = Category(
name='material_defining',
description='Quantities that add to what constitutes a different material.')
......@@ -98,8 +98,8 @@ class Run(MObject):
class System(MObject):
m_section = Section(repeats=True, parent=Run.m_section)
n_atoms = Quantity(type=int, default=0, categories=[Category.get('material_defining')])
atom_label = Quantity(type=str, shape=['n_atoms'], categories=[Category.get('material_defining')])
n_atoms = Quantity(type=int, default=0, categories=[material_defining])
atom_label = Quantity(type=str, shape=['n_atoms'], categories=[material_defining])
atom_positions = Quantity(type=np.dtype('f8'), shape=['n_atoms', 3])
......@@ -164,8 +164,8 @@ class TestM2:
def test_direct_category(self):
assert len(System.atom_label.categories)
assert Category.get('material_defining') in System.atom_label.categories
assert System.atom_label in Category.get('material_defining').definitions
assert material_defining in System.atom_label.categories
assert System.atom_label in material_defining.definitions
class TestM1:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment