(in-package #:topic-maps.ctm.tests) (defvar *last-merge-checks* nil) (defparameter *merge-fixtures* ;; File, item identifiers, subject identifiers, subject locators, name value. '(("topic-subject-identifier.ctm" ("#ctm") ("http://www.isotopicmaps.org/ctm/") nil nil) ("topic-subject-identifier-wrapped.ctm" ("#ctm") ("http://www.isotopicmaps.org/ctm/") nil nil) ("topic-subject-identifier-duplicate.ctm" nil ("http://www.isotopicmaps.org/ctm/") nil nil) ("topic-subject-locator.ctm" ("#ctm") nil ("http://www.isotopicmaps.org/ctm/") nil) ("topic-subject-locator-wrapped.ctm" ("#ctm") nil ("http://www.isotopicmaps.org/ctm/") nil) ("topic-subject-locator-semi.ctm" ("#ctm") nil ("http://www.isotopicmaps.org/ctm/") nil) ("topic-subject-locator-duplicate.ctm" nil nil ("#ctm") nil) ("topic-item-identifier.ctm" ("#topic" "http://example.org/#topic") nil nil nil) ("topic-item-identifier-wrapped.ctm" ("#topic" "http://example.org/#topic") nil nil nil) ("topic-item-identifier-duplicate.ctm" ("#topic") nil nil nil) ("name-duplicate.ctm" ("#topic") nil nil "Name"))) (defun same-locator-set-p (a b) (equal (sort (copy-list a) #'string<) (sort (copy-list b) #'string<))) (defun raw-reference-invariants (map) ;; Inspect storage too: public forwarding readers must not hide dangling refs. (and (verify-item-invariants map) (every (lambda (topic) (and (eq topic (topic-maps.tmdm:canonical-topic topic)) (every (lambda (name) (and (eq name (topic-maps.tmdm:canonical-topic-name name)) (eq topic (topic-maps.tmdm::%topic-name-parent name)) (member (topic-maps.tmdm::%topic-name-type name) (topic-map-topics map) :test #'eq))) (topic-names topic)))) (topic-map-topics map)))) (defun exercise-merge-corpus () (let ((corpus (topic-maps.test-foundation:discover-corpus)) (observations nil)) (dolist (fixture *merge-fixtures*) (destructuring-bind (file ii si sl value) fixture (let* ((id (concatenate 'string "ctm/in/" file)) (case (first (topic-maps.test-foundation:select-cases corpus :id id))) (document (topic-maps.test-foundation:test-case-document-iri case)) (map (deserialize-file (topic-maps.test-foundation:test-case-source-pathname case) :document-iri document :source-name id)) (topics (topic-map-topics map)) (topic (first topics))) (flet ((resolve (values) (mapcar (lambda (v) (if (char= #\# (char v 0)) (concatenate 'string document v) v)) values))) (check (concatenate 'string id ":item-identifiers") (same-locator-set-p (topic-item-identifiers topic) (resolve ii))) (check (concatenate 'string id ":subject-identifiers") (same-locator-set-p (topic-subject-identifiers topic) (resolve si))) (check (concatenate 'string id ":subject-locators") (same-locator-set-p (topic-subject-locators topic) (resolve sl)))) (check (concatenate 'string id ":topic-count") (= (length topics) (if value 2 1))) (check (concatenate 'string id ":actual-references") (raw-reference-invariants map)) (check (concatenate 'string id ":name-count") (= (length (topic-names topic)) (if value 1 0))) (when value (let ((name (first (topic-names topic))) (type (second topics))) (check "pinned-duplicate-name-value" (string= value (topic-name-value name))) (check "pinned-duplicate-name-type" (eq type (topic-name-type name))) (check "pinned-duplicate-default-type-properties" (and (equal (topic-subject-identifiers type) (list (default-name-type-identifier))) (null (topic-item-identifiers type)) (null (topic-subject-locators type)) (null (topic-names type)))))) (check (concatenate 'string id ":cxtm-contract-not-run") (and (eq :cxtm-equal (topic-maps.test-foundation:test-case-expected-outcome case)) (eq :not-run (topic-maps.test-foundation:test-case-status case)) (null (topic-maps.test-foundation:test-case-actual-outcome case)))) (push (list :id id :document-iri document :topic-map map) observations)))) (nreverse observations))) (defun run-merging-tests () (let ((*checks* nil)) (exercise-merge-corpus) (let* ((map (topic-maps.tmdm:make-topic-map)) (a (topic-maps.tmdm:intern-topic map :item-identifier "urn:a")) (b (topic-maps.tmdm:intern-topic map :item-identifier "urn:b")) (na (topic-maps.tmdm:add-default-name a "Same")) (nb (topic-maps.tmdm:add-default-name b "Same"))) (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:shared") (let* ((c (topic-maps.tmdm:add-topic-identifier b :subject-identifier "urn:shared")) (name (first (topic-names c)))) (check "merge-creates-new-topic" (and (not (eq c a)) (not (eq c b)))) (check "both-old-topics-resolve-to-c" (and (eq c (topic-maps.tmdm:canonical-topic a)) (eq c (topic-maps.tmdm:canonical-topic b)))) (check "merged-item-identifiers-union" (same-locator-set-p (topic-item-identifiers c) '("urn:a" "urn:b"))) (check "merged-subject-identifier-once" (equal (topic-subject-identifiers c) '("urn:shared"))) (check "merged-parent-name-deduplicated" (= 1 (length (topic-names c)))) (check "name-merge-creates-new-item" (and (not (eq name na)) (not (eq name nb)))) (check "old-names-resolve-to-new-item" (and (eq name (topic-maps.tmdm:canonical-topic-name na)) (eq name (topic-maps.tmdm:canonical-topic-name nb)))) (check "old-topic-readers-show-current-properties" (equal (topic-item-identifiers a) (topic-item-identifiers c))) (check "name-parent-raw-reference-replaced" (eq c (topic-maps.tmdm::%topic-name-parent name))) (check "lookup-through-old-item-identifier" (eq c (topic-maps.tmdm:intern-topic map :item-identifier "urn:b"))) (check "repeated-identity-addition-idempotent" (eq c (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:shared"))) (check "no-obsolete-topics-in-map" (= 2 (length (topic-map-topics map)))) (check "merged-map-raw-reference-invariants" (raw-reference-invariants map)) (check "merge-identical-handle-is-idempotent" (eq c (topic-maps.tmdm:merge-topics a b))) (let ((snapshot (topic-map-topics map))) (setf (first snapshot) a) (check "topic-set-readers-protect-storage" (not (eq a (first (topic-map-topics map)))))))) (let* ((map (topic-maps.tmdm:make-topic-map)) (a (topic-maps.tmdm:intern-topic map :item-identifier "urn:a")) (b (topic-maps.tmdm:intern-topic map :item-identifier "urn:b")) (c (topic-maps.tmdm:intern-topic map :item-identifier "urn:c"))) (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:x") (topic-maps.tmdm:add-topic-identifier b :subject-identifier "urn:y") (topic-maps.tmdm:add-topic-identifier c :subject-identifier "urn:z") (let* ((ab (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:y")) (abc (topic-maps.tmdm:add-topic-identifier c :subject-identifier "urn:x"))) (check "transitive-chain-one-live-topic" (= 1 (length (topic-map-topics map)))) (check "transitive-all-handles-converge" (every (lambda (old) (eq abc (topic-maps.tmdm:canonical-topic old))) (list a b c ab))) (check "transitive-all-item-identifiers-preserved" (same-locator-set-p (topic-item-identifiers abc) '("urn:a" "urn:b" "urn:c"))) (check "transitive-all-subject-identifiers-preserved" (same-locator-set-p (topic-subject-identifiers abc) '("urn:x" "urn:y" "urn:z"))) (check "transitive-normalization-idempotent" (and (eq map (topic-maps.tmdm:normalize-topic-map map)) (eq abc (first-topic map)))))) (dolist (kind '(:item-identifier :subject-identifier :subject-locator)) (let* ((map (topic-maps.tmdm:make-topic-map)) (a (topic-maps.tmdm:intern-topic map :item-identifier "urn:a")) (b (topic-maps.tmdm:intern-topic map :item-identifier "urn:b"))) (topic-maps.tmdm:add-topic-identifier a kind "urn:shared") (topic-maps.tmdm:add-topic-identifier b kind "urn:shared") (check (format nil "same-category-merge:~A" kind) (= 1 (length (topic-map-topics map)))))) (dolist (kinds '((:item-identifier :subject-identifier) (:subject-identifier :item-identifier) (:item-identifier :subject-locator) (:subject-identifier :subject-locator))) (let* ((map (topic-maps.tmdm:make-topic-map)) (a (topic-maps.tmdm:intern-topic map :item-identifier "urn:a")) (b (topic-maps.tmdm:intern-topic map :item-identifier "urn:b")) (merges (not (eq :subject-locator (second kinds))))) (topic-maps.tmdm:add-topic-identifier a (first kinds) "urn:shared") (topic-maps.tmdm:add-topic-identifier b (second kinds) "urn:shared") (check (format nil "cross-category-merge-rule:~S" kinds) (= (if merges 1 2) (length (topic-map-topics map)))))) (let* ((map (read-text "a - \"Same\". b - \"Same\".")) (topics (topic-map-topics map)) (a (first topics)) (type (second topics)) (na (first (topic-names a))) (nb (first (topic-names (third topics)))) (c (topic-maps.tmdm:merge-topics a type))) (check "default-type-merge-retargets-all-raw-type-references" (and (eq c (topic-maps.tmdm::%topic-name-type na)) (eq c (topic-maps.tmdm::%topic-name-type nb)))) (check "default-type-merge-retargets-parent" (eq c (topic-maps.tmdm::%topic-name-parent na))) (check "self-typed-name-is-valid" (eq (topic-name-parent na) (topic-name-type na))) (check "retained-default-type-handle-resolves" (eq c (topic-maps.tmdm:canonical-topic type))) (check "later-name-reuses-merged-default-type" (eq c (topic-name-type (topic-maps.tmdm:add-default-name a "Later"))))) (let* ((ma (read-text "a.")) (mb (read-text "b.")) (a (first-topic ma)) (b (first-topic mb))) (check "cross-map-merge-rejected" (handler-case (progn (topic-maps.tmdm:merge-topics a b) nil) (error () t))) (check "cross-map-rejection-leaves-both-maps-unchanged" (and (eq a (first-topic ma)) (eq b (first-topic mb)) (= 1 (length (topic-map-topics ma))) (= 1 (length (topic-map-topics mb)))))) (let ((map (topic-maps.tmdm:make-topic-map))) (check "invalid-kind-rejected-before-topic-creation" (handler-case (progn (topic-maps.tmdm:intern-topic map :wrong "urn:x") nil) (error () t))) (check "invalid-kind-leaves-map-empty" (null (topic-map-topics map)))) (let* ((map (read-text "a ; ; ^<#extra>; =.")) (a (first-topic map))) (check "all-identity-tail-categories-one-topic" (= 1 (length (topic-map-topics map)))) (check "tail-subject-identifiers-preserved" (same-locator-set-p (topic-subject-identifiers a) '("urn:x" "urn:y"))) (check "tail-item-identifiers-preserved" (same-locator-set-p (topic-item-identifiers a) '("https://example.invalid/a/doc.ctm#a" "https://example.invalid/a/doc.ctm#extra"))) (check "tail-subject-locator-separate" (equal (topic-subject-locators a) '("urn:r")))) (loop for declarations in '("a . b . c ." "c . b . a ." "b . a . c .") for i from 1 do (let* ((map (read-text (concatenate 'string declarations " a . c ."))) (a (first-topic map))) (check (format nil "ctm-transitive-order-topic-count:~D" i) (= 1 (length (topic-map-topics map)))) (check (format nil "ctm-transitive-order-locators:~D" i) (same-locator-set-p (topic-subject-identifiers a) '("urn:x" "urn:y" "urn:z"))))) (let* ((map (read-text "a - \"Same\". b - \"Same\". a . b ; - \"Later\".")) (a (first-topic map))) (check "ctm-late-merge-removes-duplicate-name" (equal (mapcar #'topic-name-value (topic-names a)) '("Same" "Later"))) (check "ctm-continues-on-merged-parent" (raw-reference-invariants map)) (check "ctm-late-merge-no-extra-items" (= 2 (length (topic-map-topics map))))) (dolist (text '("a ." "a - \"Name\" ." "a - \"Name\".")) (check (concatenate 'string "identity-tail-separator-required:" text) (eq :expected-separator (error-code (rejection text))))) (check "identity-tail-needs-iri-after-marker" (eq :expected-iri (error-code (rejection "a ^local.")))) (let ((condition (rejection "a ex:fake."))) (check "identity-tail-undefined-prefix-specific" (and (typep condition 'undefined-prefix) (eq :undefined-prefix (error-code condition)) (= 2 (error-position condition)) (= 1 (error-line condition)) (= 3 (error-column condition))))) (run-merging-graph-checks) (setf *last-merge-checks* (nreverse *checks*)) (format t "~&CTM-MERGING-SEMANTICS-PASS: ~D checks, 11 pinned semantic cases; no CXTM equality passes.~%" (length *last-merge-checks*)) t)) (defun write-merging-report (pathname) (let ((*checks* nil)) (let ((observations (exercise-merge-corpus))) (with-open-file (out pathname :direction :output :if-exists :supersede :if-does-not-exist :create) (topic-maps.test-foundation::emit-json-value (topic-maps.test-foundation::fields "kind" "semantic-observations-not-cxtm" "passed" (length *last-merge-checks*) "failed" 0 "checks" (topic-maps.test-foundation::json-array *last-merge-checks*) "cases" (topic-maps.test-foundation::json-array (loop for observation in observations collect (topic-maps.test-foundation::fields "id" (getf observation :id) "documentIRI" (getf observation :document-iri) "actualObjects" (model-observation (getf observation :topic-map)))))) out) (terpri out)))))