]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/tmdm/reification.lisp
Verify CTM/XTM semantic equivalence through TMDM and CXTM (Slice 22F)
[topic-maps.git] / src / tmdm / reification.lisp
index 78f934c1b2365fdcf0a9ca6ba28ff145c5530daf..66d5273addd6f0e5d0ba566ae5e3cfcec184b35b 100644 (file)
                                (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))))
+(defvar *merge-mutation-active* nil)
+(defun needs-merge-preflight-p (map)
+  "Reuse the same disposable graph for Reifier cascades and Core Subject conflicts."
+  (and (not *merge-mutation-active*)
+       (or (map-has-core-subject-p map)
+           (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)))
+(defun copy-map-graph (map copy &key (map-item-p t))
+  "Copy the existing TMDM graph into COPY, rewriting all object references.
+MAP-ITEM-P copies root metadata only for disposable preflight copies. Content
+imports append six item kinds, leaving COPY's own identifiers/reifier intact."
+  (let ((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)))
+      (setf (%topic-map-topics copy) (append (%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 (gethash n copies) (%make-topic-name (copy-seq (%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 (gethash v copies) (%make-variant (copy-seq (%variant-value v)) (copy-seq (%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)
+            (setf (gethash o copies) (%make-occurrence (copy-seq (%occurrence-value o)) (copy-seq (%occurrence-datatype o))
                                                       (ref (%occurrence-type o)) new)))
           (setf (%topic-occurrences new) (refs (%topic-occurrences tpc)))))
       (dolist (a (%topic-map-associations map))
@@ -66,8 +69,8 @@ Old handles resolve through their canonical representatives; no second identity
           (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))
+      (setf (%topic-map-associations copy) (append (%topic-map-associations copy) (refs (%topic-map-associations map))))
+      (dolist (item (if map-item-p (map-information-items map) (rest (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))))
@@ -76,14 +79,17 @@ Old handles resolve through their canonical representatives; no second identity
             (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)
+      (values copy #'ref copies))))
+(defun copy-map-for-preflight (map)
+  "Disposable graph of the SAME seven TMDM structures and their root metadata."
+  (copy-map-graph map (make-topic-map)))
+(defun preflight-merge-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)))
+    (let ((*merge-mutation-active* t)) (funcall operation resolve)))
+  (let ((*merge-mutation-active* t)) (funcall operation #'identity)))
 
 (defun set-item-reifier (item topic)
   "Assert same-map Reification, returning the current item. A different existing
@@ -96,9 +102,9 @@ Reifier is merged with TOPIC. NIL explicitly clears. Atomic validation is API po
       (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)
+    (when (needs-merge-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)))))))
+        (preflight-merge-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)))