]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-merging.lisp
Document XTM normative coverage and remaining qualification gaps
[topic-maps.git] / tests / ctm-merging.lisp
1 (in-package #:topic-maps.ctm.tests)
2
3 (defvar *last-merge-checks* nil)
4 (defparameter *merge-fixtures*
5 ;; File, item identifiers, subject identifiers, subject locators, name value.
6 '(("topic-subject-identifier.ctm" ("#ctm") ("http://www.isotopicmaps.org/ctm/") nil nil)
7 ("topic-subject-identifier-wrapped.ctm" ("#ctm") ("http://www.isotopicmaps.org/ctm/") nil nil)
8 ("topic-subject-identifier-duplicate.ctm" nil ("http://www.isotopicmaps.org/ctm/") nil nil)
9 ("topic-subject-locator.ctm" ("#ctm") nil ("http://www.isotopicmaps.org/ctm/") nil)
10 ("topic-subject-locator-wrapped.ctm" ("#ctm") nil ("http://www.isotopicmaps.org/ctm/") nil)
11 ("topic-subject-locator-semi.ctm" ("#ctm") nil ("http://www.isotopicmaps.org/ctm/") nil)
12 ("topic-subject-locator-duplicate.ctm" nil nil ("#ctm") nil)
13 ("topic-item-identifier.ctm" ("#topic" "http://example.org/#topic") nil nil nil)
14 ("topic-item-identifier-wrapped.ctm" ("#topic" "http://example.org/#topic") nil nil nil)
15 ("topic-item-identifier-duplicate.ctm" ("#topic") nil nil nil)
16 ("name-duplicate.ctm" ("#topic") nil nil "Name")))
17
18 (defun same-locator-set-p (a b)
19 (equal (sort (copy-list a) #'string<) (sort (copy-list b) #'string<)))
20
21 (defun raw-reference-invariants (map)
22 ;; Inspect storage too: public forwarding readers must not hide dangling refs.
23 (and (verify-item-invariants map)
24 (every (lambda (topic)
25 (and (eq topic (topic-maps.tmdm:canonical-topic topic))
26 (every (lambda (name)
27 (and (eq name (topic-maps.tmdm:canonical-topic-name name))
28 (eq topic (topic-maps.tmdm::%topic-name-parent name))
29 (member (topic-maps.tmdm::%topic-name-type name)
30 (topic-map-topics map) :test #'eq)))
31 (topic-names topic))))
32 (topic-map-topics map))))
33
34 (defun exercise-merge-corpus ()
35 (let ((corpus (topic-maps.test-foundation:discover-corpus)) (observations nil))
36 (dolist (fixture *merge-fixtures*)
37 (destructuring-bind (file ii si sl value) fixture
38 (let* ((id (concatenate 'string "ctm/in/" file))
39 (case (first (topic-maps.test-foundation:select-cases corpus :id id)))
40 (document (topic-maps.test-foundation:test-case-document-iri case))
41 (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
42 :document-iri document :source-name id))
43 (topics (topic-map-topics map)) (topic (first topics)))
44 (flet ((resolve (values)
45 (mapcar (lambda (v) (if (char= #\# (char v 0))
46 (concatenate 'string document v) v)) values)))
47 (check (concatenate 'string id ":item-identifiers") (same-locator-set-p (topic-item-identifiers topic) (resolve ii)))
48 (check (concatenate 'string id ":subject-identifiers") (same-locator-set-p (topic-subject-identifiers topic) (resolve si)))
49 (check (concatenate 'string id ":subject-locators") (same-locator-set-p (topic-subject-locators topic) (resolve sl))))
50 (check (concatenate 'string id ":topic-count") (= (length topics) (if value 2 1)))
51 (check (concatenate 'string id ":actual-references") (raw-reference-invariants map))
52 (check (concatenate 'string id ":name-count") (= (length (topic-names topic)) (if value 1 0)))
53 (when value
54 (let ((name (first (topic-names topic))) (type (second topics)))
55 (check "pinned-duplicate-name-value" (string= value (topic-name-value name)))
56 (check "pinned-duplicate-name-type" (eq type (topic-name-type name)))
57 (check "pinned-duplicate-default-type-properties"
58 (and (equal (topic-subject-identifiers type) (list (default-name-type-identifier)))
59 (null (topic-item-identifiers type)) (null (topic-subject-locators type))
60 (null (topic-names type))))))
61 (check (concatenate 'string id ":cxtm-contract-not-run")
62 (and (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case))
63 (eq :not-run (topic-maps.test-foundation:test-case-status case))
64 (null (topic-maps.test-foundation:test-case-actual-outcome case))))
65 (push (list :id id :document-iri document :topic-map map) observations))))
66 (nreverse observations)))
67
68 (defun run-merging-tests ()
69 (let ((*checks* nil))
70 (exercise-merge-corpus)
71 (let* ((map (topic-maps.tmdm:make-topic-map))
72 (a (topic-maps.tmdm:intern-topic map :item-identifier "urn:a"))
73 (b (topic-maps.tmdm:intern-topic map :item-identifier "urn:b"))
74 (na (topic-maps.tmdm:add-default-name a "Same"))
75 (nb (topic-maps.tmdm:add-default-name b "Same")))
76 (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:shared")
77 (let* ((c (topic-maps.tmdm:add-topic-identifier b :subject-identifier "urn:shared"))
78 (name (first (topic-names c))))
79 (check "merge-creates-new-topic" (and (not (eq c a)) (not (eq c b))))
80 (check "both-old-topics-resolve-to-c"
81 (and (eq c (topic-maps.tmdm:canonical-topic a)) (eq c (topic-maps.tmdm:canonical-topic b))))
82 (check "merged-item-identifiers-union" (same-locator-set-p (topic-item-identifiers c) '("urn:a" "urn:b")))
83 (check "merged-subject-identifier-once" (equal (topic-subject-identifiers c) '("urn:shared")))
84 (check "merged-parent-name-deduplicated" (= 1 (length (topic-names c))))
85 (check "name-merge-creates-new-item" (and (not (eq name na)) (not (eq name nb))))
86 (check "old-names-resolve-to-new-item"
87 (and (eq name (topic-maps.tmdm:canonical-topic-name na))
88 (eq name (topic-maps.tmdm:canonical-topic-name nb))))
89 (check "old-topic-readers-show-current-properties" (equal (topic-item-identifiers a) (topic-item-identifiers c)))
90 (check "name-parent-raw-reference-replaced" (eq c (topic-maps.tmdm::%topic-name-parent name)))
91 (check "lookup-through-old-item-identifier" (eq c (topic-maps.tmdm:intern-topic map :item-identifier "urn:b")))
92 (check "repeated-identity-addition-idempotent" (eq c (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:shared")))
93 (check "no-obsolete-topics-in-map" (= 2 (length (topic-map-topics map))))
94 (check "merged-map-raw-reference-invariants" (raw-reference-invariants map))
95 (check "merge-identical-handle-is-idempotent" (eq c (topic-maps.tmdm:merge-topics a b)))
96 (let ((snapshot (topic-map-topics map)))
97 (setf (first snapshot) a)
98 (check "topic-set-readers-protect-storage" (not (eq a (first (topic-map-topics map))))))))
99 (let* ((map (topic-maps.tmdm:make-topic-map))
100 (a (topic-maps.tmdm:intern-topic map :item-identifier "urn:a"))
101 (b (topic-maps.tmdm:intern-topic map :item-identifier "urn:b"))
102 (c (topic-maps.tmdm:intern-topic map :item-identifier "urn:c")))
103 (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:x")
104 (topic-maps.tmdm:add-topic-identifier b :subject-identifier "urn:y")
105 (topic-maps.tmdm:add-topic-identifier c :subject-identifier "urn:z")
106 (let* ((ab (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:y"))
107 (abc (topic-maps.tmdm:add-topic-identifier c :subject-identifier "urn:x")))
108 (check "transitive-chain-one-live-topic" (= 1 (length (topic-map-topics map))))
109 (check "transitive-all-handles-converge"
110 (every (lambda (old) (eq abc (topic-maps.tmdm:canonical-topic old))) (list a b c ab)))
111 (check "transitive-all-item-identifiers-preserved" (same-locator-set-p (topic-item-identifiers abc) '("urn:a" "urn:b" "urn:c")))
112 (check "transitive-all-subject-identifiers-preserved" (same-locator-set-p (topic-subject-identifiers abc) '("urn:x" "urn:y" "urn:z")))
113 (check "transitive-normalization-idempotent"
114 (and (eq map (topic-maps.tmdm:normalize-topic-map map)) (eq abc (first-topic map))))))
115 (dolist (kind '(:item-identifier :subject-identifier :subject-locator))
116 (let* ((map (topic-maps.tmdm:make-topic-map))
117 (a (topic-maps.tmdm:intern-topic map :item-identifier "urn:a"))
118 (b (topic-maps.tmdm:intern-topic map :item-identifier "urn:b")))
119 (topic-maps.tmdm:add-topic-identifier a kind "urn:shared")
120 (topic-maps.tmdm:add-topic-identifier b kind "urn:shared")
121 (check (format nil "same-category-merge:~A" kind) (= 1 (length (topic-map-topics map))))))
122 (dolist (kinds '((:item-identifier :subject-identifier) (:subject-identifier :item-identifier)
123 (:item-identifier :subject-locator) (:subject-identifier :subject-locator)))
124 (let* ((map (topic-maps.tmdm:make-topic-map))
125 (a (topic-maps.tmdm:intern-topic map :item-identifier "urn:a"))
126 (b (topic-maps.tmdm:intern-topic map :item-identifier "urn:b"))
127 (merges (not (eq :subject-locator (second kinds)))))
128 (topic-maps.tmdm:add-topic-identifier a (first kinds) "urn:shared")
129 (topic-maps.tmdm:add-topic-identifier b (second kinds) "urn:shared")
130 (check (format nil "cross-category-merge-rule:~S" kinds) (= (if merges 1 2) (length (topic-map-topics map))))))
131 (let* ((map (read-text "a - \"Same\". b - \"Same\"."))
132 (topics (topic-map-topics map)) (a (first topics)) (type (second topics))
133 (na (first (topic-names a))) (nb (first (topic-names (third topics))))
134 (c (topic-maps.tmdm:merge-topics a type)))
135 (check "default-type-merge-retargets-all-raw-type-references"
136 (and (eq c (topic-maps.tmdm::%topic-name-type na)) (eq c (topic-maps.tmdm::%topic-name-type nb))))
137 (check "default-type-merge-retargets-parent" (eq c (topic-maps.tmdm::%topic-name-parent na)))
138 (check "self-typed-name-is-valid" (eq (topic-name-parent na) (topic-name-type na)))
139 (check "retained-default-type-handle-resolves" (eq c (topic-maps.tmdm:canonical-topic type)))
140 (check "later-name-reuses-merged-default-type"
141 (eq c (topic-name-type (topic-maps.tmdm:add-default-name a "Later")))))
142 (let* ((ma (read-text "a.")) (mb (read-text "b.")) (a (first-topic ma)) (b (first-topic mb)))
143 (check "cross-map-merge-rejected"
144 (handler-case (progn (topic-maps.tmdm:merge-topics a b) nil) (error () t)))
145 (check "cross-map-rejection-leaves-both-maps-unchanged"
146 (and (eq a (first-topic ma)) (eq b (first-topic mb))
147 (= 1 (length (topic-map-topics ma))) (= 1 (length (topic-map-topics mb))))))
148 (let ((map (topic-maps.tmdm:make-topic-map)))
149 (check "invalid-kind-rejected-before-topic-creation"
150 (handler-case (progn (topic-maps.tmdm:intern-topic map :wrong "urn:x") nil) (error () t)))
151 (check "invalid-kind-leaves-map-empty" (null (topic-map-topics map))))
152 (let* ((map (read-text "a <urn:x>; <urn:y>; ^<#extra>; =<urn:r>.")) (a (first-topic map)))
153 (check "all-identity-tail-categories-one-topic" (= 1 (length (topic-map-topics map))))
154 (check "tail-subject-identifiers-preserved" (same-locator-set-p (topic-subject-identifiers a) '("urn:x" "urn:y")))
155 (check "tail-item-identifiers-preserved" (same-locator-set-p (topic-item-identifiers a) '("https://example.invalid/a/doc.ctm#a" "https://example.invalid/a/doc.ctm#extra")))
156 (check "tail-subject-locator-separate" (equal (topic-subject-locators a) '("urn:r"))))
157 (loop for declarations in '("a <urn:x>. b <urn:y>. c <urn:z>."
158 "c <urn:z>. b <urn:y>. a <urn:x>."
159 "b <urn:y>. a <urn:x>. c <urn:z>.")
160 for i from 1 do
161 (let* ((map (read-text (concatenate 'string declarations " a <urn:y>. c <urn:x>."))) (a (first-topic map)))
162 (check (format nil "ctm-transitive-order-topic-count:~D" i) (= 1 (length (topic-map-topics map))))
163 (check (format nil "ctm-transitive-order-locators:~D" i) (same-locator-set-p (topic-subject-identifiers a) '("urn:x" "urn:y" "urn:z")))))
164 (let* ((map (read-text "a - \"Same\". b - \"Same\". a <urn:x>. b <urn:x>; - \"Later\"."))
165 (a (first-topic map)))
166 (check "ctm-late-merge-removes-duplicate-name" (equal (mapcar #'topic-name-value (topic-names a)) '("Same" "Later")))
167 (check "ctm-continues-on-merged-parent" (raw-reference-invariants map))
168 (check "ctm-late-merge-no-extra-items" (= 2 (length (topic-map-topics map)))))
169 (dolist (text '("a <urn:x> <urn:y>." "a - \"Name\" <urn:x>." "a <urn:x> - \"Name\"."))
170 (check (concatenate 'string "identity-tail-separator-required:" text)
171 (eq :expected-separator (error-code (rejection text)))))
172 (check "identity-tail-needs-iri-after-marker" (eq :expected-iri (error-code (rejection "a ^local."))))
173 (let ((condition (rejection "a ex:fake.")))
174 (check "identity-tail-undefined-prefix-specific"
175 (and (typep condition 'undefined-prefix) (eq :undefined-prefix (error-code condition))
176 (= 2 (error-position condition)) (= 1 (error-line condition)) (= 3 (error-column condition)))))
177 (run-merging-graph-checks)
178 (setf *last-merge-checks* (nreverse *checks*))
179 (format t "~&CTM-MERGING-SEMANTICS-PASS: ~D checks, 11 pinned semantic cases; no CXTM equality passes.~%" (length *last-merge-checks*))
180 t))
181
182 (defun write-merging-report (pathname)
183 (let ((*checks* nil))
184 (let ((observations (exercise-merge-corpus)))
185 (with-open-file (out pathname :direction :output :if-exists :supersede :if-does-not-exist :create)
186 (topic-maps.test-foundation::emit-json-value
187 (topic-maps.test-foundation::fields
188 "kind" "semantic-observations-not-cxtm" "passed" (length *last-merge-checks*) "failed" 0
189 "checks" (topic-maps.test-foundation::json-array *last-merge-checks*)
190 "cases" (topic-maps.test-foundation::json-array
191 (loop for observation in observations collect
192 (topic-maps.test-foundation::fields
193 "id" (getf observation :id) "documentIRI" (getf observation :document-iri)
194 "actualObjects" (model-observation (getf observation :topic-map)))))) out)
195 (terpri out)))))