1 (defpackage #:topic-maps.equivalence.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.xtm-mergemap.tests #:check)
4 (:export #:run-tests #:write-report))
5 (in-package #:topic-maps.equivalence.tests)
8 (defun fields (&rest args) (apply #'topic-maps.test-foundation::fields args))
9 (defun octets (text) (babel:string-to-octets text :encoding :utf-8 :use-bom nil))
10 (defun xml (body &optional reifier)
11 (format nil "<topicMap xmlns='http://www.topicmaps.org/xtm/' version='2.0'~A>~A</topicMap>"
12 (if reifier (format nil " reifier='~A'" reifier) "") body))
13 (defun sorted-set (values)
14 ;; Sort only temporary property lists. Never edit model collections/CXTM bytes.
15 (sort (copy-list values) #'string< :key (lambda (v) (write-to-string v :readably t))))
17 (list (sorted-set (item-identifiers topic)) (sorted-set (topic-subject-identifiers topic))
18 (sorted-set (topic-subject-locators topic))))
19 (defun refs (topics) (sorted-set (mapcar #'ids topics)))
21 (list :association (ids (association-type a)) (refs (association-scope a))
22 (sorted-set (mapcar (lambda (r) (list (ids (association-role-type r)) (ids (association-role-player r)))) (association-roles a)))))
24 ;; Ephemeral test property tuples based on native TMDM equality rules, not
25 ;; another identity registry or an alternate Topic Maps object model.
28 (topic-map (list :map))
29 (topic (cons :topic (ids item)))
30 (topic-name (list :name (ids (topic-name-parent item)) (ids (topic-name-type item))
31 (topic-name-value item) (refs (topic-name-scope item))))
32 (variant (list :variant (ref (variant-parent item)) (variant-value item) (variant-datatype item) (refs (variant-scope item))))
33 (occurrence (list :occurrence (ids (occurrence-parent item)) (ids (occurrence-type item))
34 (occurrence-value item) (occurrence-datatype item) (refs (occurrence-scope item))))
35 (association (assoc-key item))
36 (association-role (list :role (assoc-key (association-role-parent item))
37 (ids (association-role-type item)) (ids (association-role-player item)))))))
38 (defun properties (map)
39 (sorted-set (loop for item in (topic-maps.tmdm::map-information-items map) collect
40 (list (ref item) (sorted-set (item-identifiers item))
41 (when (topic-maps.tmdm::reifiable-p item) (ref (item-reifier item)))
43 (topic-map (list (refs (topic-map-topics item)) (sorted-set (mapcar #'ref (topic-map-associations item)))))
44 (topic (list (sorted-set (mapcar #'ref (topic-names item)))
45 (sorted-set (mapcar #'ref (topic-occurrences item))) (sorted-set (mapcar #'ref (topic-roles-played item)))
46 (ref (topic-reified item))))
47 (topic-name (sorted-set (mapcar #'ref (topic-name-variants item)))))))))
49 (list (length (topic-map-topics map))
50 (loop for topic in (topic-map-topics map) sum (length (topic-names topic)))
51 (loop for topic in (topic-map-topics map) sum (loop for n in (topic-names topic) sum (length (topic-name-variants n))))
52 (loop for topic in (topic-map-topics map) sum (length (topic-occurrences topic)))
53 (length (topic-map-associations map))
54 (loop for a in (topic-map-associations map) sum (length (association-roles a)))
55 (count-if (lambda (item) (and (topic-maps.tmdm::reifiable-p item) (item-reifier item)))
56 (topic-maps.tmdm::map-information-items map))))
58 (fields "countOrder" #("topics" "names" "variants" "occurrences" "associations" "roles" "reifiedItems")
59 "counts" (coerce (counts map) 'vector) "topicIdentities" (write-to-string (refs (topic-map-topics map)) :readably t)
60 "completeNativePropertyTuples" (write-to-string (properties map) :readably t)))
61 (defun handles (map) (topic-maps.import.tests::handles map))
62 (defun state (map held) (topic-maps.import.tests::state map held))
63 (defun canonical-eq-p (map)
64 (and (topic-maps.reference.tests::references-current-p map)
65 (topic-maps.tmdm::validate-import-map map)
66 (every (lambda (i) (and (eq i (topic-maps.tmdm::canonical-item i))
67 (eq map (topic-maps.tmdm::item-topic-map i)))) (handles map))
68 (every (lambda (i) (or (not (topic-maps.tmdm::reifiable-p i)) (null (item-reifier i))
69 (and (eq (item-reifier i) (canonical-topic (item-reifier i))) (eq i (topic-reified (item-reifier i)))))) (handles map))))
70 (defun pure-cxtm (id map base)
71 (let* ((held (handles map)) (before (state map held)) (props (properties map))
72 (bytes (topic-maps.cxtm:canonicalize-octets map :base-locator base)))
73 (check (concatenate 'string "equivalence:" id ":pure-deterministic-cxtm")
74 (and (equalp bytes (topic-maps.cxtm:canonicalize-octets map :base-locator base))
75 (equal before (state map held)) (equal props (properties map))
76 (every (lambda (i) (eq i (topic-maps.tmdm::canonical-item i))) held)))
78 (defun condition-data (c)
80 (let ((code (typecase c
81 (topic-maps.ctm:ctm-error (topic-maps.ctm:error-code c))
82 (topic-maps.xtm:xtm-error (topic-maps.xtm:error-code c)))))
83 (fields "class" (string (type-of c)) "code" (when code (string-downcase (string code)))
84 "line" (typecase c (topic-maps.ctm:ctm-error (topic-maps.ctm:error-line c)) (topic-maps.xtm:xtm-error (topic-maps.xtm:error-line c)))
85 "column" (typecase c (topic-maps.ctm:ctm-error (topic-maps.ctm:error-column c)) (topic-maps.xtm:xtm-error (topic-maps.xtm:error-column c)))
86 "sourceKey" (typecase c (topic-maps.ctm:ctm-error (topic-maps.ctm:error-source c)) (topic-maps.xtm:xtm-error (topic-maps.xtm:error-source-key c)))
87 "documentIRI" (when (typep c 'topic-maps.xtm:xtm-error) (topic-maps.xtm:error-document-iri c))
88 "position" (when (typep c 'topic-maps.ctm:ctm-error) (topic-maps.ctm:error-position c))
89 "message" (princ-to-string c)))))
90 (defun local-key (doc name &key si sl ii)
91 (list (sorted-set (cons (concatenate 'string doc "#" name) ii)) (sorted-set si) (sorted-set sl)))
92 (defun default-type () (list nil (list (default-name-type-identifier)) nil))
93 (defun core-key (local) (list nil (list (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local)) nil))
94 (defun pair (id ctm body keys count why clauses &key literal datatype reifier resources)
95 (list :id id :ctm ctm :xtm (xml body reifier) :keys keys :counts count :why why :clauses clauses
96 :literal literal :datatype datatype :resources resources))
99 (flet ((add (row) (push row rows)))
100 (add (pair "local-II" "a." "<topic id='a'/>" '("a") '(1 0 0 0 0 0 0)
101 "CTM local identifier and XTM id both assign the common Document IRI followed by #a." '("CTM 3.3.6" "XTM 4.5" "TMDM 5.3.5")))
102 (dolist (row '(("SI" "<urn:subject>" "subjectIdentifier" :si)
103 ("SL" "=<urn:subject>" "subjectLocator" :sl)
104 ("II" "^<urn:item>" "itemIdentity" :ii)))
105 (destructuring-bind (id tail tag kind) row
106 (let ((value (if (eq kind :ii) "urn:item" "urn:subject")))
107 (add (pair (concatenate 'string "identity-" id) (format nil "a ~A." tail)
108 (format nil "<topic id='a'><~A href='~A'/></topic>" tag value)
109 (list (list "a" kind (list value))) '(1 0 0 0 0 0 0)
110 "Both sources preserve the local II and add the same locator in the same identity category."
111 '("CTM 3.3.6,3.9" "XTM 4.5-4.8" "TMDM 5.3.5")))
112 (add (pair (concatenate 'string "merge-" id) (format nil "a ~A. b ~A." tail tail)
113 (format nil "<topic id='b'><~A href='~A'/></topic><topic id='a'><~A href='~A'/></topic>" tag value tag value)
114 (list (if (eq kind :ii) (list "a" :ii (list value "#b")) (list "a" kind (list value) :ii '("#b")))) '(1 0 0 0 0 0 0)
115 "The shared locator merges two Topics; both document-local IIs must survive. Creation order differs."
116 '("CTM 3.3.6,3.9" "XTM 4.5-4.8" "TMDM 5.3.5,6.2"))))))
117 (add (pair "isa-instanceOf" "a isa t." "<topic id='a'><instanceOf><topicRef href='#t'/></instanceOf></topic>"
118 (list "a" "t" (core-key "type-instance") (core-key "type") (core-key "instance")) '(5 0 0 0 1 2 0)
119 "Both create the same type-instance Association with instance/type Roles and identical Core Subject SIs."
120 '("CTM 3.9" "XTM 4.9" "TMDM 7.2")))
121 (add (pair "isa-multiple-normalized" "a isa t; isa u; isa t."
122 "<topic id='u'/><topic id='a'><instanceOf><topicRef href='#t'/><topicRef href='#u'/><topicRef href='#t'/></instanceOf></topic>"
123 (list "a" "t" "u" (core-key "type-instance") (core-key "type") (core-key "instance")) '(6 0 0 0 2 4 0)
124 "Two distinct type-instance Associations remain; repeating one relation is normalized."
125 '("CTM 3.9" "XTM 4.9" "TMDM 6.6,7.2")))
126 (add (pair "default-name" "a - \"Name\"." "<topic id='a'><name><value>Name</value></name></topic>"
127 (list "a" (default-type)) '(2 1 0 0 0 0 0) "Both create one Name of the same implicit topic-name Core Subject type."
128 '("CTM 3.11" "XTM 4.10-4.11" "TMDM 5.4,7.5") :literal "Name"))
129 (add (pair "typed-scoped-name" "a - t:\"Name\"@s2,s1,s1."
130 "<topic id='a'><name><type><topicRef href='#t'/></type><scope><topicRef href='#s1'/><topicRef href='#s2'/></scope><value>Name</value></name></topic>"
131 '("a" "t" "s1" "s2") '(4 1 0 0 0 0 0) "Name Type and Scope are identical native references; Scope is a set."
132 '("CTM 3.3.9,3.11" "XTM 4.10,4.13-4.14" "TMDM 5.4") :literal "Name"))
133 (add (pair "unicode-whitespace-escaping" "a - \" 馬🦊 & < > \\n \\t \\\" \\\\ \"."
134 "<topic id='a'><name><value> 馬🦊 & < > 
 	 " \\ </value></name></topic>"
135 (list "a" (default-type)) '(2 1 0 0 0 0 0) "CTM escapes and XML references encode the same Unicode characters and whitespace."
136 '("CTM 3.4.2,3.11" "XTM 4.11" "TMDM 5.4")))
137 (add (pair "empty-name" "a - \"\"." "<topic id='a'><name><value/></name></topic>" (list "a" (default-type)) '(2 1 0 0 0 0 0)
138 "Both Name values are the empty string." '("CTM 3.11" "XTM 4.11" "TMDM 5.4") :literal ""))
139 (add (pair "duplicate-names" "a - \"N\"; - \"N\"." "<topic id='a'><name><value>N</value></name><name><value>N</value></name></topic>"
140 (list "a" (default-type)) '(2 1 0 0 0 0 0) "Equal Names normalize to one native Name in both maps."
141 '("CTM 3.11" "XTM 4.10" "TMDM 5.4,6.3") :literal "N"))
142 (add (pair "variant-inherited-scope" "a - \"N\"@base (\"V\"@extra)."
143 "<topic id='a'><name><scope><topicRef href='#base'/></scope><value>N</value><variant><scope><topicRef href='#extra'/></scope><resourceData>V</resourceData></variant></name></topic>"
144 (list "a" "base" "extra" (default-type)) '(4 1 1 0 0 0 0) "Variant Scope is Name Scope union explicit Scope, a proper superset."
145 '("CTM 3.12" "XTM 4.12" "TMDM 5.5") :literal "V" :datatype (string-datatype)))
146 (dolist (row (list (list "integer" "+00042" (integer-datatype)) (list "decimal" "01.50" (decimal-datatype))
147 (list "date" "2000-02-29" (date-datatype)) (list "datetime" "2000-02-29T01:02:03Z" (datetime-datatype))
148 (list "unknown-http" "literal%GG" "https://types.example.invalid/custom") (list "unknown-urn" " urn:literal " "urn:custom")))
149 (destructuring-bind (id value dtype) row
150 (add (pair (concatenate 'string "variant-datatype-" id)
151 (format nil "a - \"N\" (\"~A\"^^<~A>@s)." value dtype)
152 (format nil "<topic id='a'><name><value>N</value><variant><scope><topicRef href='#s'/></scope><resourceData datatype='~A'>~A</resourceData></variant></name></topic>" dtype value)
153 (list "a" "s" (default-type)) '(3 1 1 0 0 0 0)
154 "Both quoted resource forms retain the same lexical value and exact datatype IRI; datatype is not a Topic."
155 '("CTM 3.4.1,3.12" "XTM 4.12,4.16" "TMDM 4.4,5.5") :literal value :datatype dtype))))
156 (dolist (kind '("resourceRef" "anyURI-data"))
157 (add (pair (concatenate 'string "variant-" kind) "a - \"N\" (<../sound>@s)."
158 (format nil "<topic id='a'><name><value>N</value><variant><scope><topicRef href='#s'/></scope>~A</variant></name></topic>"
159 (if (equal kind "resourceRef") "<resourceRef href='../sound'/>" "<resourceData datatype='http://www.w3.org/2001/XMLSchema#anyURI'>../sound</resourceData>"))
160 (list "a" "s" (default-type)) '(3 1 1 0 0 0 0) "IRI-valued literals resolve against the same explicit Document IRI without Topic creation."
161 '("CTM 3.4.1" "XTM 4.16-4.17" "TMDM 4.4,5.5") :literal "https://equivalence.example.invalid/sound" :datatype (iri-datatype))))
162 (add (pair "variant-ctm-integer-star" "a - \"N\" (*@s)."
163 "<topic id='a'><name><value>N</value><variant><scope><topicRef href='#s'/></scope><resourceData datatype='http://psi.topicmaps.org/iso13250/ctm-integer'>*</resourceData></variant></name></topic>"
164 (list "a" "s" (default-type)) '(3 1 1 0 0 0 0) "The literal * is an opaque lexical value with ctm-integer datatype in both maps."
165 '("CTM 3.4.1,Annex A" "XTM 4.16" "TMDM 4.4") :literal "*" :datatype "http://psi.topicmaps.org/iso13250/ctm-integer"))
166 (add (pair "name-variant-reification" "a - \"N\"~nr (\"V\"@s ~vr)."
167 "<topic id='a'><name reifier='#nr'><value>N</value><variant reifier='#vr'><scope><topicRef href='#s'/></scope><resourceData>V</resourceData></variant></name></topic>"
168 (list "a" "nr" "vr" "s" (default-type)) '(5 1 1 0 0 0 2)
169 "The same local-II Topics reify the Name and Variant, with inverse references within each map."
170 '("CTM 3.3.10,3.11-3.12" "XTM 4.3.2,4.10,4.12" "TMDM 5.3.4") :literal "V" :datatype (string-datatype)))
171 (add (pair "map-reification" "~r a." "<topic id='a'/>" '("a" "r") '(2 0 0 0 0 0 1)
172 "The same local-II Topic reifies the fresh Map item; neither Map has explicit item identifiers."
173 '("CTM 3.5" "XTM 4.3.2,4.4" "TMDM 5.2,5.3.4") :reifier "#r"))
174 (add (pair "content-import" (format nil "%mergemap <child.xtm> <http://psi.topicmaps.org/iso13250/xtm>~%local.")
175 "<mergeMap href='child.xtm'/><topic id='local'/>" nil '(9 1 1 0 1 2 0)
176 "Both readers import the same supported XTM child using its resource IRI; child Map metadata is excluded."
177 '("CTM 3.16.3" "XTM 4.21" "TMDM 5.2,6.2") :resources t)))
179 (defun expanded-keys (spec doc)
180 (sorted-set (loop for key in spec collect
181 (cond ((stringp key) (local-key doc key))
182 ((stringp (first key))
183 (local-key doc (first key) :si (getf (rest key) :si) :sl (getf (rest key) :sl)
184 :ii (mapcar (lambda (iri) (if (and (plusp (length iri)) (char= #\# (char iri 0)))
185 (concatenate 'string doc iri) iri)) (getf (rest key) :ii))))
187 (defun execute (kind source doc key &optional resource-iri resource-raw)
188 (let* ((raw (octets source)) (copy (copy-seq raw)) (text-copy (copy-seq source))
189 (resource-copy (when resource-raw (copy-seq resource-raw))) (calls nil) (parses nil) (imports nil)
190 (old-parser (symbol-function 'topic-maps.xml:parse-octets))
191 (old-import (symbol-function 'import-map-content)) (map nil) (condition nil)
192 (resolver (when resource-raw (lambda (iri &key purpose from-document-iri notation)
193 (push (list iri purpose from-document-iri notation) calls)
194 (unless (equal iri resource-iri) (error "Unexpected equivalence resource ~A" iri))
195 (values resource-raw (concatenate 'string key ":child"))))))
199 (setf (symbol-function 'topic-maps.xml:parse-octets)
200 (lambda (input &rest args)
201 (push (list topic-maps.xtm::*document-iri* topic-maps.xtm::*source-key*
202 (eq input raw) (eq input resource-raw) (length input)) parses)
203 (apply old-parser input args)))
204 (setf (symbol-function 'import-map-content)
205 (lambda (target child)
206 (let* ((held (handles child)) (before (state child held)) (cx (topic-maps.cxtm:canonicalize-octets child :base-locator resource-iri))
207 (source-map-ids (copy-list (item-identifiers child))) (source-reifier (item-reifier child)))
208 (multiple-value-bind (result resolve) (funcall old-import target child)
209 (push (list source-map-ids (not (null source-reifier))
210 (and (equal before (state child held))
211 (equalp cx (topic-maps.cxtm:canonicalize-octets child :base-locator resource-iri)))) imports)
212 (values result resolve)))))
213 (setf map (ecase kind
214 (:ctm (topic-maps.ctm:deserialize-string source :document-iri doc :source-name key :resource-resolver resolver))
215 (:xtm (topic-maps.xtm:deserialize-octets raw :document-iri doc :source-key key :resource-resolver resolver)))))
216 (error (c) (setf condition c)))
217 (setf (symbol-function 'topic-maps.xml:parse-octets) old-parser
218 (symbol-function 'import-map-content) old-import))
219 (list :entry-point (ecase kind (:ctm "topic-maps.ctm:deserialize-string") (:xtm "topic-maps.xtm:deserialize-octets"))
220 :document-iri doc :source-key key :map map :condition condition :calls (nreverse calls) :parses (nreverse parses) :imports (nreverse imports)
221 :source-preserved (and (equalp raw copy) (equal source text-copy)
222 (or (null resource-raw) (equalp resource-raw resource-copy))))))
223 (defun trace-data (result)
224 (fields "entryPoint" (getf result :entry-point) "documentIRI" (getf result :document-iri) "sourceKey" (getf result :source-key)
225 "resolverCalls" (coerce (mapcar (lambda (c) (fields "resourceIRI" (first c) "purpose" (string-downcase (string (second c)))
226 "fromDocumentIRI" (third c) "notation" (fourth c))) (getf result :calls)) 'vector)
227 "qualifiedXMLParserCalls" (coerce (mapcar (lambda (p) (fields "documentIRI" (first p) "sourceKey" (second p)
228 "originalRootOctetsEQ" (if (third p) :true :false) "originalResourceOctetsEQ" (if (fourth p) :true :false) "bytes" (fifth p))) (getf result :parses)) 'vector)
229 "imports" (coerce (mapcar (lambda (i) (fields "sourceMapItemIdentifiers" (coerce (first i) 'vector)
230 "sourceMapReifierPresent" (if (second i) :true :false) "sourceMapPreserved" (if (third i) :true :false))) (getf result :imports)) 'vector)
231 "sourcePreserved" (if (getf result :source-preserved) :true :false)))
232 (defun expected-features (id map doc case)
233 (when (getf case :keys)
234 (check (format nil "equivalence:~A:~A:expected-topic-identities" (getf case :id) id)
235 (equal (expanded-keys (getf case :keys) doc) (refs (topic-map-topics map)))))
236 (check (format nil "equivalence:~A:~A:expected-native-counts" (getf case :id) id) (equal (getf case :counts) (counts map)))
237 (when (getf case :literal)
238 (check (format nil "equivalence:~A:~A:expected-exact-value-datatype" (getf case :id) id)
239 (if (getf case :datatype)
240 (some (lambda (topic) (some (lambda (name) (some (lambda (v)
241 (and (equal (getf case :literal) (variant-value v)) (equal (getf case :datatype) (variant-datatype v))))
242 (topic-name-variants name))) (topic-names topic))) (topic-map-topics map))
243 (some (lambda (topic) (find (getf case :literal) (topic-names topic) :key #'topic-name-value :test #'equal)) (topic-map-topics map)))))
244 (when (search "isa-" (getf case :id))
245 (let ((instance (find (concatenate 'string doc "#a") (topic-map-topics map)
246 :test (lambda (iri tpc) (member iri (item-identifiers tpc) :test #'equal)))))
247 (check (format nil "equivalence:~A:~A:actual-core-associations-role-types-players" (getf case :id) id)
249 (and (equal (list "http://psi.topicmaps.org/iso13250/model/type-instance") (topic-subject-identifiers (association-type a)))
250 (= 2 (length (association-roles a)))
251 (some (lambda (r) (and (eq instance (association-role-player r))
252 (member "http://psi.topicmaps.org/iso13250/model/instance" (topic-subject-identifiers (association-role-type r)) :test #'equal))) (association-roles a))
253 (some (lambda (r) (member "http://psi.topicmaps.org/iso13250/model/type" (topic-subject-identifiers (association-role-type r)) :test #'equal)) (association-roles a))))
254 (topic-map-associations map))))))
255 (defun run-pair (case)
256 (let* ((id (getf case :id)) (doc (format nil "https://equivalence.example.invalid/~A/map" id))
257 (child (format nil "https://equivalence.example.invalid/~A/child.xtm" id))
258 (resource (when (getf case :resources) (octets (xml
259 "<itemIdentity href='urn:child-map'/><topic id='owner'><instanceOf><topicRef href='#t'/></instanceOf><name><value>Imported</value><variant><scope><topicRef href='#s'/></scope><resourceData datatype='urn:custom'>V</resourceData></variant></name></topic>" "#child-r"))))
260 (ctm (execute :ctm (getf case :ctm) doc (concatenate 'string "ctm:" id) child resource))
261 (xtm (execute :xtm (getf case :xtm) doc (concatenate 'string "xtm:" id) child resource))
262 (a (getf ctm :map)) (b (getf xtm :map)) (ac nil) (bc nil) (props-equal nil))
264 (setf (getf case :keys) (list "local" (local-key child "owner") (local-key child "t")
265 (local-key child "s") (local-key child "child-r") (default-type)
266 (core-key "type-instance") (core-key "instance") (core-key "type"))))
268 (setf props-equal (equal (properties a) (properties b))
269 ac (pure-cxtm (concatenate 'string id ":CTM") a doc)
270 bc (pure-cxtm (concatenate 'string id ":XTM") b doc)))
271 ;; Record both actual states before an assertion can stop the suite.
272 (push (fields "id" id "kind" "direct-reader-equivalence-pair" "normativeReason" (getf case :why)
273 "clauses" (coerce (getf case :clauses) 'vector) "documentIRI" doc "canonicalBaseIRI" doc
274 "ctmSource" (getf case :ctm) "xtmSource" (getf case :xtm)
275 "resourceIRI" (when resource child) "resourceSource" (when resource (babel:octets-to-string resource :encoding :utf-8))
276 "ctmTrace" (trace-data ctm) "xtmTrace" (trace-data xtm)
277 "ctmCondition" (condition-data (getf ctm :condition)) "xtmCondition" (condition-data (getf xtm :condition))
278 "ctmModel" (when a (model a)) "xtmModel" (when b (model b))
279 "nativePropertiesEqual" (if props-equal :true :false)
280 "ctmCanonicalEQ" (if (and a (canonical-eq-p a)) :true :false)
281 "xtmCanonicalEQ" (if (and b (canonical-eq-p b)) :true :false) "crossMapEQRequired" :false
282 "ctmCXTM" (when ac (babel:octets-to-string ac :encoding :utf-8))
283 "xtmCXTM" (when bc (babel:octets-to-string bc :encoding :utf-8))
284 "ctmBytes" (when ac (length ac)) "xtmBytes" (when bc (length bc))
285 "firstDifferentByte" (when (and ac bc) (mismatch ac bc))
286 "byteExact" (if (and ac bc (equalp ac bc)) :true :false)) *rows*)
287 (check (format nil "equivalence:~A:both-productive-readers-succeeded" id)
288 (and a b (null (getf ctm :condition)) (null (getf xtm :condition))))
289 (dolist (pair (list (list "CTM" a ctm) (list "XTM" b xtm)))
290 (destructuring-bind (language map result) pair
291 (check (format nil "equivalence:~A:~A:canonical-EQ-parents-types-players-scopes-reifier-inverses" id language) (canonical-eq-p map))
292 (check (format nil "equivalence:~A:~A:source-preserved" id language) (getf result :source-preserved))
293 (expected-features language map doc case)))
294 (check (format nil "equivalence:~A:independent-maps-no-cross-map-objects-shared" id)
295 (and (not (eq a b)) (null (intersection (handles a) (handles b) :test #'eq))))
296 (check (format nil "equivalence:~A:native-properties-equal" id) props-equal)
297 (check (format nil "equivalence:~A:complete-unmodified-CXTM-bytes-equal" id) (equalp ac bc))
298 (check (format nil "equivalence:~A:qualified-XML-path-and-document-IRIs" id)
299 (and (= (length (getf ctm :parses)) (if resource 1 0))
300 (= (length (getf xtm :parses)) (if resource 2 1))
301 (every (lambda (p) (if (equal (first p) doc) (third p) (and (equal (first p) child) (fourth p))))
302 (append (getf ctm :parses) (getf xtm :parses)))))
304 (check "equivalence:content-import:actual-resolver-arguments-both-contexts"
305 (and (equal (getf ctm :calls) (getf xtm :calls))
306 (equal (getf ctm :calls) (list (list child :mergemap doc "http://psi.topicmaps.org/iso13250/xtm")))))
307 (check "equivalence:content-import:source-map-metadata-excluded-preserved"
308 (and (null (item-identifiers a)) (null (item-identifiers b)) (null (item-reifier a)) (null (item-reifier b))
309 (every (lambda (record) (and (equal '("urn:child-map") (first record)) (second record) (third record)))
310 (append (getf ctm :imports) (getf xtm :imports))))))
311 ;; Repeat both genuine deserializations with different diagnostic keys.
312 (let ((aa (getf (execute :ctm (getf case :ctm) doc "other:CTM-key" child resource) :map))
313 (bb (getf (execute :xtm (getf case :xtm) doc "other:XTM-key" child resource) :map)))
314 (check (format nil "equivalence:~A:source-keys-do-not-change-model-identities" id)
315 (and aa bb (equal (properties a) (properties aa)) (equal (properties b) (properties bb))
316 (equalp ac (topic-maps.cxtm:canonicalize-octets aa :base-locator doc))
317 (equalp bc (topic-maps.cxtm:canonicalize-octets bb :base-locator doc)))))))
318 (defun run-contrast (id ctm-source xtm-source doc-a doc-b base reason)
319 (let* ((ar (execute :ctm ctm-source doc-a "contrast:CTM")) (br (execute :xtm xtm-source doc-b "contrast:XTM"))
320 (a (getf ar :map)) (b (getf br :map)))
321 (check (concatenate 'string "equivalence:contrast:" id ":both-real-readers-succeeded")
322 (and a b (null (getf ar :condition)) (null (getf br :condition))))
323 (let ((ac (pure-cxtm (concatenate 'string "contrast:" id ":CTM") a base))
324 (bc (pure-cxtm (concatenate 'string "contrast:" id ":XTM") b base)))
325 (push (fields "id" id "kind" "intentional-semantic-difference" "reason" reason
326 "ctmSource" ctm-source "xtmSource" xtm-source "ctmDocumentIRI" doc-a "xtmDocumentIRI" doc-b "canonicalBaseIRI" base
327 "ctmModel" (model a) "xtmModel" (model b) "ctmBytes" (length ac) "xtmBytes" (length bc)
328 "ctmCXTM" (babel:octets-to-string ac :encoding :utf-8) "xtmCXTM" (babel:octets-to-string bc :encoding :utf-8)
329 "firstDifferentByte" (mismatch ac bc) "nativePropertiesEqual" :false "byteExact" :false) *rows*)
330 (check (concatenate 'string "equivalence:contrast:" id ":native-and-cxtm-differences-preserved")
331 (and (not (equal (properties a) (properties b))) (not (equalp ac bc)) (canonical-eq-p a) (canonical-eq-p b))))))
332 (defun contrast-tests ()
333 (let ((doc "https://equivalence.example.invalid/contrast/map"))
334 (run-contrast "different-document-IRIs" "a." (xml "<topic id='a'/>")
335 "https://equivalence.example.invalid/contrast/a.ctm" "https://equivalence.example.invalid/contrast/b.xtm" doc
336 "Different document-local IIs are real TMDM properties. A single shared CXTM base preserves the difference.")
337 (run-contrast "SI-versus-SL" "a <urn:identity>." (xml "<topic id='a'><subjectLocator href='urn:identity'/></topic>") doc doc doc
338 "Equal locator spelling in SI versus SL does not identify equal TMDM properties.")
339 (run-contrast "non-topic-item-Identifier" "a - \"N\"." (xml "<topic id='a'><name><itemIdentity href='urn:name-item'/><value>N</value></name></topic>") doc doc doc
340 "The XTM Name has an extra II that CTM cannot express here. It is not removed for comparison.")))
343 (list "association-role-scope-reifiers" "rel(role: a ~rr)@s ~ar"
344 (xml "<association reifier='#ar'><type><topicRef href='#rel'/></type><scope><topicRef href='#s'/></scope><role reifier='#rr'><type><topicRef href='#role'/></type><topicRef href='#a'/></role></association>")
345 "association" '("CTM 3.3.9-3.3.10,3.13" "XTM 4.3.2,4.18-4.19" "TMDM 5.3.4,5.7-5.8"))))
346 (destructuring-bind (id ctm-source xtm-source element clauses) row
347 (let* ((doc (format nil "https://equivalence.example.invalid/gap-~A/map" id))
348 (ctm (execute :ctm ctm-source doc (concatenate 'string "gap:CTM:" id)))
349 (xtm (execute :xtm xtm-source doc (concatenate 'string "gap:XTM:" id)))
350 (map (getf ctm :map)) (c (getf xtm :condition)))
351 (push (fields "id" id "kind" "reader-capability-gap-not-equivalence-PASS" "clauses" (coerce clauses 'vector)
352 "documentIRI" doc "ctmSource" ctm-source "xtmSource" xtm-source
353 "ctmCondition" (condition-data (getf ctm :condition)) "xtmCondition" (condition-data c)
354 "ctmModel" (when map (model map)) "ctmCXTM" (when map (babel:octets-to-string (pure-cxtm (concatenate 'string "gap:" id) map doc) :encoding :utf-8))
355 "xtmModel" (when (getf xtm :map) (model (getf xtm :map))) "successfulReaderPair" :false
356 "missingCapability" (concatenate 'string "XTM " element " deserialization")) *rows*)
357 (check (concatenate 'string "equivalence:gap:" id ":actual-unsupported-language-capability")
358 (and map (null (getf ctm :condition)) (canonical-eq-p map) (null (getf xtm :map))
359 (eq (type-of c) 'topic-maps.xtm:xtm-unsupported) (eq :unsupported-construct (topic-maps.xtm:error-code c))
360 (search element (topic-maps.xtm:error-detail c))))))))
361 (defun write-report (path &optional failure)
362 (with-open-file (out path :direction :output :if-exists :supersede :external-format :utf-8)
363 (topic-maps.test-foundation::emit-json-value
364 (fields "kind" "direct-CTM-XTM-semantic-equivalence-not-corpus-registration"
365 "runtime" (lisp-implementation-version) "checks" (coerce *checks* 'vector)
366 "pairs" (coerce (reverse *rows*) 'vector) "condition" (condition-data failure)
367 "productionModelChanges" :false "originalCorpusRegistrationsAdded" 0) out)
370 ;; Reuse the existing adapter check function while preserving its 217-check
371 ;; snapshot and all original-corpus registries. Only this dynamic list is new.
372 (let ((topic-maps.xtm-mergemap.tests::*checks* nil))
373 (setf *rows* nil *checks* nil)
374 (let ((path (let ((prefix (uiop:getenv "TOPIC_MAPS_XTM_MERGEMAP_REPORT")))
375 (when prefix (concatenate 'string prefix ".equivalence.json")))))
377 (progn (mapc #'run-pair (pair-cases))
378 (run-pair (list :id "explicit-association" :ctm "rel(role: a)"
379 :xtm (xml "<association><type><topicRef href='#rel'/></type><role><type><topicRef href='#role'/></type><topicRef href='#a'/></role></association>")
380 :keys '("rel" "role" "a") :counts '(3 0 0 0 1 1 0)
381 :why "CTM relation and explicit XTM association have the same type and one role with equal type/player, no scope or metadata."
382 :clauses '("CTM 3.13" "XTM 4.18-4.20" "TMDM 5.7-5.8,6.6-6.7")))
383 (run-pair (list :id "occurrence-scope-reifier"
384 :ctm "a t:\"42\"^^<http://www.w3.org/2001/XMLSchema#integer>@s ~r."
385 :xtm (xml "<topic id='a'><occurrence reifier='#r'><type><topicRef href='#t'/></type><scope><topicRef href='#s'/></scope><resourceData datatype='http://www.w3.org/2001/XMLSchema#integer'>42</resourceData></occurrence></topic>")
386 :keys '("a" "t" "s" "r") :counts '(4 0 0 1 0 0 1)
387 :why "Both readers create the same typed scoped integer Occurrence and the same II-identified Reifier with inverse relation."
388 :clauses '("CTM 3.10" "XTM 4.3.2,4.15" "TMDM 5.3.4-5.3.5,5.6,6.5")))
389 (contrast-tests) (gap-tests)
390 (check "equivalence:unique-check-labels"
391 (= (length topic-maps.xtm-mergemap.tests::*checks*)
392 (length (remove-duplicates topic-maps.xtm-mergemap.tests::*checks* :test #'equal)))))
393 (error (c) (setf *checks* (reverse topic-maps.xtm-mergemap.tests::*checks*))
394 (when path (write-report path c)) (error c)))
395 (setf *checks* (reverse topic-maps.xtm-mergemap.tests::*checks*))
396 (when path (write-report path))
397 (format t "CTM-XTM-EQUIVALENCE-PASS: ~D checks; ~D actual reader pairs; 3 contrasts; 1 explicit capability gap.~%"
398 (length *checks*) (+ 2 (length (pair-cases))))