]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/reification.lisp
feat(xtm): support resource references on name variants
[topic-maps.git] / tests / reification.lisp
1 (defpackage #:topic-maps.reification.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.ctm.tests #:check #:map-state #:read-text #:cxtm #:binary-file)
4 (:import-from #:topic-maps.variant.tests #:condition-from)
5 (:import-from #:topic-maps.identifier.tests #:fixture #:si #:current #:raw-handles)
6 (:export #:run-tests #:reification-checks #:write-reification-report))
7 (in-package #:topic-maps.reification.tests)
8 (defvar *last-checks* nil)
9 (defvar *observations* nil)
10 (defvar *negatives* nil)
11 (defun reification-checks () *last-checks*)
12 (defun reifiables (map) (remove-if-not #'topic-maps.tmdm::reifiable-p (topic-maps.tmdm::map-information-items map)))
13 (defun state (map &optional handles)
14 (list (map-state map) (raw-handles handles)
15 (mapcar (lambda (i) (list i (topic-maps.tmdm::%item-reifier i)))
16 (remove-if-not #'topic-maps.tmdm::reifiable-p handles))))
17 (defun invariants (map)
18 (and (topic-maps.reference.tests::references-current-p map)
19 (topic-maps.tmdm::validate-map-item-identifiers map) (topic-maps.tmdm::validate-map-reification map)))
20 (defun rejects-atomically (label map handles thunk code)
21 (let* ((before (state map handles)) (e (condition-from thunk)))
22 (check (concatenate 'string label ":condition")
23 (and (typep e 'reification-error) (eq code (reification-error-code e))))
24 (check (concatenate 'string label ":original-objects-unchanged") (equal before (state map handles)))))
25
26 (defun attachment-tests ()
27 (dolist (index '(0 2 3 4 5 6))
28 (multiple-value-bind (map items) (fixture)
29 (let* ((item (nth index items)) (r (si map "r")) (s (si map "s")))
30 (check (format nil "attach:~D:initial-null" index) (and (null (item-reifier item)) (null (topic-reified r))))
31 (set-item-reifier item r)
32 (check (format nil "attach:~D:both-directions-EQ" index) (and (eq r (item-reifier item)) (eq item (topic-reified r))))
33 (check (format nil "attach:~D:idempotent" index) (eq item (set-item-reifier item r)))
34 (set-item-reifier item s)
35 (check (format nil "attach:~D:assertion-merges-reifier-topics" index)
36 (and (eq (canonical-topic r) (canonical-topic s)) (eq (canonical-topic r) (item-reifier item))
37 (eq item (topic-reified s)) (eq map (topic-parent (item-reifier item))) (invariants map)))
38 (let ((before (state map items)))
39 (check (format nil "attach:~D:CXTM-references-and-purity" index)
40 (and (search "reifier=\"" (cxtm map)) (string= (cxtm map) (cxtm map)) (equal before (state map items)))))
41 (set-item-reifier item nil)
42 (check (format nil "attach:~D:explicit-clear" index) (and (null (item-reifier item)) (null (topic-reified r)))))))
43 (dolist (from '(0 2 3 4 5 6))
44 (dolist (to '(0 2 3 4 5 6))
45 (unless (= from to)
46 (multiple-value-bind (map items) (fixture)
47 (let ((r (si map "r")))
48 (set-item-reifier (nth from items) r)
49 (rejects-atomically (format nil "attach:conflict:~D:~D" from to) map (append items (list r))
50 (lambda () (set-item-reifier (nth to items) r)) :reifier-conflict))))))
51 (multiple-value-bind (map items) (fixture)
52 (rejects-atomically "attach:Topic-is-not-reifiable" map items
53 (lambda () (set-item-reifier (second items) (second items))) :not-reifiable)
54 (rejects-atomically "attach:wrong-reifier-type" map items
55 (lambda () (set-item-reifier (third items) "urn:r")) :invalid-reifier)
56 (rejects-atomically "attach:foreign-reifier" map items
57 (lambda () (set-item-reifier (third items) (si (make-topic-map) "foreign"))) :foreign-reifier)))
58
59 (defun paired-items (kind)
60 (let* ((map (make-topic-map)) (a (si map "a")) (b (si map "b")) (type (si map "type")) (theme (si map "theme")))
61 (multiple-value-bind (one two)
62 (ecase kind
63 (topic-name (values (add-name a "N" :type type) (add-name b "N" :type type)))
64 (variant (values (add-variant (add-name a "N" :type type) "V" :scope (list theme))
65 (add-variant (add-name b "N" :type type) "V" :scope (list theme))))
66 (occurrence (values (add-occurrence a type "O") (add-occurrence b type "O")))
67 (association (values (make-association map type (list (list type a))) (make-association map type (list (list type b)))))
68 (association-role
69 (let ((rel (make-association map type (list (list a type) (list b type)))))
70 (values (first (association-roles rel)) (second (association-roles rel))))))
71 (values map a b one two))))
72 (defun item-merge-tests ()
73 (dolist (kind '(topic-name variant occurrence association association-role))
74 (dolist (reverse '(nil t))
75 (multiple-value-bind (map a b one two) (paired-items kind)
76 (let ((r (si map "r")) (s (si map "s")))
77 (add-item-identifier one "urn:item:one") (add-item-identifier two "urn:item:two")
78 (add-item-identifier r "urn:reifier:one") (add-item-identifier s "urn:reifier:two")
79 (set-item-reifier one r) (set-item-reifier two s)
80 (if reverse (merge-topics b a) (merge-topics a b))
81 (check (format nil "merge:~A:~A:item-and-reifier-handles" kind reverse)
82 (and (eq (current one) (current two)) (eq (canonical-topic r) (canonical-topic s))
83 (eq (current one) (topic-reified r)) (eq (item-reifier one) (canonical-topic s))))
84 (check (format nil "merge:~A:~A:both-identifier-unions" kind reverse)
85 (and (equal '("urn:item:one" "urn:item:two") (sort (item-identifiers one) #'string<))
86 (equal '("urn:reifier:one" "urn:reifier:two") (sort (item-identifiers r) #'string<)) (invariants map)))
87 (let ((third (si map "third")))
88 (set-item-reifier one third)
89 (check (format nil "merge:~A:~A:reification-through-old-item-handle" kind reverse)
90 (and (eq (canonical-topic third) (item-reifier two)) (eq (current two) (topic-reified r)))))
91 (let ((before (state map (list a b one two r s))))
92 (check (format nil "merge:~A:~A:all-handles-pure" kind reverse)
93 (and (string= (cxtm map) (cxtm map)) (equal before (state map (list a b one two r s)))))))))))
94
95 (defun cascade (&optional collapse)
96 (let* ((map (make-topic-map)) (a (si map "a")) (b (si map "b")) (type (si map "type"))
97 (r1 (si map "r1")) (s1 (si map "s1")) (r2 (si map "r2")) (s2 (si map "s2"))
98 (r3 (si map "r3")) (s3 (si map "s3")) (r4 (si map "r4")) (s4 (si map "s4"))
99 (n1 (add-name a "N" :type type)) (n2 (add-name b "N" :type type))
100 (o1 (add-occurrence r1 type "O")) (o2 (add-occurrence s1 type "O"))
101 (a1 (make-association map type (list (list type r2)))) (a2 (make-association map type (list (list type s2))))
102 (p1 (first (association-roles a1))) (p2 (first (association-roles a2)))
103 (left (list n1 o1 a1 p1)) (right (list n2 o2 a2 p2))
104 (rs (list r1 r2 r3 r4)) (ss (list s1 s2 s3 s4)))
105 (loop for x in left for y in right for r in rs for s in ss for index from 1 do
106 (add-item-identifier x (format nil "urn:left:~D" index)) (add-item-identifier y (format nil "urn:right:~D" index))
107 (set-item-reifier x r) (set-item-reifier y s))
108 (when collapse
109 (let* ((r (nth (1- collapse) rs)) (s (nth (1- collapse) ss))
110 (n (add-name (si map "scope-owner") "Scoped" :type type :scope (list r))))
111 (add-variant n "V" :scope (list r s))))
112 (values map a b left right rs ss)))
113 (defun cascade-tests ()
114 (dolist (reverse '(nil t))
115 (multiple-value-bind (map a b left right rs ss) (cascade)
116 (if reverse (merge-topics b a) (merge-topics a b))
117 (loop for x in left for y in right for r in rs for s in ss for index from 1 do
118 (check (format nil "cascade:~A:level-~D:canonical-objects-and-inverse" reverse index)
119 (and (eq (current x) (current y)) (eq (canonical-topic r) (canonical-topic s))
120 (eq (current x) (topic-reified r)) (eq (item-reifier x) (canonical-topic s))
121 (= 2 (length (item-identifiers x))))))
122 (check (format nil "cascade:~A:stored-references-and-fixed-point" reverse)
123 (and (invariants map) (let ((before (state map (append left right rs ss))))
124 (normalize-topic-map map) (equal before (state map (append left right rs ss))))))))
125 (dolist (level '(1 2 3 4))
126 (dolist (route '(:merge :topic-identifier :general-identifier))
127 (multiple-value-bind (map a b left right rs ss) (cascade level)
128 (add-item-identifier a "urn:trigger")
129 (let* ((handles (append (list a b) left right rs ss)) (before (state map handles))
130 (e (condition-from (lambda ()
131 (ecase route (:merge (merge-topics a b))
132 (:topic-identifier (add-topic-identifier b :item-identifier "urn:trigger"))
133 (:general-identifier (add-item-identifier b "urn:trigger")))))))
134 (check (format nil "cascade:late-scope-collapse:~D:~A:specific" level route)
135 (and (typep e 'variant-error) (eq :variant-scope-collapse (variant-error-code e))))
136 (check (format nil "cascade:late-scope-collapse:~D:~A:fully-atomic" level route)
137 (and (equal before (state map handles)) (invariants map)))))))
138 (multiple-value-bind (map a b left right rs ss) (cascade)
139 (declare (ignore a b))
140 (let ((handles (append left right rs ss)))
141 (rejects-atomically "conflict:direct-reifier-Topic-merge" map handles
142 (lambda () (merge-topics (first rs) (first ss))) :reifier-conflict)
143 (rejects-atomically "conflict:already-reifies-another-item" map handles
144 (lambda () (set-item-reifier (second left) (first rs))) :reifier-conflict)))
145 ;; Setter-induced Topic merging must also preflight the complete scope effect.
146 (multiple-value-bind (map items) (fixture)
147 (let* ((r (si map "r")) (s (si map "s")) (n (third items))
148 (scoped (add-name (second items) "Scoped" :scope (list r))))
149 (add-variant scoped "V" :scope (list r s)) (set-item-reifier n r)
150 (let* ((before (state map items)) (e (condition-from (lambda () (set-item-reifier n s)))))
151 (check "setter:scope-collapse-atomic"
152 (and (typep e 'variant-error) (equal before (state map items)) (invariants map)))))))
153
154 (defun observe-reification (map)
155 (let ((topics (topic-map-topics map)))
156 (loop for item in (reifiables map) when (item-reifier item) collect
157 (topic-maps.test-foundation::fields "kind" (format nil "~(~A~)" (type-of item))
158 "value" (typecase item (topic-name (topic-name-value item)) (variant (variant-value item))
159 (occurrence (occurrence-value item)) (t nil))
160 "reifierTopic" (1+ (position (item-reifier item) topics :test #'eq))
161 "inverseIsItem" (if (eq item (topic-reified (item-reifier item))) :true :false)
162 "itemIsCanonical" (if (eq item (current item)) :true :false)
163 "reifierIsCanonical" (if (eq (item-reifier item) (canonical-topic (item-reifier item))) :true :false)))))
164 (defun original-cases ()
165 (setf *observations* nil *negatives* nil)
166 (dolist (file '("tm-reifier" "tm-reifier2" "tm-reifier3" "name-reifier" "variant-reifier"
167 "occurrence-reifier" "occurrence-reifier-nested" "occurrence-reifier-nested-nested"
168 "association-reifier" "role-reifier" "role-duplicate-reified-qnames"))
169 (let* ((id (format nil "ctm/in/~A.ctm" file))
170 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
171 (doc (topic-maps.test-foundation:test-case-document-iri case))
172 (map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id))
173 (before (state map)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
174 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case))))
175 (check (concatenate 'string id ":current-object-identity-and-bidirectional-relations") (invariants map))
176 (check (concatenate 'string id ":byte-exact-original-CXTM") (equalp actual expected))
177 (check (concatenate 'string id ":serialization-pure") (equal before (state map)))
178 (topic-maps.test-foundation:record-result case :cxtm-match)
179 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.3.10" "CTM 3.5" "CTM 3.10-3.13" "TMDM 5.3.4-5.8" "TMDM 6.1-6.7" "CXTM 3.4-3.11"))
180 (setf topic-maps.ctm.tests::*cxtm-results* (append topic-maps.ctm.tests::*cxtm-results*
181 (list (list :id id :base doc :actual (cxtm map doc) :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case))
182 :match t :bytes (length actual)))))
183 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc
184 "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case))
185 "model" (topic-maps.variant.tests::observe-map map)
186 "reifications" (topic-maps.test-foundation::json-array (observe-reification map))) *observations*)
187 (format t "~&REIFICATION-CXTM ~A: PASS (~D bytes)~%" id (length actual))))
188 (dolist (file '("reifier-collision" "tm-reifier-not-ontop"))
189 (let* ((id (format nil "ctm/invalid/~A.ctm" file))
190 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
191 (source (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case)))
192 (e (condition-from (lambda () (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
193 :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id))))
194 (code (if (string= file "reifier-collision") :reifier-conflict :topic-map-reifier-position)))
195 (check (concatenate 'string id ":specific-reason-and-marker-position")
196 (and (eq (type-of e) 'topic-maps.ctm:ctm-rejection) (eq code (topic-maps.ctm:error-code e))
197 (= (position #\~ source :from-end t) (topic-maps.ctm:error-position e))))
198 (topic-maps.test-foundation:record-result case :ctm-rejection :diagnostics (list (princ-to-string e)))
199 (setf (topic-maps.test-foundation:test-case-clauses case) '("CTM 3.3.10" "CTM 3.5" "TMDM 5.3.4-5.3.5"))
200 (push (topic-maps.test-foundation::fields "id" id "code" (string-downcase (symbol-name code))
201 "position" (topic-maps.ctm:error-position e) "line" (topic-maps.ctm:error-line e)
202 "column" (topic-maps.ctm:error-column e)) *negatives*)))
203 (setf *observations* (nreverse *observations*) *negatives* (nreverse *negatives*)))
204
205 (defun reader-tests ()
206 (dolist (fixture '(("~" :expected-reifier-topic 1) ("a - \"N\" ~ ." :expected-reifier-topic 10)
207 ("a. ~r" :topic-map-reifier-position 3) ("~r ~s" :topic-map-reifier-position 3)))
208 (let ((e (topic-maps.ctm.tests::ctm-condition (first fixture))))
209 (check (concatenate 'string "reader:reifier-diagnostic:" (first fixture))
210 (and (eq (type-of e) 'topic-maps.ctm:ctm-rejection) (eq (second fixture) (topic-maps.ctm:error-code e))
211 (= (third fixture) (topic-maps.ctm:error-position e))))))
212 (multiple-value-bind (map counter) (topic-maps.ctm:deserialize-string
213 "~?map a - \"N\" ~[-\"R\"] (\"V\"@?theme ~?variant); t:\"O\"~?. rel(role: a ~?role)~?assoc"
214 :document-iri "https://example.invalid/reifiers.ctm")
215 (check "reader:all-six-bindings-use-shared-wildcard-and-embedded-resolver"
216 (and (= 7 counter) (= 6 (length (observe-reification map))) (invariants map))))
217 (let* ((map (read-text "a - \"N\"~r1; - \"N\"~r2. rel(role: a ~s1, role: a ~s2)"))
218 (n (first (mapcan #'topic-names (topic-map-topics map))))
219 (a (first (topic-map-associations map))))
220 (check "reader:duplicate-Names-and-Roles-merge-different-reifiers"
221 (and (= 1 (length (association-roles a))) (item-reifier n)
222 (= 2 (length (topic-item-identifiers (item-reifier n))))
223 (= 2 (length (topic-item-identifiers (item-reifier (first (association-roles a)))))) (invariants map)))))
224
225 (defun additional-model-tests ()
226 (multiple-value-bind (map items) (fixture)
227 (let* ((r (si map "scope:r")) (s (si map "scope:s")) (extra (si map "scope:extra"))
228 (target (third items)) (name (add-name (second items) "Scoped" :scope (list r s)))
229 (v (add-variant name "V" :scope (list r s extra))))
230 (add-item-identifier v "urn:scoped-variant")
231 (set-item-reifier target r) (set-item-reifier target s)
232 (check "scope:Reifier-theme-merge-reduces-cardinality-with-valid-proper-superset"
233 (and (= 1 (length (topic-name-scope name))) (= 2 (length (variant-scope v)))
234 (eq (canonical-topic r) (first (topic-name-scope name)))
235 (member (canonical-topic r) (variant-scope v) :test #'eq)
236 (member extra (variant-scope v) :test #'eq)
237 (eq target (topic-reified s)) (equal '("urn:scoped-variant") (item-identifiers v)) (invariants map)))))
238 ;; Reifiers can also be ordinary Parent/Type Topics; rescan current owners.
239 (let* ((map (make-topic-map)) (owner (si map "owner")) (t1 (si map "t1")) (t2 (si map "t2"))
240 (n1 (add-name owner "N" :type t1)) (n2 (add-name owner "N" :type t2)))
241 (set-item-reifier n1 owner) (set-item-reifier n2 t2)
242 (merge-topics t1 t2)
243 (check "alias:reifier-is-also-Parent-and-Type-Topic"
244 (and (eq (current n1) (current n2)) (eq (canonical-topic owner) (canonical-topic t2))
245 (eq (item-reifier n1) (topic-name-parent n1)) (eq (item-reifier n1) (topic-name-type n1))
246 (eq (current n1) (topic-reified owner)) (invariants map))))
247 (dolist (kind '(:item-identifier :subject-identifier :subject-locator))
248 (multiple-value-bind (map a b left right rs ss) (cascade)
249 (declare (ignore a b))
250 (add-topic-identifier (first rs) kind "urn:conflict")
251 (rejects-atomically (format nil "conflict:Topic-identity-route:~A" kind) map (append left right rs ss)
252 (lambda () (add-topic-identifier (first ss) kind "urn:conflict")) :reifier-conflict)))
253 (let ((reference nil))
254 (dolist (reverse '(nil t))
255 (multiple-value-bind (map items) (fixture)
256 (dolist (index (if reverse '(6 5 4 3 2 0) '(0 2 3 4 5 6)))
257 (set-item-reifier (nth index items) (si map (format nil "reifier:~D" index))))
258 (let ((before (state map items)) (out (cxtm map)))
259 (unless reference (setf reference out))
260 (check (format nil "serialization:construction-order:~A:canonical-numbering-and-purity" reverse)
261 (and (string= out reference) (equal before (state map items)) (invariants map)))))))
262 (let* ((map (read-text "%prefix xsd <http://www.w3.org/2001/XMLSchema#> a t:\"01\"^^xsd:integer~r; t: 1~s."))
263 (items (mapcan #'topic-occurrences (topic-map-topics map))))
264 (check "equality:canonical-value-output-does-not-merge-stored-lexical-values-or-reifiers"
265 (and (= 2 (length items)) (not (eq (item-reifier (first items)) (item-reifier (second items))))
266 (equal '("01" "1") (mapcar #'occurrence-value items)) (invariants map) (search "<value>1</value>" (cxtm map)))))
267 (multiple-value-bind (map items) (fixture)
268 (setf (topic-maps.tmdm::%topic-name-reifier (third items)) (si (make-topic-map) "foreign"))
269 (let* ((before (state map items)) (e (condition-from (lambda () (cxtm map)))))
270 (check "serialization:foreign-Reifier-is-rejected-without-repair"
271 (and (typep e 'topic-maps.cxtm:cxtm-error) (eq :invalid-reifier (topic-maps.cxtm:error-code e))
272 (equal before (state map items))))))
273 (multiple-value-bind (map items) (fixture)
274 (let ((r (si map "r")))
275 (setf (topic-maps.tmdm::%topic-name-reifier (third items)) r (topic-maps.tmdm::%occurrence-reifier (fifth items)) r)
276 (let* ((before (state map items)) (e (condition-from (lambda () (cxtm map)))))
277 (check "serialization:duplicate-Reified-targets-rejected-without-repair"
278 (and (typep e 'topic-maps.cxtm:cxtm-error) (eq :reifier-conflict (topic-maps.cxtm:error-code e))
279 (equal before (state map items))))))))
280
281 (defun run-tests ()
282 (topic-maps.identifier.tests:run-tests)
283 (let ((topic-maps.ctm.tests::*checks* nil))
284 (attachment-tests) (item-merge-tests) (cascade-tests) (reader-tests) (additional-model-tests) (original-cases)
285 (check "reification:only-thirteen-imported-contract-transitions"
286 (equal '(:pass 106 :fail 2 :unsupported 0 :not-run 165)
287 (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses)))
288 (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*))
289 (format t "~&REIFICATION-PASS: ~D checks; 11 original byte-exact comparisons, 2 specific rejections; 106 PASS, 2 known FAIL, 165 NOT-RUN.~%" (length *last-checks*)) t))
290 (defun write-reification-report (path)
291 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
292 (topic-maps.test-foundation::emit-json-value
293 (topic-maps.test-foundation::fields "kind" "reification-evidence" "passed" (length *last-checks*) "failed" 0
294 "checks" (topic-maps.test-foundation::json-array *last-checks*)
295 "cases" (topic-maps.test-foundation::json-array *observations*)
296 "negativeCases" (topic-maps.test-foundation::json-array *negatives*)) out)
297 (terpri out)))