(in-package #:topic-maps.ctm.tests) (defvar *last-association-checks* nil) (defparameter *association-files* '("association.ctm" "association-binary.ctm" "association-binary-duplicate.ctm" "association-type-sid.ctm" "association-with-comment.ctm" "association-with-comment2.ctm" "association-with-comment3.ctm" "association-with-comment4.ctm")) (defun association-invariants (map) (let* ((topics (topic-map-topics map)) (associations (topic-maps.tmdm:topic-map-associations map)) (roles (loop for a in associations append (topic-maps.tmdm:association-roles a)))) (and (raw-reference-invariants map) (every (lambda (a) (and (eq a (topic-maps.tmdm:canonical-association a)) (eq map (topic-maps.tmdm::%association-parent a)) (member (topic-maps.tmdm::%association-type a) topics :test #'eq) (topic-maps.tmdm:association-roles a) (every (lambda (r) (and (eq r (topic-maps.tmdm:canonical-association-role r)) (eq a (topic-maps.tmdm::%association-role-parent r)) (member (topic-maps.tmdm::%association-role-type r) topics :test #'eq) (member (topic-maps.tmdm::%association-role-player r) topics :test #'eq))) (topic-maps.tmdm:association-roles a)))) associations) (= (length roles) (length (remove-duplicates roles :test #'eq))) (every (lambda (topic) (let ((played (topic-maps.tmdm:topic-roles-played topic)) (expected (remove-if-not (lambda (r) (eq topic (topic-maps.tmdm::%association-role-player r))) roles))) (and (= (length played) (length expected)) (= (length played) (length (remove-duplicates played :test #'eq))) (every (lambda (r) (member r expected :test #'eq)) played) (every (lambda (r) (member r played :test #'eq)) expected)))) topics)))) (defun exercise-association-corpus () (let ((corpus (topic-maps.test-foundation:discover-corpus)) (observations nil)) (dolist (file *association-files*) (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)) (binary (member file '("association-binary.ctm" "association-binary-duplicate.ctm") :test #'string=)) (association (first (topic-maps.tmdm:topic-map-associations map)))) (check (concatenate 'string id ":topic-count") (= (length (topic-map-topics map)) (if binary 5 3))) (check (concatenate 'string id ":association-count") (= 1 (length (topic-maps.tmdm:topic-map-associations map)))) (check (concatenate 'string id ":role-count") (= (length (topic-maps.tmdm:association-roles association)) (if binary 2 1))) (check (concatenate 'string id ":actual-information-items") (and (typep association 'topic-maps.tmdm:association) (every (lambda (r) (typep r 'topic-maps.tmdm:association-role)) (topic-maps.tmdm:association-roles association)))) (check (concatenate 'string id ":raw-references-and-roles-played") (association-invariants map)) (check (concatenate 'string id ":no-unwarranted-name-items") (every (lambda (topic) (null (topic-names topic))) (topic-map-topics map))) (check (concatenate 'string id ":canonical-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 api-topic (map name) (topic-maps.tmdm:intern-topic map :item-identifier (concatenate 'string "urn:" name))) (defun run-association-tests () (let ((*checks* nil)) (exercise-association-corpus) ;; Acceptance matrix: every nonempty subset of TYPE, ROLE TYPE and PLAYER ;; refers to A; B's later identity causes A/B replacement after construction. (loop for mask from 1 to 7 do (let* ((map (topic-maps.tmdm:make-topic-map)) (a (api-topic map "a")) (b (api-topic map "b")) (x (api-topic map "x")) (y (api-topic map "y")) (z (api-topic map "z")) (at (if (logbitp 0 mask) a x)) (rt (if (logbitp 1 mask) a y)) (p (if (logbitp 2 mask) a z)) (association (topic-maps.tmdm:make-association map at (list (list rt p)))) (role (first (topic-maps.tmdm:association-roles association)))) (topic-maps.tmdm:add-topic-identifier b :subject-identifier "urn:shared") (let ((c (topic-maps.tmdm:add-topic-identifier a :subject-identifier "urn:shared"))) (check (format nil "acceptance-type-reference:~D" mask) (eq (if (logbitp 0 mask) c x) (topic-maps.tmdm::%association-type association))) (check (format nil "acceptance-role-type-reference:~D" mask) (eq (if (logbitp 1 mask) c y) (topic-maps.tmdm::%association-role-type role))) (check (format nil "acceptance-player-reference:~D" mask) (eq (if (logbitp 2 mask) c z) (topic-maps.tmdm::%association-role-player role))) (check (format nil "acceptance-retained-handles:~D" mask) (and (eq association (first (topic-maps.tmdm:topic-map-associations map))) (eq role (first (topic-maps.tmdm:association-roles association))) (eq c (topic-maps.tmdm:canonical-topic a)) (eq c (topic-maps.tmdm:canonical-topic b)))) (check (format nil "acceptance-model-integrity:~D" mask) (association-invariants map)) (check (format nil "acceptance-inverse-through-old-topic:~D" mask) (equal (topic-maps.tmdm:topic-roles-played a) (topic-maps.tmdm:topic-roles-played c)))))) (let* ((map (read-text "a - \"Name\".")) (a (first-topic map)) (type (second (topic-map-topics map))) (name (first (topic-names a))) (association (topic-maps.tmdm:make-association map type (list (list a type)))) (role (first (topic-maps.tmdm:association-roles association))) (c (topic-maps.tmdm:merge-topics a type))) (check "combined-name-parent-reference" (eq c (topic-maps.tmdm::%topic-name-parent name))) (check "combined-name-type-reference" (eq c (topic-maps.tmdm::%topic-name-type name))) (check "combined-association-type-reference" (eq c (topic-maps.tmdm::%association-type association))) (check "combined-role-type-and-player-references" (and (eq c (topic-maps.tmdm::%association-role-type role)) (eq c (topic-maps.tmdm::%association-role-player role)))) (check "combined-information-item-integrity" (and (= 1 (length (topic-map-topics map))) (= 1 (length (topic-names c))) (= 1 (length (topic-maps.tmdm:topic-map-associations map))) (association-invariants map)))) (let* ((map (read-text "a(r: a) b.")) (a (first-topic map)) (r (second (topic-map-topics map))) (b (third (topic-map-topics map))) (association (first (topic-maps.tmdm:topic-map-associations map))) (role (first (topic-maps.tmdm:association-roles association)))) ;; All three reference properties converge to one Topic through two merges. (let ((ab (topic-maps.tmdm:merge-topics a b))) (let ((c (topic-maps.tmdm:merge-topics ab r))) (check "association-transitive-three-reference-kinds" (and (eq c (topic-maps.tmdm:association-type association)) (eq c (topic-maps.tmdm:association-role-type role)) (eq c (topic-maps.tmdm:association-role-player role)))) (check "self-referential-association-no-extra-items" (= 1 (length (topic-map-topics map)))) (check "self-referential-association-raw-invariants" (association-invariants map))))) (dolist (text '("a(r: p1, r: p2)" "a(r1: p, r2: p)")) (let* ((map (read-text text)) (association (first (topic-maps.tmdm:topic-map-associations map))) (roles (topic-maps.tmdm:association-roles association)) (ra (first roles)) (rb (second roles)) (player-change (string= text "a(r: p1, r: p2)"))) (topic-maps.tmdm:merge-topics (if player-change (topic-maps.tmdm:association-role-player ra) (topic-maps.tmdm:association-role-type ra)) (if player-change (topic-maps.tmdm:association-role-player rb) (topic-maps.tmdm:association-role-type rb))) (let ((role (first (topic-maps.tmdm:association-roles association)))) (check (concatenate 'string "late-role-merge-new-item:" text) (and (= 1 (length (topic-maps.tmdm:association-roles association))) (not (eq role ra)) (not (eq role rb)))) (check (concatenate 'string "late-role-merge-handles:" text) (and (eq role (topic-maps.tmdm:canonical-association-role ra)) (eq role (topic-maps.tmdm:canonical-association-role rb)))) (check (concatenate 'string "late-role-merge-inverse:" text) (= 1 (length (topic-maps.tmdm:topic-roles-played (topic-maps.tmdm:association-role-player role))))) (check (concatenate 'string "late-role-merge-integrity:" text) (association-invariants map))))) (let* ((map (read-text "a(r: p) b(r: p)")) (associations (topic-maps.tmdm:topic-map-associations map)) (aa (first associations)) (ab (second associations)) (ra (first (topic-maps.tmdm:association-roles aa))) (rb (first (topic-maps.tmdm:association-roles ab)))) (check "equal-role-signatures-different-parents-stay-separate" (and (not (eq ra rb)) (= 2 (length (topic-maps.tmdm:topic-roles-played (topic-maps.tmdm:association-role-player ra)))))) (let ((type (topic-maps.tmdm:merge-topics (topic-maps.tmdm:association-type aa) (topic-maps.tmdm:association-type ab)))) (let ((c (first (topic-maps.tmdm:topic-map-associations map)))) (check "late-association-merge-new-item" (and (= 1 (length (topic-maps.tmdm:topic-map-associations map))) (not (eq c aa)) (not (eq c ab)))) (check "retained-association-handles-resolve" (and (eq c (topic-maps.tmdm:canonical-association aa)) (eq c (topic-maps.tmdm:canonical-association ab)))) (check "retained-role-parents-after-association-merge" (and (eq c (topic-maps.tmdm:association-role-parent ra)) (eq c (topic-maps.tmdm:association-role-parent rb)))) (check "discarded-role-handle-resolves-to-live-role" (eq (topic-maps.tmdm:canonical-association-role ra) (topic-maps.tmdm:canonical-association-role rb))) (let* ((next (api-topic map "next")) (merged (topic-maps.tmdm:merge-topics type next))) (check "obsolete-association-getters-follow-further-topic-merges" (and (eq merged (topic-maps.tmdm:association-type aa)) (eq merged (topic-maps.tmdm:association-type ab))))) (check "late-association-merge-raw-invariants" (association-invariants map))))) (let ((map (read-text "a(r1: p1, r2: p2) a(r2: p2, r1: p1)"))) (check "association-equality-ignores-role-order" (= 1 (length (topic-maps.tmdm:topic-map-associations map)))) (check "role-order-inverse-and-parent-integrity" (association-invariants map))) (let* ((map (read-text "a(r: p, r: p)")) (association (first (topic-maps.tmdm:topic-map-associations map)))) (check "duplicate-role-in-binary-declaration-normalized" (= 1 (length (topic-maps.tmdm:association-roles association))))) (let* ((map (read-text "a. b.")) (a (first-topic map)) (b (second (topic-map-topics map))) (c (topic-maps.tmdm:merge-topics a b)) (association (topic-maps.tmdm:make-association map a (list (list b a)))) (role (first (topic-maps.tmdm:association-roles association)))) (check "constructor-resolves-obsolete-topic-handles" (and (eq c (topic-maps.tmdm::%association-type association)) (eq c (topic-maps.tmdm::%association-role-type role)) (eq c (topic-maps.tmdm::%association-role-player role))))) (let* ((map (read-text "a.")) (other (read-text "b.")) (a (first-topic map)) (b (first-topic other))) (loop for fixture in (list (list b (list (list a a))) (list a (list (list b a))) (list a (list (list a b))) (list a nil)) for i from 1 do (check (format nil "constructor-rejects-invalid-map-or-empty-role:~D" i) (handler-case (progn (topic-maps.tmdm:make-association map (first fixture) (second fixture)) nil) (error () t)))) (check "invalid-construction-is-atomic" (and (null (topic-maps.tmdm:topic-map-associations map)) (null (topic-maps.tmdm:topic-map-associations other))))) (dolist (text '("a()" "a(r: )" "a(r p)" "a(r: p" "a(r: p,)" "a(r: p s: q)" "a(:)")) (check (concatenate 'string "malformed-association-is-rejection:" text) (typep (rejection text) 'ctm-rejection))) (check "role-separator-needs-layout" (eq :role-separator-whitespace (error-code (rejection "a(:)")))) (let ((condition (rejection (format nil "a(r p)")))) (check "role-syntax-diagnostic-position" (and (eq :expected-role-type-separator (error-code condition)) (= 4 (error-position condition)) (= 1 (error-line condition)) (= 5 (error-column condition))))) (let ((condition (rejection "a(r: ex:p)"))) (check "association-undefined-prefix-remains-specific" (and (typep condition 'undefined-prefix) (eq :undefined-prefix (error-code condition))))) (check "scoped-association-now-deserialized" (= 1 (length (topic-maps.tmdm:association-scope (first (topic-maps.tmdm:topic-map-associations (read-text "a(r: p) @theme"))))))) (dolist (text '("a(r: p) ~reifier" "a(r: p, s: q, t: z)")) (check (concatenate 'string "association-feature-boundary:" text) (association-invariants (read-text text)))) (check "comment-can-separate-role-colon" (association-invariants (read-text "a(r#( separator )#:p)"))) (check "delimited-iri-role-references" (association-invariants (read-text "(: )"))) (run-association-graph-checks) (setf *last-association-checks* (nreverse *checks*)) (format t "~&CTM-ASSOCIATIONS-SEMANTICS-PASS: ~D checks, 8 pinned cases; no CXTM equality passes.~%" (length *last-association-checks*)) t))