|
| 1 | +from logging import Logger |
| 2 | +from typing import cast |
| 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_report(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 debug_output(range_class_has_identifier: bool) -> None: |
| 24 | + msg = ( |
| 25 | + f"Slot '{slot.name}' has a complete inlining specification: " |
| 26 | + + f"range class has identifier: {range_class_has_identifier}," |
| 27 | + ) |
| 28 | + if slot.inlined_as_list is None: |
| 29 | + msg += f"`inlined: {slot.inlined}`, `inlined_as_list` unspecified." |
| 30 | + else: |
| 31 | + msg += f"`inlined: {slot.inlined}` and `inlined_as_list: {slot.inlined_as_list}`" |
| 32 | + logger.debug(msg) |
| 33 | + |
| 34 | + def warning(range_class_has_identifier: bool) -> None: |
| 35 | + text_identifier = "with an identifier" if range_class_has_identifier else "without an identifier" |
| 36 | + msg = ( |
| 37 | + f"Slot '{slot.name}' has following illogic or incomplete inlining " |
| 38 | + + f"specification: `inlined: {slot.inlined}` and `inlined_as_list: " |
| 39 | + + f"{slot.inlined_as_list}` for objects {text_identifier}" |
| 40 | + ) |
| 41 | + logger.warning(msg) |
| 42 | + |
| 43 | + function_map = { |
| 44 | + # OK |
| 45 | + (True, True, True): debug_output, |
| 46 | + # OK |
| 47 | + (True, True, False): debug_output, |
| 48 | + # what type of inlining to use? |
| 49 | + (True, True, None): warning, |
| 50 | + # overriding specified value!! |
| 51 | + (True, False, True): set_inlined_and_report, |
| 52 | + # why specifying inlining type if no inlining? |
| 53 | + (True, False, False): warning, |
| 54 | + # OK |
| 55 | + (True, False, None): debug_output, |
| 56 | + # applying implicit default!! |
| 57 | + (True, None, True): set_inlined_and_report, |
| 58 | + # why specifying inlining type if inlining not requested? |
| 59 | + (True, None, False): warning, |
| 60 | + # no defaults, in-code implicit defaults will apply |
| 61 | + (True, None, None): warning, |
| 62 | + # OK |
| 63 | + (False, True, True): debug_output, |
| 64 | + # how to select a key for an object without an identifier? |
| 65 | + (False, True, False): warning, |
| 66 | + # no defaults, in-code implicit defaults will apply |
| 67 | + (False, True, None): warning, |
| 68 | + # how to add a reference to an object without an identifier? |
| 69 | + (False, False, True): warning, |
| 70 | + # how to add a reference to an object without an identifier? |
| 71 | + (False, False, False): warning, |
| 72 | + # how to add a reference to an object without an identifier? |
| 73 | + (False, False, None): warning, |
| 74 | + # applying implicit default!! |
| 75 | + (False, None, True): set_inlined_and_report, |
| 76 | + # why specifying inlining type if inlining not requested? |
| 77 | + (False, None, False): warning, |
| 78 | + # no defaults, in-code implicit defaults will apply |
| 79 | + (False, None, None): warning, |
| 80 | + } |
| 81 | + |
| 82 | + def dispatch(range_class_has_identifier, inlined, inlined_as_list): |
| 83 | + # func = function_map.get((range_class_has_identifier, inlined, inlined_as_list), default_function) |
| 84 | + func = function_map.get((range_class_has_identifier, inlined, inlined_as_list)) |
| 85 | + return func(range_class_has_identifier) |
| 86 | + |
| 87 | + return dispatch |
| 88 | + |
| 89 | + |
| 90 | +def process(slot: SlotDefinition, schema: SchemaDefinition, logger: Logger) -> None: |
| 91 | + """ |
| 92 | + Processing the inlining behavior of a slot, including the type of inlining |
| 93 | + (as a list or as a dictionary). |
| 94 | +
|
| 95 | + Processing encompasses analyzing the combination of elements relevant for |
| 96 | + object inlining (reporting the result of the analysis with different logging |
| 97 | + levels) and enforcing certain values. |
| 98 | +
|
| 99 | + It is important to take into account following: |
| 100 | + - slot.inlined and slot.inlined_as_list can have three different values: |
| 101 | + True, False or None (if nothing specified in the schema) |
| 102 | + - if a class has an identifier is a pure boolean |
| 103 | +
|
| 104 | + Changes to `inlined` are applied directly on the provided slot object. |
| 105 | +
|
| 106 | + :param slot: the slot to process |
| 107 | + :param schema: the schema in which the slot is contained |
| 108 | + :param logger: the logger to use |
| 109 | + """ |
| 110 | + |
| 111 | + # first of all, validate that the values of `inlined` and `inlined_as_list` are legal |
| 112 | + # either `True` or `False` (if specified) or `None` (if nothing specified) |
| 113 | + for value in ("inlined", "inlined_as_list"): |
| 114 | + if getattr(slot, value) not in (True, False, None): |
| 115 | + raise ValueError( |
| 116 | + f"Invalid value for '{value}' in the schema for slot " + f"'{slot.name}': '{getattr(slot, value)}'" |
| 117 | + ) |
| 118 | + range_class = schema.classes[cast(ClassDefinitionName, slot.range)] |
| 119 | + range_has_identifier = any([schema.slots[s].identifier or schema.slots[s].key for s in range_class.slots]) |
| 120 | + |
| 121 | + dispatcher = _create_function_dispatcher(slot, logger) |
| 122 | + dispatcher(range_has_identifier, slot.inlined, slot.inlined_as_list) |
0 commit comments