]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/xtm-association-reification.lisp
examples: explore project work through native Topic Maps (slice 22U)
[topic-maps.git] / tests / xtm-association-reification.lisp
1 (in-package #:topic-maps.xtm.tests)
2
3 (defparameter *association-reifier-originals*
4 '("association-reifier" "association-duplicate-reified" "association-duplicate-reified2"
5 "association-duplicate-reified3" "association-duplicate-reified4"))
6 (defun arr-observe (raw key)
7 ;; AS-OBSERVE retains actual pre-normalization Association/Role handles.
8 ;; Also retain every actual Topic and current Reifier after each Reader call.
9 (let ((saved (symbol-function 'topic-maps.xtm::read-association)) (held nil) (counts nil)
10 (map nil) (native nil) (stages nil) (calls 0) (same nil))
11 (unwind-protect
12 (progn
13 (setf (symbol-function 'topic-maps.xtm::read-association)
14 (lambda (node model) (let ((a (funcall saved node model)))
15 (dolist (topic (topic-map-topics model)) (pushnew topic held :test #'eq))
16 (push (length (topic-map-associations model)) counts) a)))
17 (multiple-value-setq (map native stages calls same) (as-observe raw key)))
18 (setf (symbol-function 'topic-maps.xtm::read-association) saved))
19 (values map (union held native :test #'eq) stages calls same (nreverse counts))))
20 (defun arr-pure (label map held)
21 (let ((before (ik-state map)) (cx (mm-cxtm map)))
22 (check (format nil "association-reifier:~A:pure-CXTM-all-II-reifiers-old-handles" label)
23 (and (equalp cx (mm-cxtm map)) (equal before (ik-state map)) (arr-current-p map) (orr-held-p map held))) cx))
24 (defun arr-original-model (name map)
25 (let* ((a (first (topic-map-associations map))) (single (equal name "association-reifier"))
26 (r (by-id map (if single "reifier" "reifier1")))
27 (players (if single '("topic") '("player1" "player2")))
28 (types (if single '("roletype") '("roletype1" "roletype2")))
29 (ii (member name '("association-duplicate-reified2" "association-duplicate-reified3" "association-duplicate-reified4") :test #'equal)))
30 (check (format nil "association-reifier-original:~A:exact-native-Association-Roles-Reifier-identities" name)
31 (and (= 1 (length (topic-map-associations map))) (= (if single 4 6) (length (topic-map-topics map)))
32 (= (length players) (length (association-roles a))) (eq map (association-parent a))
33 (eq (by-id map "assoctype") (association-type a)) (null (association-scope a))
34 (eq r (item-reifier a)) (eq a (topic-reified r))
35 (same-identifiers-p (when ii (list (concatenate 'string *document* "#association"))) (item-identifiers a))
36 (loop for p in players for rt in types always (some (lambda (role)
37 (and (eq (by-id map p) (association-role-player role)) (eq (by-id map rt) (association-role-type role))
38 (eq a (association-role-parent role)) (equal (list role) (topic-roles-played (by-id map p)))
39 (null (item-identifiers role)) (null (item-reifier role)))) (association-roles a)))
40 (every (lambda (topic) (and (= 1 (length (item-identifiers topic))) (null (topic-subject-identifiers topic))
41 (null (topic-subject-locators topic)) (null (topic-names topic)))) (topic-map-topics map))
42 (null (all-occurrences map)) (null (item-identifiers map)) (null (item-reifier map)) (arr-current-p map)))))
43 (defun association-reifier-original-contracts ()
44 (setf *association-reifier-outputs* nil)
45 (let ((previous (copy-list *cases*)))
46 (dolist (name *association-reifier-originals*)
47 (let* ((id (format nil "xtm2/in/~A.xtm" name))
48 (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
49 (raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
50 (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name))) (expected (file-bytes baseline))
51 (case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname (corpus-path id)
52 :document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline
53 :clauses '("XTM 4.3.2,4.3.4,4.6,4.18-4.20" "TMDM 5.1,5.3.4-5.3.5,5.7,6.2,6.6")))
54 (map nil) (held nil) (stages nil) (calls 0) (same nil) (counts nil) (actual nil) (c nil))
55 (check (format nil "association-reifier-original:~A:unique-new-case" name)
56 (null (find id *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))
57 (setf *cases* (append *cases* (list case)))
58 (handler-case
59 (progn
60 (multiple-value-setq (map held stages calls same counts) (arr-observe raw id))
61 (arr-original-model name map)
62 (check (format nil "association-reifier-original:~A:actual-reads-original-octets-qualified-parser" name)
63 (and (= 1 calls) same (equalp raw copy) (equal counts (if (equal name "association-reifier") '(1) '(1 1)))))
64 (check (format nil "association-reifier-original:~A:all-old-native-handles-live" name) (orr-held-p map held))
65 (unless (equal name "association-reifier")
66 (check (format nil "association-reifier-original:~A:replaced-Association-and-Role-handles" name)
67 (and (some (lambda (i) (and (typep i 'association) (not (eq i (canonical-association i))))) held)
68 (some (lambda (i) (and (typep i 'association-role) (not (eq i (canonical-association-role i))))) held))))
69 (setf actual (arr-pure name map held))
70 (let ((other (deserialize-octets raw :document-iri *document* :source-key "different:Association-Reifier-provenance")))
71 (check (format nil "association-reifier-original:~A:source-key-independent" name)
72 (and (equalp (arr-model map) (arr-model other)) (equalp actual (mm-cxtm other))))))
73 (error (condition) (setf c condition)))
74 (record-original-result case actual expected c)
75 (push (mm-fields "id" id "documentIRI" *document* "sourceKey" id "sourceBytes" (length raw)
76 "sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8)
77 "qualifiedParserCalls" calls "originalOctetsEQ" (if same :true :false) "model" (when map (arr-model map))
78 "associationCountsAfterEachRead" (coerce counts 'vector)
79 "normalizationStages" (coerce (mapcar (lambda (s) (coerce s 'vector)) stages) 'vector)
80 "observedNativeHandles" (length held) "oldHandlesCanonicalAndLive" (if (and map (orr-held-p map held)) :true :false)
81 "actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
82 "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8)) "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
83 "firstDifferentByte" (when actual (mismatch actual expected)) "condition" (mm-condition c)
84 "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *association-reifier-outputs*)
85 (when c (error c))
86 (check (format nil "association-reifier-original:~A:complete-original-CXTM-byte-exact" name) (equalp actual expected))))
87 (check "association-reifier-original:requested-five-once-all-prior-PASS-preserved"
88 (and (every (lambda (case) (and (member case *cases* :test #'eq) (eq :pass (topic-maps.test-foundation:test-case-status case)))) previous)
89 (same-identifiers-p (mapcar #'topic-maps.test-foundation:test-case-id (set-difference *cases* previous :test #'eq))
90 (mapcar (lambda (n) (format nil "xtm2/in/~A.xtm" n)) *association-reifier-originals*))
91 (every (lambda (case) (= 1 (count (topic-maps.test-foundation:test-case-id case) *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))) *cases*))))
92 (setf *association-reifier-outputs* (nreverse *association-reifier-outputs*)))
93
94 (defun arr-xml (reifier &key (type "#t") (roles '(("#rt" "#p"))) themes ids role-id)
95 (let ((body (as-xml type roles themes)))
96 (format nil "<association~A>~A~A</association>"
97 (if reifier (format nil " reifier='~A'" reifier) "") (ik-ids ids)
98 (let ((text (subseq body (length "<association>") (- (length body) (length "</association>")))))
99 (if role-id (let ((i (search "<role>" text)))
100 (concatenate 'string (subseq text 0 (+ i 6)) (ik-ids (list role-id)) (subseq text (+ i 6)))) text)))))
101 (defun arr-probe (label body verify &optional replaced)
102 (let* ((raw (bytes (document body))) (copy (copy-seq raw)))
103 (multiple-value-bind (map held stages calls same counts) (arr-observe raw label)
104 (check (format nil "association-reifier:~A:productive-reader-qualified-XML-EQ-inverses" label)
105 (and (= 1 calls) same (equalp raw copy) (arr-current-p map) (orr-held-p map held)))
106 (funcall verify map)
107 (when replaced (check (format nil "association-reifier:~A:old-handles-replaced-and-resolvable" label)
108 (some (lambda (i) (not (eq i (topic-maps.tmdm::canonical-item i)))) held)))
109 (let ((cx (arr-pure label map held)))
110 (push (mm-fields "label" label "documentIRI" *document* "sourceKey" label "sourceText" (document body)
111 "model" (arr-model map) "CXTM" (babel:octets-to-string cx :encoding :utf-8)
112 "readAssociationCounts" (coerce counts 'vector) "normalizationStages" (coerce (mapcar (lambda (s) (coerce s 'vector)) stages) 'vector)
113 "oldHandlesCanonicalAndLive" :true) *association-reifier-controls*)) map)))
114 (defun arr-error (label body class code &key map-reifier)
115 (let ((source (if map-reifier (map-reifier-xml map-reifier body) (document body))))
116 (multiple-value-bind (c frames) (orr-condition-from (bytes source) label)
117 (let ((actual (cond ((typep c 'topic-maps.xtm:xtm-error) (error-code c))
118 ((typep c 'reification-error) (reification-error-code c))
119 ((typep c 'item-identifier-error) (item-identifier-error-code c)))))
120 (check (format nil "association-reifier-error:~A:exact-class-code" label)
121 (and (eq (type-of c) class) (eq code actual)))
122 (when (typep c 'topic-maps.xtm:xtm-error)
123 (check (format nil "association-reifier-error:~A:source-document-position-and-cause" label)
124 (and (equal *document* (error-document-iri c)) (equal label (error-source-key c))
125 (if (typep c 'xtm-xml-error) (topic-maps.xtm:error-cause c) (and (integerp (error-line c)) (integerp (error-column c)))))))
126 (push (mm-fields "label" label "sourceText" source "documentIRI" *document* "sourceKey" label
127 "condition" (mm-condition c) "actualNativeAssignments" (coerce frames 'vector)) *association-reifier-controls*)) c)))
128 (defun association-reifier-tests ()
129 (setf *association-reifier-controls* nil)
130 (dolist (position '("unknown" "before" "after"))
131 (arr-probe (concatenate 'string "definition-" position)
132 (concatenate 'string (if (equal position "before") "<topic id='r'/>" "") (arr-xml "#r")
133 (if (equal position "after") "<topic id='r'/>" ""))
134 (lambda (map) (let ((a (first (topic-map-associations map))) (r (by-id map "r")))
135 (check (format nil "association-reifier:definition-~A:II-only-Reifier-no-invented-SI-SL" position)
136 (and (= 4 (length (topic-map-topics map))) (eq r (item-reifier a)) (eq a (topic-reified r))
137 (equal (list (concatenate 'string *document* "#r")) (item-identifiers r))
138 (null (topic-subject-identifiers r)) (null (topic-subject-locators r))))))))
139 (dolist (category '("itemIdentity" "subjectIdentifier"))
140 (arr-probe (concatenate 'string "lookup-" category)
141 (format nil "<topic id='known'><~A href='urn:reifier'/></topic>~A" category (arr-xml "urn:reifier"))
142 (lambda (map) (let ((r (by-id map "known")))
143 (check (format nil "association-reifier:lookup-~A:II-or-II-miss-then-native-cross-merge" category)
144 (and (= 4 (length (topic-map-topics map))) (eq r (item-reifier (first (topic-map-associations map))))
145 (same-identifiers-p (list (concatenate 'string *document* "#known") "urn:reifier") (item-identifiers r))
146 (equal (when (equal category "subjectIdentifier") '("urn:reifier")) (topic-subject-identifiers r))
147 (null (topic-subject-locators r))))))))
148 (dolist (ref '("urn:reifier" "../%E9%A6%AC#r"))
149 (arr-probe (concatenate 'string "resolved-IRI:" ref) (arr-xml ref :ids '("urn:association"))
150 (lambda (map) (let ((a (first (topic-map-associations map))))
151 (check (format nil "association-reifier:~A:IRI-and-separate-Association-II" ref)
152 (and (equal '("urn:association") (item-identifiers a))
153 (equal (list (mm-child-iri ref)) (item-identifiers (item-reifier a)))))))))
154 (dolist (kind '("same-Reifier" "different-Reifiers" "unreified-first" "reified-first"))
155 (arr-probe kind
156 (concatenate 'string (arr-xml (unless (equal kind "unreified-first") "#r") :ids '("urn:a") :role-id "urn:role-a")
157 (arr-xml (cond ((equal kind "different-Reifiers") "#u") ((equal kind "reified-first") nil) (t "#r")) :ids '("urn:b") :role-id "urn:role-b"))
158 (lambda (map) (let* ((a (first (topic-map-associations map))) (r (first (association-roles a))))
159 (check (format nil "association-reifier:~A:normalization-Reifier-Association-and-Role-II-union" kind)
160 (and (= 1 (length (topic-map-associations map))) (eq (by-id map "r") (item-reifier a))
161 (same-identifiers-p '("urn:a" "urn:b") (item-identifiers a))
162 (same-identifiers-p '("urn:role-a" "urn:role-b") (item-identifiers r))
163 (or (not (equal kind "different-Reifiers")) (and (eq (by-id map "r") (by-id map "u"))
164 (same-identifiers-p (list (concatenate 'string *document* "#r") (concatenate 'string *document* "#u")) (item-identifiers (item-reifier a))))))))) t))
165 (arr-probe "roles-order-duplicates-and-Scope"
166 (concatenate 'string (arr-xml "#r" :roles '(("#rt" "#p") ("#rt2" "#q")) :themes '("#s" "#s"))
167 (arr-xml "#u" :roles '(("#rt2" "#q") ("#rt" "#p") ("#rt" "#p")) :themes '("#s")))
168 (lambda (map) (let ((a (first (topic-map-associations map))))
169 (check "association-reifier:roles-order:one-Association-two-Roles-one-Scope-Reifiers-merged"
170 (and (= 1 (length (topic-map-associations map))) (= 2 (length (association-roles a)))
171 (equal (list (by-id map "s")) (association-scope a)) (eq (by-id map "r") (by-id map "u")))))) t)
172 (arr-late-merges)
173 (arr-negative-tests)
174 (arr-import-test)
175 (setf *association-reifier-controls* (nreverse *association-reifier-controls*)))
176 (defun arr-late-merges ()
177 (dolist (category '("subjectIdentifier" "subjectLocator" "itemIdentity"))
178 (arr-probe (concatenate 'string "late-" category)
179 (concatenate 'string (arr-xml "#r" :ids '("urn:a") :role-id "urn:ra" :themes '("#s"))
180 (arr-xml "#u" :type "#tt" :roles '(("#rrt" "#q")) :ids '("urn:b") :role-id "urn:rb" :themes '("#ss"))
181 (format nil "<topic id='p'><~A href='urn:p'/></topic><topic id='q'><~A href='urn:p'/></topic><topic id='rt'><~A href='urn:rt'/></topic><topic id='rrt'><~A href='urn:rt'/></topic><topic id='s'><~A href='urn:s'/></topic><topic id='ss'><~A href='urn:s'/></topic><topic id='t'><~A href='urn:t'/></topic><topic id='tt'><~A href='urn:t'/></topic><topic id='r'><~A href='urn:r'/></topic><topic id='u'><~A href='urn:r'/></topic>" category category category category category category category category category category))
182 (lambda (map) (let* ((a (first (topic-map-associations map))) (role (first (association-roles a))))
183 (check (format nil "association-reifier:late-~A:all-native-references-II-and-old-handles" category)
184 (and (= 1 (length (topic-map-associations map))) (eq (by-id map "r") (by-id map "u"))
185 (eq (by-id map "t") (association-type a)) (eq (by-id map "rt") (association-role-type role))
186 (eq (by-id map "p") (association-role-player role)) (equal (list (by-id map "s")) (association-scope a))
187 (same-identifiers-p '("urn:a" "urn:b") (item-identifiers a))
188 (same-identifiers-p '("urn:ra" "urn:rb") (item-identifiers role)))))) t)))
189 (defun arr-negative-tests ()
190 (dolist (row '(("bad-percent" "%ZZ" :invalid-percent-escape) ("bad-percent-UTF8" "%C3%28" :invalid-percent-utf8) ("bad-IRI" "x#y#z" :invalid-iri)))
191 (arr-error (first row) (arr-xml (second row)) 'xtm-rejection (third row)))
192 (dolist (row '(("extra-attribute" "<association reifier='#r' bogus='1'><type><topicRef href='#t'/></type><role><type><topicRef href='#rt'/></type><topicRef href='#p'/></role></association>" xtm-rejection :unexpected-attribute)
193 ("reifier-on-type" "<association><type reifier='#r'><topicRef href='#t'/></type><role><type><topicRef href='#rt'/></type><topicRef href='#p'/></role></association>" xtm-rejection :unexpected-attribute)
194 ("reifier-on-topicRef" "<association><type><topicRef href='#t'/></type><role><type><topicRef href='#rt'/></type><topicRef reifier='#r' href='#p'/></role></association>" xtm-rejection :unexpected-attribute)
195 ("malformed-XML" "<association reifier='#r'></topic>" xtm-xml-error :xml-not-well-formed)
196 ("missing-Type" "<association reifier='#r'><role><type><topicRef href='#rt'/></type><topicRef href='#p'/></role></association>" xtm-rejection :missing-type)
197 ("missing-Roles" "<association reifier='#r'><type><topicRef href='#t'/></type></association>" xtm-rejection :missing-roles)
198 ("wrong-order" "<association reifier='#r'><role><type><topicRef href='#rt'/></type><topicRef href='#p'/></role><type><topicRef href='#t'/></type></association>" xtm-rejection :missing-type)))
199 (apply #'arr-error row))
200 (arr-error "different-Associations-same-Reifier" (concatenate 'string (arr-xml "#r") (arr-xml "#r" :type "#other")) 'reification-error :reifier-conflict)
201 (arr-error "Occurrence-and-Association-same-Reifier"
202 (concatenate 'string "<topic id='owner'>" (orr-xml "#r") "</topic>" (arr-xml "#r")) 'reification-error :reifier-conflict)
203 (arr-error "Map-and-Association-same-Reifier" (arr-xml "#r") 'reification-error :reifier-conflict :map-reifier "#r")
204 (dolist (category '("subjectIdentifier" "subjectLocator" "itemIdentity"))
205 (arr-error (concatenate 'string "late-Reifier-conflict-" category)
206 (concatenate 'string (arr-xml "#r") (arr-xml "#u" :type "#other")
207 (format nil "<topic id='r'><~A href='urn:r'/></topic><topic id='u'><~A href='urn:r'/></topic>" category category)) 'reification-error :reifier-conflict))
208 (arr-error "Association-II-not-Reifier-Topic-II" (arr-xml "urn:same" :ids '("urn:same")) 'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier)
209 (arr-error "Role-Reification-remains-Unsupported" "<association reifier='#r'><type><topicRef href='#t'/></type><role reifier='#rr%GG'><type><topicRef href='#rt'/></type><topicRef href='#p'/></role></association>" 'xtm-rejection :invalid-percent-escape))
210 (defun arr-import-test ()
211 (let* ((child (mm-child-iri "child.xtm"))
212 (raw (bytes (document (concatenate 'string "<topic id='p'><subjectIdentifier href='urn:player'/></topic>"
213 (arr-xml "#r" :ids '("urn:imported-Association") :role-id "urn:imported-Role" :themes '("#s")))))))
214 (mm-call "Association-Reification-import" (document (concatenate 'string (mm-directive "child.xtm") "<topic id='local'><subjectIdentifier href='urn:player'/></topic>"))
215 (list (list child raw "child:Association-Reification"))
216 (lambda (map imports calls) (declare (ignore calls))
217 (let* ((a (first (topic-map-associations map))) (role (first (association-roles a))) (r (mm-local map child "r")))
218 (check "association-reifier:import:Parent-Role-Player-II-Reifier-inverse-source-preservation"
219 (and (arr-current-p map) (eq map (association-parent a)) (eq a (association-role-parent role))
220 (eq (by-id map "local") (association-role-player role)) (eq r (item-reifier a)) (eq a (topic-reified r))
221 (equal '("urn:imported-Association") (item-identifiers a)) (equal '("urn:imported-Role") (item-identifiers role))
222 (null (item-identifiers map))
223 (every (lambda (record) (let ((source (first record)) (resolve (second record)))
224 (and (arr-current-p source) (eq a (funcall resolve (first (topic-map-associations source))))
225 (eq r (funcall resolve (item-reifier (first (topic-map-associations source)))))))) imports))))
226 (push (mm-fields "label" "native-Association-Reification-import" "sourceText" (babel:octets-to-string raw :encoding :utf-8)
227 "model" (arr-model map) "sourceMapPreserved" :true) *association-reifier-controls*)))))