]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-relationships.py
perf(tmdm): index topic normalization merge candidates
[topic-maps.git] / scripts / check-relationships.py
1 #!/usr/bin/env python3
2 """Compare actual TMDM reference graphs to the untouched original CXTM graphs."""
3 import collections
4 import json
5 import pathlib
6 import sys
7 import xml.etree.ElementTree as ET
8
9 ROOT = pathlib.Path(__file__).resolve().parent.parent
10 report = json.loads(pathlib.Path(sys.argv[1]).read_text())
11 assert report['kind'] == 'type-relationship-evidence' and report['failed'] == 0
12 assert report['passed'] == len(report['checks']) == len(set(report['checks']))
13 prefix = 'http://psi.topicmaps.org/iso13250/model/'
14 core = ['type-instance', 'type', 'instance', 'supertype-subtype', 'supertype', 'subtype', 'sort', 'name-type']
15 assert report['coreSubjectIdentifiers'] == [prefix + name for name in core]
16 names = ['isa-relationship', 'isa-relationship-duplicate', 'isa-relationship-duplicate2',
17 'isa-relationship-embedded', 'isa-relationship-type-qname', 'ako-relationship',
18 'ako-relationship-duplicate', 'ako-relationship-duplicate2', 'ako-relationship-embedded',
19 'ako-relationship-supertype-qname', 'ako-merge-away', 'subject-identifier-topicref', 'item-identifier-topicref']
20 assert [c['id'] for c in report['cases']] == ['ctm/in/' + name + '.ctm' for name in names]
21 properties = ['itemIdentifiers', 'subjectIdentifiers', 'subjectLocators']
22 for case in report['cases']:
23 assert case['ctmSource'] == (ROOT / 'vendor/cxtm' / case['id']).read_text()
24 baseline = ET.parse(ROOT / 'vendor/cxtm/ctm/baseline' / (pathlib.Path(case['id']).name + '.cxtm')).getroot()
25 def absolute(s):
26 return case['documentIRI'] + s if s.startswith('#') else s
27 expected_keys = {t.attrib['number']: tuple(tuple(sorted(absolute(l.text) for l in t.findall(p + '/locator')))
28 for p in properties) for t in baseline.findall('topic')}
29 observed_keys = {t['ordinal']: tuple(tuple(sorted(t[p])) for p in properties) for t in case['model']['topics']}
30 assert collections.Counter(expected_keys.values()) == collections.Counter(observed_keys.values()), case['id']
31 expected = collections.Counter((expected_keys[a.find('type').attrib['topicref']],
32 frozenset(expected_keys[s.attrib['topicref']] for s in a.findall('scope/scopingTopic')),
33 frozenset((expected_keys[r.find('type').attrib['topicref']],
34 expected_keys[r.find('player').attrib['topicref']]) for r in a.findall('role')))
35 for a in baseline.findall('association'))
36 observed = collections.Counter((observed_keys[a['type']], frozenset(observed_keys[t] for t in a['scope']),
37 frozenset((observed_keys[r['type']], observed_keys[r['player']]) for r in a['roles']))
38 for a in case['model']['associations'])
39 assert expected == observed, case['id']
40 for a in case['model']['associations']:
41 assert a['parentIsMap'] and len(a['roles']) == 2 and len({r['type'] for r in a['roles']}) == 2
42 assert all(r['parentIsAssociation'] and r['roleIsCanonical'] and r['inverseContainsRole'] for r in a['roles'])
43 expected_vocabulary = ('supertype-subtype', 'subtype', 'supertype') if '/ako-' in case['id'] else ('type-instance', 'instance', 'type')
44 assert observed_keys[a['type']][1] == (prefix + expected_vocabulary[0],)
45 assert {observed_keys[r['type']][1] for r in a['roles']} == {(prefix + v,) for v in expected_vocabulary[1:]}
46 labels = [r['label'] for r in report['rejections']]
47 assert len(labels) == len(set(labels))
48 expected_pairs = {f'core:{i}:{j}:{route}' for i in range(1, 9) for j in range(1, 9) if i != j
49 for route in ['EXPLICIT', 'SUBJECT', 'ITEM', 'LOCATOR', 'INTERN']}
50 assert {label for label in labels if label.startswith('core:')} == expected_pairs
51 assert all(r['code'] == 'core-subject-conflict' and r['unchanged'] and 'TMDM 7.1' in r['detail'] for r in report['rejections'])
52 assert {'reifier:four-level-cascade:EXPLICIT', 'reifier:four-level-cascade:SUBJECT',
53 'chain:normalize-entire-transitive-closure', 'intern:no-dangling-new-topic-after-late-conflict'} <= set(labels)
54 print(f'RELATIONSHIPS-INDEPENDENT-PASS: 13 original TMDM reference graphs; eight exact Core Subject locators; {len(labels)} atomic conflict observations')