(in-package #:topic-maps.xtm.tests) (defun name-reifier-names (map) (loop for topic in (topic-map-topics map) append (topic-names topic))) (defun name-reifier-current-p (map) (and (variants-current-p map) (topic-maps.tmdm::validate-map-reification map) (topic-maps.tmdm::validate-map-item-identifiers map) (every (lambda (name) (let ((r (item-reifier name))) (or (null r) (and (typep r 'topic) (eq r (canonical-topic r)) (member r (topic-map-topics map) :test #'eq) (eq name (topic-reified r)))))) (name-reifier-names map)))) (defun name-reifier-model-data (map) (let ((topics (topic-map-topics map)) (names (name-reifier-names map))) (topic-maps.test-foundation::fields "identity" (variant-model-data map) "canonicalNameReifierInverseEQ" (if (name-reifier-current-p map) :true :false) "mapReifierTopic" (when (item-reifier map) (1+ (position (item-reifier map) topics :test #'eq))) "nameReifierTopics" (coerce (mapcar (lambda (name) (when (item-reifier name) (1+ (position (item-reifier name) topics :test #'eq)))) names) 'vector) "topicReifiedNames" (coerce (mapcar (lambda (topic) (let ((p (position (topic-reified topic) names :test #'eq))) (when p (1+ p)))) topics) 'vector)))) (defun name-reifier-state (map &optional held) (list (map-reifier-state map) (loop for item in held collect (list item (topic-maps.tmdm::canonical-item item) (when (topic-maps.tmdm::reifiable-p item) (topic-maps.tmdm::%item-reifier item)) (typecase item (topic (topic-maps.tmdm::%topic-forward item)) (topic-name (topic-maps.tmdm::%topic-name-forward item))))))) (defun name-reifier-pure (label map &optional held) (let* ((before (name-reifier-state map held)) (actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*))) (check (concatenate 'string "name-reifier:" label ":pure-deterministic-current-and-retained-handles") (and (equal before (name-reifier-state map held)) (equalp actual (topic-maps.cxtm:canonicalize-octets map :base-locator *document*)) (equal before (name-reifier-state map held)))) actual)) (defun name-reifier-xml (iri &optional (value "N") (tail "")) (format nil "~A~A" iri value tail)) (defun name-reifier-original-contracts () (setf *name-reifier-outputs* nil) (dolist (spec '(("name-reifier" "reifier" 1 180 434) ("name-duplicate-reified" "reifier1" 2 251 435))) (destructuring-bind (stem r-id source-count source-size baseline-size) spec (let* ((id (format nil "xtm2/in/~A.xtm" stem)) (*document* (concatenate 'string "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/" id)) (label (concatenate 'string "name-reifier-original:" stem)) (raw (file-bytes (corpus-path id))) (copy (copy-seq raw)) (baseline (corpus-path (format nil "xtm2/baseline/~A.xtm.cxtm" stem))) (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 2.0 4.3.1-4.3.4,4.10" "TMDM 5.3.4,5.3.5,5.4,6.2,6.3" "CXTM 3,4"))) (map nil) (actual nil) (c nil)) (setf *cases* (append *cases* (list case))) (handler-case (progn (let ((saved (symbol-function 'topic-maps.xml:parse-octets)) (calls 0) (same nil)) (unwind-protect (progn (setf (symbol-function 'topic-maps.xml:parse-octets) (lambda (source handler) (incf calls) (setf same (eq raw source)) (funcall saved source handler))) (setf map (deserialize-octets raw :document-iri *document* :source-key id)) (check (concatenate 'string label ":qualified-parser-once-original-octets-eq") (and (= 1 calls) same))) (setf (symbol-function 'topic-maps.xml:parse-octets) saved))) (let* ((root (topic-maps.xml:parse-octets raw (make-instance 'topic-maps.xtm::source-handler))) (source-topic (first (topic-maps.xtm::elements root))) (source-names (topic-maps.xtm::elements source-topic)) (owner (by-id map "topic")) (name (first (topic-names owner))) (r (by-id map r-id)) (type (by-si map (default-name-type-identifier)))) (check (concatenate 'string label ":actual-original-source-properties") (and (= source-size (length raw)) (= source-count (length source-names)) (equal "topic" (topic-maps.xtm::attr source-topic "id")) (every (lambda (node) (and (equal "name" (topic-maps.xtm::node-name node)) (equal (concatenate 'string "#" r-id) (topic-maps.xtm::attr node "reifier")) (= 1 (length (topic-maps.xtm::elements node))) (equal "value" (topic-maps.xtm::node-name (first (topic-maps.xtm::elements node)))) (equal '("Topic") (topic-maps.xtm::node-children (first (topic-maps.xtm::elements node)))))) source-names))) (check (concatenate 'string label ":native-normalized-name-reifier-inverse-and-canonical-eq") (and (= 3 (length (topic-map-topics map))) (null (topic-map-associations map)) (= 1 (length (name-reifier-names map))) (= 1 (length (topic-names owner))) (eq owner (topic-name-parent name)) (eq name (canonical-topic-name name)) (eq type (topic-name-type name)) (equal "Topic" (topic-name-value name)) (null (topic-name-scope name)) (null (topic-name-variants name)) (eq r (item-reifier name)) (eq name (topic-reified r)) (eq r (canonical-topic r)) (null (item-reifier map)) (null (topic-reified owner)) (null (topic-reified type)) (name-reifier-current-p map))) (check (concatenate 'string label ":complete-separated-identifier-sets") (and (null (item-identifiers map)) (null (item-identifiers name)) (equal (list (concatenate 'string *document* "#topic")) (item-identifiers owner)) (equal (list (concatenate 'string *document* "#" r-id)) (item-identifiers r)) (equal (list (default-name-type-identifier)) (topic-subject-identifiers type)) (null (item-identifiers type)) (null (topic-subject-identifiers owner)) (null (topic-subject-locators owner)) (null (topic-subject-identifiers r)) (null (topic-subject-locators r)) (null (topic-subject-locators type)) (null (topic-names r)) (null (topic-names type)) (every (lambda (topic) (and (null (topic-occurrences topic)) (null (topic-roles-played topic)))) (topic-map-topics map)))) (setf actual (name-reifier-pure stem map (list owner name r type))) (check (concatenate 'string label ":unmodified-source-baseline-size") (and (equalp raw copy) (= baseline-size (length expected)))) (let ((other (deserialize-octets raw :document-iri *document* :source-key "independent:name-reifier"))) (check (concatenate 'string label ":source-key-independent-model-cxtm") (and (equalp (name-reifier-model-data map) (name-reifier-model-data other)) (equalp actual (topic-maps.cxtm:canonicalize-octets other :base-locator *document*))))))) (error (condition) (setf c condition))) (record-original-result case actual expected c) (push (topic-maps.test-foundation::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) "sourceNameCount" source-count "reifierLocator" (concatenate 'string *document* "#" r-id) "model" (when map (name-reifier-model-data map)) "condition" (map-reifier-condition-data c) "actualCXTM" (when actual (babel:octets-to-string actual :encoding :utf-8)) "expectedCXTM" (babel:octets-to-string expected :encoding :utf-8) "actualBytes" (when actual (length actual)) "expectedBytes" (length expected) "firstDifferentByte" (when actual (mismatch actual expected)) "status" (string-downcase (string (topic-maps.test-foundation:test-case-status case)))) *name-reifier-outputs*) (when c (error c)) (check (concatenate 'string label ":byte-exact") (equalp actual expected))))) (setf *name-reifier-outputs* (nreverse *name-reifier-outputs*)) (check "name-reifier-originals:65-unique-records-two-new-positives" (and (= 65 (length *cases*)) (= 57 (count :valid *cases* :key #'topic-maps.test-foundation:test-case-category)) (= 65 (length (remove-duplicates *cases* :key #'topic-maps.test-foundation:test-case-id :test #'equal)))))) (defun name-reifier-control (label body verify) (let* ((source (document body)) (raw (bytes source)) (copy (copy-seq raw)) (map (deserialize-octets raw :document-iri *document* :source-key label))) (funcall verify map) (check (concatenate 'string "name-reifier:" label ":canonical-native-reification") (name-reifier-current-p map)) (let ((actual (name-reifier-pure label map (append (topic-map-topics map) (name-reifier-names map))))) (check (concatenate 'string "name-reifier:" label ":original-source-bytes-preserved") (equalp raw copy)) (push (topic-maps.test-foundation::fields "label" label "sourceText" source "model" (name-reifier-model-data map) "actualCXTM" (babel:octets-to-string actual :encoding :utf-8) "condition" nil) *name-reifier-controls*)))) (defun name-reifier-error-control (label body class code &optional thunk map) ;; Reuse the existing native/XTM Condition verifier and JSON format. Bind its ;; output separately so historical Map-Reification evidence stays independent. (let ((*map-reifier-controls* nil)) (unwind-protect (map-reifier-error-control (concatenate 'string "name:" label) (when body (document body)) class code thunk map) (when *map-reifier-controls* (push (first *map-reifier-controls*) *name-reifier-controls*))))) (defun name-reifier-value-tests () (dolist (row `(("absolute" "urn:name-r" "urn:name-r") ("relative" "../reifiers#n" "https://xtm-tests.example.invalid/cd165b150176c1d6842b37be9cc9051b40d4996b/xtm2/reifiers#n") ("fragment" "#r" ,(concatenate 'string *document* "#r")) ("empty" "" ,*document*) ("not-topicref-fragment" "#2r/path" ,(concatenate 'string *document* "#2r/path")) ("percent-utf8" "urn:%E9%A6%AC%F0%9F%A6%8A" "urn:馬🦊") ("unicode-xml" "urn:馬&🦊" "urn:馬&🦊"))) (destructuring-bind (label reference expected) row (name-reifier-control label (format nil "~A" (name-reifier-xml reference " N & 馬🦊 ")) (lambda (map) (let* ((owner (by-id map "a")) (name (first (topic-names owner))) (r (item-reifier name))) (check (concatenate 'string "name-reifier:" label ":correct-name-not-parent-map-or-type") (and (= 3 (length (topic-map-topics map))) (eq name (topic-reified r)) (equal (list expected) (item-identifiers r)) (null (topic-subject-identifiers r)) (null (topic-subject-locators r)) (equal " N & 馬🦊 " (topic-name-value name)) (null (item-identifiers name)) (null (item-identifiers map)) (null (item-reifier map)) (null (topic-reified owner)) (null (topic-reified (topic-name-type name)))))))))) (dolist (spec '(("existing-II" "itemIdentity" t) ("SI-only" "subjectIdentifier" t) ("SL-only" "subjectLocator" nil))) (destructuring-bind (label kind merged) spec (name-reifier-control label (format nil "<~A href='urn:reifier'/>~A" kind (name-reifier-xml "urn:reifier")) (lambda (map) (let* ((name (first (topic-names (by-id map "a")))) (r (item-reifier name)) (old (by-id map "r"))) (check (concatenate 'string "name-reifier:" label ":ii-required-independent-of-si-sl") (and (member "urn:reifier" (item-identifiers r) :test #'equal) (if merged (and (eq r old) (same-identifiers-p (item-identifiers r) (list "urn:reifier" (concatenate 'string *document* "#r")))) (and (not (eq r old)) (equal '("urn:reifier") (item-identifiers r)) (null (topic-reified old))))))))))) (name-reifier-control "name-II-type-scope-variant-forward" (concatenate 'string "NV") (lambda (map) (let* ((owner (by-id map "a")) (name (first (topic-names owner))) (r (item-reifier name)) (v (first (topic-name-variants name)))) (check "name-reifier:identifiers-type-scope-variant:complete-model-and-eq" (and (eq r (by-id map "r")) (eq name (topic-reified r)) (eq owner (topic-name-parent name)) (equal '("urn:map") (item-identifiers map)) (equal '("urn:name") (item-identifiers name)) (equal (list (concatenate 'string *document* "#r")) (item-identifiers r)) (eq (by-id map "t") (topic-name-type name)) (scope-eq-p (topic-name-scope name) (list (by-id map "s"))) (eq name (variant-parent v)) (equal "V" (variant-value v)) (equal (string-datatype) (variant-datatype v)) (scope-eq-p (variant-scope v) (list (by-id map "s") (by-id map "v")))))))) (dolist (same '(nil t)) (let ((label (if same "equal-names-same-reifier" "equal-names-different-reifiers"))) (name-reifier-control label (format nil "NNV" (if same "r1" "r2")) (lambda (map) (let* ((names (name-reifier-names map)) (name (first names)) (r (item-reifier name))) (check (concatenate 'string "name-reifier:" label ":normalized-name-identifier-union-native-reifier") (and (= 1 (length names)) (same-identifiers-p '("urn:n1" "urn:n2") (item-identifiers name)) (same-identifiers-p (item-identifiers r) (if same (list (concatenate 'string *document* "#r1")) (list (concatenate 'string *document* "#r1") (concatenate 'string *document* "#r2")))) (eq name (variant-parent (first (topic-name-variants name)))) (eq name (topic-reified r))))))))) (name-reifier-control "different-names-different-reifiers" (format nil "~A~A" (name-reifier-xml "#r1" "A") (name-reifier-xml "#r2" "B")) (lambda (map) (let* ((names (topic-names (by-id map "a")))) (check "name-reifier:different-names:two-independent-reifications" (and (= 2 (length names)) (not (eq (item-reifier (first names)) (item-reifier (second names)))) (every (lambda (n) (eq n (topic-reified (item-reifier n)))) names)))))) (let* ((map (deserialize-octets (bytes (map-reifier-xml "#map-r" (format nil "~A" (name-reifier-xml "#name-r")))) :document-iri *document*)) (name (first (topic-names (by-id map "a"))))) (check "name-reifier:map-and-name:distinct-reifiers-inverses-identifiers" (and (map-reifier-current-p map) (name-reifier-current-p map) (not (eq (item-reifier map) (item-reifier name))) (eq name (topic-reified (item-reifier name))) (null (item-identifiers map)) (null (item-identifiers name)))) (name-reifier-pure "map-and-name" map))) (defun name-reifier-merge-tests () ;; Nine reader-level routes: Parent, Type or Scope merging by SI, SL or II. (dolist (where '(:parent :type :scope)) (dolist (kind '("subjectIdentifier" "subjectLocator" "itemIdentity")) (let* ((label (format nil "merge-~A-~A" where kind)) (one (format nil "~A~AN" (if (eq where :type) "" "") (if (eq where :scope) "" ""))) (two (format nil "~A~AN" (if (eq where :type) "" "") (if (eq where :scope) "" ""))) (body (concatenate 'string (if (eq where :parent) (format nil "~A~A" one two) (format nil "~A~A" one two)) (format nil "<~A href='urn:shared'/><~A href='urn:shared'/>" kind kind))) (topic-maps.xtm::*document-iri* *document*) (topic-maps.xtm::*source-key* label) (root (topic-maps.xml:parse-octets (bytes (document body)) (make-instance 'topic-maps.xtm::source-handler))) (map (make-topic-map)) (held nil)) (topic-maps.xtm::validate-source root) (loop for node in (topic-maps.xtm::elements root) for step from 1 do (topic-maps.xtm::read-topic node map) (when (= step (if (eq where :parent) 2 1)) (setf held (append (name-reifier-names map) (mapcar #'item-reifier (name-reifier-names map)) (list (by-id map "x") (by-id map "y")))) (check (concatenate 'string "name-reifier:" label ":before-distinct-native-names-reifiers") (and (= 2 (length (name-reifier-names map))) (not (eq (first held) (second held))) (not (eq (third held) (fourth held))))))) (let* ((name (first (name-reifier-names map))) (r (item-reifier name))) (check (concatenate 'string "name-reifier:" label ":normalized-old-new-names-reifiers-eq") (and (= 1 (length (name-reifier-names map))) (eq name (canonical-topic-name (first held))) (eq name (canonical-topic-name (second held))) (eq r (canonical-topic (third held))) (eq r (canonical-topic (fourth held))) (eq name (topic-reified (third held))) (eq name (topic-reified (fourth held))) (same-identifiers-p '("urn:n1" "urn:n2") (item-identifiers name)) (same-identifiers-p (item-identifiers r) (list (concatenate 'string *document* "#r1") (concatenate 'string *document* "#r2"))) (eq (canonical-topic (fifth held)) (canonical-topic (sixth held))) (case where (:parent (eq (topic-name-parent name) (by-id map "x"))) (:type (eq (topic-name-type name) (by-id map "x"))) (:scope (scope-eq-p (topic-name-scope name) (list (by-id map "x"))))) (name-reifier-current-p map))) (name-reifier-pure (concatenate 'string label ":staged") map held) (name-reifier-control label body (lambda (other) (check (concatenate 'string "name-reifier:" label ":public-api-equivalent") (equalp (name-reifier-model-data map) (name-reifier-model-data other))))))))) (let* ((body "N") (topic-maps.xtm::*document-iri* *document*) (root (topic-maps.xml:parse-octets (bytes (document body)) (make-instance 'topic-maps.xtm::source-handler))) (map (make-topic-map))) (topic-maps.xtm::validate-source root) (topic-maps.xtm::read-topic (first (topic-maps.xtm::elements root)) map) (let* ((name (first (name-reifier-names map))) (r (item-reifier name))) (check "name-reifier:forward-definition:created-before-later-topic" (and r (eq name (topic-reified r)) (null (by-id map "definition")))) (topic-maps.xtm::read-topic (second (topic-maps.xtm::elements root)) map) (check "name-reifier:forward-definition:old-reifier-new-canonical-eq-and-ii-union" (and (not (eq r (canonical-topic r))) (eq (canonical-topic r) (by-id map "definition")) (eq (canonical-topic r) (by-id map "r")) (eq (canonical-topic r) (item-reifier name)) (eq name (topic-reified r)) (same-identifiers-p (item-identifiers r) (list (concatenate 'string *document* "#r") (concatenate 'string *document* "#definition"))))) (name-reifier-pure "forward-definition:staged" map (list name r)) (name-reifier-control "forward-definition" body (lambda (other) (check "name-reifier:forward-definition:public-api-equivalent" (equalp (name-reifier-model-data map) (name-reifier-model-data other))))))) ;; A previously reified Name also survives merging with an unreified equal Name. (let* ((map (make-topic-map)) (p (intern-topic map :item-identifier "urn:p")) (q (intern-topic map :item-identifier "urn:q")) (a (add-default-name p "N")) (b (add-default-name q "N")) (r (intern-topic map :item-identifier "urn:r"))) (set-item-reifier a r) (merge-topics p q) (check "name-reifier:normalization:one-reified-one-unreified-preserves-canonical-inverse" (and (eq (canonical-topic-name a) (canonical-topic-name b)) (eq r (item-reifier b)) (eq (canonical-topic-name a) (topic-reified r)) (name-reifier-current-p map))) (name-reifier-pure "one-reified-one-unreified" map (list p q a b r)))) (defun name-reifier-syntax-conflict-tests () (dolist (row '(("space" "urn:a b" :invalid-iri) ("bad-percent" "urn:%GG" :invalid-percent-escape) ("bad-utf8" "urn:%C3%28" :invalid-percent-utf8) ("surrogate-utf8" "urn:%ED%A0%80" :invalid-percent-utf8) ("decoded-control" "urn:%00" :invalid-iri) ("relative-colon" "1bad:iri" :invalid-iri))) (name-reifier-error-control (first row) (format nil "~A" (name-reifier-xml (second row))) 'xtm-rejection (third row))) (dolist (row '(("extra-attribute" "N" :unexpected-attribute) ("qualified-reifier" "N" :unexpected-attribute) ("reifier-child" "N" :unexpected-element) ("wrong-namespace" "N" :invalid-namespace) ("identity-order" "N" :element-order))) (name-reifier-error-control (first row) (format nil "~A" (second row)) 'xtm-rejection (third row))) (name-reifier-error-control "malformed-XML" "N" 'xtm-xml-error :xml-not-well-formed) (name-reifier-error-control "variant-reifier-excluded-datatype" "N42" 'xtm-unsupported :resource-data-datatype) (name-reifier-error-control "name-own-II-collision" "N" 'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier) (name-reifier-error-control "map-II-collision" "N" 'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier) (name-reifier-error-control "reuse-reifier" (format nil "~A~A" (name-reifier-xml "#r" "A") (name-reifier-xml "#r" "B")) 'reification-error :reifier-conflict) (let* ((map (read-body (format nil "~A~A" (name-reifier-xml "#r1" "A") (name-reifier-xml "#r2" "B")))) (names (name-reifier-names map)) (a (first names)) (b (second names)) (r (item-reifier a)) (s (item-reifier b))) (name-reifier-error-control "native-reuse-reifier" nil 'reification-error :reifier-conflict (lambda () (set-item-reifier b r)) map) (name-reifier-error-control "native-distinct-reified-merge" nil 'reification-error :reifier-conflict (lambda () (merge-topics r s)) map) (add-item-identifier a "urn:name") (name-reifier-error-control "native-II-collision" nil 'topic-maps.tmdm::occurrence-identifier-error :duplicate-item-identifier (lambda () (intern-topic map :item-identifier "urn:name")) map) ;; Here Parent merging would make the Names equal, but both Topics already ;; reify DIFFERENT live Names. TMDM 6.2 rejects the Topic merge first. (let* ((self-map (read-body "NN")) (one (by-id self-map "one")) (two (by-id self-map "two"))) (name-reifier-error-control "native-parent-merge-reifier-conflict" nil 'reification-error :reifier-conflict (lambda () (merge-topics one two)) self-map)) (check "name-reifier:conflicts:canonical-name-and-reifier-handles-preserved" (and (eq a (canonical-topic-name a)) (eq b (canonical-topic-name b)) (eq a (topic-reified r)) (eq b (topic-reified s)) (eq r (item-reifier a)) (eq s (item-reifier b)) (name-reifier-current-p map))))) (defun name-reifier-tests () (name-reifier-value-tests) (name-reifier-merge-tests) (name-reifier-syntax-conflict-tests))