(in-package #:topic-maps.tmdm) (define-condition reification-error (error) ((code :initarg :code :reader reification-error-code) (detail :initarg :detail :reader reification-error-detail)) (:report (lambda (c out) (format out "Reification ~A: ~A" (reification-error-code c) (reification-error-detail c))))) (defun reification-problem (code detail) (error 'reification-error :code code :detail detail)) (defun reifiable-p (item) (typep item '(or topic-map topic-name variant occurrence association association-role))) (defun %item-reifier (item) (etypecase item (topic-map (%topic-map-reifier item)) (topic-name (%topic-name-reifier item)) (variant (%variant-reifier item)) (occurrence (%occurrence-reifier item)) (association (%association-reifier item)) (association-role (%association-role-reifier item)))) (defun (setf %item-reifier) (value item) (etypecase item (topic-map (setf (%topic-map-reifier item) value)) (topic-name (setf (%topic-name-reifier item) value)) (variant (setf (%variant-reifier item) value)) (occurrence (setf (%occurrence-reifier item) value)) (association (setf (%association-reifier item) value)) (association-role (setf (%association-role-reifier item) value)))) (defun item-reifier (item) "Read the stored current Reifier Topic (or NIL); Topics cannot be reified." (unless (reifiable-p item) (reification-problem :not-reifiable "Topics cannot be reified.")) (%item-reifier (canonical-item item))) (defun topic-reified (topic) "Computed TMDM 5.3.5 inverse, not an independently maintained identity index." (let* ((topic (canonical-topic topic)) (items (remove-if-not (lambda (item) (and (reifiable-p item) (eq topic (%item-reifier item)))) (map-information-items (%topic-parent topic))))) (when (rest items) (reification-problem :reifier-conflict "A Topic reifies different live information items.")) (first items))) (defvar *reification-mutation-active* nil) (defun needs-reification-preflight-p (map) (and (not *reification-mutation-active*) (some (lambda (item) (and (reifiable-p item) (%item-reifier item))) (map-information-items map)))) (defun copy-map-for-preflight (map) "Disposable graph of the SAME seven TMDM structures, using the same merge code. Old handles resolve through their canonical representatives; no second identity model." (let ((copy (make-topic-map)) (copies (make-hash-table :test #'eq))) (setf (gethash map copies) copy) (labels ((ref (item) (and item (or (gethash (canonical-item item) copies) (reification-problem :foreign-item "Reference is outside this Topic Map.")))) (refs (items) (mapcar #'ref items))) (dolist (tpc (%topic-map-topics map)) (setf (gethash tpc copies) (%make-topic copy))) (setf (%topic-map-topics copy) (refs (%topic-map-topics map))) (dolist (tpc (%topic-map-topics map)) (let ((new (ref tpc))) (setf (%topic-subject-identifiers new) (mapcar #'copy-seq (%topic-subject-identifiers tpc)) (%topic-subject-locators new) (mapcar #'copy-seq (%topic-subject-locators tpc))) (dolist (n (%topic-names tpc)) (setf (gethash n copies) (%make-topic-name (%topic-name-value n) (ref (%topic-name-type n)) new))) (setf (%topic-names new) (refs (%topic-names tpc))) (dolist (n (%topic-names tpc)) (dolist (v (%topic-name-variants n)) (setf (gethash v copies) (%make-variant (%variant-value v) (%variant-datatype v) (ref n)))) (setf (%topic-name-variants (ref n)) (refs (%topic-name-variants n)))) (dolist (o (%topic-occurrences tpc)) (setf (gethash o copies) (%make-occurrence (%occurrence-value o) (%occurrence-datatype o) (ref (%occurrence-type o)) new))) (setf (%topic-occurrences new) (refs (%topic-occurrences tpc))))) (dolist (a (%topic-map-associations map)) (setf (gethash a copies) (%make-association copy (ref (%association-type a)))) (dolist (r (%association-roles a)) (setf (gethash r copies) (%make-association-role (ref a) (ref (%association-role-type r)) (ref (%association-role-player r))))) (setf (%association-roles (ref a)) (refs (%association-roles a)))) (setf (%topic-map-associations copy) (refs (%topic-map-associations map))) (dolist (item (map-information-items map)) (let ((new (ref item))) (setf (%item-identifiers new) (mapcar #'copy-seq (%item-identifiers item))) (when (reifiable-p item) (setf (%item-reifier new) (ref (%item-reifier item)))) (typecase item (topic-name (setf (%topic-name-scope new) (refs (%topic-name-scope item)))) (variant (setf (%variant-scope new) (refs (%variant-scope item)))) (occurrence (setf (%occurrence-scope new) (refs (%occurrence-scope item)))) (association (setf (%association-scope new) (refs (%association-scope item))))))) (values copy #'ref)))) (defun preflight-reification-mutation (map operation) "Validate the complete cascade on a disposable graph before changing originals. This guarantees semantic rejection before mutation, not recovery from resource exhaustion." (multiple-value-bind (copy resolve) (copy-map-for-preflight map) (declare (ignore copy)) (let ((*reification-mutation-active* t)) (funcall operation resolve))) (let ((*reification-mutation-active* t)) (funcall operation #'identity))) (defun set-item-reifier (item topic) "Assert same-map Reification, returning the current item. A different existing Reifier is merged with TOPIC. NIL explicitly clears. Atomic validation is API policy." (unless (reifiable-p item) (reification-problem :not-reifiable "Topics cannot be reified.")) (let* ((item (canonical-item item)) (map (item-topic-map item))) (unless (member item (map-information-items map) :test #'eq) (reification-problem :invalid-item "Item is not live in its Topic Map.")) (when topic (unless (typep topic 'topic) (reification-problem :invalid-reifier "Reifier must be a Topic or NIL.")) (setf topic (live-topic topic)) (unless (eq map (%topic-parent topic)) (reification-problem :foreign-reifier "Reifier belongs to another Topic Map."))) (when (needs-reification-preflight-p map) (return-from set-item-reifier (preflight-reification-mutation map (lambda (ref) (set-item-reifier (funcall ref item) (and topic (funcall ref topic))))))) (when topic (let ((other (topic-reified topic))) (when (and other (not (eq other item))) (reification-problem :reifier-conflict "Topic already reifies a different information item."))) (let ((old (%item-reifier item))) (when (and old (not (eq old topic))) (setf topic (merge-topics old topic))))) (setf item (canonical-item item) (%item-reifier item) (and topic (canonical-topic topic))) item)) (defun merge-item-reifiers (item a b) ;; A/B have been replaced. Capture raw metadata; resolve again after cascades. (dolist (r (list (%item-reifier a) (%item-reifier b))) (when r (set-item-reifier (canonical-item item) (canonical-topic r)))) (canonical-item item)) (defun validate-map-reification (map) "Validate stored references and the inverse without repair/mutation." (dolist (item (map-information-items map)) (when (and (reifiable-p item) (%item-reifier item)) (let ((r (%item-reifier item))) (unless (and (typep r 'topic) (eq r (canonical-topic r)) (eq map (%topic-parent r)) (member r (%topic-map-topics map) :test #'eq)) (reification-problem :invalid-reifier "Reifier must be a current same-map Topic.")) (unless (eq item (topic-reified r)) (reification-problem :reifier-conflict "Inconsistent inverse."))))) t)