|
6 | 6 | from deprecated.classic import deprecated |
7 | 7 | from jsonasobj2 import JsonObj, as_json, as_dict, JsonObjTypes, items |
8 | 8 | import jsonasobj2 |
9 | | -from rdflib import Graph |
| 9 | +from rdflib import Graph, URIRef |
10 | 10 | from yaml.constructor import ConstructorError |
11 | 11 |
|
12 | 12 | from linkml_runtime.utils.context_utils import CONTEXTS_PARAM_TYPE, merge_contexts |
@@ -243,6 +243,26 @@ def _normalize_inlined_slot(self, slot_name: str, slot_type: Type, key_name: Opt |
243 | 243 | self[slot_name] = dict_slot[0] |
244 | 244 | self._normalize_inlined_as_dict(slot_name, slot_type, key_name, keyed) |
245 | 245 |
|
| 246 | + @classmethod |
| 247 | + def _class_for(cls, attribute: str, uri_or_curie: Union[str, URIRef]) -> Optional[Type["YAMLRoot"]]: |
| 248 | + """ Locate self or descendant class that has attribute == uri_or_curie """ |
| 249 | + if getattr(cls, attribute, None) == uri_or_curie: |
| 250 | + return cls |
| 251 | + for subclass in cls.__subclasses__(): |
| 252 | + match = subclass._class_for(attribute, uri_or_curie) |
| 253 | + if match: |
| 254 | + return match |
| 255 | + return None |
| 256 | + |
| 257 | + def _class_for_uri(self, uri: str, use_model_uri: bool = False) -> Optional["YAMLRoot"]: |
| 258 | + """ |
| 259 | + Return the self or descendant of self having with a matching class uri |
| 260 | + """ |
| 261 | + return self.__class__._class_for('class_model_uri' if use_model_uri else 'class_class_uri', URIRef(uri)) |
| 262 | + |
| 263 | + def _class_for_curie(self, curie: str) -> Optional["YAMLRoot"]: |
| 264 | + return self.__class__._class_for('class_class_curie', curie) |
| 265 | + |
246 | 266 | # ================== |
247 | 267 | # Error intercepts |
248 | 268 | # ================== |
|
0 commit comments