diff --git a/bigraph_schema/type_functions.py b/bigraph_schema/type_functions.py index ff406c4..6bcd7cc 100644 --- a/bigraph_schema/type_functions.py +++ b/bigraph_schema/type_functions.py @@ -76,7 +76,7 @@ from typing import NewType, Union, Mapping, List, Dict, Optional, Callable from dataclasses import field, make_dataclass -from bigraph_schema.units import units +from bigraph_schema.units import units, render_units_type from bigraph_schema.registry import ( NONE_SYMBOL, type_schema_keys, is_schema_key, non_schema_keys, type_parameter_key, @@ -2520,11 +2520,57 @@ def read_shape(shape): shape)]) +# handling unit registration +# TODO -- can this be done onto the dicts, rather than using the core? +def register_units(core, units): + for unit_name in units._units: + try: + unit = getattr(units, unit_name) + except: + # print(f'no unit named {unit_name}') + continue + + dimensionality = unit.dimensionality + type_key = render_units_type(dimensionality) + if not core.exists(type_key): + core.register(type_key, { + '_default': '', + '_apply': apply_units, + '_check': check_units, + '_serialize': serialize_units, + '_deserialize': deserialize_units, + '_description': 'type to represent values with scientific units'}) + + return core + + +# function to add the unit types to the type library +def add_units_to_library(units, type_library): + for unit_name in units._units: + try: + unit = getattr(units, unit_name) + except AttributeError: + continue + + dimensionality = unit.dimensionality + type_key = render_units_type(dimensionality) + if type_key not in type_library: + type_library[type_key] = { + '_type': 'unit', + 'name': unit_name, + 'dimensionality': str(dimensionality) + } + + return type_library + # =============================== # Types with their type functions # =============================== # These dictionaries define the types and their corresponding type functions. +unit_types = {} +unit_types = add_units_to_library(units, unit_types) + registry_types = { 'any': { '_type': 'any', @@ -2735,3 +2781,4 @@ def read_shape(shape): '_description': 'hyperedges in the bigraph, with inputs and outputs as type parameters', 'inputs': 'wires', 'outputs': 'wires'}} + diff --git a/bigraph_schema/type_system.py b/bigraph_schema/type_system.py index 6b76fd3..6743ceb 100644 --- a/bigraph_schema/type_system.py +++ b/bigraph_schema/type_system.py @@ -8,15 +8,15 @@ import functools import inspect import random +import traceback from pprint import pformat as pf from bigraph_schema import Registry, non_schema_keys, is_schema_key, deep_merge, type_parameter_key from bigraph_schema.parse import parse_expression from bigraph_schema.registry import type_schema_keys, remove_omitted, set_path, transform_path -from bigraph_schema.type_functions import set_apply, registry_types, base_type_library, register_base_reactions, \ - union_keys, is_empty, \ - apply_schema, apply_units, check_units, serialize_units, deserialize_units -from bigraph_schema.units import units, render_units_type +from bigraph_schema.type_functions import set_apply, registry_types, base_type_library, unit_types, register_base_reactions, \ + union_keys, is_empty, apply_schema, register_units +from bigraph_schema.units import units TYPE_FUNCTION_KEYS = [ @@ -107,7 +107,9 @@ def __init__(self): self.register_types(registry_types) self.register_types(base_type_library) + # self.register_types(unit_types) + # # TODO -- add a proper registration into registry register_units(self, units) register_base_reactions(self) @@ -211,7 +213,6 @@ def register(self, key, schema, alternate_keys=tuple(), force=False): else: lookup = self.find(subschema) if lookup is None: - import ipdb; ipdb.set_trace() raise Exception( f'trying to register a new type ({key}), ' f'but it depends on a type ({subkey}) which is not in the registry') @@ -2060,25 +2061,3 @@ def compose(self, a, b): def query(self, schema, instance, redex): subschema = {} return subschema - - -def register_units(core, units): - for unit_name in units._units: - try: - unit = getattr(units, unit_name) - except: - # print(f'no unit named {unit_name}') - continue - - dimensionality = unit.dimensionality - type_key = render_units_type(dimensionality) - if not core.exists(type_key): - core.register(type_key, { - '_default': '', - '_apply': apply_units, - '_check': check_units, - '_serialize': serialize_units, - '_deserialize': deserialize_units, - '_description': 'type to represent values with scientific units'}) - - return core