]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/reification.lisp
Add XTM Variant reification and qualify Slice 21O originals
[topic-maps.git] / src / tmdm / reification.lisp
1 (in-package #:topic-maps.tmdm)
2
3 (define-condition reification-error (error)
4 ((code :initarg :code :reader reification-error-code)
5 (detail :initarg :detail :reader reification-error-detail))
6 (:report (lambda (c out) (format out "Reification ~A: ~A" (reification-error-code c)
7 (reification-error-detail c)))))
8 (defun reification-problem (code detail) (error 'reification-error :code code :detail detail))
9 (defun reifiable-p (item)
10 (typep item '(or topic-map topic-name variant occurrence association association-role)))
11 (defun %item-reifier (item)
12 (etypecase item
13 (topic-map (%topic-map-reifier item)) (topic-name (%topic-name-reifier item))
14 (variant (%variant-reifier item)) (occurrence (%occurrence-reifier item))
15 (association (%association-reifier item)) (association-role (%association-role-reifier item))))
16 (defun (setf %item-reifier) (value item)
17 (etypecase item
18 (topic-map (setf (%topic-map-reifier item) value)) (topic-name (setf (%topic-name-reifier item) value))
19 (variant (setf (%variant-reifier item) value)) (occurrence (setf (%occurrence-reifier item) value))
20 (association (setf (%association-reifier item) value))
21 (association-role (setf (%association-role-reifier item) value))))
22 (defun item-reifier (item)
23 "Read the stored current Reifier Topic (or NIL); Topics cannot be reified."
24 (unless (reifiable-p item) (reification-problem :not-reifiable "Topics cannot be reified."))
25 (%item-reifier (canonical-item item)))
26 (defun topic-reified (topic)
27 "Computed TMDM 5.3.5 inverse, not an independently maintained identity index."
28 (let* ((topic (canonical-topic topic))
29 (items (remove-if-not (lambda (item) (and (reifiable-p item) (eq topic (%item-reifier item))))
30 (map-information-items (%topic-parent topic)))))
31 (when (rest items) (reification-problem :reifier-conflict "A Topic reifies different live information items."))
32 (first items)))
33 (defvar *merge-mutation-active* nil)
34 (defun needs-merge-preflight-p (map)
35 "Reuse the same disposable graph for Reifier cascades and Core Subject conflicts."
36 (and (not *merge-mutation-active*)
37 (or (map-has-core-subject-p map)
38 (some (lambda (item) (and (reifiable-p item) (%item-reifier item))) (map-information-items map)))))
39
40 (defun copy-map-graph (map copy &key (map-item-p t))
41 "Copy the existing TMDM graph into COPY, rewriting all object references.
42 MAP-ITEM-P copies root metadata only for disposable preflight copies. Content
43 imports append six item kinds, leaving COPY's own identifiers/reifier intact."
44 (let ((copies (make-hash-table :test #'eq)))
45 (setf (gethash map copies) copy)
46 (labels ((ref (item) (and item (or (gethash (canonical-item item) copies)
47 (reification-problem :foreign-item "Reference is outside this Topic Map."))))
48 (refs (items) (mapcar #'ref items)))
49 (dolist (tpc (%topic-map-topics map)) (setf (gethash tpc copies) (%make-topic copy)))
50 (setf (%topic-map-topics copy) (append (%topic-map-topics copy) (refs (%topic-map-topics map))))
51 (dolist (tpc (%topic-map-topics map))
52 (let ((new (ref tpc)))
53 (setf (%topic-subject-identifiers new) (mapcar #'copy-seq (%topic-subject-identifiers tpc))
54 (%topic-subject-locators new) (mapcar #'copy-seq (%topic-subject-locators tpc)))
55 (dolist (n (%topic-names tpc))
56 (setf (gethash n copies) (%make-topic-name (copy-seq (%topic-name-value n)) (ref (%topic-name-type n)) new)))
57 (setf (%topic-names new) (refs (%topic-names tpc)))
58 (dolist (n (%topic-names tpc))
59 (dolist (v (%topic-name-variants n))
60 (setf (gethash v copies) (%make-variant (copy-seq (%variant-value v)) (copy-seq (%variant-datatype v)) (ref n))))
61 (setf (%topic-name-variants (ref n)) (refs (%topic-name-variants n))))
62 (dolist (o (%topic-occurrences tpc))
63 (setf (gethash o copies) (%make-occurrence (copy-seq (%occurrence-value o)) (copy-seq (%occurrence-datatype o))
64 (ref (%occurrence-type o)) new)))
65 (setf (%topic-occurrences new) (refs (%topic-occurrences tpc)))))
66 (dolist (a (%topic-map-associations map))
67 (setf (gethash a copies) (%make-association copy (ref (%association-type a))))
68 (dolist (r (%association-roles a))
69 (setf (gethash r copies) (%make-association-role (ref a) (ref (%association-role-type r))
70 (ref (%association-role-player r)))))
71 (setf (%association-roles (ref a)) (refs (%association-roles a))))
72 (setf (%topic-map-associations copy) (append (%topic-map-associations copy) (refs (%topic-map-associations map))))
73 (dolist (item (if map-item-p (map-information-items map) (rest (map-information-items map))))
74 (let ((new (ref item)))
75 (setf (%item-identifiers new) (mapcar #'copy-seq (%item-identifiers item)))
76 (when (reifiable-p item) (setf (%item-reifier new) (ref (%item-reifier item))))
77 (typecase item
78 (topic-name (setf (%topic-name-scope new) (refs (%topic-name-scope item))))
79 (variant (setf (%variant-scope new) (refs (%variant-scope item))))
80 (occurrence (setf (%occurrence-scope new) (refs (%occurrence-scope item))))
81 (association (setf (%association-scope new) (refs (%association-scope item)))))))
82 (values copy #'ref copies))))
83 (defun copy-map-for-preflight (map)
84 "Disposable graph of the SAME seven TMDM structures and their root metadata."
85 (copy-map-graph map (make-topic-map)))
86 (defun preflight-merge-mutation (map operation)
87 "Validate the complete cascade on a disposable graph before changing originals.
88 This guarantees semantic rejection before mutation, not recovery from resource exhaustion."
89 (multiple-value-bind (copy resolve) (copy-map-for-preflight map)
90 (declare (ignore copy))
91 (let ((*merge-mutation-active* t)) (funcall operation resolve)))
92 (let ((*merge-mutation-active* t)) (funcall operation #'identity)))
93
94 (defun set-item-reifier (item topic)
95 "Assert same-map Reification, returning the current item. A different existing
96 Reifier is merged with TOPIC. NIL explicitly clears. Atomic validation is API policy."
97 (unless (reifiable-p item) (reification-problem :not-reifiable "Topics cannot be reified."))
98 (let* ((item (canonical-item item)) (map (item-topic-map item)))
99 (unless (member item (map-information-items map) :test #'eq)
100 (reification-problem :invalid-item "Item is not live in its Topic Map."))
101 (when topic
102 (unless (typep topic 'topic) (reification-problem :invalid-reifier "Reifier must be a Topic or NIL."))
103 (setf topic (live-topic topic))
104 (unless (eq map (%topic-parent topic)) (reification-problem :foreign-reifier "Reifier belongs to another Topic Map.")))
105 (when (needs-merge-preflight-p map)
106 (return-from set-item-reifier
107 (preflight-merge-mutation map (lambda (ref) (set-item-reifier (funcall ref item) (and topic (funcall ref topic)))))))
108 (when topic
109 (let ((other (topic-reified topic)))
110 (when (and other (not (eq other item)))
111 (reification-problem :reifier-conflict "Topic already reifies a different information item.")))
112 (let ((old (%item-reifier item)))
113 (when (and old (not (eq old topic))) (setf topic (merge-topics old topic)))))
114 (setf item (canonical-item item) (%item-reifier item) (and topic (canonical-topic topic)))
115 item))
116 (defun merge-item-reifiers (item a b)
117 ;; A/B have been replaced. Capture raw metadata; resolve again after cascades.
118 (dolist (r (list (%item-reifier a) (%item-reifier b)))
119 (when r (set-item-reifier (canonical-item item) (canonical-topic r))))
120 (canonical-item item))
121 (defun validate-map-reification (map)
122 "Validate stored references and the inverse without repair/mutation."
123 (dolist (item (map-information-items map))
124 (when (and (reifiable-p item) (%item-reifier item))
125 (let ((r (%item-reifier item)))
126 (unless (and (typep r 'topic) (eq r (canonical-topic r))
127 (eq map (%topic-parent r)) (member r (%topic-map-topics map) :test #'eq))
128 (reification-problem :invalid-reifier "Reifier must be a current same-map Topic."))
129 (unless (eq item (topic-reified r)) (reification-problem :reifier-conflict "Inconsistent inverse.")))))
130 t)