|
| 1 | +from logging import Logger |
| 2 | +from typing import cast, Dict |
| 3 | + |
| 4 | +from linkml_runtime.linkml_model.meta import ( |
| 5 | + ClassDefinitionName, |
| 6 | + SchemaDefinition, |
| 7 | + SlotDefinition, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +def _create_function_dispatcher(slot: SlotDefinition, logger: Logger) -> None: |
| 12 | + """Function dispatcher for slot inlining processing""" |
| 13 | + |
| 14 | + def set_inlined_and_warn(range_class_has_identifier: bool) -> None: |
| 15 | + slot.inlined = True |
| 16 | + text_identifier = "with an identifier" if range_class_has_identifier else "without an identifier" |
| 17 | + msg = ( |
| 18 | + f"Slot '{slot.name}' is requesting for an object {text_identifier} inlining as a " |
| 19 | + + "list, but no inlining requested! Forcing `inlined: true`!!" |
| 20 | + ) |
| 21 | + logger.warning(msg) |
| 22 | + |
| 23 | + def set_inlined_and_report(range_class_has_identifier: bool) -> None: |
| 24 | + slot.inlined = True |
| 25 | + text_identifier = "with an identifier" if range_class_has_identifier else "without an identifier" |
| 26 | + msg = ( |
| 27 | + f"Slot '{slot.name}' is requesting for an object {text_identifier} inlining as a " |
| 28 | + + "list, but no inlining requested! Forcing `inlined: true`!!" |
| 29 | + ) |
| 30 | + logger.info(msg) |
| 31 | + |
| 32 | + def debug_output(range_class_has_identifier: bool) -> None: |
| 33 | + msg = ( |
| 34 | + f"Slot '{slot.name}' has a complete inlining specification: " |
| 35 | + + f"range class has identifier: {range_class_has_identifier}," |
| 36 | + ) |
| 37 | + if slot.inlined_as_list is None: |
| 38 | + msg += f"`inlined: {slot.inlined}`, `inlined_as_list` unspecified." |
| 39 | + else: |
| 40 | + msg += f"`inlined: {slot.inlined}` and `inlined_as_list: {slot.inlined_as_list}`" |
| 41 | + logger.debug(msg) |
| 42 | + |
| 43 | + def info(range_class_has_identifier: bool) -> None: |
| 44 | + text_identifier = "with an identifier" if range_class_has_identifier else "without an identifier" |
| 45 | + msg = ( |
| 46 | + f"Slot '{slot.name}' has following illogic or incomplete inlining " |
| 47 | + + f"specification: `inlined: {slot.inlined}` and `inlined_as_list: " |
| 48 | + + f"{slot.inlined_as_list}` for objects {text_identifier}" |
| 49 | + ) |
| 50 | + logger.info(msg) |
| 51 | + |
| 52 | + function_map = { |
| 53 | + # OK |
| 54 | + (True, True, True): debug_output, |
| 55 | + # OK |
| 56 | + (True, True, False): debug_output, |
| 57 | + # what type of inlining to use? |
| 58 | + (True, True, None): info, |
| 59 | + # overriding specified value!! |
| 60 | + (True, False, True): set_inlined_and_warn, |
| 61 | + # why specifying inlining type if no inlining? |
| 62 | + (True, False, False): info, |
| 63 | + # OK |
| 64 | + (True, False, None): debug_output, |
| 65 | + # applying implicit default!! |
| 66 | + (True, None, True): set_inlined_and_report, |
| 67 | + # why specifying inlining type if inlining not requested? |
| 68 | + (True, None, False): info, |
| 69 | + # no defaults, in-code implicit defaults will apply |
| 70 | + (True, None, None): info, |
| 71 | + # OK |
| 72 | + (False, True, True): debug_output, |
| 73 | + # how to select a key for an object without an identifier? |
| 74 | + (False, True, False): info, |
| 75 | + # no defaults, in-code implicit defaults will apply |
| 76 | + (False, True, None): info, |
| 77 | + # how to add a reference to an object without an identifier? |
| 78 | + (False, False, True): info, |
| 79 | + # how to add a reference to an object without an identifier? |
| 80 | + (False, False, False): info, |
| 81 | + # how to add a reference to an object without an identifier? |
| 82 | + (False, False, None): info, |
| 83 | + # applying implicit default!! |
| 84 | + (False, None, True): set_inlined_and_report, |
| 85 | + # why specifying inlining type if inlining not requested? |
| 86 | + (False, None, False): info, |
| 87 | + # no defaults, in-code implicit defaults will apply |
| 88 | + (False, None, None): info, |
| 89 | + } |
| 90 | + |
| 91 | + def dispatch(range_class_has_identifier, inlined, inlined_as_list): |
| 92 | + # func = function_map.get((range_class_has_identifier, inlined, inlined_as_list), default_function) |
| 93 | + func = function_map.get((range_class_has_identifier, inlined, inlined_as_list)) |
| 94 | + return func(range_class_has_identifier) |
| 95 | + |
| 96 | + return dispatch |
| 97 | + |
| 98 | + |
| 99 | +def process(slot: SlotDefinition, schema_map: Dict[str, SchemaDefinition], logger: Logger) -> None: |
| 100 | + """ |
| 101 | + Processing the inlining behavior of a slot, including the type of inlining |
| 102 | + (as a list or as a dictionary). |
| 103 | +
|
| 104 | + Processing encompasses analyzing the combination of elements relevant for |
| 105 | + object inlining (reporting the result of the analysis with different logging |
| 106 | + levels) and enforcing certain values. |
| 107 | +
|
| 108 | + It is important to take into account following: |
| 109 | + - slot.inlined and slot.inlined_as_list can have three different values: |
| 110 | + True, False or None (if nothing specified in the schema) |
| 111 | + - if a class has an identifier is a pure boolean |
| 112 | +
|
| 113 | + Changes to `inlined` are applied directly on the provided slot object. |
| 114 | +
|
| 115 | + :param slot: the slot to process |
| 116 | + :param schema: the schema in which the slot is contained |
| 117 | + :param logger: the logger to use |
| 118 | + """ |
| 119 | + |
| 120 | + # first of all, validate that the values of `inlined` and `inlined_as_list` are legal |
| 121 | + # either `True` or `False` (if specified) or `None` (if nothing specified) |
| 122 | + for value in ("inlined", "inlined_as_list"): |
| 123 | + if getattr(slot, value) not in (True, False, None): |
| 124 | + raise ValueError( |
| 125 | + f"Invalid value for '{value}' in the schema for slot " + f"'{slot.name}': '{getattr(slot, value)}'" |
| 126 | + ) |
| 127 | + range_class = None |
| 128 | + for schema in schema_map.values(): |
| 129 | + if cast(ClassDefinitionName, slot.range) in schema.classes: |
| 130 | + range_class = schema.classes[cast(ClassDefinitionName, slot.range)] |
| 131 | + break |
| 132 | + # range is a type |
| 133 | + if range_class is None: |
| 134 | + return |
| 135 | + range_has_identifier = False |
| 136 | + for sn in range_class.slots: |
| 137 | + for schema in schema_map.values(): |
| 138 | + if sn in schema.slots: |
| 139 | + range_has_identifier = bool(schema.slots[sn].identifier or schema.slots[sn].key) |
| 140 | + break |
| 141 | + else: |
| 142 | + continue |
| 143 | + break |
| 144 | + |
| 145 | + dispatcher = _create_function_dispatcher(slot, logger) |
| 146 | + dispatcher(range_has_identifier, slot.inlined, slot.inlined_as_list) |
0 commit comments