]> dreyeck.freedombox.rocks Git - topic-maps.git/commitdiff
feat(tmdm): readers for the TMDM 7.2/7.3 relationships; tests for both default name...
authorClaude <noreply@anthropic.com>
Wed, 11 Nov 2026 20:06:29 +0000 (20:06 +0000)
committerClaude <noreply@anthropic.com>
Wed, 11 Nov 2026 20:06:29 +0000 (20:06 +0000)
Add six read-only functions to topic-maps.tmdm that recognize the
type-instance (TMDM 7.2) and supertype-subtype (TMDM 7.3) Associations the
CTM and XTM readers already create and store: type-instance-relationship,
supertype-subtype-relationship, and four Topic readers that return the
Associations in which a Topic is instance, type, subtype or supertype.
An Association is recognized exactly when its type and its two Role types
carry the clause's Subject Identifiers and it has exactly two Roles. The
readers are added API, not a conformance repair: they intern no Topic,
change nothing, evaluate no Scope and infer nothing. Sort names (7.4) are
deferred.

tests/core-relationships.lisp argues each expectation from its clause
(50 checks, eleven original isa/ako inputs).
scripts/check-core-relationships.py counts such Associations in the
unchanged CXTM baselines; it evaluates serialized data and is not a proof
of conformance.

tests/default-name-types.lisp (21 checks) shows that both documented
default name type Subject Identifiers, .../name-type and .../topic-name,
are preserved on import and in CXTM, coexist, and are never equated
automatically. It needs no production change. An explicit merge of the two
type Topics and the unchanged Core Subject set are recorded as a
characterization of current behaviour, not as a rule.

Both suites are registered in topic-maps.asd and run by the offline launcher.
Verified with SBCL 2.4.10: the unchanged baseline passed before these
changes, and both new suites pass. The full launcher has not yet been
re-run with them; the XTM suites were not run.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014ewLGkKYU24V8KWmZKpjGx

scripts/check-core-relationships.py [new file with mode: 0644]
scripts/fresh-sbcl.lisp
scripts/test.sh
src/tmdm/core-relationships.lisp [new file with mode: 0644]
src/tmdm/package.lisp
tests/core-relationships.lisp [new file with mode: 0644]
tests/default-name-types.lisp [new file with mode: 0644]
topic-maps.asd

diff --git a/scripts/check-core-relationships.py b/scripts/check-core-relationships.py
new file mode 100644 (file)
index 0000000..71deff7
--- /dev/null
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+"""Evaluate the stored CXTM baselines of the original isa/ako inputs and compare with the Lisp readers.
+
+This is an independent evaluation of serialized data: it counts, in each unchanged baseline, the
+associations that have the structure of TMDM 7.2 or 7.3. It is not a proof of conformance to the
+standard; the normative expectations are argued clause by clause in tests/core-relationships.lisp.
+"""
+import json
+import pathlib
+import sys
+import xml.etree.ElementTree as ET
+
+ROOT = pathlib.Path(__file__).resolve().parent.parent
+PREFIX = 'http://psi.topicmaps.org/iso13250/model/'
+RULES = {'type_instance': ('type-instance', 'type', 'instance'),
+         'supertype_subtype': ('supertype-subtype', 'supertype', 'subtype')}
+
+
+def count(baseline, rule):
+    relationship, first, second = (PREFIX + local for local in RULES[rule])
+    identifiers = {topic.attrib['number']: {locator.text for locator in topic.findall('subjectIdentifiers/locator')}
+                   for topic in baseline.findall('topic')}
+    found = 0
+    for association in baseline.findall('association'):
+        roles = [identifiers[role.find('type').attrib['topicref']] for role in association.findall('role')]
+        if (relationship in identifiers[association.find('type').attrib['topicref']] and len(roles) == 2
+                and any(first in a and second in b for a, b in (roles, roles[::-1]))):
+            found += 1
+    return found
+
+
+report = json.loads(pathlib.Path(sys.argv[1]).read_text())
+assert len(report['cases']) == 11 and len({case['id'] for case in report['cases']}) == 11
+for case in report['cases']:
+    assert case['id'].startswith('ctm/in/') and case['id'].endswith('.ctm'), case['id']
+    name = pathlib.Path(case['id']).name
+    baseline = ET.parse(ROOT / 'vendor/cxtm/ctm/baseline' / (name + '.cxtm')).getroot()
+    for rule in RULES:
+        assert count(baseline, rule) == case[rule], (case['id'], rule, count(baseline, rule), case[rule])
+print('CORE-RELATIONSHIPS-SERIALIZED-DATA-PASS: 11 original CXTM baselines hold the association counts '
+      'the TMDM 7.2/7.3 readers report; an evaluation of serialized data, not a conformance proof')
index f0bfdee77d6123abc162548afb601ae062bca356..acfab5cea2549bb9362bb364e130dbbd5d7038ee 100644 (file)
 (asdf:test-system "topic-maps/tmdm/normalization-index-tests")
 (asdf:test-system "topic-maps/tmdm/association-index-tests")
 
