Skip to content

Commit

Permalink
Merge pull request #190 from linkml/pythonize-slot-names
Browse files Browse the repository at this point in the history
Ensure inference_utils works for slots with spaces
  • Loading branch information
cmungall authored Jul 6, 2022
2 parents f6c94a6 + 9f04377 commit 597d29d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
2 changes: 2 additions & 0 deletions linkml_runtime/utils/inference_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def generate_slot_value(obj: YAMLRoot, slot_name: Union[str, SlotDefinitionName]
:return: inferred value, or None if not inference performed
"""
cls_name = type(obj).class_name
mapped_slot = schemaview.slot_name_mappings()[slot_name]
slot_name = mapped_slot.name
slot = schemaview.induced_slot(slot_name, cls_name)
logging.debug(f' CONF={config}')
if config.use_string_serialization:
Expand Down
11 changes: 10 additions & 1 deletion tests/test_utils/input/inference-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,14 @@ slots:
range: Address
description:
summary:
slot with spaces:
derived slot with spaces:
string_serialization: "{slot_with_spaces}"
derived expression from spaces:
equals_expression: "{slot_with_spaces}"




#==================================
# Classes #
#==================================
Expand All @@ -83,6 +89,9 @@ classes:
- description
- is_juvenile
- age_category
- slot with spaces
- derived slot with spaces
- derived expression from spaces
slot_usage:
description:
string_serialization: |-
Expand Down
56 changes: 55 additions & 1 deletion tests/test_utils/model/inference_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Auto generated from inference-example.yaml by pythongen.py version: 0.9.0
# Generation date: 2022-01-24T10:55:25
# Generation date: 2022-07-01T19:52:59
# Schema: inference
#
# id: https://w3id.org/linkml/examples/inference
Expand Down Expand Up @@ -46,6 +46,33 @@



@dataclass
class Term(YAMLRoot):
_inherited_slots: ClassVar[List[str]] = []

class_class_uri: ClassVar[URIRef] = EX.Term
class_class_curie: ClassVar[str] = "ex:Term"
class_name: ClassVar[str] = "Term"
class_model_uri: ClassVar[URIRef] = EX.Term

id: Optional[str] = None
name: Optional[str] = None
synonyms: Optional[Union[str, List[str]]] = empty_list()

def __post_init__(self, *_: List[str], **kwargs: Dict[str, Any]):
if self.id is not None and not isinstance(self.id, str):
self.id = str(self.id)

if self.name is not None and not isinstance(self.name, str):
self.name = str(self.name)

if not isinstance(self.synonyms, list):
self.synonyms = [self.synonyms] if self.synonyms is not None else []
self.synonyms = [v if isinstance(v, str) else str(v) for v in self.synonyms]

super().__post_init__(**kwargs)


@dataclass
class Person(YAMLRoot):
_inherited_slots: ClassVar[List[str]] = []
Expand All @@ -65,6 +92,9 @@ class Person(YAMLRoot):
description: Optional[str] = None
is_juvenile: Optional[Union[bool, Bool]] = None
age_category: Optional[Union[str, "AgeEnum"]] = None
slot_with_spaces: Optional[str] = None
derived_slot_with_spaces: Optional[str] = None
derived_expression_from_spaces: Optional[str] = None
summary: Optional[str] = None

def __post_init__(self, *_: List[str], **kwargs: Dict[str, Any]):
Expand Down Expand Up @@ -98,6 +128,15 @@ def __post_init__(self, *_: List[str], **kwargs: Dict[str, Any]):
if self.age_category is not None and not isinstance(self.age_category, AgeEnum):
self.age_category = AgeEnum(self.age_category)

if self.slot_with_spaces is not None and not isinstance(self.slot_with_spaces, str):
self.slot_with_spaces = str(self.slot_with_spaces)

if self.derived_slot_with_spaces is not None and not isinstance(self.derived_slot_with_spaces, str):
self.derived_slot_with_spaces = str(self.derived_slot_with_spaces)

if self.derived_expression_from_spaces is not None and not isinstance(self.derived_expression_from_spaces, str):
self.derived_expression_from_spaces = str(self.derived_expression_from_spaces)

if self.summary is not None and not isinstance(self.summary, str):
self.summary = str(self.summary)

Expand Down Expand Up @@ -215,6 +254,12 @@ class slots:
slots.id = Slot(uri=EX.id, name="id", curie=EX.curie('id'),
model_uri=EX.id, domain=None, range=Optional[str])

slots.name = Slot(uri=EX.name, name="name", curie=EX.curie('name'),
model_uri=EX.name, domain=None, range=Optional[str])

slots.synonyms = Slot(uri=EX.synonyms, name="synonyms", curie=EX.curie('synonyms'),
model_uri=EX.synonyms, domain=None, range=Optional[Union[str, List[str]]])

slots.full_name = Slot(uri=EX.full_name, name="full_name", curie=EX.curie('full_name'),
model_uri=EX.full_name, domain=None, range=Optional[str])

Expand Down Expand Up @@ -257,6 +302,15 @@ class slots:
slots.summary = Slot(uri=EX.summary, name="summary", curie=EX.curie('summary'),
model_uri=EX.summary, domain=None, range=Optional[str])

slots.slot_with_spaces = Slot(uri=EX.slot_with_spaces, name="slot with spaces", curie=EX.curie('slot_with_spaces'),
model_uri=EX.slot_with_spaces, domain=None, range=Optional[str])

slots.derived_slot_with_spaces = Slot(uri=EX.derived_slot_with_spaces, name="derived slot with spaces", curie=EX.curie('derived_slot_with_spaces'),
model_uri=EX.derived_slot_with_spaces, domain=None, range=Optional[str])

slots.derived_expression_from_spaces = Slot(uri=EX.derived_expression_from_spaces, name="derived expression from spaces", curie=EX.curie('derived_expression_from_spaces'),
model_uri=EX.derived_expression_from_spaces, domain=None, range=Optional[str])

slots.relationship__person1 = Slot(uri=EX.person1, name="relationship__person1", curie=EX.curie('person1'),
model_uri=EX.relationship__person1, domain=None, range=Optional[Union[dict, Person]])

Expand Down
9 changes: 8 additions & 1 deletion tests/test_utils/test_inference_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def test_string_serialization(self):
c = Container(persons=[Person(first_name=FIRST, last_name=LAST)])
infer_all_slot_values(c, schemaview=sv)
self.assertEqual(c.persons[0].full_name, FULL)

# test slots with spaces
p = Person(slot_with_spaces="test")
infer_all_slot_values(p, schemaview=sv)
self.assertEqual(p.derived_slot_with_spaces, "test")

def test_infer_expressions(self):
"""
Expand Down Expand Up @@ -105,6 +108,10 @@ def test_infer_expressions(self):
#e2 = AgeEnum.juvenile
#print(f'e1={e1} c={e1.code} t={type(e1)}')
#print(f'e2={e2} {type(e2)}')
# test slots with spaces
p = Person(slot_with_spaces="test")
infer_all_slot_values(p, schemaview=sv, config=config)
self.assertEqual(p.derived_expression_from_spaces, "test")

def test_if_then(self):
sv = SchemaView(SCHEMA)
Expand Down

0 comments on commit 597d29d

Please sign in to comment.