]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/item-identifiers.lisp
Implement CTM Mergemap with explicit byte resources and isolated environments
[topic-maps.git] / src / tmdm / item-identifiers.lisp
1 (in-package #:topic-maps.tmdm)
2
3 (define-condition item-identifier-error (error)
4 ((code :initarg :code :reader item-identifier-error-code)
5 (detail :initarg :detail :reader item-identifier-error-detail))
6 (:report (lambda (c out)
7 (format out "Item Identifier ~A: ~A" (item-identifier-error-code c)
8 (item-identifier-error-detail c)))))
9 (defun identifier-problem (code detail)
10 (error 'item-identifier-error :code code :detail detail))
11
12 (defun absolute-locator-p (value)
13 ;; Callers supply validated/resolved locators, not CTM source or CXTM output.
14 (and (stringp value)
15 (let ((colon (position #\: value)))
16 (and colon (plusp colon)
17 (let ((head (subseq value 0 colon)))
18 (and (or (char<= #\a (char head 0) #\z) (char<= #\A (char head 0) #\Z))
19 (every (lambda (c) (or (char<= #\a c #\z) (char<= #\A c #\Z)
20 (char<= #\0 c #\9) (find c "+-."))) head)))))))
21
22 (defun canonical-item (item)
23 (etypecase item
24 (topic-map item) (topic (canonical-topic item)) (topic-name (canonical-topic-name item))
25 (variant (canonical-variant item)) (occurrence (canonical-occurrence item))
26 (association (canonical-association item)) (association-role (canonical-association-role item))))
27 (defun %item-identifiers (item)
28 (etypecase item
29 (topic-map (%topic-map-item-identifiers item)) (topic (%topic-item-identifiers item))
30 (topic-name (%topic-name-item-identifiers item)) (variant (%variant-item-identifiers item))
31 (occurrence (%occurrence-item-identifiers item)) (association (%association-item-identifiers item))
32 (association-role (%association-role-item-identifiers item))))
33 (defun (setf %item-identifiers) (value item)
34 (etypecase item
35 (topic-map (setf (%topic-map-item-identifiers item) value))
36 (topic (setf (%topic-item-identifiers item) value))
37 (topic-name (setf (%topic-name-item-identifiers item) value))
38 (variant (setf (%variant-item-identifiers item) value))
39 (occurrence (setf (%occurrence-item-identifiers item) value))
40 (association (setf (%association-item-identifiers item) value))
41 (association-role (setf (%association-role-item-identifiers item) value))))
42 (defun item-identifiers (item)
43 "Return a fresh list of fresh locator strings for any of the seven TMDM items.
44 Retained handles resolve without modifying forward links."
45 (mapcar #'copy-seq (%item-identifiers (canonical-item item))))
46 (defun item-topic-map (item)
47 (let ((item (canonical-item item)))
48 (etypecase item
49 (topic-map item) (topic (%topic-parent item))
50 (topic-name (item-topic-map (%topic-name-parent item)))
51 (variant (item-topic-map (%variant-parent item)))
52 (occurrence (item-topic-map (%occurrence-parent item)))
53 (association (%association-parent item))
54 (association-role (item-topic-map (%association-role-parent item))))))
55 (defun map-information-items (map)
56 "Enumerate current information items, including MAP; no independent identity index."
57 (append (list map)
58 (loop for topic in (%topic-map-topics map) append
59 (append (list topic) (copy-list (%topic-names topic))
60 (loop for name in (%topic-names topic) append (copy-list (%topic-name-variants name)))
61 (copy-list (%topic-occurrences topic))))
62 (loop for association in (%topic-map-associations map) append
63 (cons association (copy-list (%association-roles association))))))
64 (defun validate-item-identifiers (map identifiers &key allowed topic-p)
65 "Check an assignment before mutation. ALLOWED contains identical/equal current
66 items that this operation will retain or merge. Only Topics merge by II alone."
67 (unless (and (proper-list-p identifiers) (every #'absolute-locator-p identifiers))
68 (identifier-problem :invalid-item-identifiers "Supply a proper finite list of resolved absolute locators."))
69 (dolist (other (map-information-items map))
70 (unless (or (member other allowed :test #'eq) (and topic-p (typep other 'topic)))
71 (when (intersection identifiers (%item-identifiers other) :test #'string=)
72 (identifier-problem :duplicate-item-identifier "Locator already identifies a different information item."))))
73 identifiers)
74 (defun validate-map-item-identifiers (map)
75 "Validate current sets/uniqueness without normalizing or changing the map."
76 (let ((items (map-information-items map)))
77 (dolist (item items)
78 (let ((ids (%item-identifiers item)))
79 (unless (and (proper-list-p ids) (every #'absolute-locator-p ids)
80 (= (length ids) (length (remove-duplicates ids :test #'string=))))
81 (identifier-problem :invalid-item-identifiers "Item Identifier storage must be a locator set."))))
82 (loop for tail on items for ids = (%item-identifiers (first tail)) do
83 (dolist (other (rest tail))
84 (when (intersection ids (%item-identifiers other) :test #'string=)
85 (identifier-problem :duplicate-item-identifier "Different live items share an Item Identifier.")))))
86 t)
87 (defun add-item-identifier (item iri)
88 "Add an already resolved absolute locator, returning the canonical item.
89 TMDM locator equality is STRING=, never CXTM base/NFC normalization.
90 Topics delegate to ADD-TOPIC-IDENTIFIER and its existing merge/preflight rules.
91 Non-Topics do not become equal merely by sharing identifiers."
92 (let* ((item (canonical-item item)) (map (item-topic-map item)))
93 (unless (member item (map-information-items map) :test #'eq)
94 (identifier-problem :invalid-item "Item is not a current member of its Topic Map."))
95 (unless (absolute-locator-p iri)
96 (identifier-problem :invalid-item-identifiers "Supply a resolved absolute locator."))
97 (if (typep item 'topic)
98 (add-topic-identifier item :item-identifier iri)
99 (progn
100 (validate-item-identifiers map (list iri) :allowed (list item))
101 ;; Retain the Occurrence constructor compatibility condition contract.
102 (when (typep item 'occurrence)
103 (validate-occurrence-identifiers map (%occurrence-parent item) (%occurrence-type item)
104 (%occurrence-value item) (%occurrence-datatype item) (list iri) (%occurrence-scope item)))
105 (pushnew (copy-seq iri) (%item-identifiers item) :test #'string=)
106 item))))