+;; Both documented default name type identifiers, and the TMDM 7.2/7.3 relationship readers.
+(asdf:test-system "topic-maps/tmdm/default-name-type-tests")
+(asdf:test-system "topic-maps/tmdm/core-relationship-tests")
+(let ((report (uiop:getenv "TOPIC_MAPS_REPORT")))
+  (when report
+    (uiop:symbol-call :topic-maps.core-relationship.tests :write-report
+                      (concatenate 'string report ".core-relationships.json"))))
+
 ;; Optional executable learning example, after all historical stage snapshots.
 (asdf:test-system "topic-maps/project-work-tests")
index f84ecd8030caa9a14680cb9bdc70592654b543e1..34ab76f03396d6d6c5683e475e4938a65a36cc38 100755 (executable)
@@ -31,4 +31,5 @@ if [ -n "${TOPIC_MAPS_REPORT:-}" ]; then
   python3 "$project_root/scripts/check-map-import.py" "$TOPIC_MAPS_REPORT.map-import.json"
   python3 "$project_root/scripts/check-mergemap.py" "$TOPIC_MAPS_REPORT.mergemap.json"
   python3 "$project_root/scripts/check-include.py" "$TOPIC_MAPS_REPORT.include.json"
+  python3 "$project_root/scripts/check-core-relationships.py" "$TOPIC_MAPS_REPORT.core-relationships.json"
 fi
diff --git a/src/tmdm/core-relationships.lisp b/src/tmdm/core-relationships.lisp
new file mode 100644 (file)
index 0000000..8927947
--- /dev/null
@@ -0,0 +1,69 @@
+(in-package #:topic-maps.tmdm)
+
+;;; Readers for the two relationships of TMDM 2006, 7.2 and 7.3.
+;;; They recognize the structure those clauses define and report it; they are added API, not a repair.
+;;; They read stored items only: no Topic is interned, nothing is merged, cached or inferred, and Scope
+;;; is not evaluated. An Association that uses the Core Subject Identifiers without conforming to the
+;;; structural rules is, in the words of both clauses, not considered to represent the relationship.
+
+(defun core-locator (local)
+  (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local))
+
+(defun has-subject-identifier-p (topic iri)
+  ;; The clauses name the [subject identifiers] property; an equal Item Identifier or Subject Locator
+  ;; does not count.
+  (member iri (%topic-subject-identifiers (canonical-topic topic)) :test #'string=))
+
+(defun core-relationship-players (association type first-role second-role)
+  "The players of FIRST-ROLE and SECOND-ROLE when ASSOCIATION conforms to the structural rules shared by
+TMDM 7.2 and 7.3 for the Core Subject locals TYPE, FIRST-ROLE and SECOND-ROLE; otherwise NIL and NIL."
+  (check-type association association)
+  (let* ((association (canonical-association association)) (roles (%association-roles association)))
+    (if (and (has-subject-identifier-p (%association-type association) (core-locator type))
+             (= 2 (length roles)))
+        (flet ((role-of (local)
+                 (find-if (lambda (role) (has-subject-identifier-p (%association-role-type role) (core-locator local)))
+                          roles)))
+          (let ((first (role-of first-role)) (second (role-of second-role)))
+            (if (and first second (not (eq first second)))
+                (values (canonical-topic (%association-role-player first))
+                        (canonical-topic (%association-role-player second)))
+                (values nil nil))))
+        (values nil nil))))
+
+(defun type-instance-relationship (association)
+  "TMDM 7.2. Return (values TYPE INSTANCE), the two player Topics, when ASSOCIATION represents a
+type-instance relationship: its type has the Subject Identifier .../type-instance, it has exactly two
+Roles, one whose type has .../type and one whose type has .../instance. Otherwise (values NIL NIL).
+The Association keeps its Scope; this reader does not interpret it."
+  (core-relationship-players association "type-instance" "type" "instance"))
+
+(defun supertype-subtype-relationship (association)
+  "TMDM 7.3. Return (values SUPERTYPE SUBTYPE), the two player Topics, when ASSOCIATION represents a
+supertype-subtype relationship: its type has the Subject Identifier .../supertype-subtype, it has exactly
+two Roles, one whose type has .../supertype and one whose type has .../subtype. Otherwise (values NIL NIL).
+Only this stated relationship is reported; transitive consequences are not computed."
+  (core-relationship-players association "supertype-subtype" "supertype" "subtype"))
+
+(defun relationships-played (topic reader position)
+  ;; Computed from the Roles the Topic plays (TMDM 5.3.5); no index is kept.
+  (let ((topic (canonical-topic topic)) (found nil))
+    (dolist (role (topic-roles-played topic) (nreverse found))
+      (let ((association (canonical-association (%association-role-parent role))))
+        (unless (member association found :test #'eq)
+          (multiple-value-bind (first second) (funcall reader association)
+            (when (eq topic (ecase position (:first first) (:second second)))
+              (push association found))))))))
+
+(defun topic-type-associations (topic)
+  "The Associations that state a type of TOPIC: TMDM 7.2 relationships in which TOPIC is the instance."
+  (relationships-played topic #'type-instance-relationship :second))
+(defun topic-instance-associations (topic)
+  "The Associations that state an instance of TOPIC: TMDM 7.2 relationships in which TOPIC is the type."
+  (relationships-played topic #'type-instance-relationship :first))
+(defun topic-supertype-associations (topic)
+  "The Associations that state a supertype of TOPIC: TMDM 7.3 relationships in which TOPIC is the subtype."
+  (relationships-played topic #'supertype-subtype-relationship :second))
+(defun topic-subtype-associations (topic)
+  "The Associations that state a subtype of TOPIC: TMDM 7.3 relationships in which TOPIC is the supertype."
+  (relationships-played topic #'supertype-subtype-relationship :first))
index 90d820dbc14510d7f5f7b4a9bfb5d14517f3c10e..e8bb20325242be8f83baab7fe4fa9261cd7cf85f 100644 (file)
@@ -27,4 +27,7 @@
            #:date-datatype #:date-lexical-p #:canonical-date-lexical-form
            #:datetime-datatype #:datetime-lexical-p #:canonical-datetime-lexical-form
            #:occurrence-error #:occurrence-error-code
-           #:add-name #:add-default-name #:default-name-type-identifier))
+           #:add-name #:add-default-name #:default-name-type-identifier
+           #:type-instance-relationship #:supertype-subtype-relationship
+           #:topic-type-associations #:topic-instance-associations
+           #:topic-supertype-associations #:topic-subtype-associations))
diff --git a/tests/core-relationships.lisp b/tests/core-relationships.lisp
new file mode 100644 (file)
index 0000000..cc55f7d
--- /dev/null
@@ -0,0 +1,202 @@
+(defpackage #:topic-maps.core-relationship.tests
+  (:use #:cl #:topic-maps.tmdm)
+  (:import-from #:topic-maps.ctm.tests #:check)
+  (:export #:run-tests #:core-relationship-checks #:write-report))
+(in-package #:topic-maps.core-relationship.tests)
+
+;;; Readers for TMDM 2006, 7.2 (type-instance) and 7.3 (supertype-subtype).
+;;; Each expectation is argued from the clause named in its label or comment. The readers are added
+;;; API over stored items; these tests also show that they change nothing and infer nothing.
+
+(defvar *last-checks* nil)
+(defvar *original-observations* nil)
+(defun core-relationship-checks () *last-checks*)
+(defparameter +document+ "https://example.invalid/a/doc.ctm")
+(defparameter +originals+
+  '("isa-relationship" "isa-relationship-duplicate" "isa-relationship-duplicate2"
+    "isa-relationship-embedded" "isa-relationship-type-qname"
+    "ako-relationship" "ako-relationship-duplicate" "ako-relationship-duplicate2"
+    "ako-relationship-embedded" "ako-relationship-supertype-qname" "ako-merge-away"))
+
+(defun core-iri (local) (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local))
+(defun core (map local) (intern-topic map :subject-identifier (core-iri local)))
+(defun plain (map name) (intern-topic map :subject-identifier (concatenate 'string "urn:test:" name)))
+(defun read-ctm (text)
+  (topic-maps.ctm:deserialize-string text :document-iri +document+ :source-name "core-relationships"))
+(defun cxtm (map) (topic-maps.cxtm:canonicalize map :base-locator +document+))
+(defun local (map id)
+  (find-if (lambda (topic) (member (format nil "~A#~A" +document+ id) (topic-item-identifiers topic) :test #'string=))
+           (topic-map-topics map)))
+(defun pair (reader association) (multiple-value-list (funcall reader association)))
+(defun only (list) (and list (null (rest list)) (first list)))
+(defun same-set-p (a b) (null (set-exclusive-or a b :test #'eq)))
+(defun counts (map)
+  (list (count-if #'type-instance-relationship (topic-map-associations map))
+        (count-if #'supertype-subtype-relationship (topic-map-associations map))))
+
+(defun positive-tests ()
+  ;; 7.2: type .../type-instance, exactly two Roles, one of type .../type, one of type .../instance.
+  (let* ((map (read-ctm "a isa b.")) (a (local map "a")) (b (local map "b"))
+         (association (only (topic-map-associations map))))
+    (check "7.2:isa-is-recognized-with-type-and-instance"
+           (equal (list b a) (pair #'type-instance-relationship association)))
+    (check "7.2:isa-is-not-a-supertype-subtype-relationship"
+           (equal '(nil nil) (pair #'supertype-subtype-relationship association)))
+    (check "7.2:topic-readers-return-the-association"
+           (and (equal (list association) (topic-type-associations a))
+                (equal (list association) (topic-instance-associations b))
+                (null (topic-type-associations b)) (null (topic-instance-associations a))
+                (null (topic-supertype-associations a)) (null (topic-subtype-associations b)))))
+  ;; 7.3: type .../supertype-subtype, exactly two Roles, of types .../supertype and .../subtype.
+  (let* ((map (read-ctm "a ako b.")) (a (local map "a")) (b (local map "b"))
+         (association (only (topic-map-associations map))))
+    (check "7.3:ako-is-recognized-with-supertype-and-subtype"
+           (equal (list b a) (pair #'supertype-subtype-relationship association)))
+    (check "7.3:ako-is-not-a-type-instance-relationship"
+           (equal '(nil nil) (pair #'type-instance-relationship association)))
+    (check "7.3:topic-readers-return-the-association"
+           (and (equal (list association) (topic-supertype-associations a))
+                (equal (list association) (topic-subtype-associations b))
+                (null (topic-supertype-associations b)) (null (topic-subtype-associations a)))))
+  ;; 7.2, last sentence: Scope applies as to any other Association; the reader keeps it reachable.
+  (let* ((map (make-topic-map)) (a (plain map "a")) (b (plain map "b")) (theme (plain map "theme"))
+         (association (make-association map (core map "type-instance")
+                                        (list (list (core map "instance") a) (list (core map "type") b))
+                                        :scope (list theme))))
+    (check "7.2:scoped-association-is-recognized-and-keeps-its-scope"
+           (and (equal (list b a) (pair #'type-instance-relationship association))
+                (equal (list theme) (association-scope (only (topic-type-associations a)))))))
+  ;; [roles] is a set (5.7); the order in which Roles were supplied is not significant.
+  (let* ((map (make-topic-map)) (a (plain map "a")) (b (plain map "b"))
+         (association (make-association map (core map "supertype-subtype")
+                                        (list (list (core map "supertype") b) (list (core map "subtype") a)))))
+    (check "7.3:role-order-is-irrelevant"
+           (equal (list b a) (pair #'supertype-subtype-relationship association))))
+  ;; 7.3 NOTE: loops are allowed and do not imply that the types are the same.
+  (let* ((map (read-ctm "a ako b. b ako a.")) (a (local map "a")) (b (local map "b")))
+    (check "7.3:loop-is-two-relationships-and-two-topics"
+           (and (not (eq (canonical-topic a) (canonical-topic b))) (equal '(0 2) (counts map))
+                (= 1 (length (topic-supertype-associations a))) (= 1 (length (topic-subtype-associations a))))))
+  ;; Nothing in 7.2 forbids one Topic playing both Roles; the two Roles differ in type and stay two (5.8).
+  (let* ((map (read-ctm "a isa a.")) (a (local map "a")) (association (only (topic-map-associations map))))
+    (check "7.2:one-topic-playing-both-roles-is-reported-as-stored"
+           (and (equal (list a a) (pair #'type-instance-relationship association))
+                (equal (list association) (topic-type-associations a))
+                (equal (list association) (topic-instance-associations a))))))
+
+(defun negative-tests ()
+  "Associations that use the Core Subject Identifiers without the structure of 7.2/7.3 are not relationships."
+  (flet ((unrecognized-p (association)
+           (and (equal '(nil nil) (pair #'type-instance-relationship association))
+                (equal '(nil nil) (pair #'supertype-subtype-relationship association)))))
+    (let* ((map (make-topic-map)) (a (plain map "a")) (b (plain map "b")) (c (plain map "c")))
+      (check "7.2:three-roles-are-not-exactly-two"
+             (unrecognized-p (make-association map (core map "type-instance")
+                               (list (list (core map "instance") a) (list (core map "type") b)
+                                     (list (plain map "extra") c)))))
+      (check "7.2:one-role-is-not-exactly-two"
+             (unrecognized-p (make-association map (core map "type-instance") (list (list (core map "instance") a)))))
+      (check "7.2:two-instance-roles-lack-the-type-role"
+             (unrecognized-p (make-association map (core map "type-instance")
+                               (list (list (core map "instance") a) (list (core map "instance") b)))))
+      (check "7.2:association-type-without-the-core-identifier"
+             (unrecognized-p (make-association map (plain map "type-instance")
+                               (list (list (core map "instance") a) (list (core map "type") b)))))
+      (check "7.2:role-types-of-the-other-relationship"
+             (unrecognized-p (make-association map (core map "type-instance")
+                               (list (list (core map "subtype") a) (list (core map "supertype") b)))))
+      (check "7.3:role-types-of-the-other-relationship"
+             (unrecognized-p (make-association map (core map "supertype-subtype")
+                               (list (list (core map "instance") a) (list (core map "type") b)))))
+      (check "7.3:one-role-is-not-exactly-two"
+             (unrecognized-p (make-association map (core map "supertype-subtype") (list (list (core map "subtype") a)))))
+      (check "negative:no-topic-reader-reports-these"
+             (and (null (topic-type-associations a)) (null (topic-instance-associations b))
+                  (null (topic-supertype-associations a)) (null (topic-subtype-associations b)))))
+    ;; The clauses name the [subject identifiers] property. The same string as a Subject Locator or as an
+    ;; Item Identifier is another property.
+    (dolist (kind '(:subject-locator :item-identifier))
+      (let* ((map (make-topic-map)) (a (plain map "a")) (b (plain map "b"))
+             (association (make-association map (intern-topic map kind (core-iri "type-instance"))
+                            (list (list (intern-topic map kind (core-iri "instance")) a)
+                                  (list (intern-topic map kind (core-iri "type")) b)))))
+        (check (format nil "7.2:core-locator-only-as-~(~A~)-is-not-recognized" kind)
+               (and (unrecognized-p association) (null (topic-type-associations a))))))))
+
+(defun merge-tests ()
+  "5.3.5: a Topic whose Item Identifier equals another Topic's Subject Identifier merges with it;
+after that merge the Association's type has the Subject Identifier, and 7.2 holds."
+  (let* ((map (make-topic-map)) (a (plain map "a")) (b (plain map "b"))
+         (type (intern-topic map :item-identifier (core-iri "type-instance")))
+         (association (make-association map type (list (list (core map "instance") a) (list (core map "type") b)))))
+    (check "merge:not-recognized-before" (equal '(nil nil) (pair #'type-instance-relationship association)))
+    (let ((merged (core map "type-instance")))
+      (check "merge:item-and-subject-identifier-merged" (eq merged (canonical-topic type)))
+      (check "merge:recognized-after-through-the-retained-handle"
+             (and (equal (list b a) (pair #'type-instance-relationship association))
+                  (equal (list (canonical-association association)) (topic-type-associations a)))))))
+
+(defun no-inference-tests ()
+  "7.3 does not require Associations for inferred relationships; the readers report stated ones only."
+  (let* ((map (read-ctm "a isa b. b ako c.")) (a (local map "a")) (b (local map "b")) (c (local map "c")))
+    (check "no-inference:instance-of-subtype-is-not-reported-as-instance-of-supertype"
+           (and (equal (list b) (mapcar #'type-instance-relationship (topic-type-associations a)))
+                (null (topic-instance-associations c)))))
+  (let* ((map (read-ctm "a ako b. b ako c.")) (a (local map "a")) (b (local map "b")))
+    (check "no-inference:supertype-subtype-is-not-closed-transitively"
+           (equal (list b) (mapcar #'supertype-subtype-relationship (topic-supertype-associations a))))))
+
+(defun no-mutation-tests ()
+  "The readers intern no Topic and leave every item and the canonical output as they were."
+  (let* ((map (read-ctm "a - \"A\" . r(p: a, q: b)")) (topics (topic-map-topics map))
+         (associations (topic-map-associations map)) (before (cxtm map)))
+    (dolist (association associations)
+      (type-instance-relationship association) (supertype-subtype-relationship association))
+    (dolist (topic topics)
+      (topic-type-associations topic) (topic-instance-associations topic)
+      (topic-supertype-associations topic) (topic-subtype-associations topic))
+    (check "no-mutation:no-core-topic-is-created"
+           (and (same-set-p topics (topic-map-topics map))
+                (notany (lambda (topic) (intersection (topic-subject-identifiers topic) (core-subject-identifiers)
+                                                      :test #'string=))
+                        (topic-map-topics map))))
+    (check "no-mutation:items-and-cxtm-unchanged"
+           (and (same-set-p associations (topic-map-associations map)) (string= before (cxtm map)))))
+  (let* ((map (read-ctm "a isa b. c ako d.")) (before (cxtm map)) (topics (topic-map-topics map)))
+    (dolist (topic topics) (topic-type-associations topic) (topic-subtype-associations topic))
+    (check "no-mutation:reading-relationships-changes-nothing"
+           (and (equal '(1 1) (counts map)) (same-set-p topics (topic-map-topics map)) (string= before (cxtm map))))))
+
+(defun original-tests ()
+  "The eleven original isa/ako inputs: what the readers report for the maps the CTM reader builds."
+  (setf *original-observations* nil)
+  (let ((corpus (topic-maps.test-foundation:discover-corpus)))
+    (dolist (name +originals+)
+      (let* ((id (format nil "ctm/in/~A.ctm" name))
+             (case (first (topic-maps.test-foundation:select-cases corpus :id id)))
+             (map (topic-maps.ctm:deserialize-file (topic-maps.test-foundation:test-case-source-pathname case)
+                    :document-iri (topic-maps.test-foundation:test-case-document-iri case) :source-name id))
+             (counts (counts map)))
+        (check (concatenate 'string id ":at-least-one-relationship") (plusp (+ (first counts) (second counts))))
+        (check (concatenate 'string id ":only-the-keyword's-relationship")
+               (zerop (if (eql 0 (search "isa" name)) (second counts) (first counts))))
+        (push (list id (first counts) (second counts)) *original-observations*))))
+  (setf *original-observations* (nreverse *original-observations*)))
+
+(defun write-report (path)
+  "Counts per original input, for comparison with an evaluation of the stored CXTM baselines."
+  (with-open-file (out path :direction :output :if-exists :supersede)
+    (format out "{\"cases\": [~%")
+    (loop for (id type-instance supertype-subtype) in *original-observations* for first = t then nil
+          do (format out "~:[,~%~;~]  {\"id\": \"~A\", \"type_instance\": ~D, \"supertype_subtype\": ~D}"
+                     first id type-instance supertype-subtype))
+    (format out "~%]}~%"))
+  path)
+
+(defun run-tests ()
+  (let ((topic-maps.ctm.tests::*checks* nil))
+    (positive-tests) (negative-tests) (merge-tests) (no-inference-tests) (no-mutation-tests) (original-tests)
+    (setf *last-checks* (reverse topic-maps.ctm.tests::*checks*))
+    (format t "~&CORE-RELATIONSHIP-PASS: ~D checks; TMDM 7.2 and 7.3 recognized as stated, ~D original inputs, nothing inferred or changed.~%"
+            (length *last-checks*) (length *original-observations*))
+    *last-checks*))
diff --git a/tests/default-name-types.lisp b/tests/default-name-types.lisp
new file mode 100644 (file)
index 0000000..fb2d973
--- /dev/null
@@ -0,0 +1,116 @@
+(defpackage #:topic-maps.default-name-type.tests
+  (:use #:cl #:topic-maps.tmdm)
+  (:import-from #:topic-maps.ctm.tests #:check)
+  (:export #:run-tests #:default-name-type-checks))
+(in-package #:topic-maps.default-name-type.tests)
+
+;;; Two documented default name type Subject Identifiers.
+;;; The TMDM text dated 2006-06-18 spells clause 7.5's identifier .../model/name-type; the text dated
+;;; 2008-06-03 spells it .../model/topic-name, as CTM 3.11 and XTM 2.0 do. Both are supported as different
+;;; identifiers. Nothing here equates them, and nothing here decides which text is authoritative.
+;;; These tests exercise the unchanged model, CTM reader and CXTM canonicalizer; they add no production code.
+
+(defvar *last-checks* nil)
+(defun default-name-type-checks () *last-checks*)
+(defparameter +document+ "https://example.invalid/a/doc.ctm")
+(defparameter +topic-name+ "http://psi.topicmaps.org/iso13250/model/topic-name")
+(defparameter +name-type+ "http://psi.topicmaps.org/iso13250/model/name-type")
+(defparameter +type+ "http://psi.topicmaps.org/iso13250/model/type")
+
+(defun read-ctm (text)
+  (topic-maps.ctm:deserialize-string text :document-iri +document+ :source-name "default-name-types"))
+(defun cxtm (map) (topic-maps.cxtm:canonicalize map :base-locator +document+))
+(defun with-si (map iri)
+  (find-if (lambda (topic) (member iri (topic-subject-identifiers topic) :test #'string=)) (topic-map-topics map)))
+(defun local (map id)
+  (find-if (lambda (topic) (member (format nil "~A#~A" +document+ id) (topic-item-identifiers topic) :test #'string=))
+           (topic-map-topics map)))
+(defun name-types (topic) (mapcar #'topic-name-type (topic-names topic)))
+
+(defun implicit-tests ()
+  "CTM 3.11: a name without a type gets the default name type, spelled .../topic-name."
+  (let* ((map (read-ctm "a - \"N\" .")) (a (local map "a")) (type (first (name-types a))))
+    (check "implicit:ctm-untyped-name-uses-topic-name"
+           (and (= 1 (length (topic-names a))) (equal (list +topic-name+) (topic-subject-identifiers type))))
+    (check "implicit:no-name-type-topic-is-created" (null (with-si map +name-type+)))
+    (check "implicit:identifier-reader-is-unchanged" (string= +topic-name+ (default-name-type-identifier)))))
+
+(defun explicit-tests ()
+  "An explicitly stated name type is kept, whichever of the two identifiers it carries."
+  (let* ((map (read-ctm (format nil "a - <~A> : \"N\" ." +name-type+)))
+         (a (local map "a")) (type (first (name-types a))))
+    (check "explicit:name-type-is-preserved-on-import"
+           (and (= 1 (length (topic-names a))) (equal (list +name-type+) (topic-subject-identifiers type))))
+    (check "explicit:name-type-creates-no-topic-name-topic" (null (with-si map +topic-name+)))
+    (check "explicit:name-type-is-preserved-on-cxtm-export"
+           (and (search +name-type+ (cxtm map)) (not (search +topic-name+ (cxtm map))))))
+  (let* ((map (read-ctm (format nil "a - <~A> : \"N\" ; - \"N\" ." +topic-name+))) (a (local map "a")))
+    ;; Same Subject Identifier, hence the same Topic (TMDM 5.3.5) and equal Names (TMDM 5.4).
+    (check "explicit:topic-name-is-the-implicit-default"
+           (and (= 1 (length (topic-names a))) (eq (with-si map +topic-name+) (first (name-types a)))))))
+
+(defun coexistence-tests ()
+  "Both identifiers in one map stay two Topics; equal-looking Names of the two types stay two Names."
+  (let* ((map (read-ctm (format nil "a - \"N\" ; - <~A> : \"N\" ." +name-type+)))
+         (a (local map "a")) (old (with-si map +name-type+)) (new (with-si map +topic-name+)))
+    (check "coexistence:two-type-topics" (and old new (not (eq old new))))
+    (check "coexistence:two-names" (= 2 (length (topic-names a))))
+    (check "coexistence:each-name-keeps-its-type"
+           (null (set-exclusive-or (list old new) (name-types a) :test #'eq)))
+    (let ((before (cxtm map)))
+      (normalize-topic-map map)
+      (check "coexistence:normalization-equates-nothing"
+             (and (= 2 (length (topic-names a))) (not (eq (canonical-topic old) (canonical-topic new)))
+                  (string= before (cxtm map)))))
+    (check "coexistence:both-identifiers-in-cxtm"
+           (and (search +name-type+ (cxtm map)) (search +topic-name+ (cxtm map))))))
+
+(defun import-tests ()
+  "IMPORT-MAP-CONTENT copies explicit name types of both identifiers unchanged."
+  (let* ((source (read-ctm (format nil "a - \"N\" ; - <~A> : \"M\" ." +name-type+)))
+         (target (make-topic-map)))
+    (import-map-content target source)
+    (let ((a (local target "a")))
+      (check "import:both-type-topics-arrive"
+             (and (with-si target +name-type+) (with-si target +topic-name+)
+                  (not (eq (with-si target +name-type+) (with-si target +topic-name+)))))
+      (check "import:names-keep-their-types"
+             (and (= 2 (length (topic-names a)))
+                  (every (lambda (name)
+                           (equal (topic-subject-identifiers (topic-name-type name))
+                                  (list (if (string= "N" (topic-name-value name)) +topic-name+ +name-type+))))
+                         (topic-names a))))
+      (check "import:cxtm-equal-to-source" (string= (cxtm source) (cxtm target))))))
+
+(defun characterization-tests ()
+  "What the library does today. Not a normative equivalence and not an interoperability rule."
+  ;; An explicit merge of the two type Topics is a caller's own assertion that they have one subject.
+  (let* ((map (read-ctm (format nil "a - \"N\" ; - <~A> : \"N\" ." +name-type+)))
+         (a (local map "a")) (old (with-si map +name-type+)) (new (with-si map +topic-name+))
+         (merged (merge-topics old new)))
+    (check "characterization:explicit-merge-is-accepted"
+           (and (eq merged (canonical-topic old)) (eq merged (canonical-topic new))))
+    (check "characterization:merged-topic-carries-both-identifiers"
+           (null (set-exclusive-or (list +name-type+ +topic-name+) (topic-subject-identifiers merged) :test #'string=)))
+    (check "characterization:names-become-equal-and-merge" (= 1 (length (topic-names a)))))
+  ;; The protected Core Subject set holds .../name-type and not .../topic-name; it is left as it is.
+  (check "characterization:core-set-is-unchanged"
+         (and (member +name-type+ (core-subject-identifiers) :test #'string=)
+              (not (member +topic-name+ (core-subject-identifiers) :test #'string=))
+              (= 8 (length (core-subject-identifiers)))))
+  (let* ((map (make-topic-map)) (topic (intern-topic map :subject-identifier +name-type+)))
+    (check "characterization:name-type-with-another-core-subject-is-rejected"
+           (handler-case (progn (add-topic-identifier topic :subject-identifier +type+) nil)
+             (core-subject-error (c) (eq :core-subject-conflict (core-subject-error-code c))))))
+  (let* ((map (make-topic-map)) (topic (intern-topic map :subject-identifier +topic-name+))
+         (result (add-topic-identifier topic :subject-identifier +type+)))
+    (check "characterization:topic-name-with-another-core-subject-is-accepted-today"
+           (= 2 (length (topic-subject-identifiers result))))))
+
+(defun run-tests ()
+  (let ((topic-maps.ctm.tests::*checks* nil))
+    (implicit-tests) (explicit-tests) (coexistence-tests) (import-tests) (characterization-tests)
+    (setf *last-checks* (reverse topic-maps.ctm.tests::*checks*))
+    (format t "~&DEFAULT-NAME-TYPE-PASS: ~D checks; both identifiers kept apart; XTM path NOT-RUN in this suite.~%"
+            (length *last-checks*))
+    *last-checks*))
index 625c8f45d140bb460d169586b05ca9b49518b633..be9b23c43cb9b4137ebc1ec25dceae7621309c0a 100644 (file)
@@ -29,7 +29,7 @@
   :description "Topics, Names, Variants, Occurrences, Associations and Roles with same-map merging and source-preserving content import"
   :version "0.16.1" :license "MIT" :serial t
   :components ((:file "src/tmdm/package") (:file "src/tmdm/integers") (:file "src/tmdm/decimals") (:file "src/tmdm/dates") (:file "src/tmdm/datetimes") (:file "src/tmdm/items")
-               (:file "src/tmdm/core-subjects") (:file "src/tmdm/item-identifiers") (:file "src/tmdm/reification") (:file "src/tmdm/merging") (:file "src/tmdm/occurrences") (:file "src/tmdm/variants") (:file "src/tmdm/associations") (:file "src/tmdm/import")))
+               (:file "src/tmdm/core-subjects") (:file "src/tmdm/item-identifiers") (:file "src/tmdm/reification") (:file "src/tmdm/merging") (:file "src/tmdm/occurrences") (:file "src/tmdm/variants") (:file "src/tmdm/associations") (:file "src/tmdm/import") (:file "src/tmdm/core-relationships")))
 
 (asdf:defsystem "topic-maps/ctm"
   :description "CTM recursive Topic references, Wildcards, names/variants/associations and supported literals"
   :perform (asdf:test-op (operation component)
              (declare (ignore operation component))
              (uiop:symbol-call :topic-maps.association-index.tests :run-tests)))
+
+(asdf:defsystem "topic-maps/tmdm/default-name-type-tests"
+  :description "Both documented default name type Subject Identifiers: preserved, coexisting, never equated automatically"
+  :depends-on ("topic-maps/tmdm/import-tests") :serial t
+  :components ((:file "tests/default-name-types"))
+  :perform (asdf:test-op (operation component)
+             (declare (ignore operation component))
+             (uiop:symbol-call :topic-maps.default-name-type.tests :run-tests)))
+
+(asdf:defsystem "topic-maps/tmdm/core-relationship-tests"
+  :description "Readers that recognize the type-instance and supertype-subtype Associations of TMDM 7.2 and 7.3"
+  :depends-on ("topic-maps/tmdm/import-tests") :serial t
+  :components ((:file "tests/core-relationships"))
+  :perform (asdf:test-op (operation component)
+             (declare (ignore operation component))
+             (uiop:symbol-call :topic-maps.core-relationship.tests :run-tests)))