]>
dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-arity.py
2 """Independently verify observed n-ary object references and emitted CXTM."""
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'] == 'association-arity-evidence' and report
['failed'] == 0
11 assert report
['passed'] == len(report
['checks']) == len(set(report
['checks']))
12 original
= report
['original']
13 assert original
['id'] == 'ctm/in/association-duplicate-role.ctm'
14 assert original
['ctmSource'] == (ROOT
/ 'vendor/cxtm' / original
['id']).read_text()
15 assert original
['topicCount'] == 7 and original
['associationCount'] == 1
16 baseline
= ROOT
/ 'vendor/cxtm/ctm/baseline/association-duplicate-role.ctm.cxtm'
17 assert len(baseline
.read_bytes()) == 1157
18 xml
= ET
.parse(baseline
).getroot()
19 doc
= original
['documentIRI']
20 identifiers
= {t
.attrib
['number']: [doc
+ l
.text
if l
.text
.startswith('#') else l
.text
21 for l
in t
.findall('itemIdentifiers/locator')]
22 for t
in xml
.findall('topic')}
23 association
= xml
.find('association')
24 assert original
['typeIdentifiers'] == identifiers
[association
.find('type').attrib
['topicref']]
25 expected_roles
= {(tuple(identifiers
[r
.find('type').attrib
['topicref']]),
26 tuple(identifiers
[r
.find('player').attrib
['topicref']]))
27 for r
in association
.findall('role')}
28 actual_roles
= {(tuple(r
['typeIdentifiers']), tuple(r
['playerIdentifiers'])) for r
in original
['roles']}
29 assert expected_roles
== actual_roles
and len(original
['roles']) == 3
30 assert all(r
['parentIsAssociation'] and r
['roleIsCanonical'] and r
['inverseContainsRole'] for r
in original
['roles'])
31 assert len(report
['witnesses']) == 12
32 for witness
in report
['witnesses']:
33 xml
= ET
.fromstring(witness
['actualCXTM'])
34 assert ET
.canonicalize(witness
['actualCXTM']) + '\n' == witness
['actualCXTM']
35 topics
= xml
.findall('topic')
37 for i
, a
in enumerate(xml
.findall('association'), 1):
38 assert a
.attrib
['number'] == str(i
)
39 for j
, role
in enumerate(a
.findall('role'), 1):
40 assert role
.attrib
['number'] == str(j
)
41 roles
[f
'association.{i}.role.{j}'] = role
.find('player').attrib
['topicref']
42 assert len(roles
) == witness
['roleCount']
43 played
= [(t
.attrib
['number'], r
.attrib
['ref']) for t
in topics
for r
in t
.findall('rolePlayed')]
44 assert len(played
) == len(roles
)
45 assert len({ref
for _
, ref
in played
}) == len(roles
)
46 assert all(roles
[ref
] == number
for number
, ref
in played
)
47 for node
in xml
.iter():
48 for attribute
in ['topicref', 'reifier']:
49 if attribute
in node
.attrib
:
50 assert 1 <= int(node
.attrib
[attribute
]) <= len(topics
)
51 print('ARITY-INDEPENDENT-PASS: original Topic/Role object observations; 12 canonical XML witnesses with complete Role references')