Skip to content

Commit 49aecb2

Browse files
authored
Merge pull request #323 from linkml/improve-class-usages
Display `any_of`, `exactly_one_of`, `none_of`, `all_of` range assertions in Usages section of class documentation page
2 parents c7815cb + ef4f5a5 commit 49aecb2

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

linkml_runtime/utils/schemaview.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ def usage_index(self) -> Dict[ElementName, List[SchemaUsage]]:
16571657
16581658
:return: dictionary of SchemaUsages keyed by used elements
16591659
"""
1660-
ROLES = ['domain', 'range']
1660+
ROLES = ['domain', 'range', 'any_of', 'exactly_one_of', 'none_of', 'all_of']
16611661
ix = defaultdict(list)
16621662
for cn, c in self.all_classes().items():
16631663
direct_slots = c.slots
@@ -1671,6 +1671,10 @@ def usage_index(self) -> Dict[ElementName, List[SchemaUsage]]:
16711671
vl = [v]
16721672
for x in vl:
16731673
if x is not None:
1674+
if isinstance(x, AnonymousSlotExpression):
1675+
x = x.range
1676+
k = f"{k}[range]"
1677+
k = k.split("[")[0] + "[range]" if "[range]" in k else k
16741678
u = SchemaUsage(used_by=cn, slot=sn, metaslot=k, used=x)
16751679
u.inferred = sn in direct_slots
16761680
ix[x].append(u)

tests/test_utils/input/kitchen_sink_noimports.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,15 @@ classes:
8787
- age in years
8888
- addresses
8989
- has birth event
90+
- reason_for_happiness
9091
slot_usage:
9192
name:
9293
pattern: "^\\S+ \\S+" ## do not do this in a real schema, people have all kinds of names
94+
reason_for_happiness:
95+
any_of:
96+
- range: BirthEvent
97+
- range: EmploymentEvent
98+
- range: MarriageEvent
9399

94100
Adult:
95101
is_a: Person
@@ -353,6 +359,10 @@ slots:
353359
alias: zip
354360
range: string
355361

362+
reason_for_happiness:
363+
range: string
364+
required: false
365+
356366

357367
enums:
358368
FamilialRelationshipType:

tests/test_utils/test_schemaview.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def test_schemaview(self):
186186

187187
self.assertCountEqual(['id', 'name', ## From Thing
188188
'has employment history', 'has familial relationships', 'has medical history',
189-
AGE_IN_YEARS, 'addresses', 'has birth event', ## From Person
189+
AGE_IN_YEARS, 'addresses', 'has birth event', 'reason_for_happiness', ## From Person
190190
'aliases' ## From HasAliases
191191
],
192192
view.class_slots('Person'))
@@ -256,6 +256,42 @@ def test_schemaview(self):
256256
logging.debug(f' {k} = {v}')
257257
self.assertIn(SchemaUsage(used_by='FamilialRelationship', slot=RELATED_TO,
258258
metaslot='range', used='Person', inferred=False), u['Person'])
259+
self.assertListEqual(
260+
[SchemaUsage(used_by='Person',
261+
slot='reason_for_happiness',
262+
metaslot='any_of[range]',
263+
used='MarriageEvent',
264+
inferred=True
265+
),
266+
SchemaUsage(used_by='Adult',
267+
slot='reason_for_happiness',
268+
metaslot='any_of[range]',
269+
used='MarriageEvent',
270+
inferred=False
271+
)],
272+
u['MarriageEvent'])
273+
self.assertListEqual(
274+
[SchemaUsage(used_by='Person',
275+
slot='has employment history',
276+
metaslot='range',
277+
used='EmploymentEvent',
278+
inferred=True),
279+
SchemaUsage(used_by='Person',
280+
slot='reason_for_happiness',
281+
metaslot='any_of[range]',
282+
used='EmploymentEvent',
283+
inferred=True),
284+
SchemaUsage(used_by='Adult',
285+
slot='has employment history',
286+
metaslot='range',
287+
used='EmploymentEvent',
288+
inferred=False),
289+
SchemaUsage(used_by='Adult',
290+
slot='reason_for_happiness',
291+
metaslot='any_of[range]',
292+
used='EmploymentEvent',
293+
inferred=False)],
294+
u['EmploymentEvent'])
259295

260296
# test methods also work for attributes
261297
leaves = view.class_leaves()

0 commit comments

Comments
 (0)