Skip to content

Commit

Permalink
Merge pull request #323 from linkml/improve-class-usages
Browse files Browse the repository at this point in the history
Display `any_of`, `exactly_one_of`, `none_of`, `all_of` range assertions in Usages section of class documentation page
  • Loading branch information
cmungall authored May 29, 2024
2 parents c7815cb + ef4f5a5 commit 49aecb2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
6 changes: 5 additions & 1 deletion linkml_runtime/utils/schemaview.py
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ def usage_index(self) -> Dict[ElementName, List[SchemaUsage]]:
:return: dictionary of SchemaUsages keyed by used elements
"""
ROLES = ['domain', 'range']
ROLES = ['domain', 'range', 'any_of', 'exactly_one_of', 'none_of', 'all_of']
ix = defaultdict(list)
for cn, c in self.all_classes().items():
direct_slots = c.slots
Expand All @@ -1671,6 +1671,10 @@ def usage_index(self) -> Dict[ElementName, List[SchemaUsage]]:
vl = [v]
for x in vl:
if x is not None:
if isinstance(x, AnonymousSlotExpression):
x = x.range
k = f"{k}[range]"
k = k.split("[")[0] + "[range]" if "[range]" in k else k
u = SchemaUsage(used_by=cn, slot=sn, metaslot=k, used=x)
u.inferred = sn in direct_slots
ix[x].append(u)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_utils/input/kitchen_sink_noimports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,15 @@ classes:
- age in years
- addresses
- has birth event
- reason_for_happiness
slot_usage:
name:
pattern: "^\\S+ \\S+" ## do not do this in a real schema, people have all kinds of names
reason_for_happiness:
any_of:
- range: BirthEvent
- range: EmploymentEvent
- range: MarriageEvent

Adult:
is_a: Person
Expand Down Expand Up @@ -353,6 +359,10 @@ slots:
alias: zip
range: string

reason_for_happiness:
range: string
required: false


enums:
FamilialRelationshipType:
Expand Down
38 changes: 37 additions & 1 deletion tests/test_utils/test_schemaview.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def test_schemaview(self):

self.assertCountEqual(['id', 'name', ## From Thing
'has employment history', 'has familial relationships', 'has medical history',
AGE_IN_YEARS, 'addresses', 'has birth event', ## From Person
AGE_IN_YEARS, 'addresses', 'has birth event', 'reason_for_happiness', ## From Person
'aliases' ## From HasAliases
],
view.class_slots('Person'))
Expand Down Expand Up @@ -256,6 +256,42 @@ def test_schemaview(self):
logging.debug(f' {k} = {v}')
self.assertIn(SchemaUsage(used_by='FamilialRelationship', slot=RELATED_TO,
metaslot='range', used='Person', inferred=False), u['Person'])
self.assertListEqual(
[SchemaUsage(used_by='Person',
slot='reason_for_happiness',
metaslot='any_of[range]',
used='MarriageEvent',
inferred=True
),
SchemaUsage(used_by='Adult',
slot='reason_for_happiness',
metaslot='any_of[range]',
used='MarriageEvent',
inferred=False
)],
u['MarriageEvent'])
self.assertListEqual(
[SchemaUsage(used_by='Person',
slot='has employment history',
metaslot='range',
used='EmploymentEvent',
inferred=True),
SchemaUsage(used_by='Person',
slot='reason_for_happiness',
metaslot='any_of[range]',
used='EmploymentEvent',
inferred=True),
SchemaUsage(used_by='Adult',
slot='has employment history',
metaslot='range',
used='EmploymentEvent',
inferred=False),
SchemaUsage(used_by='Adult',
slot='reason_for_happiness',
metaslot='any_of[range]',
used='EmploymentEvent',
inferred=False)],
u['EmploymentEvent'])

# test methods also work for attributes
leaves = view.class_leaves()
Expand Down

0 comments on commit 49aecb2

Please sign in to comment.