From 7391b8a408da02f502b4e20591efa27a74f452fc Mon Sep 17 00:00:00 2001 From: caufieldjh Date: Sun, 11 Aug 2024 13:49:27 -0400 Subject: [PATCH] Add ephemeral cache to mapping --- src/ontogpt/engines/knowledge_engine.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/ontogpt/engines/knowledge_engine.py b/src/ontogpt/engines/knowledge_engine.py index 3dc6e64c6..c8d482db7 100644 --- a/src/ontogpt/engines/knowledge_engine.py +++ b/src/ontogpt/engines/knowledge_engine.py @@ -106,6 +106,9 @@ class KnowledgeEngine(ABC): mappers: Optional[List[BasicOntologyInterface]] = None """List of concept mappers, to assist in grounding to desired ID prefix""" + map_cache: Dict[str, str] = field(default_factory=dict) + """Cache of mappings obtained using the specified mappers.""" + labelers: Optional[List[BasicOntologyInterface]] = None """Labelers that map CURIEs to labels""" @@ -395,6 +398,12 @@ def map_identifier(self, input_id: str, cls: ClassDefinition) -> Iterator[str]: :param cls: :return: """ + if input_id not in self.map_cache: + self.map_cache[input_id] = [] + else: + for obj_id in self.map_cache[input_id]: + yield obj_id + try: if input_id.startswith("http://purl.bioontology.org/ontology"): # TODO: this should be fixed upstream in OAK @@ -416,6 +425,7 @@ def map_identifier(self, input_id: str, cls: ClassDefinition) -> Iterator[str]: for mapper in self.mappers: if isinstance(mapper, MappingProviderInterface): for mapping in mapper.sssom_mappings([input_id]): + self.map_cache[input_id].append(str(mapping.object_id)) yield str(mapping.object_id) else: raise ValueError(f"Unknown mapper type {mapper}")