1 (in-package #:topic-maps.tmdm)
3 (defun identifiers (topic kind)
5 (:item-identifier (%topic-item-identifiers topic))
6 (:subject-identifier (%topic-subject-identifiers topic))
7 (:subject-locator (%topic-subject-locators topic))))
9 (defun identifier-matches-p (topic kind iri)
10 ;; One identity rule for lookup and merging: TMDM 5.3.5, excluding reification.
11 (some (lambda (property) (member iri (identifiers topic property) :test #'string=))
13 ((:item-identifier :subject-identifier) '(:item-identifier :subject-identifier))
14 (:subject-locator '(:subject-locator)))))
16 (defun equal-topics-p (a b)
18 (some (lambda (iri) (identifier-matches-p a kind iri)) (identifiers b kind)))
19 '(:item-identifier :subject-identifier :subject-locator)))
21 (defun set-union (a b test)
22 (remove-duplicates (append a b) :test test :from-end t))
24 (defun replace-pair (items a b replacement)
25 ;; Set ordering is not normative; retain the first replaced position.
26 (loop with inserted = nil for item in items
27 append (if (or (eq item a) (eq item b))
28 (unless inserted (setf inserted t) (list replacement))
31 (defun equal-pair (items predicate)
32 (loop for tail on items do
33 (let ((other (find-if (lambda (item) (funcall predicate (first tail) item)) (rest tail))))
34 (when other (return (list (first tail) other))))))
36 (defun normalize-names (map)
37 ;; TMDM 5.4 and 6.3: all supported names have empty scope/variants/II,
38 ;; null reifier. Parent and type may have changed during a Topic merge.
39 (dolist (topic (%topic-map-topics map))
40 (loop for pair = (equal-pair (%topic-names topic)
42 (and (string= (%topic-name-value a) (%topic-name-value b))
43 (eq (%topic-name-type a) (%topic-name-type b)))))
45 (let* ((a (first pair)) (b (second pair))
46 (c (%make-topic-name (%topic-name-value a) (%topic-name-type a) topic)))
47 (setf (%topic-names topic) (replace-pair (%topic-names topic) a b c)
48 (%topic-name-forward a) c (%topic-name-forward b) c)))))
50 (defun merge-two-topics (map a b)
51 ;; TMDM 6.2: create C, union supported properties, replace model references.
52 ;; Scope/reifiers remain unimplemented; Association/Role refs are materialized.
53 (let ((c (%make-topic map)))
54 (setf (%topic-item-identifiers c) (set-union (%topic-item-identifiers a) (%topic-item-identifiers b) #'string=)
55 (%topic-subject-identifiers c) (set-union (%topic-subject-identifiers a) (%topic-subject-identifiers b) #'string=)
56 (%topic-subject-locators c) (set-union (%topic-subject-locators a) (%topic-subject-locators b) #'string=)
57 (%topic-names c) (set-union (%topic-names a) (%topic-names b) #'eq)
58 (%topic-occurrences c) (set-union (%topic-occurrences a) (%topic-occurrences b) #'eq)
59 (%topic-map-topics map) (replace-pair (%topic-map-topics map) a b c)
60 (%topic-forward a) c (%topic-forward b) c)
61 (dolist (topic (%topic-map-topics map))
62 (dolist (name (%topic-names topic))
63 ;; Parent is the computed containing topic (TMDM 5.4).
64 (setf (%topic-name-parent name) topic
65 (%topic-name-type name) (canonical-topic (%topic-name-type name)))))
66 (dolist (topic (%topic-map-topics map))
67 (dolist (occurrence (%topic-occurrences topic))
68 (setf (%occurrence-parent occurrence) topic
69 (%occurrence-type occurrence) (canonical-topic (%occurrence-type occurrence)))))
70 (dolist (association (%topic-map-associations map))
71 (setf (%association-type association) (canonical-topic (%association-type association)))
72 (dolist (role (%association-roles association))
73 (setf (%association-role-type role) (canonical-topic (%association-role-type role))
74 (%association-role-player role) (canonical-topic (%association-role-player role)))))
76 (normalize-occurrences map)
77 (normalize-associations map)
80 (defun normalize-topic-map (map)
81 "Apply same-map identity merging, then remove equal Names, Occurrences, Roles and Associations.
82 Only implemented properties participate; no cross-map merging."
83 (check-type map topic-map)
84 (loop for pair = (equal-pair (%topic-map-topics map) #'equal-topics-p)
85 while pair do (merge-two-topics map (first pair) (second pair)))
87 (normalize-occurrences map)
88 (normalize-associations map)
91 (defun live-topic (topic)
92 (let ((current (canonical-topic topic)))
93 (unless (member current (%topic-map-topics (%topic-parent current)) :test #'eq)
94 (error "Topic is not a live member of its map."))
97 (defun merge-topics (a b)
98 "Merge two Topics in the same map, returning their current representative.
99 Explicit merging may assert shared subject identity beyond locator equality.
100 Cross-map merging is rejected before mutation. Obsolete handles remain resolvable."
101 (let* ((a (live-topic a)) (b (live-topic b)) (map (%topic-parent a)))
102 (unless (eq map (%topic-parent b)) (error "Cross-map Topic merging is outside this API."))
103 (unless (eq a b) (setf a (merge-two-topics map a b)))
104 (normalize-topic-map map)
105 (canonical-topic a)))
107 (defun add-topic-identifier (topic kind iri)
108 "Add a locator to its distinct identity property and perform required merging.
109 KIND is :ITEM-IDENTIFIER, :SUBJECT-IDENTIFIER or :SUBJECT-LOCATOR.
110 IRI is an already resolved, nonempty absolute locator string; CTM validates it."
111 (unless (and (stringp iri) (plusp (length iri))) (error "A nonempty locator string is required."))
112 (let ((topic (live-topic topic)))
113 (check-topic-occurrence-identifiers (%topic-parent topic) kind iri)
115 (:item-identifier (pushnew iri (%topic-item-identifiers topic) :test #'string=))
116 (:subject-identifier (pushnew iri (%topic-subject-identifiers topic) :test #'string=))
117 (:subject-locator (pushnew iri (%topic-subject-locators topic) :test #'string=)))
118 (normalize-topic-map (%topic-parent topic))
119 (canonical-topic topic)))
121 (defun intern-topic (map kind iri)
122 "Lookup/create a Topic using the same identity rule as same-map merging."
123 (check-type map topic-map)
124 (unless (and (stringp iri) (plusp (length iri))) (error "A nonempty locator string is required."))
125 ;; Validate the category even for an empty map, before adding a Topic.
126 (ecase kind ((:item-identifier :subject-identifier :subject-locator)))
127 (check-topic-occurrence-identifiers map kind iri)
128 (let ((topic (or (find-if (lambda (topic) (identifier-matches-p topic kind iri))
129 (%topic-map-topics map))
130 (let ((new (%make-topic map)))
131 (setf (%topic-map-topics map) (append (%topic-map-topics map) (list new)))
133 (add-topic-identifier topic kind iri)))
135 (defun add-default-name (topic value)
136 "CTM 3.11 default type; equal unscoped names share one information item."
137 (check-type value string)
138 (let* ((topic (live-topic topic))
139 (type (intern-topic (%topic-parent topic) :subject-identifier (default-name-type-identifier)))
140 (topic (canonical-topic topic))
141 (existing (find-if (lambda (name)
142 (and (string= value (%topic-name-value name))
143 (eq type (%topic-name-type name))))
144 (%topic-names topic))))
146 (let ((name (%make-topic-name value type topic)))
147 (setf (%topic-names topic) (append (%topic-names topic) (list name)))