1 (in-package #:topic-maps.tmdm)
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)
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)
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."))
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)))))
40 (defun copy-map-for-preflight (map)
41 "Disposable graph of the SAME seven TMDM structures, using the same merge code.
42 Old handles resolve through their canonical representatives; no second identity model."
43 (let ((copy (make-topic-map)) (copies (make-hash-table :test #'eq)))
44 (setf (gethash map copies) copy)
45 (labels ((ref (item) (and item (or (gethash (canonical-item item) copies)
46 (reification-problem :foreign-item "Reference is outside this Topic Map."))))
47 (refs (items) (mapcar #'ref items)))
48 (dolist (tpc (%topic-map-topics map)) (setf (gethash tpc copies) (%make-topic copy)))
49 (setf (%topic-map-topics copy) (refs (%topic-map-topics map)))
50 (dolist (tpc (%topic-map-topics map))
51 (let ((new (ref tpc)))
52 (setf (%topic-subject-identifiers new) (mapcar #'copy-seq (%topic-subject-identifiers tpc))
53 (%topic-subject-locators new) (mapcar #'copy-seq (%topic-subject-locators tpc)))
54 (dolist (n (%topic-names tpc))
55 (setf (gethash n copies) (%make-topic-name (%topic-name-value n) (ref (%topic-name-type n)) new)))
56 (setf (%topic-names new) (refs (%topic-names tpc)))
57 (dolist (n (%topic-names tpc))
58 (dolist (v (%topic-name-variants n))
59 (setf (gethash v copies) (%make-variant (%variant-value v) (%variant-datatype v) (ref n))))
60 (setf (%topic-name-variants (ref n)) (refs (%topic-name-variants n))))
61 (dolist (o (%topic-occurrences tpc))
62 (setf (gethash o copies) (%make-occurrence (%occurrence-value o) (%occurrence-datatype o)
63 (ref (%occurrence-type o)) new)))
64 (setf (%topic-occurrences new) (refs (%topic-occurrences tpc)))))
65 (dolist (a (%topic-map-associations map))
66 (setf (gethash a copies) (%make-association copy (ref (%association-type a))))
67 (dolist (r (%association-roles a))
68 (setf (gethash r copies) (%make-association-role (ref a) (ref (%association-role-type r))
69 (ref (%association-role-player r)))))
70 (setf (%association-roles (ref a)) (refs (%association-roles a))))
71 (setf (%topic-map-associations copy) (refs (%topic-map-associations map)))
72 (dolist (item (map-information-items map))
73 (let ((new (ref item)))
74 (setf (%item-identifiers new) (mapcar #'copy-seq (%item-identifiers item)))
75 (when (reifiable-p item) (setf (%item-reifier new) (ref (%item-reifier item))))
77 (topic-name (setf (%topic-name-scope new) (refs (%topic-name-scope item))))
78 (variant (setf (%variant-scope new) (refs (%variant-scope item))))
79 (occurrence (setf (%occurrence-scope new) (refs (%occurrence-scope item))))
80 (association (setf (%association-scope new) (refs (%association-scope item)))))))
81 (values copy #'ref))))
82 (defun preflight-merge-mutation (map operation)
83 "Validate the complete cascade on a disposable graph before changing originals.
84 This guarantees semantic rejection before mutation, not recovery from resource exhaustion."
85 (multiple-value-bind (copy resolve) (copy-map-for-preflight map)
86 (declare (ignore copy))
87 (let ((*merge-mutation-active* t)) (funcall operation resolve)))
88 (let ((*merge-mutation-active* t)) (funcall operation #'identity)))
90 (defun set-item-reifier (item topic)
91 "Assert same-map Reification, returning the current item. A different existing
92 Reifier is merged with TOPIC. NIL explicitly clears. Atomic validation is API policy."
93 (unless (reifiable-p item) (reification-problem :not-reifiable "Topics cannot be reified."))
94 (let* ((item (canonical-item item)) (map (item-topic-map item)))
95 (unless (member item (map-information-items map) :test #'eq)
96 (reification-problem :invalid-item "Item is not live in its Topic Map."))
98 (unless (typep topic 'topic) (reification-problem :invalid-reifier "Reifier must be a Topic or NIL."))
99 (setf topic (live-topic topic))
100 (unless (eq map (%topic-parent topic)) (reification-problem :foreign-reifier "Reifier belongs to another Topic Map.")))
101 (when (needs-merge-preflight-p map)
102 (return-from set-item-reifier
103 (preflight-merge-mutation map (lambda (ref) (set-item-reifier (funcall ref item) (and topic (funcall ref topic)))))))
105 (let ((other (topic-reified topic)))
106 (when (and other (not (eq other item)))
107 (reification-problem :reifier-conflict "Topic already reifies a different information item.")))
108 (let ((old (%item-reifier item)))
109 (when (and old (not (eq old topic))) (setf topic (merge-topics old topic)))))
110 (setf item (canonical-item item) (%item-reifier item) (and topic (canonical-topic topic)))
112 (defun merge-item-reifiers (item a b)
113 ;; A/B have been replaced. Capture raw metadata; resolve again after cascades.
114 (dolist (r (list (%item-reifier a) (%item-reifier b)))
115 (when r (set-item-reifier (canonical-item item) (canonical-topic r))))
116 (canonical-item item))
117 (defun validate-map-reification (map)
118 "Validate stored references and the inverse without repair/mutation."
119 (dolist (item (map-information-items map))
120 (when (and (reifiable-p item) (%item-reifier item))
121 (let ((r (%item-reifier item)))
122 (unless (and (typep r 'topic) (eq r (canonical-topic r))
123 (eq map (%topic-parent r)) (member r (%topic-map-topics map) :test #'eq))
124 (reification-problem :invalid-reifier "Reifier must be a current same-map Topic."))
125 (unless (eq item (topic-reified r)) (reification-problem :reifier-conflict "Inconsistent inverse.")))))