]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - tests/core-relationships.lisp
docs(tmdm): coverage matrix, relationship readers, default name types; run report
[topic-maps.git] / tests / core-relationships.lisp
1 (defpackage #:topic-maps.core-relationship.tests
2 (:use #:cl #:topic-maps.tmdm)
3 (:import-from #:topic-maps.ctm.tests #:check)
4 (:export #:run-tests #:core-relationship-checks #:write-report))
5 (in-package #:topic-maps.core-relationship.tests)
6
7 ;;; Readers for TMDM 2006, 7.2 (type-instance) and 7.3 (supertype-subtype).
8 ;;; Each expectation is argued from the clause named in its label or comment. The readers are added
9 ;;; API over stored items; these tests also show that they change nothing and infer nothing.
10
11 (defvar *last-checks* nil)
12 (defvar *original-observations* nil)
13 (defun core-relationship-checks () *last-checks*)
14 (defparameter +document+ "https://example.invalid/a/doc.ctm")
15 (defparameter +originals+
16 '("isa-relationship" "isa-relationship-duplicate" "isa-relationship-duplicate2"
17 "isa-relationship-embedded" "isa-relationship-type-qname"
18 "ako-relationship" "ako-relationship-duplicate" "ako-relationship-duplicate2"
19 "ako-relationship-embedded" "ako-relationship-supertype-qname" "ako-merge-away"))
20
21 (defun core-iri (local) (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local))
22 (defun core (map local) (intern-topic map :subject-identifier (core-iri local)))
23 (defun plain (map name) (intern-topic map :subject-identifier (concatenate 'string "urn:test:" name)))
24 (defun read-ctm (text)
25 (topic-maps.ctm:deserialize-string text :document-iri +document+ :source-name "core-relationships"))
26 (defun cxtm (map) (topic-maps.cxtm:canonicalize map :base-locator +document+))
27 (defun local (map id)
28 (find-if (lambda (topic) (member (format nil "~A#~A" +document+ id) (topic-item-identifiers topic) :test #'string=))
29 (topic-map-topics map)))
30 (defun pair (reader association) (multiple-value-list (funcall reader association)))
31 (defun only (list) (and list (null (rest list)) (first list)))
32 (defun same-set-p (a b) (null (set-exclusive-or a b :test #'eq)))
33 (defun counts (map)
34 (list (count-if #'type-instance-relationship (topic-map-associations map))
35 (count-if #'supertype-subtype-relationship (topic-map-associations map))))
36
37 (defun positive-tests ()
38 ;; 7.2: type .../type-instance, exactly two Roles, one of type .../type, one of type .../instance.
39 (let* ((map (read-ctm "a isa b.")) (a (local map "a")) (b (local map "b"))
40 (association (only (topic-map-associations map))))
41 (check "7.2:isa-is-recognized-with-type-and-instance"
42 (equal (list b a) (pair #'type-instance-relationship association)))
43 (check "7.2:isa-is-not-a-supertype-subtype-relationship"
44 (equal '(nil nil) (pair #'supertype-subtype-relationship association)))
45 (check "7.2:topic-readers-return-the-association"
46 (and (equal (list association) (topic-type-associations a))
47 (equal (list association) (topic-instance-associations b))
48 (null (topic-type-associations b)) (null (topic-instance-associations a))
49 (null (topic-supertype-associations a)) (null (topic-subtype-associations b)))))
50 ;; 7.3: type .../supertype-subtype, exactly two Roles, of types .../supertype and .../subtype.
51 (let* ((map (read-ctm "a ako b.")) (a (local map "a")) (b (local map "b"))
52 (association (only (topic-map-associations map))))
53 (check "7.3:ako-is-recognized-with-supertype-and-subtype"
54 (equal (list b a) (pair #'supertype-subtype-relationship association)))
55 (check "7.3:ako-is-not-a-type-instance-relationship"
56 (equal '(nil nil) (pair #'type-instance-relationship association)))
57 (check "7.3:topic-readers-return-the-association"
58 (and (equal (list association) (topic-supertype-associations a))
59 (equal (list association) (topic-subtype-associations b))
60 (null (topic-supertype-associations b)) (null (topic-subtype-associations a)))))
61 ;; 7.2, last sentence: Scope applies as to any other Association; the reader keeps it reachable.
62 (let* ((map (make-topic-map)) (a (plain map "a")) (b (plain map "b")) (theme (plain map "theme"))
63 (association (make-association map (core map "type-instance")
64 (list (list (core map "instance") a) (list (core map "type") b))
65 :scope (list theme))))
66 (check "7.2:scoped-association-is-recognized-and-keeps-its-scope"
67 (and (equal (list b a) (pair #'type-instance-relationship association))
68 (equal (list theme) (association-scope (only (topic-type-associations a)))))))
69 ;; [roles] is a set (5.7); the order in which Roles were supplied is not significant.
70 (let* ((map (make-topic-map)) (a (plain map "a")) (b (plain map "b"))
71 (association (make-association map (core map "supertype-subtype")
72 (list (list (core map "supertype") b) (list (core map "subtype") a)))))
73 (check "7.3:role-order-is-irrelevant"
74 (equal (list b a) (pair #'supertype-subtype-relationship association))))
75 ;; 7.3 NOTE: loops are allowed and do not imply that the types are the same.
76 (let* ((map (read-ctm "a ako b. b ako a.")) (a (local map "a")) (b (local map "b")))
77 (check "7.3:loop-is-two-relationships-and-two-topics"
78 (and (not (eq (canonical-topic a) (canonical-topic b))) (equal '(0 2) (counts map))
79 (= 1 (length (topic-supertype-associations a))) (= 1 (length (topic-subtype-associations a))))))
80 ;; Nothing in 7.2 forbids one Topic playing both Roles; the two Roles differ in type and stay two (5.8).
81 (let* ((map (read-ctm "a isa a.")) (a (local map "a")) (association (only (topic-map-associations map))))
82 (check "7.2:one-topic-playing-both-roles-is-reported-as-stored"
83 (and (equal (list a a) (pair #'type-instance-relationship association))
84 (equal (list association) (topic-type-associations a))
85 (equal (list association) (topic-instance-associations a))))))
86
87 (defun negative-tests ()
88 "Associations that use the Core Subject Identifiers without the structure of 7.2/7.3 are not relationships."
89 (flet ((unrecognized-p (association)
90 (and (equal '(nil nil) (pair #'type-instance-relationship association))
91 (equal '(nil nil) (pair #'supertype-subtype-relationship association)))))
92 (let* ((map (make-topic-map)) (a (plain map "a")) (b (plain map "b")) (c (plain map "c")))
93 (check "7.2:three-roles-are-not-exactly-two"
94 (unrecognized-p (make-association map (core map "type-instance")
95 (list (list (core map "instance") a) (list (core map "type") b)
96 (list (plain map "extra") c)))))
97 (check "7.2:one-role-is-not-exactly-two"
98 (unrecognized-p (make-association map (core map "type-instance") (list (list (core map "instance") a)))))
99 (check "7.2:two-instance-roles-lack-the-type-role"
100 (unrecognized-p (make-association map (core map "type-instance")
101 (list (list (core map "instance") a) (list (core map "instance") b)))))
102 (check "7.2:association-type-without-the-core-identifier"
103 (unrecognized-p (make-association map (plain map "type-instance")
104 (list (list (core map "instance") a) (list (core map "type") b)))))
105 (check "7.2:role-types-of-the-other-relationship"
106 (unrecognized-p (make-association map (core map "type-instance")
107 (list (list (core map "subtype") a) (list (core map "supertype") b)))))
108 (check "7.3:role-types-of-the-other-relationship"
109 (unrecognized-p (make-association map (core map "supertype-subtype")
110 (list (list (core map "instance") a) (list (core map "type") b)))))
111 (check "7.3:one-role-is-not-exactly-two"
112 (unrecognized-p (make-association map (core map "supertype-subtype") (list (list (core map "subtype") a)))))
113 (check "negative:no-topic-reader-reports-these"
114 (and (null (topic-type-associations a)) (null (topic-instance-associations b))
115 (null (topic-supertype-associations a)) (null (topic-subtype-associations b)))))
116 ;; The clauses name the [subject identifiers] property. The same string as a Subject Locator or as an
117 ;; Item Identifier is another property.
118 (dolist (kind '(:subject-locator :item-identifier))
119 (let* ((map (make-topic-map)) (a (plain map "a")) (b (plain map "b"))
120 (association (make-association map (intern-topic map kind (core-iri "type-instance"))
121 (list (list (intern-topic map kind (core-iri "instance")) a)
122 (list (intern-topic map kind (core-iri "type")) b)))))
123 (check (format nil "7.2:core-locator-only-as-~(~A~)-is-not-recognized" kind)
124 (and (unrecognized-p association) (null (topic-type-associations a))))))))
125
126 (defun merge-tests ()
127 "5.3.5: a Topic whose Item Identifier equals another Topic's Subject Identifier merges with it;
128 after that merge the Association's type has the Subject Identifier, and 7.2 holds."
129 (let* ((map (make-topic-map)) (a (plain map "a")) (b (plain map "b"))
130 (type (intern-topic map :item-identifier (core-iri "type-instance")))
131 (association (make-association map type (list (list (core map "instance") a) (list (core map "type") b)))))
132 (check "merge:not-recognized-before" (equal '(nil nil) (pair #'type-instance-relationship association)))
133 (let ((merged (core map "type-instance")))
134 (check "merge:item-and-subject-identifier-merged" (eq merged (canonical-topic type)))
135 (check "merge:recognized-after-through-the-retained-handle"
136 (and (equal (list b a) (pair #'type-instance-relationship association))
137 (equal (list (canonical-association association)) (topic-type-associations a)))))))
138
139 (defun no-inference-tests ()
140 "7.3 does not require Associations for inferred relationships; the readers report stated ones only."
141 (let* ((map (read-ctm "a isa b. b ako c.")) (a (local map "a")) (b (local map "b")) (c (local map "c")))
142 (check "no-inference:instance-of-subtype-is-not-reported-as-instance-of-supertype"
143 (and (equal (list b) (mapcar #'type-instance-relationship (topic-type-associations a)))
144 (null (topic-instance-associations c)))))
145 (let* ((map (read-ctm "a ako b. b ako c.")) (a (local map "a")) (b (local map "b")))
146 (check "no-inference:supertype-subtype-is-not-closed-transitively"
147 (equal (list b) (mapcar #'supertype-subtype-relationship (topic-supertype-associations a))))))
148
149 (defun no-mutation-tests ()
150 "The readers intern no Topic and leave every item and the canonical output as they were."
151 (let* ((map (read-ctm "a - \"A\" . r(p: a, q: b)")) (topics (topic-map-topics map))
152 (associations (topic-map-associations map)) (before (cxtm map)))
153 (dolist (association associations)
154 (type-instance-relationship association) (supertype-subtype-relationship association))
155 (dolist (topic topics)
156 (topic-type-associations topic) (topic-instance-associations topic)
157 (topic-supertype-associations topic) (topic-subtype-associations topic))
158 (check "no-mutation:no-core-topic-is-created"
159 (and (same-set-p topics (topic-map-topics map))
160 (notany (lambda (topic) (intersection (topic-subject-identifiers topic) (core-subject-identifiers)
161 :test #'string=))
162 (topic-map-topics map))))
163 (check "no-mutation:items-and-cxtm-unchanged"
164 (and (same-set-p associations (topic-map-associations map)) (string= before (cxtm map)))))
165 (let* ((map (read-ctm "a isa b. c ako d.")) (before (cxtm map)) (topics (topic-map-topics map)))
166 (dolist (topic topics) (topic-type-associations topic) (topic-subtype-associations topic))
167 (check "no-mutation:reading-relationships-changes-nothing"
168 (and (equal '(1 1) (counts map)) (same-set-p topics (topic-map-topics map)) (string= before (cxtm map))))))
169
170 (defun original-tests ()
171 "The eleven original isa/ako inputs: what the readers report for the maps the CTM reader builds."
172 (setf *original-observations* nil)
173 (let ((corpus (topic-maps.test-foundation:discover-corpus)))
174 (dolist (name +originals+)
175 (let* ((id (format nil "ctm/in/~A.ctm" name))
176 (case (first (topic-maps.test-foundation:select-cases corpus :id id)))
177 (map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
178 :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id))
179 (counts (counts map)))
180 (check (concatenate 'string id ":at-least-one-relationship") (plusp (+ (first counts) (second counts))))
181 (check (concatenate 'string id ":only-the-keyword's-relationship")
182 (zerop (if (eql 0 (search "isa" name)) (second counts) (first counts))))
183 (push (list id (first counts) (second counts)) *original-observations*))))
184 (setf *original-observations* (nreverse *original-observations*)))
185
186 (defun write-report (path)
187 "Counts per original input, for comparison with an evaluation of the stored CXTM baselines."
188 (with-open-file (out path :direction :output :if-exists :supersede)
189 (format out "{\"cases\": [~%")
190 (loop for (id type-instance supertype-subtype) in *original-observations* for first = t then nil
191 do (format out "~:[,~%~;~] {\"id\": \"~A\", \"type_instance\": ~D, \"supertype_subtype\": ~D}"
192 first id type-instance supertype-subtype))
193 (format out "~%]}~%"))
194 path)
195
196 (defun run-tests ()
197 (let ((topic-maps.ctm.tests::*checks* nil))
198 (positive-tests) (negative-tests) (merge-tests) (no-inference-tests) (no-mutation-tests) (original-tests)
199 (setf *last-checks* (reverse topic-maps.ctm.tests::*checks*))
200 (format t "~&CORE-RELATIONSHIP-PASS: ~D checks; TMDM 7.2 and 7.3 recognized as stated, ~D original inputs, nothing inferred or changed.~%"
201 (length *last-checks*) (length *original-observations*))
202 *last-checks*))