(in-package #:topic-maps.xtm.tests)
;; Slice 22J uses the existing native TMDM model and the existing case registry.
(defparameter *association-originals*
'("association" "association-binary" "association-binary-duplicate"
"association-ternary" "association-scope" "association-duplicate-role"
"association-instanceof-duplicate" "association-instanceof-scope" "merge-itemid-with-association"))
(defun as-roles (map) (loop for a in (topic-map-associations map) append (association-roles a)))
(defun as-current-p (map)
(and (occurrence-current-p map)
(every (lambda (a)
(and (eq a (canonical-association a)) (eq map (association-parent a))
(eq (association-type a) (canonical-topic (association-type a)))
(= (length (association-scope a)) (length (remove-duplicates (association-scope a) :test #'eq)))
(every (lambda (s) (eq s (canonical-topic s))) (association-scope a))
(association-roles a)
(every (lambda (r) (and (eq r (canonical-association-role r))
(eq a (association-role-parent r))
(eq (association-role-type r) (canonical-topic (association-role-type r)))
(eq (association-role-player r) (canonical-topic (association-role-player r)))
(= 1 (count r (topic-roles-played (association-role-player r)) :test #'eq)))) (association-roles a))))
(topic-map-associations map))
(every (lambda (topic) (every (lambda (r)
(and (member r (as-roles map) :test #'eq) (eq topic (association-role-player r)))) (topic-roles-played topic))) (topic-map-topics map))))
(defun as-model (map)
(let ((topics (topic-map-topics map)) (associations (topic-map-associations map)))
(flet ((ref (topic) (1+ (position topic topics :test #'eq))))
(mm-fields "existingModel" (occurrence-model map) "canonicalAssociationRoleReferencesEQ" (if (as-current-p map) :true :false)
"topicCount" (length topics) "associationCount" (length associations) "roleCount" (length (as-roles map))
"associations" (coerce (loop for a in associations collect
(mm-fields "parentIsActualMapEQ" (if (eq map (association-parent a)) :true :false)
"type" (ref (association-type a)) "scope" (coerce (mapcar #'ref (association-scope a)) 'vector)
"itemIdentifiers" (coerce (item-identifiers a) 'vector)
"roles" (coerce (loop for r in (association-roles a) collect
(mm-fields "parentAssociation" (1+ (position (association-role-parent r) associations :test #'eq))
"type" (ref (association-role-type r)) "player" (ref (association-role-player r))
"inverseRolesPlayedEQ" (if (member r (topic-roles-played (association-role-player r)) :test #'eq) :true :false)
"itemIdentifiers" (coerce (item-identifiers r) 'vector))) 'vector))) 'vector)))))
(defun as-observe (raw key)
;; Passive observation of the actual pre-normalization native handles. This
;; observes duplicate roles before MAKE-ASSOCIATION normalizes them; no model
;; item is constructed or changed by the observer. Always restore operations.
(let ((normalize (symbol-function 'topic-maps.tmdm::normalize-associations))
(parser (symbol-function 'topic-maps.xml:parse-octets))
(observations (make-hash-table :test #'eq)) (calls 0) (same nil) (map nil))
(unwind-protect
(progn
(setf (symbol-function 'topic-maps.xml:parse-octets)
(lambda (input &rest args) (incf calls) (setf same (eq raw input)) (apply parser input args)))
(setf (symbol-function 'topic-maps.tmdm::normalize-associations)
(lambda (model)
(unless (gethash model observations) (setf (gethash model observations) (list nil nil)))
(dolist (i (append (topic-map-associations model) (as-roles model)))
(pushnew i (first (gethash model observations)) :test #'eq))
(let ((before (list (length (topic-map-associations model)) (length (as-roles model)))))
(prog1 (funcall normalize model)
(push (append before (list (length (topic-map-associations model)) (length (as-roles model)))) (second (gethash model observations)))))))
(setf map (deserialize-octets raw :document-iri *document* :source-key key)))
(setf (symbol-function 'topic-maps.xml:parse-octets) parser
(symbol-function 'topic-maps.tmdm::normalize-associations) normalize))
;; Core-subject preflights normalize private shadow maps as well. Only
;; handles belonging to the actual returned reader Map are reader evidence.
(values map (first (gethash map observations)) (nreverse (second (gethash map observations))) calls same)))
(defun as-held-current-p (map held)
(every (lambda (i) (member (topic-maps.tmdm::canonical-item i)
(append (topic-map-associations map) (as-roles map)) :test #'eq)) held))
(defun as-pure (label map)
(let ((before (mm-state map)) (cx (mm-cxtm map)) (held (topic-maps.tmdm::map-information-items map)))
(check (format nil "association:~A:pure-repeated-CXTM-canonical-handles" label)
(and (equalp cx (mm-cxtm map)) (equal before (mm-state map)) (as-current-p map)
(every (lambda (i) (eq i (topic-maps.tmdm::canonical-item i))) held))) cx))
(defun as-native-original (name map)
(let* ((a (first (topic-map-associations map)))
(single (equal name "association"))
(ternary (member name '("association-ternary" "association-duplicate-role") :test #'equal))
(corep (search "instanceof" name)) (merged (equal name "merge-itemid-with-association"))
(theme (cond ((equal name "association-scope") "scopingtopic") ((equal name "association-instanceof-scope") "expert1")))
(players (cond (single '("topic")) (ternary '("topic1" "topic2" "topic3")) (merged '("topic" "topic3")) (t '("topic1" "topic2"))))
(types (cond (single '("roletype")) (ternary '("roletype1" "roletype2" "roletype3")) (t '("roletype1" "roletype2")))))
(check (format nil "association-original:~A:exact-native-types-players-scope-parent" name)
(and (= 1 (length (topic-map-associations map)))
(= (+ 1 (* 2 (length players)) (if theme 1 0)) (length (topic-map-topics map)))
(= (length players) (length (association-roles a)))
(eq (by-id map "assoctype") (association-type a))
(scope-eq-p (association-scope a) (when theme (list (by-id map theme))))
(loop for p in players for rt in types always
(some (lambda (r) (and (eq (by-id map p) (association-role-player r))
(eq (by-id map rt) (association-role-type r)))) (association-roles a)))
(null (all-occurrences map)) (null (name-reifier-names map)) (null (item-identifiers map))
(every (lambda (i) (and (null (item-identifiers i)) (null (item-reifier i)))) (cons a (association-roles a)))
(as-current-p map)))
(when corep
(check (format nil "association-original:~A:implicit-explicit-core-SI-identity" name)
(and (eq (core map "type-instance") (by-id map "assoctype"))
(eq (core map "instance") (by-id map "roletype1")) (eq (core map "type") (by-id map "roletype2")))))
(when merged
(check "association-original:merge-itemid:all-identifiers-retained-canonical-player"
(and (eq (by-id map "topic") (by-id map "topic2"))
(same-identifiers-p (item-identifiers (by-id map "topic"))
(list (concatenate 'string *document* "#topic") (concatenate 'string *document* "#topic2") "http://example.org/#topic")))))))
(defun association-original-contracts ()
(setf *association-outputs* nil)
(let ((previous (copy-list *cases*)))
(dolist (name *association-originals*)
(let* ((id (format nil "xtm2/in/~A.xtm" name))
(*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
(raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
(baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" name))) (expected (file-bytes baseline))
(case (topic-maps.test-foundation:make-test-case :id id :category :valid :source-pathname (corpus-path id)
:document-iri *document* :expected-outcome :cxtm-equal :expected-baseline baseline
:clauses '("XTM 4.13-4.14,4.18-4.20" "TMDM 5.7-5.8,6.2,6.6-6.7" "CXTM 3.9-3.10,4.10-4.11")))
(map nil) (held nil) (stages nil) (calls 0) (same nil) (actual nil) (c nil))
(check (format nil "association-original:~A:unique-new-case" name)
(null (find id *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))
(setf *cases* (append *cases* (list case)))
(handler-case
(progn
(multiple-value-setq (map held stages calls same) (as-observe raw id))
(as-native-original name map)
(check (format nil "association-original:~A:raw-octets-qualified-parser-document-context" name)
(and (= 1 calls) same (equalp raw copy)))
(check (format nil "association-original:~A:all-pre-normalization-handles-resolve" name) (as-held-current-p map held))
(when (member name '("association-binary-duplicate" "association-duplicate-role" "association-instanceof-duplicate" "merge-itemid-with-association") :test #'equal)
(check (format nil "association-original:~A:actual-replacement-forwarding" name)
(some (lambda (i) (not (eq i (topic-maps.tmdm::canonical-item i)))) held)))
(setf actual (as-pure name map))
(let ((other (deserialize-octets raw :document-iri *document* :source-key "independent:association-original")))
(check (format nil "association-original:~A:source-key-independent" name)
(and (equalp (as-model map) (as-model other)) (equalp actual (mm-cxtm other))))))
(error (condition) (setf c condition)))
(record-original-result case actual expected c)
(push (mm-fields "id" id "documentIRI" *document* "sourceKey" id "sourceBytes" (length raw)
"sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8)
"qualifiedParserCalls" calls "originalOctetsEQ" (if same :true :false)
"model" (when map (as-model map)) "normalizationStages" (coerce (mapcar (lambda (s) (coerce s 'vector)) stages) 'vector)
"observedNativeHandles" (length held) "replacedNativeHandles" (count-if (lambda (i) (not (eq i (topic-maps.tmdm::canonical-item i)))) held)
"oldHandlesCanonicalAndLive" (if (and map (as-held-current-p map held)) :true :false)
"actualBytes" (when actual (length actual)) "expectedBytes" (length expected)
"actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8)) "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8)
"firstDifferentByte" (when actual (mismatch actual expected)) "condition" (mm-condition c)
"status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *association-outputs*)
(when c (error c))
(check (format nil "association-original:~A:complete-original-CXTM-byte-exact" name) (equalp actual expected))))
(as-negative-original)
(check "association-original:requested-set-once-all-prior-PASS-preserved"
(and (every (lambda (case) (and (member case *cases* :test #'eq) (eq :pass (topic-maps.test-foundation:test-case-status case)))) previous)
(same-identifiers-p (mapcar #'topic-maps.test-foundation:test-case-id (set-difference *cases* previous :test #'eq))
(append (mapcar (lambda (n) (format nil "xtm2/in/~A.xtm" n)) *association-originals*) '("xtm2/invalid/topicref-no-fragment-id.xtm")))
(every (lambda (case) (= 1 (count (topic-maps.test-foundation:test-case-id case) *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal))) *cases*))))
(setf *association-outputs* (nreverse *association-outputs*)))
(defun as-negative-original ()
(let* ((id "xtm2/invalid/topicref-no-fragment-id.xtm")
(iri (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id))
(raw (file-bytes (corpus-path id))) (copy (copy-seq raw))
(spec '("topicref-no-fragment-id" xtm-rejection :invalid-topic-ref "topicRef requires an XPointer shorthand fragment." 7 38 "XTM 4.20: href must contain an XPointer shorthand fragment"))
(c (condition-from (lambda () (deserialize-octets raw :document-iri iri :source-key id))))
(case (topic-maps.test-foundation:make-test-case :id id :category :invalid :source-pathname (corpus-path id)
:document-iri iri :expected-outcome :ctm-rejection :clauses (list (seventh spec)))))
(record-negative-original-result case spec c iri id)
(setf *cases* (append *cases* (list case)))
(push (mm-fields "id" id "documentIRI" iri "sourceKey" id "sourceBytes" (length raw)
"sourceHex" (format nil "~{~2,'0X~}" (coerce raw 'list)) "sourceText" (babel:octets-to-string raw :encoding :utf-8)
"condition" (mm-condition c) "intendedCauseReached" (if (intended-negative-p spec c iri id) :true :false)
"status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *association-outputs*)
(check "association-negative-original:intended-class-code-detail-position-context"
(and (intended-negative-p spec c iri id) (equalp copy raw)))
(check "association-negative-original:no-preliminary-Unsupported"
(and (not (typep c 'xtm-unsupported)) (eq :pass (topic-maps.test-foundation:test-case-status case))))))
(defun as-role-xml (type player)
(format nil "" type player))
(defun as-xml (type roles &optional themes)
(format nil "~A~A" type
(if themes (format nil "~{~}" themes) "")
(apply #'concatenate 'string (mapcar (lambda (pair) (apply #'as-role-xml pair)) roles))))
(defun as-probe (label text verify &optional replacement)
(multiple-value-bind (map held stages calls same) (as-observe (bytes (document text)) label)
(check (format nil "association:~A:actual-XML-reader-native-EQ" label) (and (= 1 calls) same (as-current-p map) (as-held-current-p map held)))
(funcall verify map)
(when replacement
(check (format nil "association:~A:actual-native-handle-replacement" label)
(some (lambda (i) (not (eq i (topic-maps.tmdm::canonical-item i)))) held)))
(let ((cx (as-pure label map)))
(push (mm-fields "label" label "sourceText" (document text) "model" (as-model map)
"CXTM" (babel:octets-to-string cx :encoding :utf-8) "bytes" (length cx)
"normalizationStages" (coerce (mapcar (lambda (s) (coerce s 'vector)) stages) 'vector)
"observedHandles" (length held) "replacedHandles" (count-if (lambda (i) (not (eq i (topic-maps.tmdm::canonical-item i)))) held)
"oldHandlesResolveToLiveCanonicalItems" :true) *association-controls*)) map))
(defun as-counts (label associations roles)
(lambda (map) (check (format nil "association:~A:expected-distinct-associations-role-sets" label)
(and (= associations (length (topic-map-associations map))) (= roles (length (as-roles map)))))))
(defun as-error (label body class code)
(let* ((raw (document body)) (c (condition-from (lambda () (deserialize-octets (bytes raw) :document-iri *document* :source-key label)))))
(push (mm-fields "label" label "sourceText" raw "condition" (mm-condition c)) *association-controls*)
(check (format nil "association-error:~A:actual-class-code-and-context" label)
(and (eq (type-of c) class) (eq code (error-code c))
(equal *document* (error-document-iri c)) (equal label (error-source-key c))
(if (typep c 'xtm-xml-error) (topic-maps.xtm:error-cause c) (and (integerp (error-line c)) (integerp (error-column c)))))) c))
(defun association-tests ()
(setf *association-controls* nil)
(let ((base (as-xml "#t" '(("#r" "#p") ("#s" "#q")))))
(as-probe "role-order-set-equality" (concatenate 'string base (as-xml "#t" '(("#s" "#q") ("#r" "#p")))) (as-counts "role-order-set-equality" 1 2) t)
(dolist (row (list (list "different-association-types" (as-xml "#other" '(("#r" "#p") ("#s" "#q"))))
(list "different-players" (as-xml "#t" '(("#r" "#other") ("#s" "#q"))))
(list "different-role-types" (as-xml "#t" '(("#other" "#p") ("#s" "#q"))))
(list "different-scopes" (as-xml "#t" '(("#r" "#p") ("#s" "#q")) '("#theme")))))
(as-probe (first row) (concatenate 'string base (second row)) (as-counts (first row) 2 4)))
(as-probe "different-arity" (concatenate 'string base (as-xml "#t" '(("#r" "#p")))) (as-counts "different-arity" 2 3))
(as-probe "duplicate-themes-and-roles" (as-xml "#t" '(("#r" "#p") ("#r" "#p")) '("#theme" "#theme"))
(lambda (map) (funcall (as-counts "duplicate-themes-and-roles" 1 1) map)
(check "association:duplicate-themes:one-canonical-theme" (equal (list (by-id map "theme")) (association-scope (first (topic-map-associations map)))))) t))
;; Identity cascades occur AFTER both complete associations have been read.
(dolist (kind '("subjectIdentifier" "subjectLocator" "itemIdentity"))
(let* ((label (concatenate 'string "late-" kind))
(source (concatenate 'string (as-xml "#t" '(("#r" "#p")) '("#s")) (as-xml "#u" '(("#rr" "#q")) '("#v"))
(format nil "<~A href='urn:type'/><~A href='urn:type'/><~A href='urn:role'/><~A href='urn:role'/><~A href='urn:player'/><~A href='urn:player'/><~A href='urn:scope'/><~A href='urn:scope'/>" kind kind kind kind kind kind kind kind))))
(as-probe label source
(lambda (map) (funcall (as-counts label 1 1) map)
(check (format nil "association:~A:late-types-players-themes-all-EQ" label)
(and (eq (by-id map "p") (by-id map "q")) (eq (by-id map "t") (by-id map "u"))
(eq (by-id map "r") (by-id map "rr")) (eq (by-id map "s") (by-id map "v"))))) t)))
(as-probe "late-player-collapse-within-one-parent"
(concatenate 'string (as-xml "#t" '(("#r" "#p") ("#r" "#q")))
"")
(as-counts "late-player-collapse-within-one-parent" 1 1) t)
(as-probe "SI-reference-hit-no-new-II-absolute-relative-and-forward"
(concatenate 'string ""
(as-xml "#t" '(("#r" "https://external.example/map#p") ("#r" "../other.xtm#late")))
"")
(lambda (map)
(check "association:SI-lookup:no-new-II-on-existing-topic-and-forward-II-merge"
(and (equal (list (concatenate 'string *document* "#known")) (item-identifiers (by-id map "known")))
(some (lambda (r) (eq (by-id map "known") (association-role-player r))) (as-roles map))
(some (lambda (r) (eq (by-id map "later") (association-role-player r))) (as-roles map))
(member (mm-child-iri "../other.xtm#late") (item-identifiers (by-id map "later")) :test #'equal)))))
(as-probe "instanceOf-explicit-late-player-merge"
(concatenate 'string
""
(as-xml "#at" '(("#ir" "#q") ("#tr" "#t")))
"")
(lambda (map)
(funcall (as-counts "instanceOf-explicit-late-player-merge" 1 2) map)
(check "association:instanceOf-explicit:core-identities-preserved-after-late-player-merge"
(and (eq (by-id map "p") (by-id map "q"))
(eq (core map "type-instance") (association-type (first (topic-map-associations map))))))) t)
;; Contexts/cardinalities are validated before native model construction.
(dolist (row '(("missing-association-type" "" xtm-rejection :missing-type)
("missing-roles" "" xtm-rejection :missing-roles)
("missing-role-type" "" xtm-rejection :missing-type)
("missing-role-player" "" xtm-rejection :missing-role-player)
("multiple-players" "" xtm-rejection :role-player-cardinality)
("role-before-type" "" xtm-rejection :missing-type)
("scope-after-role" "" xtm-rejection :element-order)
("role-player-before-type" "" xtm-rejection :missing-type)
("duplicate-type" "" xtm-rejection :duplicate-type)
("duplicate-scope" "" xtm-rejection :duplicate-scope)
("type-cardinality" "" xtm-rejection :type-cardinality)
("empty-scope" "" xtm-rejection :empty-scope)
("association-in-topic" "" xtm-rejection :unexpected-element)
("role-in-map" "" xtm-rejection :unexpected-element)
("bad-namespace" "" xtm-rejection :invalid-namespace)
("text-in-role" "bad" xtm-rejection :unexpected-text)
("unknown-child" "" xtm-rejection :unexpected-element)
("unknown-attribute" "" xtm-rejection :unexpected-attribute)
("malformed-XML" "" xtm-xml-error :xml-not-well-formed)
("association-reifier" "" xtm-unsupported :unsupported-construct)
("role-reifier" "" xtm-unsupported :unsupported-construct)
("association-II" "" xtm-unsupported :unsupported-construct)
("role-II" "" xtm-unsupported :unsupported-construct)))
(apply #'as-error row))
(dolist (value '("no-fragment" "#" "#1bad" "#a:b" "#xpointer(id)"))
(as-error (concatenate 'string "invalid-fragment:" value) (as-xml value '(("#r" "#p"))) 'xtm-rejection :invalid-topic-ref))
(dolist (context '("role-type" "player" "scope"))
(as-error (concatenate 'string "bad-reference:" context)
(as-xml "#t" (if (equal context "role-type") '(("#1bad" "#p")) (if (equal context "player") '(("#r" "#1bad")) '(("#r" "#p"))))
(when (equal context "scope") '("#1bad"))) 'xtm-rejection :invalid-topic-ref))
(as-native-conflict)
(as-import-test)
(setf *association-controls* (nreverse *association-controls*)))
(defun as-native-conflict ()
;; Real native core identity conflict, after an explicit association exists.
(let* ((source (document (concatenate 'string (as-xml "#t" '(("#r" "#p")))
"")))
(c (condition-from (lambda () (deserialize-octets (bytes source) :document-iri *document* :source-key "association-native-core-conflict")))))
(check "association-error:native-core-conflict-original-TMDM-class"
(and (typep c 'core-subject-error) (eq :core-subject-conflict (core-subject-error-code c))))
(push (mm-fields "label" "native-core-conflict" "sourceText" source "condition" (mm-condition c)
"nativeCode" (when (typep c 'core-subject-error) (string-downcase (string (core-subject-error-code c))))
"nativeConditionHasNoSourcePosition" :true) *association-controls*)))
(defun as-import-test ()
(let* ((mid (mm-child-iri "mid.xtm")) (child (mm-child-iri "leaf.xtm"))
(raw (bytes (document (concatenate 'string "Name"
(as-xml "#t" '(("#r" "#p") ("#r2" "#q")) '("#s")))))))
(mm-call "association-native-nested-import"
(document (concatenate 'string (mm-directive "mid.xtm") ""))
(list (list mid (bytes (document (mm-directive "leaf.xtm"))) "middle:diagnostic-only") (list child raw "child:diagnostic-only"))
(lambda (map imports calls)
(check "association:import:two-real-contexts-exact-resolver-IRIs"
(equal calls (list (list mid :mergemap *document* "http://psi.topicmaps.org/iso13250/xtm")
(list child :mergemap mid "http://psi.topicmaps.org/iso13250/xtm"))))
(check "association:import:native-EQ-players-types-scope-parents-source-preserved"
(and (as-current-p map) (eq (by-id map "local") (mm-local map child "p"))
(null (item-identifiers map)) (= 1 (length (topic-map-associations map)))
(let ((a (first (topic-map-associations map))))
(and (eq (mm-local map child "t") (association-type a))
(equal (list (mm-local map child "s")) (association-scope a))
(some (lambda (r) (eq (by-id map "local") (association-role-player r))) (association-roles a))))
(every (lambda (record)
(let* ((source (first record)) (resolve (second record)) (a (first (topic-map-associations source))))
(and (not (eq source (association-parent (funcall resolve a))))
(mm-current-p (association-parent (funcall resolve a)))
(not (eq a (funcall resolve a)))
(every (lambda (r) (and (not (eq r (funcall resolve r)))
(member (funcall resolve r) (association-roles (funcall resolve a)) :test #'eq))) (association-roles a))))) imports)))
(push (mm-fields "label" "nested-native-association-import" "sourceText" (babel:octets-to-string raw :encoding :utf-8)
"model" (as-model map) "sourceMapIdentifiersExcluded" :true "sourceMapsUnchanged" :true) *association-controls*)))))