]>
dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-associations.py
2 """Selected Association/Role properties from frozen baselines; no canonical XML."""
6 import xml
.etree
.ElementTree
as ET
8 root
= pathlib
.Path(__file__
).resolve().parent
.parent
9 report
= json
.loads(pathlib
.Path(sys
.argv
[1]).read_text())
10 assert report
['kind'] == 'semantic-observations-not-cxtm'
11 assert report
['failed'] == 0 and report
['passed'] == len(report
['checks'])
12 assert len(report
['checks']) == len(set(report
['checks']))
13 files
= ['association', 'association-binary', 'association-binary-duplicate', 'association-type-sid',
14 'association-with-comment', 'association-with-comment2', 'association-with-comment3', 'association-with-comment4']
15 assert {c
['id'] for c
in report
['cases']} == {'ctm/in/'+f
+'.ctm' for f
in files
}
16 assert len(report
['cases']) == len(files
)
17 for case
in report
['cases']:
18 assert case
['ctmSource'] == (root
/'vendor/cxtm'/case
['id']).read_bytes().decode('utf-8')
19 xml
= ET
.parse(root
/'vendor/cxtm/ctm/baseline'/(pathlib
.Path(case
['id']).name
+'.cxtm')).getroot()
20 assert xml
.tag
== 'topicMap' and all(c
.tag
in ['topic', 'association'] for c
in xml
)
21 model
= case
['actualObjects']
22 expected_topics
, expected_associations
= xml
.findall('topic'), xml
.findall('association')
23 assert model
['topicCount'] == len(expected_topics
)
24 assert model
['associationCount'] == len(expected_associations
)
25 assert model
['roleCount'] == sum(len(a
.findall('role')) for a
in expected_associations
)
27 def identifiers(topic
, baseline
=False):
29 for prop
in ['itemIdentifiers', 'subjectIdentifiers', 'subjectLocators']:
30 values
= ([v
.text
for v
in topic
.findall(prop
+'/locator')] if baseline
else topic
[prop
])
31 values
= [case
['documentIRI']+v
if v
.startswith('#') else v
for v
in values
]
32 assert len(values
) == len(set(values
))
33 result
.append(tuple(sorted(values
)))
36 actual_topics
= {identifiers(t
): t
for t
in model
['topics']}
37 assert len(actual_topics
) == model
['topicCount']
39 for topic
in expected_topics
:
40 actual
= actual_topics
[identifiers(topic
, True)]
41 assert actual
['parentIsMap'] and actual
['nameCount'] == 0
42 assert all(c
.tag
in ['itemIdentifiers', 'subjectIdentifiers', 'subjectLocators', 'rolePlayed'] for c
in topic
)
43 topic_mapping
[int(topic
.attrib
['number'])] = actual
['ordinal']
45 def role_signature(role
, baseline
=False):
47 return (topic_mapping
[int(role
.find('type').attrib
['topicref'])],
48 topic_mapping
[int(role
.find('player').attrib
['topicref'])])
49 return role
['typeTopicOrdinal'], role
['playerTopicOrdinal']
51 def association_signature(association
, baseline
=False):
52 return ((topic_mapping
[int(association
.find('type').attrib
['topicref'])] if baseline
53 else association
['typeTopicOrdinal']),
54 tuple(sorted(role_signature(r
, baseline
) for r
in
55 (association
.findall('role') if baseline
else association
['roles']))))
57 actual_associations
= {association_signature(a
): a
for a
in model
['associations']}
58 assert len(actual_associations
) == model
['associationCount']
60 for association
in expected_associations
:
61 assert all(c
.tag
in ['type', 'role'] for c
in association
)
62 actual
= actual_associations
[association_signature(association
, True)]
63 assert actual
['parentIsMap'] and actual
['roles']
64 roles
= {role_signature(r
): r
for r
in actual
['roles']}
65 assert len(roles
) == len(actual
['roles'])
66 for role
in association
.findall('role'):
67 assert all(c
.tag
in ['type', 'player'] for c
in role
)
68 observed
= roles
[role_signature(role
, True)]
69 assert observed
['parentAssociationOrdinal'] == actual
['ordinal']
70 role_mapping
[(int(association
.attrib
['number']), int(role
.attrib
['number']))] = (actual
['ordinal'], observed
['ordinal'])
71 for topic
in expected_topics
:
73 for ref
in topic
.findall('rolePlayed'):
74 parts
= ref
.attrib
['ref'].split('.')
75 assert parts
[0] == 'association' and parts
[2] == 'role' and len(parts
) == 4
76 expected_played
.append(role_mapping
[(int(parts
[1]), int(parts
[3]))])
77 actual
= actual_topics
[identifiers(topic
, True)]
78 observed
= [(r
['associationOrdinal'], r
['roleOrdinal']) for r
in actual
['rolesPlayed']]
79 assert sorted(observed
) == sorted(expected_played
)
80 print('ASSOCIATION-BASELINE-SEMANTIC-EVIDENCE-PASS: 8 frozen baselines, Role items and roles-played; no canonicalization')