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 (define-condition scope-error (error)
37 ((code :initarg :code :reader scope-error-code)
38 (detail :initarg :detail :reader scope-error-detail))
39 (:report (lambda (c stream) (format stream "Scope ~A: ~A" (scope-error-code c) (scope-error-detail c)))))
40 (defun scope-problem (code detail) (error 'scope-error :code code :detail detail))
41 (defun proper-list-p (value)
42 ;; LIST-LENGTH detects cycles; dotted tails signal TYPE-ERROR. ANSI CL only.
43 (and (listp value) (handler-case (integerp (list-length value)) (type-error () nil))))
44 (defun canonical-scope (map themes)
45 "Validate before mutation and copy a set of current same-map Topic references."
46 (unless (proper-list-p themes)
47 (scope-problem :invalid-scope "Supply a proper finite list of Topics."))
49 (mapcar (lambda (theme)
50 (unless (typep theme 'topic) (scope-problem :invalid-theme "A theme must be a Topic object."))
51 (let ((current (live-topic theme)))
52 (unless (eq map (%topic-parent current))
53 (scope-problem :foreign-theme "Every theme must belong to the item's Topic Map."))
55 :test #'eq :from-end t))
56 (defun same-scope-p (a b)
57 ;; Storage is canonical and duplicate-free; equality has no sequence ordering.
58 (and (= (length a) (length b)) (every (lambda (theme) (member theme b :test #'eq)) a)))
60 (defun normalize-names (map)
61 ;; TMDM 5.4 and 6.3: all supported names have empty variants/II,
62 ;; null reifier. Parent and type may have changed during a Topic merge.
63 (dolist (topic (%topic-map-topics map))
64 (loop for pair = (equal-pair (%topic-names topic)
66 (and (string= (%topic-name-value a) (%topic-name-value b))
67 (eq (%topic-name-type a) (%topic-name-type b))
68 (same-scope-p (%topic-name-scope a) (%topic-name-scope b)))))
70 (let* ((a (first pair)) (b (second pair))
71 (c (%make-topic-name (%topic-name-value a) (%topic-name-type a) topic)))
72 (setf (%topic-name-scope c) (copy-list (%topic-name-scope a))
73 (%topic-names topic) (replace-pair (%topic-names topic) a b c)
74 (%topic-name-forward a) c (%topic-name-forward b) c)))))
76 (defun merge-two-topics (map a b)
77 ;; TMDM 6.2: create C, union supported properties, replace model references.
78 ;; Replace Scope sets too; Reification remains outside the supported model.
79 (let ((c (%make-topic map)))
80 (setf (%topic-item-identifiers c) (set-union (%topic-item-identifiers a) (%topic-item-identifiers b) #'string=)
81 (%topic-subject-identifiers c) (set-union (%topic-subject-identifiers a) (%topic-subject-identifiers b) #'string=)
82 (%topic-subject-locators c) (set-union (%topic-subject-locators a) (%topic-subject-locators b) #'string=)
83 (%topic-names c) (set-union (%topic-names a) (%topic-names b) #'eq)
84 (%topic-occurrences c) (set-union (%topic-occurrences a) (%topic-occurrences b) #'eq)
85 (%topic-map-topics map) (replace-pair (%topic-map-topics map) a b c)
86 (%topic-forward a) c (%topic-forward b) c)
87 (dolist (topic (%topic-map-topics map))
88 (dolist (name (%topic-names topic))
89 ;; Parent is the computed containing topic (TMDM 5.4).
90 (setf (%topic-name-parent name) topic
91 (%topic-name-type name) (canonical-topic (%topic-name-type name))
92 (%topic-name-scope name) (canonical-scope map (%topic-name-scope name)))))
93 (dolist (topic (%topic-map-topics map))
94 (dolist (occurrence (%topic-occurrences topic))
95 (setf (%occurrence-parent occurrence) topic
96 (%occurrence-type occurrence) (canonical-topic (%occurrence-type occurrence))
97 (%occurrence-scope occurrence) (canonical-scope map (%occurrence-scope occurrence)))))
98 (dolist (association (%topic-map-associations map))
99 (setf (%association-type association) (canonical-topic (%association-type association))
100 (%association-scope association) (canonical-scope map (%association-scope association)))
101 (dolist (role (%association-roles association))
102 (setf (%association-role-type role) (canonical-topic (%association-role-type role))
103 (%association-role-player role) (canonical-topic (%association-role-player role)))))
104 (normalize-names map)
105 (normalize-occurrences map)
106 (normalize-associations map)
109 (defun normalize-topic-map (map)
110 "Apply same-map identity merging, then remove equal Names, Occurrences, Roles and Associations.
111 Only implemented properties participate; no cross-map merging."
112 (check-type map topic-map)
113 (loop for pair = (equal-pair (%topic-map-topics map) #'equal-topics-p)
114 while pair do (merge-two-topics map (first pair) (second pair)))
115 (normalize-names map)
116 (normalize-occurrences map)
117 (normalize-associations map)
120 (defun live-topic (topic)
121 (let ((current (canonical-topic topic)))
122 (unless (member current (%topic-map-topics (%topic-parent current)) :test #'eq)
123 (error "Topic is not a live member of its map."))
126 (defun merge-topics (a b)
127 "Merge two Topics in the same map, returning their current representative.
128 Explicit merging may assert shared subject identity beyond locator equality.
129 Cross-map merging is rejected before mutation. Obsolete handles remain resolvable."
130 (let* ((a (live-topic a)) (b (live-topic b)) (map (%topic-parent a)))
131 (unless (eq map (%topic-parent b)) (error "Cross-map Topic merging is outside this API."))
132 (unless (eq a b) (setf a (merge-two-topics map a b)))
133 (normalize-topic-map map)
134 (canonical-topic a)))
136 (defun add-topic-identifier (topic kind iri)
137 "Add a locator to its distinct identity property and perform required merging.
138 KIND is :ITEM-IDENTIFIER, :SUBJECT-IDENTIFIER or :SUBJECT-LOCATOR.
139 IRI is an already resolved, nonempty absolute locator string; CTM validates it."
140 (unless (and (stringp iri) (plusp (length iri))) (error "A nonempty locator string is required."))
141 (let ((topic (live-topic topic)))
142 (check-topic-occurrence-identifiers (%topic-parent topic) kind iri)
144 (:item-identifier (pushnew iri (%topic-item-identifiers topic) :test #'string=))
145 (:subject-identifier (pushnew iri (%topic-subject-identifiers topic) :test #'string=))
146 (:subject-locator (pushnew iri (%topic-subject-locators topic) :test #'string=)))
147 (normalize-topic-map (%topic-parent topic))
148 (canonical-topic topic)))
150 (defun intern-topic (map kind iri)
151 "Lookup/create a Topic using the same identity rule as same-map merging."
152 (check-type map topic-map)
153 (unless (and (stringp iri) (plusp (length iri))) (error "A nonempty locator string is required."))
154 ;; Validate the category even for an empty map, before adding a Topic.
155 (ecase kind ((:item-identifier :subject-identifier :subject-locator)))
156 (check-topic-occurrence-identifiers map kind iri)
157 (let ((topic (or (find-if (lambda (topic) (identifier-matches-p topic kind iri))
158 (%topic-map-topics map))
159 (let ((new (%make-topic map)))
160 (setf (%topic-map-topics map) (append (%topic-map-topics map) (list new)))
162 (add-topic-identifier topic kind iri)))
164 (defun add-name (topic value &key (type nil type-supplied-p) scope)
165 "Add a Name with VALUE, optional TYPE Topic and SCOPE Topic set (TMDM 5.4).
166 Validate all arguments before interning the default type. Retained Topic handles
167 are canonicalized, including after default-type identity merging."
168 (check-type value string)
169 (let* ((topic (live-topic topic)) (map (%topic-parent topic))
170 (scope (canonical-scope map scope))
171 (type (when type-supplied-p (live-topic type))))
172 (when (and type-supplied-p (not (eq map (%topic-parent type))))
173 (error "Name type belongs to another map."))
174 (unless type-supplied-p
175 (setf type (intern-topic map :subject-identifier (default-name-type-identifier))))
176 (let* ((topic (canonical-topic topic)) (type (canonical-topic type))
177 (scope (canonical-scope map scope))
178 (existing (find-if (lambda (name)
179 (and (string= value (%topic-name-value name))
180 (eq type (%topic-name-type name))
181 (same-scope-p scope (%topic-name-scope name))))
182 (%topic-names topic))))
184 (let ((name (%make-topic-name value type topic)))
185 (setf (%topic-name-scope name) scope
186 (%topic-names topic) (append (%topic-names topic) (list name)))
189 (defun add-default-name (topic value &key scope)
190 "Compatibility entry point for ADD-NAME with the CTM 3.11 default type."
191 (add-name topic value :scope scope))