]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/merging.lisp
feat(ctm): read associations and roles with merge-safe topic references
[topic-maps.git] / src / tmdm / merging.lisp
1 (in-package #:topic-maps.tmdm)
2
3 (defun identifiers (topic kind)
4 (ecase kind
5 (:item-identifier (%topic-item-identifiers topic))
6 (:subject-identifier (%topic-subject-identifiers topic))
7 (:subject-locator (%topic-subject-locators topic))))
8
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=))
12 (ecase kind
13 ((:item-identifier :subject-identifier) '(:item-identifier :subject-identifier))
14 (:subject-locator '(:subject-locator)))))
15
16 (defun equal-topics-p (a b)
17 (some (lambda (kind)
18 (some (lambda (iri) (identifier-matches-p a kind iri)) (identifiers b kind)))
19 '(:item-identifier :subject-identifier :subject-locator)))
20
21 (defun set-union (a b test)
22 (remove-duplicates (append a b) :test test :from-end t))
23
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))
29 (list item))))
30
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))))))
35
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)
41 (lambda (a b)
42 (and (string= (%topic-name-value a) (%topic-name-value b))
43 (eq (%topic-name-type a) (%topic-name-type b)))))
44 while pair do
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)))))
49
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-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)))))
65 (dolist (association (%topic-map-associations map))
66 (setf (%association-type association) (canonical-topic (%association-type association)))
67 (dolist (role (%association-roles association))
68 (setf (%association-role-type role) (canonical-topic (%association-role-type role))
69 (%association-role-player role) (canonical-topic (%association-role-player role)))))
70 (normalize-names map)
71 (normalize-associations map)
72 c))
73
74 (defun normalize-topic-map (map)
75 "Apply same-map identity merging, then remove equal Names, Roles and Associations.
76 Only implemented properties participate; no cross-map merging."
77 (check-type map topic-map)
78 (loop for pair = (equal-pair (%topic-map-topics map) #'equal-topics-p)
79 while pair do (merge-two-topics map (first pair) (second pair)))
80 (normalize-names map)
81 (normalize-associations map)
82 map)
83
84 (defun live-topic (topic)
85 (let ((current (canonical-topic topic)))
86 (unless (member current (%topic-map-topics (%topic-parent current)) :test #'eq)
87 (error "Topic is not a live member of its map."))
88 current))
89
90 (defun merge-topics (a b)
91 "Merge two Topics in the same map, returning their current representative.
92 Explicit merging may assert shared subject identity beyond locator equality.
93 Cross-map merging is rejected before mutation. Obsolete handles remain resolvable."
94 (let* ((a (live-topic a)) (b (live-topic b)) (map (%topic-parent a)))
95 (unless (eq map (%topic-parent b)) (error "Cross-map Topic merging is outside this API."))
96 (unless (eq a b) (setf a (merge-two-topics map a b)))
97 (normalize-topic-map map)
98 (canonical-topic a)))
99
100 (defun add-topic-identifier (topic kind iri)
101 "Add a locator to its distinct identity property and perform required merging.
102 KIND is :ITEM-IDENTIFIER, :SUBJECT-IDENTIFIER or :SUBJECT-LOCATOR.
103 IRI is an already resolved, nonempty absolute locator string; CTM validates it."
104 (unless (and (stringp iri) (plusp (length iri))) (error "A nonempty locator string is required."))
105 (let ((topic (live-topic topic)))
106 (ecase kind
107 (:item-identifier (pushnew iri (%topic-item-identifiers topic) :test #'string=))
108 (:subject-identifier (pushnew iri (%topic-subject-identifiers topic) :test #'string=))
109 (:subject-locator (pushnew iri (%topic-subject-locators topic) :test #'string=)))
110 (normalize-topic-map (%topic-parent topic))
111 (canonical-topic topic)))
112
113 (defun intern-topic (map kind iri)
114 "Lookup/create a Topic using the same identity rule as same-map merging."
115 (check-type map topic-map)
116 (unless (and (stringp iri) (plusp (length iri))) (error "A nonempty locator string is required."))
117 ;; Validate the category even for an empty map, before adding a Topic.
118 (ecase kind ((:item-identifier :subject-identifier :subject-locator)))
119 (let ((topic (or (find-if (lambda (topic) (identifier-matches-p topic kind iri))
120 (%topic-map-topics map))
121 (let ((new (%make-topic map)))
122 (setf (%topic-map-topics map) (append (%topic-map-topics map) (list new)))
123 new))))
124 (add-topic-identifier topic kind iri)))
125
126 (defun add-default-name (topic value)
127 "CTM 3.11 default type; equal unscoped names share one information item."
128 (check-type value string)
129 (let* ((topic (live-topic topic))
130 (type (intern-topic (%topic-parent topic) :subject-identifier (default-name-type-identifier)))
131 (topic (canonical-topic topic))
132 (existing (find-if (lambda (name)
133 (and (string= value (%topic-name-value name))
134 (eq type (%topic-name-type name))))
135 (%topic-names topic))))
136 (or existing
137 (let ((name (%make-topic-name value type topic)))
138 (setf (%topic-names topic) (append (%topic-names topic) (list name)))
139 name))))