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 ;; Unimplemented associations/scope/reifiers cannot contain hidden references.
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-map-topics map) (replace-pair (%topic-map-topics map) a b c)
59 (%topic-forward a) c (%topic-forward b) c)
60 (dolist (topic (%topic-map-topics map))
61 (dolist (name (%topic-names topic))
62 ;; Parent is the computed containing topic (TMDM 5.4).
63 (setf (%topic-name-parent name) topic
64 (%topic-name-type name) (canonical-topic (%topic-name-type name)))))
68 (defun normalize-topic-map (map)
69 "Apply same-map identity merging to a fixed point, then remove equal Names.
70 Only implemented Topic/Name properties participate; no cross-map merging."
71 (check-type map topic-map)
72 (loop for pair = (equal-pair (%topic-map-topics map) #'equal-topics-p)
73 while pair do (merge-two-topics map (first pair) (second pair)))
77 (defun live-topic (topic)
78 (let ((current (canonical-topic topic)))
79 (unless (member current (%topic-map-topics (%topic-parent current)) :test #'eq)
80 (error "Topic is not a live member of its map."))
83 (defun merge-topics (a b)
84 "Merge two Topics in the same map, returning their current representative.
85 Explicit merging may assert shared subject identity beyond locator equality.
86 Cross-map merging is rejected before mutation. Obsolete handles remain resolvable."
87 (let* ((a (live-topic a)) (b (live-topic b)) (map (%topic-parent a)))
88 (unless (eq map (%topic-parent b)) (error "Cross-map Topic merging is outside this API."))
89 (unless (eq a b) (setf a (merge-two-topics map a b)))
90 (normalize-topic-map map)
93 (defun add-topic-identifier (topic kind iri)
94 "Add a locator to its distinct identity property and perform required merging.
95 KIND is :ITEM-IDENTIFIER, :SUBJECT-IDENTIFIER or :SUBJECT-LOCATOR.
96 IRI is an already resolved, nonempty absolute locator string; CTM validates it."
97 (unless (and (stringp iri) (plusp (length iri))) (error "A nonempty locator string is required."))
98 (let ((topic (live-topic topic)))
100 (:item-identifier (pushnew iri (%topic-item-identifiers topic) :test #'string=))
101 (:subject-identifier (pushnew iri (%topic-subject-identifiers topic) :test #'string=))
102 (:subject-locator (pushnew iri (%topic-subject-locators topic) :test #'string=)))
103 (normalize-topic-map (%topic-parent topic))
104 (canonical-topic topic)))
106 (defun intern-topic (map kind iri)
107 "Lookup/create a Topic using the same identity rule as same-map merging."
108 (check-type map topic-map)
109 (unless (and (stringp iri) (plusp (length iri))) (error "A nonempty locator string is required."))
110 ;; Validate the category even for an empty map, before adding a Topic.
111 (ecase kind ((:item-identifier :subject-identifier :subject-locator)))
112 (let ((topic (or (find-if (lambda (topic) (identifier-matches-p topic kind iri))
113 (%topic-map-topics map))
114 (let ((new (%make-topic map)))
115 (setf (%topic-map-topics map) (append (%topic-map-topics map) (list new)))
117 (add-topic-identifier topic kind iri)))
119 (defun add-default-name (topic value)
120 "CTM 3.11 default type; equal unscoped names share one information item."
121 (check-type value string)
122 (let* ((topic (live-topic topic))
123 (type (intern-topic (%topic-parent topic) :subject-identifier (default-name-type-identifier)))
124 (topic (canonical-topic topic))
125 (existing (find-if (lambda (name)
126 (and (string= value (%topic-name-value name))
127 (eq type (%topic-name-type name))))
128 (%topic-names topic))))
130 (let ((name (%make-topic-name value type topic)))
131 (setf (%topic-names topic) (append (%topic-names topic) (list name)))