]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-type-relationships.lisp
Implement noncyclic XTM mergeMap content import (Slice 22E)
[topic-maps.git] / tests / ctm-type-relationships.lisp
1 (defpackage #:topic-maps.relationship.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.ctm.tests #:check #:read-text #:cxtm #:binary-file)
4 (:import-from #:topic-maps.variant.tests #:condition-from)
5 (:import-from #:topic-maps.identifier.tests #:si)
6 (:export #:run-tests #:relationship-checks #:write-relationship-report))
7 (in-package #:topic-maps.relationship.tests)
8 (defvar *last-checks* nil)
9 (defvar *observations* nil)
10 (defvar *rejections* nil)
11 (defparameter *originals*
12 '("isa-relationship" "isa-relationship-duplicate" "isa-relationship-duplicate2"
13 "isa-relationship-embedded" "isa-relationship-type-qname"
14 "ako-relationship" "ako-relationship-duplicate" "ako-relationship-duplicate2"
15 "ako-relationship-embedded" "ako-relationship-supertype-qname" "ako-merge-away"
16 "subject-identifier-topicref" "item-identifier-topicref"))
17 (defun relationship-checks () *last-checks*)
18 (defun core-iri (local) (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local))
19 (defun core (map local) (intern-topic map :subject-identifier (core-iri local)))
20 (defun named (map local) (topic-maps.arity.tests::named-topic map local))
21 (defun state (map handles) (topic-maps.reification.tests::state map handles))
22 (defun handles (map) (copy-list (topic-maps.tmdm::map-information-items map)))
23 (defun valid-model-p (map)
24 (and (topic-maps.reification.tests::invariants map) (topic-maps.tmdm::validate-map-core-subjects map)))
25 (defun with-si (map iri) (find iri (topic-map-topics map) :test (lambda (id topic) (member id (topic-subject-identifiers topic) :test #'string=))))
26 (defun typed (map local) (find (with-si map (core-iri local)) (topic-map-associations map) :key #'association-type :test #'eq))
27 (defun role (a local) (find (core-iri local) (association-roles a)
28 :test (lambda (id r) (member id (topic-subject-identifiers (association-role-type r)) :test #'string=))))
29
30 (defun rejects-atomically (label map thunk &key (extra nil) (valid t))
31 (let* ((all (append (handles map) extra)) (before (state map all)) (c (condition-from thunk))
32 (unchanged (equal before (state map all))))
33 (check (concatenate 'string label ":condition")
34 (and (eq (type-of c) 'core-subject-error) (eq :core-subject-conflict (core-subject-error-code c))))
35 (check (concatenate 'string label ":all-original-slots-identifiers-and-forward-links-unchanged") unchanged)
36 (check (concatenate 'string label ":no-invalid-state-introduced") (or (not valid) (valid-model-p map)))
37 (push (topic-maps.test-foundation::fields "label" label "code" "core-subject-conflict"
38 "unchanged" (if unchanged :true :false) "detail" (princ-to-string c)) *rejections*)))
39
40 (defun core-tests ()
41 (let ((expected (mapcar #'core-iri '("type-instance" "type" "instance" "supertype-subtype" "supertype" "subtype" "sort" "name-type"))))
42 (check "core:closed-vocabulary-from-TMDM-7.1-through-7.5" (equal expected (core-subject-identifiers)))
43 (let ((copy (core-subject-identifiers)))
44 (setf (char (first copy) 0) #\X (rest copy) nil)
45 (check "core:public-vocabulary-returns-private-copies" (equal expected (core-subject-identifiers)))))
46 (loop for left in (core-subject-identifiers) for i from 1 do
47 (loop for right in (core-subject-identifiers) for j from 1 unless (string= left right) do
48 (dolist (route '(:explicit :subject :item :locator :intern))
49 (let* ((map (make-topic-map)) (a (intern-topic map :subject-identifier left))
50 (b (unless (eq route :intern) (intern-topic map :subject-identifier right)))
51 (label (format nil "core:~D:~D:~A" i j route)))
52 (case route
53 (:intern (add-item-identifier a right))
54 (:subject (add-item-identifier b "urn:core:bridge"))
55 (:locator (add-topic-identifier b :subject-locator "urn:core:bridge")))
56 (rejects-atomically label map
57 (lambda ()
58 (ecase route
59 (:explicit (merge-topics a b))
60 (:subject (add-topic-identifier a :subject-identifier "urn:core:bridge"))
61 (:item (add-item-identifier a right))
62 (:locator (add-topic-identifier a :subject-locator "urn:core:bridge"))
63 (:intern (intern-topic map :subject-identifier right)))))))))
64 (dolist (iri (core-subject-identifiers))
65 (let* ((map (make-topic-map)) (a (intern-topic map :subject-identifier iri)) (alias (si map "alias")))
66 (check (concatenate 'string "core:same-subject-idempotent:" iri)
67 (and (eq a (intern-topic map :subject-identifier iri)) (eq a (add-topic-identifier a :subject-identifier iri))))
68 (let ((c (add-topic-identifier alias :subject-identifier iri)))
69 (check (concatenate 'string "core:legal-alias-replacement:" iri)
70 (and (eq c (canonical-topic a)) (eq c (canonical-topic alias))
71 (not (eq c a)) (= 1 (length (topic-map-topics map))) (valid-model-p map))))))
72 (let* ((map (make-topic-map))
73 (a (intern-topic map :subject-locator (core-iri "type")))
74 (b (intern-topic map :subject-locator (core-iri "instance"))) (c (merge-topics a b)))
75 (check "core:protected-spelling-as-subject-locator-is-not-a-Core-Subject-Identifier"
76 (and (null (topic-subject-identifiers c)) (= 2 (length (topic-subject-locators c))) (valid-model-p map))))
77 (let* ((map (make-topic-map)) (a (core map "type")))
78 (add-item-identifier a (core-iri "instance"))
79 (check "core:item-locator-alone-does-not-assert-another-Core-Subject"
80 (and (eq a (intern-topic map :item-identifier (core-iri "instance"))) (valid-model-p map)))))
81
82 (defun chain-tests ()
83 ;; Existing legal alias chains: reject an assertion made through old handles.
84 (dolist (route '(:explicit :subject))
85 (let* ((map (make-topic-map)) (a (core map "type")) (b (core map "instance"))
86 (x (si map "x")) (y (si map "y")) (z (si map "z")))
87 (merge-topics a x) (merge-topics x y) (merge-topics b z)
88 (rejects-atomically (format nil "chain:old-handles:~A" route) map
89 (lambda () (ecase route
90 (:explicit (merge-topics x z))
91 (:subject (add-topic-identifier y :subject-identifier (core-iri "instance")))))
92 :extra (list a b x y z))))
93 (let* ((map (make-topic-map)) (a (core map "type")) (x (si map "x")) (y (si map "y")))
94 (merge-topics a x) (merge-topics x y)
95 (add-item-identifier x (core-iri "instance"))
96 (rejects-atomically "chain:intern-through-existing-alias-chain" map
97 (lambda () (intern-topic map :subject-identifier (core-iri "instance"))) :extra (list a x y)))
98 ;; White-box input for NORMALIZE-TOPIC-MAP: the first merges are benign;
99 ;; a later SI/II chain would unite the two Core Subjects. This is deliberately
100 ;; not an already-normalized public-API state. Rejection must preserve it too.
101 (let* ((map (make-topic-map)) (a (core map "type")) (x (si map "x")) (y (si map "y")) (b (core map "instance")))
102 (push "urn:edge:1" (topic-maps.tmdm::%topic-subject-identifiers a))
103 (push "urn:edge:1" (topic-maps.tmdm::%topic-item-identifiers x))
104 (push "urn:edge:2" (topic-maps.tmdm::%topic-subject-identifiers x))
105 (push "urn:edge:2" (topic-maps.tmdm::%topic-item-identifiers y))
106 (push "urn:edge:3" (topic-maps.tmdm::%topic-subject-identifiers y))
107 (push "urn:edge:3" (topic-maps.tmdm::%topic-item-identifiers b))
108 (rejects-atomically "chain:normalize-entire-transitive-closure" map (lambda () (normalize-topic-map map)) :valid nil))
109 (let* ((map (make-topic-map)) (a (core map "type")))
110 (push (core-iri "instance") (topic-maps.tmdm::%topic-subject-identifiers a))
111 (rejects-atomically "chain:normalize-preexisting-core-conflict" map (lambda () (normalize-topic-map map)) :valid nil)
112 (let ((before (state map (handles map))) (c (condition-from (lambda () (cxtm map)))))
113 (check "core:CXTM-rejects-corrupted-core-state-without-changing-it"
114 (and (eq (type-of c) 'topic-maps.cxtm:cxtm-error)
115 (eq :core-subject-conflict (topic-maps.cxtm:error-code c))
116 (equal before (state map (handles map))))))))
117
118 (defun reifier-tests ()
119 (dolist (kind '(topic-name variant occurrence association association-role))
120 (dolist (route '(:explicit :subject :item))
121 (multiple-value-bind (map a b one two) (topic-maps.reification.tests::paired-items kind)
122 (let ((left (core map "type")) (right (core map "instance")))
123 (set-item-reifier one left) (set-item-reifier two right)
124 (case route ((:subject :item) (add-item-identifier b "urn:parents:join")))
125 (rejects-atomically (format nil "reifier:~A:~A" kind route) map
126 (lambda () (ecase route
127 (:explicit (merge-topics a b))
128 (:subject (add-topic-identifier a :subject-identifier "urn:parents:join"))
129 (:item (add-item-identifier a "urn:parents:join")))))))))
130 (multiple-value-bind (map items) (topic-maps.identifier.tests::fixture)
131 (let ((left (core map "type")) (right (core map "instance")) (name (third items)))
132 (set-item-reifier name left)
133 (rejects-atomically "reifier:direct-setter-core-conflict" map (lambda () (set-item-reifier name right)))))
134 ;; Four levels: Name -> Occurrence -> Association -> Role -> Core Reifiers.
135 (dolist (route '(:explicit :subject))
136 (multiple-value-bind (map a b left right rs ss) (topic-maps.reification.tests::cascade)
137 (add-topic-identifier (fourth rs) :subject-identifier (core-iri "type"))
138 (add-topic-identifier (fourth ss) :subject-identifier (core-iri "instance"))
139 (when (eq route :subject) (add-item-identifier b "urn:root:join"))
140 (rejects-atomically (format nil "reifier:four-level-cascade:~A" route) map
141 (lambda () (if (eq route :explicit) (merge-topics a b)
142 (add-topic-identifier a :subject-identifier "urn:root:join")))
143 :extra (append left right rs ss))))
144 ;; A previously unnormalized graph may fail even when interning a fresh,
145 ;; unrelated Topic. The constructor must preflight BEFORE appending that item.
146 (let* ((map (make-topic-map)) (a (core map "type")) (b (core map "instance")))
147 (push "urn:late:join" (topic-maps.tmdm::%topic-subject-identifiers a))
148 (push "urn:late:join" (topic-maps.tmdm::%topic-item-identifiers b))
149 (rejects-atomically "intern:no-dangling-new-topic-after-late-conflict" map
150 (lambda () (intern-topic map :item-identifier "urn:brand-new")) :valid nil)))
151
152 (defun semantic-tests ()
153 (dolist (keyword '("isa" "ako"))
154 (let* ((isa (string= keyword "isa")) (map (read-text (format nil "a ~A b." keyword)))
155 (type (if isa "type-instance" "supertype-subtype"))
156 (left (if isa "instance" "subtype")) (right (if isa "type" "supertype"))
157 (a (typed map type)) (roles (association-roles a))
158 (owner (named map "a")) (target (named map "b")))
159 (check (concatenate 'string keyword ":exact-items-and-six-vocabulary-subjects-used-correctly")
160 (and (= 5 (length (topic-map-topics map))) (= 1 (length (topic-map-associations map)))
161 (= 2 (length roles)) (null (association-scope a)) (null (item-reifier a))
162 (eq owner (association-role-player (role a left))) (eq target (association-role-player (role a right)))
163 (every (lambda (r) (and (eq a (association-role-parent r))
164 (eq r (first (topic-roles-played (association-role-player r)))))) roles)
165 (valid-model-p map)))
166 (loop for local in (list type left right) do
167 (check (format nil "~A:standard-Subject-Identifier:~A" keyword local)
168 (let ((tpc (with-si map (core-iri local))))
169 (and tpc (equal (list (core-iri local)) (topic-subject-identifiers tpc))
170 (null (topic-item-identifiers tpc)) (null (topic-subject-locators tpc))))))
171 (let* ((held (cons a roles)) (before (state map held)) (output (cxtm map)))
172 (check (concatenate 'string keyword ":pure-and-repeatable-CXTM")
173 (and (string= output (cxtm map)) (equal before (state map held)))))
174 (let* ((explicit (make-association map (association-type a)
175 (list (list (association-role-type (role a right)) target)
176 (list (association-role-type (role a left)) owner)))))
177 (check (concatenate 'string keyword ":longhand-merges-and-retained-Association-and-Role-handles")
178 (and (not (eq explicit a)) (eq explicit (canonical-association a))
179 (every (lambda (r) (member (canonical-association-role r) (association-roles explicit) :test #'eq)) roles)
180 (valid-model-p map))))))
181 (let* ((map (read-text "a isa b; isa b; ako c; ako c.")))
182 (check "relationships:repeated-tail-elements-use-role-set-merging" (= 2 (length (topic-map-associations map)))))
183 (let ((map (read-text "a ako b. b ako a. a isa c. c isa d.")))
184 (check "relationships:ako-cycle-and-isa-chain-do-not-merge-subjects-or-materialize-inferences"
185 (and (= 4 (length (topic-map-associations map))) (not (eq (named map "a") (named map "b")))
186 (not (eq (named map "c") (named map "d"))) (valid-model-p map))))
187 (let* ((map (read-text "a isa a; ako a.")))
188 (check "relationships:self-player-has-two-distinct-Role-types"
189 (every (lambda (a) (and (= 2 (length (association-roles a)))
190 (every (lambda (r) (eq (named map "a") (association-role-player r))) (association-roles a))))
191 (topic-map-associations map))))
192 (dolist (text '("a isa ?target; ako ?target." "?owner isa ?target; ako [ako ?target]."
193 "a isa [isa [-\"C\"]]; ako [ako b]." "a isa =<urn:resource>."
194 "a isa ^<urn:item>." "a isa <urn:subject>."
195 "a isa [^<https://example.invalid/a/doc.ctm#a>]."
196 "a isa b; - \"Name\"." "a ISA: \"N\"; ako b."))
197 (check (concatenate 'string "relationships:existing-topic-reference-and-tail-syntax:" text) (valid-model-p (read-text text))))
198 (let* ((map (read-text "a isa [^<https://example.invalid/a/doc.ctm#a>].")) (a (first (topic-map-associations map))))
199 (check "relationships:RHS-merge-keeps-owner-and-target-canonical-EQ"
200 (every (lambda (r) (eq (named map "a") (association-role-player r))) (association-roles a))))
201 (let ((map (read-text (format nil "%prefix isa http://example.org/~%a isa isa:t. isa:x."))))
202 (check "relationships:keyword-not-confused-with-QName-prefix"
203 (and (with-si map "http://example.org/t") (with-si map "http://example.org/x") (valid-model-p map))))
204 (check "relationships:keyword-matching-preserves-case-and-identifier-boundaries"
205 (let ((map (read-text "isabel. ako2. ISA.")))
206 (and (= 3 (length (topic-map-topics map))) (null (topic-map-associations map))))))
207
208 (defun diagnostic-tests ()
209 (dolist (fixture '(("a isa ." :expected-relationship-topic 6) ("a ako" :expected-relationship-topic 5)
210 ("a isa b,c." :unexpected-relationship-list 7) ("a ako b, c." :unexpected-relationship-list 7)
211 ("a isa b isa c." :expected-separator 8) ("a isa [ako]." :expected-relationship-topic 10)
212 ("a isa ako." :expected-relationship-topic 6) ("isa." :reserved-topic-identifier 0)
213 ("a isa def." :expected-relationship-topic 6) ("a ako end." :expected-relationship-topic 6)
214 ("a isa b @s." :unexpected-token 8) ("a ako b ~r." :unexpected-token 8)))
215 (destructuring-bind (text code position) fixture
216 (let ((c (condition-from (lambda () (read-text text)))))
217 (check (concatenate 'string "diagnostic:class-code-and-position:" text)
218 (and (eq (type-of c) 'topic-maps.ctm:ctm-rejection) (eq code (topic-maps.ctm:error-code c))
219 (= position (topic-maps.ctm:error-position c)) (= 1 (topic-maps.ctm:error-line c))
220 (= (1+ position) (topic-maps.ctm:error-column c)))))))
221 (let ((c (condition-from (lambda () (read-text (format nil "a isa~% ."))))))
222 (check "diagnostic:missing-RHS-newline-and-column"
223 (and (eq :expected-relationship-topic (topic-maps.ctm:error-code c))
224 (= 9 (topic-maps.ctm:error-position c)) (= 2 (topic-maps.ctm:error-line c)) (= 4 (topic-maps.ctm:error-column c)))))
225 (let ((c (condition-from (lambda () (read-text "a isa bad:t.")))))
226 (check "diagnostic:undefined-prefix-remains-its-own-class"
227 (and (eq (type-of c) 'topic-maps.ctm:undefined-prefix) (= 6 (topic-maps.ctm:error-position c)))))
228 (let* ((text (format nil "a <~A>; <~A>." (core-iri "type") (core-iri "instance")))
229 (c (condition-from (lambda () (read-text text)))))
230 (check "diagnostic:core-identity-conflict-is-specific-CTM-rejection"
231 (and (eq (type-of c) 'topic-maps.ctm:ctm-rejection) (eq :core-subject-conflict (topic-maps.ctm:error-code c))
232 (= (search "<http" text :from-end t) (topic-maps.ctm:error-position c)))))
233 (let* ((text (format nil "a - \"N\" ~~<~A>; - \"N\" ~~<~A>." (core-iri "type") (core-iri "instance")))
234 (c (condition-from (lambda () (read-text text)))))
235 (check "diagnostic:core-reifier-conflict-points-to-reifier-marker"
236 (and (eq (type-of c) 'topic-maps.ctm:ctm-rejection) (eq :core-subject-conflict (topic-maps.ctm:error-code c))
237 (= (position #\~ text :from-end t) (topic-maps.ctm:error-position c))))))
238
239 (defun observe-model (map)
240 (let ((topics (topic-map-topics map)))
241 (flet ((ordinal (tpc) (1+ (position tpc topics :test #'eq))))
242 (topic-maps.test-foundation::fields
243 "topics" (topic-maps.test-foundation::json-array
244 (loop for tpc in topics collect (topic-maps.test-foundation::fields
245 "ordinal" (ordinal tpc)
246 "itemIdentifiers" (topic-maps.test-foundation::json-array (topic-item-identifiers tpc))
247 "subjectIdentifiers" (topic-maps.test-foundation::json-array (topic-subject-identifiers tpc))
248 "subjectLocators" (topic-maps.test-foundation::json-array (topic-subject-locators tpc)))))
249 "associations" (topic-maps.test-foundation::json-array
250 (loop for a in (topic-map-associations map) collect (topic-maps.test-foundation::fields
251 "type" (ordinal (association-type a)) "scope" (topic-maps.test-foundation::json-array (mapcar #'ordinal (association-scope a)))
252 "parentIsMap" (if (eq map (association-parent a)) :true :false)
253 "roles" (topic-maps.test-foundation::json-array
254 (loop for r in (association-roles a) collect (topic-maps.test-foundation::fields
255 "type" (ordinal (association-role-type r)) "player" (ordinal (association-role-player r))
256 "parentIsAssociation" (if (eq a (association-role-parent r)) :true :false)
257 "roleIsCanonical" (if (eq r (canonical-association-role r)) :true :false)
258 "inverseContainsRole" (if (member r (topic-roles-played (association-role-player r)) :test #'eq) :true :false)))))))))))
259 (defun original-cases ()
260 (setf *observations* nil)
261 (dolist (file *originals*)
262 (let* ((id (format nil "ctm/in/~A.ctm" file))
263 (case (first (topic-maps.test-foundation:select-cases topic-maps.ctm.tests::*cxtm-corpus* :id id)))
264 (doc (topic-maps.test-foundation:test-case-document-iri case))
265 (map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri doc :source-name id))
266 (held (handles map)) (before (state map held))
267 (actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc))
268 (expected (binary-file (topic-maps.test-foundation:test-case-expected-baseline case))))
269 (check (concatenate 'string id ":actual-canonical-TMDM-object-references") (valid-model-p map))
270 (check (concatenate 'string id ":byte-exact-original-CXTM") (equalp actual expected))
271 (check (concatenate 'string id ":pure-repeatable-CXTM-and-retained-handles")
272 (and (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator doc)) (equal before (state map held))))
273 (topic-maps.test-foundation:record-result case :cxtm-match)
274 (setf (topic-maps.test-foundation:test-case-clauses case)
275 '("CTM 3.9" "CTM 3.3.6-3.3.8" "TMDM 5.3.5" "TMDM 5.7-5.8" "TMDM 6.2" "TMDM 6.6-6.7" "TMDM 7.1-7.3" "CXTM 3.9-3.10" "CXTM 4.10-4.11"))
276 (setf topic-maps.ctm.tests::*cxtm-results* (append topic-maps.ctm.tests::*cxtm-results*
277 (list (list :id id :base doc :actual (cxtm map doc) :expected (uiop:read-file-string (topic-maps.test-foundation:test-case-expected-baseline case))
278 :match t :bytes (length actual)))))
279 (push (topic-maps.test-foundation::fields "id" id "documentIRI" doc
280 "ctmSource" (uiop:read-file-string (topic-maps.test-foundation:test-case-source-pathname case))
281 "model" (observe-model map)) *observations*)
282 (format t "~&RELATIONSHIP-CXTM ~A: PASS (~D bytes)~%" id (length actual))))
283 (setf *observations* (nreverse *observations*)))
284 (defun run-tests ()
285 (topic-maps.arity.tests:run-tests)
286 (let ((topic-maps.ctm.tests::*checks* nil))
287 (setf *rejections* nil)
288 (core-tests) (chain-tests) (reifier-tests) (semantic-tests) (diagnostic-tests) (original-cases)
289 (check "relationships:only-thirteen-new-imported-contracts"
290 (equal '(:pass 120 :fail 2 :unsupported 0 :not-run 151)
291 (getf (topic-maps.test-foundation:summarize-corpus topic-maps.ctm.tests::*cxtm-corpus*) :statuses)))
292 (setf *last-checks* (nreverse topic-maps.ctm.tests::*checks*) *rejections* (nreverse *rejections*))
293 (format t "~&TYPE-RELATIONSHIPS-PASS: ~D checks; 13 original CXTM contracts; 120 PASS, 2 known FAIL, 151 NOT-RUN.~%" (length *last-checks*)) t))
294 (defun write-relationship-report (path)
295 (with-open-file (out path :direction :output :if-exists :supersede :if-does-not-exist :create)
296 (topic-maps.test-foundation::emit-json-value
297 (topic-maps.test-foundation::fields "kind" "type-relationship-evidence" "passed" (length *last-checks*) "failed" 0
298 "checks" (topic-maps.test-foundation::json-array *last-checks*)
299 "coreSubjectIdentifiers" (topic-maps.test-foundation::json-array (core-subject-identifiers))
300 "rejections" (topic-maps.test-foundation::json-array *rejections*)
301 "cases" (topic-maps.test-foundation::json-array *observations*)) out)
302 (terpri out)))