]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/ctm-xtm-anytype-equivalence.lisp
perf(tmdm): index topic normalization merge candidates
[topic-maps.git] / tests / ctm-xtm-anytype-equivalence.lisp
1 (in-package #:topic-maps.equivalence.tests)
2 ;; Independent values fixed before either productive Reader runs. XML is never
3 ;; serialized/repaired here; CTM explicitly carries the canonical lexical value.
4 (defparameter *at-datatype* "http://www.w3.org/2001/XMLSchema#anyType")
5 (defvar *at-language* nil)
6 (defvar *at-key* nil)
7 (defun at-literal (value) (format nil "~S^^<~A>" value *at-datatype*))
8 (defun at-resource (content &optional (declarations ""))
9 (format nil "<resourceData datatype='~A'~A>~A</resourceData>" *at-datatype* declarations content))
10 (defun at-pair-cases ()
11 (let* ((occ "<p:x xmlns:p=\"urn:payload\" a=\"A\">Grüße 水 &amp; B</p:x>")
12 (var "<p:outer xmlns:p=\"urn:variant\"><p:leaf a=\"1\" b=\"2\">V&amp;🦊</p:leaf></p:outer>")
13 (norm "<x a=\"A\" b=\"2\"><y></y>a&amp;b</x>")
14 (norm-var "<p:x xmlns:p=\"urn:variant\" a=\"A\" b=\"2\">V&amp;🦊</p:x>")
15 (clauses '("CTM 3.4.1 [28]-[29],3.10-3.12" "XTM 4.16.2-4.16.3" "TMDM 4.4,5.5-5.6,6.4-6.5")))
16 (flet ((spec (id kind value ctm body keys counts why &key normalized)
17 (append (pair id ctm body keys counts why clauses)
18 (list :anytype-kind kind :canonical-value value :normalization normalized))))
19 (list
20 (spec "anytype-occurrence" :occurrence occ
21 (format nil "a <urn:anytype:subject>. a =<urn:anytype:resource>. a ^<urn:anytype:item>. a t:~A@s ~~r." (at-literal occ))
22 (format nil "<topic id='a'><subjectIdentifier href='urn:anytype:subject'/><subjectLocator href='urn:anytype:resource'/><itemIdentity href='urn:anytype:item'/><occurrence reifier='#r'><type><topicRef href='#t'/></type><scope><topicRef href='#s'/></scope>~A</occurrence></topic>"
23 (at-resource "<p:x a='&#65;'>Grüße 水 &amp; B</p:x>" " xmlns:p='urn:payload' xmlns:u='urn:unused'"))
24 (list (list "a" :si '("urn:anytype:subject") :sl '("urn:anytype:resource") :ii '("urn:anytype:item")) "t" "s" "r")
25 '(4 0 0 1 0 0 1) "Same canonical XML string/datatype, Occurrence Parent/Type/Scope/Reifier and all three Topic identifier categories.")
26 (spec "anytype-variant" :variant var
27 (format nil "a - nt:\"N\"@base ~~nr (~A@extra,extra ~~vr)." (at-literal var))
28 (format nil "<topic id='a'><name reifier='#nr'><type><topicRef href='#nt'/></type><scope><topicRef href='#base'/></scope><value>N</value><variant reifier='#vr'><scope><topicRef href='#extra'/><topicRef href='#extra'/></scope>~A</variant></name></topic>"
29 (at-resource "<p:outer><p:leaf b='2' a='1'>V&amp;🦊</p:leaf></p:outer>" " xmlns:p='urn:variant' xmlns:u='urn:unused'"))
30 '("a" "nt" "base" "extra" "nr" "vr") '(6 1 1 0 0 0 2)
31 "Same explicit Name Type, Name Scope, proper-superset Variant Scope, inherited XML prefix, Name/Variant Reifiers and inverses.")
32 (spec "anytype-occurrence-normalization" :occurrence norm
33 (format nil "a t:~A@s ~~r; t:~A@s ~~r." (at-literal norm) (at-literal norm))
34 (format nil "<topic id='a'><occurrence reifier='#r'><type><topicRef href='#t'/></type><scope><topicRef href='#s'/></scope>~A</occurrence><occurrence reifier='#r'><type><topicRef href='#t'/></type><scope><topicRef href='#s'/></scope>~A</occurrence></topic>"
35 (at-resource "<x xmlns='' b='2' a='&#65;'><y/><![CDATA[a&b]]></x>")
36 (at-resource "<x xmlns='' a='A' b='2'><y></y>a&amp;b</x>"))
37 '("a" "t" "s" "r") '(4 0 0 1 0 0 1)
38 "Two source occurrences in each notation have the same Parent/Type/Scope/value/datatype and normalize to one canonical Occurrence; XML attribute order, references, CDATA and empty-tag spelling differ." :normalized t)
39 (spec "anytype-variant-normalization" :variant norm-var
40 (format nil "a - nt:\"N\"@base ~~nr (~A@extra ~~vr) (~A@extra ~~vr)." (at-literal norm-var) (at-literal norm-var))
41 (format nil "<topic id='a'><name reifier='#nr'><type><topicRef href='#nt'/></type><scope><topicRef href='#base'/></scope><value>N</value><variant reifier='#vr'><scope><topicRef href='#extra'/></scope>~A</variant><variant reifier='#vr'><scope><topicRef href='#extra'/></scope>~A</variant></name></topic>"
42 (at-resource "<p:x b='2' a='&#65;'><![CDATA[V&🦊]]></p:x>" " xmlns:p='urn:variant'")
43 (at-resource "<p:x a='A' b='2'>V&amp;🦊</p:x>" " xmlns:p='urn:variant'"))
44 '("a" "nt" "base" "extra" "nr" "vr") '(6 1 1 0 0 0 2)
45 "Two source Variants with equal canonical XML values retain the same Parent Name, inherited/explicit Scope and Reifier and normalize through the existing TMDM constructor." :normalized t)))))
46 (defun at-topic (map doc id)
47 (find-if (lambda (topic) (member (concatenate 'string doc "#" id) (item-identifiers topic) :test #'equal))
48 (topic-map-topics map)))
49 (defun at-features (language map doc case)
50 (let* ((id (getf case :id)) (owner (at-topic map doc "a"))
51 (want (getf case :canonical-value)) (kind (getf case :anytype-kind))
52 (name (first (topic-names owner)))
53 (item (if (eq kind :occurrence) (first (topic-occurrences owner)) (first (topic-name-variants name))))
54 (r (at-topic map doc (if (eq kind :occurrence) "r" "vr"))))
55 (check (format nil "equivalence:~A:~A:independent-anyType-value-and-datatype" id language)
56 (and item (equal want (if (eq kind :occurrence) (occurrence-value item) (variant-value item)))
57 (equal *at-datatype* (if (eq kind :occurrence) (occurrence-datatype item) (variant-datatype item)))))
58 (check (format nil "equivalence:~A:~A:expected-parent-type-scope-and-reifier-inverses" id language)
59 (and (eq owner (canonical-topic owner)) (null (item-identifiers item))
60 (eq r (item-reifier item)) (if r (eq item (topic-reified r)) (null (item-reifier item)))
61 (if (eq kind :occurrence)
62 (and (eq owner (occurrence-parent item)) (eq (at-topic map doc "t") (occurrence-type item))
63 (equal (refs (occurrence-scope item)) (refs (when r (list (at-topic map doc "s"))))))
64 (and (eq name (variant-parent item)) (eq owner (topic-name-parent name))
65 (equal "N" (topic-name-value name)) (eq (at-topic map doc "nt") (topic-name-type name))
66 (equal (refs (topic-name-scope name)) (refs (list (at-topic map doc "base"))))
67 (equal (refs (variant-scope item)) (refs (list (at-topic map doc "base") (at-topic map doc "extra"))))
68 (eq (at-topic map doc "nr") (item-reifier name)) (eq name (topic-reified (item-reifier name)))
69 (null (item-identifiers name))))))
70 (check (format nil "equivalence:~A:~A:canonical-EQ-entire-native-map" id language) (canonical-eq-p map))))
71 (defun at-add-report-fields (&rest args)
72 (setf (topic-maps.test-foundation::json-object-members (first *rows*))
73 (append (topic-maps.test-foundation::json-object-members (first *rows*))
74 (topic-maps.test-foundation::json-object-members (apply #'fields args)))))
75 (defun at-observe (case thunk &optional right-value)
76 ;; Observe the actual productive calls/constructors, always returning their
77 ;; unchanged native results. This supplements the existing execute/run-pair
78 ;; infrastructure; it creates no substitute model or registry.
79 (let* ((ctm (symbol-function 'topic-maps.ctm:deserialize-string))
80 (xtm (symbol-function 'topic-maps.xtm:deserialize-octets))
81 (kind (getf case :anytype-kind))
82 (constructor (if (eq kind :occurrence) 'add-occurrence 'add-variant))
83 (native (symbol-function constructor)) (calls nil) (items nil))
84 (labels ((entry (language saved input args)
85 (let* ((*at-language* language)
86 (*at-key* (getf args (if (eq language :ctm) :source-name :source-key)))
87 (doc (getf args :document-iri)) (copy (copy-seq input))
88 (tag (format nil "~A/~D" language (length calls)))
89 (expect (copy-list case)))
90 (push (fields "entryPoint" (if (eq language :ctm) "topic-maps.ctm:deserialize-string" "topic-maps.xtm:deserialize-octets")
91 "documentIRI" doc "sourceKey" *at-key* "inputHex" (format nil "~{~2,'0X~}" (coerce (if (stringp input) (octets input) input) 'list))) calls)
92 (let ((map (apply saved input args)))
93 (when (and right-value (eq language :xtm)) (setf (getf expect :canonical-value) right-value))
94 (check (format nil "equivalence:~A:~A:actual-entry-source-unchanged" (getf case :id) tag) (equalp copy input))
95 (expected-features tag map doc expect)
96 (at-features tag map doc expect) map))))
97 (unwind-protect
98 (progn
99 (setf (symbol-function 'topic-maps.ctm:deserialize-string) (lambda (input &rest args) (entry :ctm ctm input args))
100 (symbol-function 'topic-maps.xtm:deserialize-octets) (lambda (input &rest args) (entry :xtm xtm input args))
101 (symbol-function constructor)
102 (lambda (&rest args)
103 (let* ((map (topic-maps.tmdm::item-topic-map (first args)))
104 (before (nth (if (eq kind :occurrence) 3 2) (counts map)))
105 (result (apply native args))
106 (after (nth (if (eq kind :occurrence) 3 2) (counts map))))
107 (push (list *at-language* *at-key* before after result map) items) result)))
108 (funcall thunk))
109 (setf (symbol-function 'topic-maps.ctm:deserialize-string) ctm
110 (symbol-function 'topic-maps.xtm:deserialize-octets) xtm
111 (symbol-function constructor) native)))
112 (let ((proof nil) (times (if right-value 2 4)) (source-count (if (getf case :normalization) 2 1)))
113 (check (format nil "equivalence:~A:both-actual-Reader-entries-and-repetitions-observed" (getf case :id)) (= times (length calls)))
114 (dolist (call (reverse calls))
115 (let* ((members (topic-maps.test-foundation::json-object-members call))
116 (key (cdr (assoc "sourceKey" members :test #'equal)))
117 (entry-point (cdr (assoc "entryPoint" members :test #'equal)))
118 (language (if (search ":deserialize-string" entry-point) :ctm :xtm))
119 (group (reverse (remove-if-not (lambda (x) (and (eq language (first x)) (equal key (second x)))) items)))
120 (canonical (when group (topic-maps.tmdm::canonical-item (fifth (first group))))))
121 (check (format nil "equivalence:~A:~A/~A:actual-source-items-normalize-to-one-live-handle" (getf case :id) language key)
122 (and (= source-count (length group))
123 (equal (mapcar #'third group) (if (= source-count 2) '(0 1) '(0)))
124 (every (lambda (x) (and (= 1 (fourth x)) (eq canonical (topic-maps.tmdm::canonical-item (fifth x)))
125 (eq (sixth x) (topic-maps.tmdm::item-topic-map canonical)))) group)))
126 (push (fields "entryPoint" entry-point "sourceKey" key "sourceItemCalls" (length group)
127 "nativeItemCountsBefore" (coerce (mapcar #'third group) 'vector) "nativeItemCountsAfter" (coerce (mapcar #'fourth group) 'vector)
128 "allReturnedHandlesCanonicalAndLive" :true) proof)))
129 (at-add-report-fields "slice" "22T" "independentExpectedCanonicalValue" (getf case :canonical-value)
130 "expectedXTMCanonicalValue" (or right-value (getf case :canonical-value)) "expectedDatatype" *at-datatype*
131 "expectedNativeCounts" (coerce (getf case :counts) 'vector) "normalizationRequired" (if (getf case :normalization) :true :false)
132 "actualReaderEntries" (coerce (reverse calls) 'vector) "nativeConstructorEvidence" (coerce (nreverse proof) 'vector)))))
133 (defun at-run-pairs ()
134 (dolist (case (at-pair-cases)) (at-observe case (lambda () (run-pair case)))))
135 (defun at-contrast-tests ()
136 (let* ((left "<p:x xmlns:p=\"urn:left\"></p:x>") (right "<p:x xmlns:p=\"urn:right\"></p:x>")
137 (case (list :id "anytype-namespace-meaning" :anytype-kind :occurrence :canonical-value left
138 :keys '("a" "t") :counts '(2 0 0 1 0 0 0)))
139 (doc "https://equivalence.example.invalid/anytype-namespace-meaning/map"))
140 (at-observe case (lambda ()
141 (run-contrast (getf case :id) (format nil "a t:~A." (at-literal left))
142 (xml (format nil "<topic id='a'><occurrence><type><topicRef href='#t'/></type>~A</occurrence></topic>"
143 (at-resource "<p:x/>" " xmlns:p='urn:right'"))) doc doc doc
144 "Same prefix/local name but different namespace URI produces different canonical XML values. All other native properties are held equal; neither is rewritten.")) right)))