(in-package #:topic-maps.xtm.tests)
(defparameter *association-reifier-originals*
'("association-reifier" "association-duplicate-reified" "association-duplicate-reified2"
"association-duplicate-reified3" "association-duplicate-reified4"))
(defun arr-observe (raw key)
;; AS-OBSERVE retains actual pre-normalization Association/Role handles.
;; Also retain every actual Topic and current Reifier after each Reader call.
(let ((saved (symbol-function 'topic-maps.xtm::read-association)) (held nil) (counts nil)
(map nil) (native nil) (stages nil) (calls 0) (same nil))
(unwind-protect
(progn
(setf (symbol-function 'topic-maps.xtm::read-association)
(lambda (node model) (let ((a (funcall saved node model)))
(dolist (topic (topic-map-topics model)) (pushnew topic held :test #'eq))
(push (length (topic-map-associations model)) counts) a)))
(multiple-value-setq (map native stages calls same) (as-observe raw key)))
(setf (symbol-function 'topic-maps.xtm::read-association) saved))
(values map (union held native :test #'eq) stages calls same (nreverse counts))))
(defun arr-pure (label map held)
(let ((before (ik-state map)) (cx (mm-cxtm map)))
(check (format nil "association-reifier:~A:pure-CXTM-all-II-reifiers-old-handles" label)
(and (equalp cx (mm-cxtm map)) (equal before (ik-state map)) (arr-current-p map) (orr-held-p map held))) cx))
(defun arr-original-model (name map)
(let* ((a (first (topic-map-associations map))) (single (equal name "association-reifier"))
(r (by-id map (if single "reifier" "reifier1")))
(players (if single '("topic") '("player1" "player2")))
(types (if single '("roletype") '("roletype1" "roletype2")))
(ii (member name '("association-duplicate-reified2" "association-duplicate-reified3" "association-duplicate-reified4") :test #'equal)))
(check (format nil "association-reifier-original:~A:exact-native-Association-Roles-Reifier-identities" name)
(and (= 1 (length (topic-map-associations map))) (= (if single 4 6) (length (topic-map-topics map)))
(= (length players) (length (association-roles a))) (eq map (association-parent a))
(eq (by-id map "assoctype") (association-type a)) (null (association-scope a))
(eq r (item-reifier a)) (eq a (topic-reified r))
(same-identifiers-p (when ii (list (concatenate 'string *document* "#association"))) (item-identifiers a))
(loop for p in players for rt in types always (some (lambda (role)
(and (eq (by-id map p) (association-role-player role)) (eq (by-id map rt) (association-role-type role))
(eq a (association-role-parent role)) (equal (list role) (topic-roles-played (by-id map p)))
(null (item-identifiers role)) (null (item-reifier role)))) (association-roles a)))
(every (lambda (topic) (and (= 1 (length (item-identifiers topic))) (null (topic-subject-identifiers topic))
(null (topic-subject-locators topic)) (null (topic-names topic)))) (topic-map-topics map))
(null (all-occurrences map)) (null (item-identifiers map)) (null (item-reifier map)) (arr-current-p map)))))
(defun association-reifier-original-contracts ()
(setf *association-reifier-outputs* nil)
(let ((previous (copy-list *cases*)))
(dolist (name *association-reifier-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.3.2,4.3.4,4.6,4.18-4.20" "TMDM 5.1,5.3.4-5.3.5,5.7,6.2,6.6")))
(map nil) (held nil) (stages nil) (calls 0) (same nil) (counts nil) (actual nil) (c nil))
(check (format nil "association-reifier-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 counts) (arr-observe raw id))
(arr-original-model name map)
(check (format nil "association-reifier-original:~A:actual-reads-original-octets-qualified-parser" name)
(and (= 1 calls) same (equalp raw copy) (equal counts (if (equal name "association-reifier") '(1) '(1 1)))))
(check (format nil "association-reifier-original:~A:all-old-native-handles-live" name) (orr-held-p map held))
(unless (equal name "association-reifier")
(check (format nil "association-reifier-original:~A:replaced-Association-and-Role-handles" name)
(and (some (lambda (i) (and (typep i 'association) (not (eq i (canonical-association i))))) held)
(some (lambda (i) (and (typep i 'association-role) (not (eq i (canonical-association-role i))))) held))))
(setf actual (arr-pure name map held))
(let ((other (deserialize-octets raw :document-iri *document* :source-key "different:Association-Reifier-provenance")))
(check (format nil "association-reifier-original:~A:source-key-independent" name)
(and (equalp (arr-model map) (arr-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 (arr-model map))
"associationCountsAfterEachRead" (coerce counts 'vector)
"normalizationStages" (coerce (mapcar (lambda (s) (coerce s 'vector)) stages) 'vector)
"observedNativeHandles" (length held) "oldHandlesCanonicalAndLive" (if (and map (orr-held-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-reifier-outputs*)
(when c (error c))
(check (format nil "association-reifier-original:~A:complete-original-CXTM-byte-exact" name) (equalp actual expected))))
(check "association-reifier-original:requested-five-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))
(mapcar (lambda (n) (format nil "xtm2/in/~A.xtm" n)) *association-reifier-originals*))
(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-reifier-outputs* (nreverse *association-reifier-outputs*)))
(defun arr-xml (reifier &key (type "#t") (roles '(("#rt" "#p"))) themes ids role-id)
(let ((body (as-xml type roles themes)))
(format nil "~A~A"
(if reifier (format nil " reifier='~A'" reifier) "") (ik-ids ids)
(let ((text (subseq body (length "") (- (length body) (length "")))))
(if role-id (let ((i (search "" text)))
(concatenate 'string (subseq text 0 (+ i 6)) (ik-ids (list role-id)) (subseq text (+ i 6)))) text)))))
(defun arr-probe (label body verify &optional replaced)
(let* ((raw (bytes (document body))) (copy (copy-seq raw)))
(multiple-value-bind (map held stages calls same counts) (arr-observe raw label)
(check (format nil "association-reifier:~A:productive-reader-qualified-XML-EQ-inverses" label)
(and (= 1 calls) same (equalp raw copy) (arr-current-p map) (orr-held-p map held)))
(funcall verify map)
(when replaced (check (format nil "association-reifier:~A:old-handles-replaced-and-resolvable" label)
(some (lambda (i) (not (eq i (topic-maps.tmdm::canonical-item i)))) held)))
(let ((cx (arr-pure label map held)))
(push (mm-fields "label" label "documentIRI" *document* "sourceKey" label "sourceText" (document body)
"model" (arr-model map) "CXTM" (babel:octets-to-string cx :encoding :utf-8)
"readAssociationCounts" (coerce counts 'vector) "normalizationStages" (coerce (mapcar (lambda (s) (coerce s 'vector)) stages) 'vector)
"oldHandlesCanonicalAndLive" :true) *association-reifier-controls*)) map)))
(defun arr-error (label body class code &key map-reifier)
(let ((source (if map-reifier (map-reifier-xml map-reifier body) (document body))))
(multiple-value-bind (c frames) (orr-condition-from (bytes source) label)
(let ((actual (cond ((typep c 'topic-maps.xtm:xtm-error) (error-code c))
((typep c 'reification-error) (reification-error-code c))
((typep c 'item-identifier-error) (item-identifier-error-code c)))))
(check (format nil "association-reifier-error:~A:exact-class-code" label)
(and (eq (type-of c) class) (eq code actual)))
(when (typep c 'topic-maps.xtm:xtm-error)
(check (format nil "association-reifier-error:~A:source-document-position-and-cause" label)
(and (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)))))))
(push (mm-fields "label" label "sourceText" source "documentIRI" *document* "sourceKey" label
"condition" (mm-condition c) "actualNativeAssignments" (coerce frames 'vector)) *association-reifier-controls*)) c)))
(defun association-reifier-tests ()
(setf *association-reifier-controls* nil)
(dolist (position '("unknown" "before" "after"))
(arr-probe (concatenate 'string "definition-" position)
(concatenate 'string (if (equal position "before") "" "") (arr-xml "#r")
(if (equal position "after") "" ""))
(lambda (map) (let ((a (first (topic-map-associations map))) (r (by-id map "r")))
(check (format nil "association-reifier:definition-~A:II-only-Reifier-no-invented-SI-SL" position)
(and (= 4 (length (topic-map-topics map))) (eq r (item-reifier a)) (eq a (topic-reified r))
(equal (list (concatenate 'string *document* "#r")) (item-identifiers r))
(null (topic-subject-identifiers r)) (null (topic-subject-locators r))))))))
(dolist (category '("itemIdentity" "subjectIdentifier"))
(arr-probe (concatenate 'string "lookup-" category)
(format nil "<~A href='urn:reifier'/>~A" category (arr-xml "urn:reifier"))
(lambda (map) (let ((r (by-id map "known")))
(check (format nil "association-reifier:lookup-~A:II-or-II-miss-then-native-cross-merge" category)
(and (= 4 (length (topic-map-topics map))) (eq r (item-reifier (first (topic-map-associations map))))
(same-identifiers-p (list (concatenate 'string *document* "#known") "urn:reifier") (item-identifiers r))
(equal (when (equal category "subjectIdentifier") '("urn:reifier")) (topic-subject-identifiers r))
(null (topic-subject-locators r))))))))
(dolist (ref '("urn:reifier" "../%E9%A6%AC#r"))
(arr-probe (concatenate 'string "resolved-IRI:" ref) (arr-xml ref :ids '("urn:association"))
(lambda (map) (let ((a (first (topic-map-associations map))))
(check (format nil "association-reifier:~A:IRI-and-separate-Association-II" ref)
(and (equal '("urn:association") (item-identifiers a))
(equal (list (mm-child-iri ref)) (item-identifiers (item-reifier a)))))))))
(dolist (kind '("same-Reifier" "different-Reifiers" "unreified-first" "reified-first"))
(arr-probe kind
(concatenate 'string (arr-xml (unless (equal kind "unreified-first") "#r") :ids '("urn:a") :role-id "urn:role-a")
(arr-xml (cond ((equal kind "different-Reifiers") "#u") ((equal kind "reified-first") nil) (t "#r")) :ids '("urn:b") :role-id "urn:role-b"))
(lambda (map) (let* ((a (first (topic-map-associations map))) (r (first (association-roles a))))
(check (format nil "association-reifier:~A:normalization-Reifier-Association-and-Role-II-union" kind)
(and (= 1 (length (topic-map-associations map))) (eq (by-id map "r") (item-reifier a))
(same-identifiers-p '("urn:a" "urn:b") (item-identifiers a))
(same-identifiers-p '("urn:role-a" "urn:role-b") (item-identifiers r))
(or (not (equal kind "different-Reifiers")) (and (eq (by-id map "r") (by-id map "u"))
(same-identifiers-p (list (concatenate 'string *document* "#r") (concatenate 'string *document* "#u")) (item-identifiers (item-reifier a))))))))) t))
(arr-probe "roles-order-duplicates-and-Scope"
(concatenate 'string (arr-xml "#r" :roles '(("#rt" "#p") ("#rt2" "#q")) :themes '("#s" "#s"))
(arr-xml "#u" :roles '(("#rt2" "#q") ("#rt" "#p") ("#rt" "#p")) :themes '("#s")))
(lambda (map) (let ((a (first (topic-map-associations map))))
(check "association-reifier:roles-order:one-Association-two-Roles-one-Scope-Reifiers-merged"
(and (= 1 (length (topic-map-associations map))) (= 2 (length (association-roles a)))
(equal (list (by-id map "s")) (association-scope a)) (eq (by-id map "r") (by-id map "u")))))) t)
(arr-late-merges)
(arr-negative-tests)
(arr-import-test)
(setf *association-reifier-controls* (nreverse *association-reifier-controls*)))
(defun arr-late-merges ()
(dolist (category '("subjectIdentifier" "subjectLocator" "itemIdentity"))
(arr-probe (concatenate 'string "late-" category)
(concatenate 'string (arr-xml "#r" :ids '("urn:a") :role-id "urn:ra" :themes '("#s"))
(arr-xml "#u" :type "#tt" :roles '(("#rrt" "#q")) :ids '("urn:b") :role-id "urn:rb" :themes '("#ss"))
(format nil "<~A href='urn:p'/><~A href='urn:p'/><~A href='urn:rt'/><~A href='urn:rt'/><~A href='urn:s'/><~A href='urn:s'/><~A href='urn:t'/><~A href='urn:t'/><~A href='urn:r'/><~A href='urn:r'/>" category category category category category category category category category category))
(lambda (map) (let* ((a (first (topic-map-associations map))) (role (first (association-roles a))))
(check (format nil "association-reifier:late-~A:all-native-references-II-and-old-handles" category)
(and (= 1 (length (topic-map-associations map))) (eq (by-id map "r") (by-id map "u"))
(eq (by-id map "t") (association-type a)) (eq (by-id map "rt") (association-role-type role))
(eq (by-id map "p") (association-role-player role)) (equal (list (by-id map "s")) (association-scope a))
(same-identifiers-p '("urn:a" "urn:b") (item-identifiers a))
(same-identifiers-p '("urn:ra" "urn:rb") (item-identifiers role)))))) t)))
(defun arr-negative-tests ()
(dolist (row '(("bad-percent" "%ZZ" :invalid-percent-escape) ("bad-percent-UTF8" "%C3%28" :invalid-percent-utf8) ("bad-IRI" "x#y#z" :invalid-iri)))
(arr-error (first row) (arr-xml (second row)) 'xtm-rejection (third row)))
(dolist (row '(("extra-attribute" "" xtm-rejection :unexpected-attribute)
("reifier-on-type" "" xtm-rejection :unexpected-attribute)
("reifier-on-topicRef" "" xtm-rejection :unexpected-attribute)
("malformed-XML" "" xtm-xml-error :xml-not-well-formed)
("missing-Type" "" xtm-rejection :missing-type)
("missing-Roles" "" xtm-rejection :missing-roles)
("wrong-order" "" xtm-rejection :missing-type)))
(apply #'arr-error row))
(arr-error "different-Associations-same-Reifier" (concatenate 'string (arr-xml "#r") (arr-xml "#r" :type "#other")) 'reification-error :reifier-conflict)
(arr-error "Occurrence-and-Association-same-Reifier"
(concatenate 'string "" (orr-xml "#r") "" (arr-xml "#r")) 'reification-error :reifier-conflict)
(arr-error "Map-and-Association-same-Reifier" (arr-xml "#r") 'reification-error :reifier-conflict :map-reifier "#r")
(dolist (category '("subjectIdentifier" "subjectLocator" "itemIdentity"))
(arr-error (concatenate 'string "late-Reifier-conflict-" category)
(concatenate 'string (arr-xml "#r") (arr-xml "#u" :type "#other")
(format nil "<~A href='urn:r'/><~A href='urn:r'/>" category category)) 'reification-error :reifier-conflict))
(arr-error "Association-II-not-Reifier-Topic-II" (arr-xml "urn:same" :ids '("urn:same")) 'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier)
(arr-error "Role-Reification-remains-Unsupported" "" 'xtm-unsupported :unsupported-construct))
(defun arr-import-test ()
(let* ((child (mm-child-iri "child.xtm"))
(raw (bytes (document (concatenate 'string ""
(arr-xml "#r" :ids '("urn:imported-Association") :role-id "urn:imported-Role" :themes '("#s")))))))
(mm-call "Association-Reification-import" (document (concatenate 'string (mm-directive "child.xtm") ""))
(list (list child raw "child:Association-Reification"))
(lambda (map imports calls) (declare (ignore calls))
(let* ((a (first (topic-map-associations map))) (role (first (association-roles a))) (r (mm-local map child "r")))
(check "association-reifier:import:Parent-Role-Player-II-Reifier-inverse-source-preservation"
(and (arr-current-p map) (eq map (association-parent a)) (eq a (association-role-parent role))
(eq (by-id map "local") (association-role-player role)) (eq r (item-reifier a)) (eq a (topic-reified r))
(equal '("urn:imported-Association") (item-identifiers a)) (equal '("urn:imported-Role") (item-identifiers role))
(null (item-identifiers map))
(every (lambda (record) (let ((source (first record)) (resolve (second record)))
(and (arr-current-p source) (eq a (funcall resolve (first (topic-map-associations source))))
(eq r (funcall resolve (item-reifier (first (topic-map-associations source)))))))) imports))))
(push (mm-fields "label" "native-Association-Reification-import" "sourceText" (babel:octets-to-string raw :encoding :utf-8)
"model" (arr-model map) "sourceMapPreserved" :true) *association-reifier-controls*)))))