]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/tmdm/merging.lisp
test(ctm): register seven reviewed negative Slice 20B contracts
[topic-maps.git] / src / tmdm / merging.lisp
index 391c577ecd5982aac0c6f288ff4515100c85418b..b34766d0fdaaca222277e7b5c1195370648bf149 100644 (file)
           (:subject-locator '(:subject-locator)))))
 
 (defun equal-topics-p (a b)
           (:subject-locator '(:subject-locator)))))
 
 (defun equal-topics-p (a b)
-  (some (lambda (kind)
-          (some (lambda (iri) (identifier-matches-p a kind iri)) (identifiers b kind)))
-        '(:item-identifier :subject-identifier :subject-locator)))
+  (or (some (lambda (kind)
+              (some (lambda (iri) (identifier-matches-p a kind iri)) (identifiers b kind)))
+            '(:item-identifier :subject-identifier :subject-locator))
+      (let ((target (topic-reified a))) (and target (eq target (topic-reified b))))))
 
 (defun set-union (a b test)
   (remove-duplicates (append a b) :test test :from-end t))
 
 (defun set-union (a b test)
   (remove-duplicates (append a b) :test test :from-end t))
   (and (= (length a) (length b)) (every (lambda (theme) (member theme b :test #'eq)) a)))
 
 (defun normalize-names (map)
   (and (= (length a) (length b)) (every (lambda (theme) (member theme b :test #'eq)) a)))
 
 (defun normalize-names (map)
-  ;; TMDM 5.4 and 6.3: all supported names have empty variants/II,
-  ;; null reifier. Parent and type may have changed during a Topic merge.
-  (dolist (topic (%topic-map-topics map))
-    (loop for pair = (equal-pair (%topic-names topic)
-                       (lambda (a b)
-                         (and (string= (%topic-name-value a) (%topic-name-value b))
-                              (eq (%topic-name-type a) (%topic-name-type b))
-                              (same-scope-p (%topic-name-scope a) (%topic-name-scope b)))))
-          while pair do
-            (let* ((a (first pair)) (b (second pair))
-                   (c (%make-topic-name (%topic-name-value a) (%topic-name-type a) topic)))
-              (setf (%topic-name-scope c) (copy-list (%topic-name-scope a))
-                    (%topic-names topic) (replace-pair (%topic-names topic) a b c)
-                    (%topic-name-forward a) c (%topic-name-forward b) c)))))
+  ;; Rescan live owners after each replacement: merging Reifiers can replace
+  ;; the owner/type/theme of the very Name currently being normalized.
+  (loop for entry = (loop for topic in (%topic-map-topics map)
+                         for pair = (equal-pair (%topic-names topic)
+                           (lambda (a b) (and (string= (%topic-name-value a) (%topic-name-value b))
+                                             (eq (%topic-name-type a) (%topic-name-type b))
+                                             (same-scope-p (%topic-name-scope a) (%topic-name-scope b)))))
+                         when pair return (cons topic pair))
+        while entry do
+    (let* ((topic (first entry)) (a (second entry)) (b (third entry))
+           (c (%make-topic-name (%topic-name-value a) (%topic-name-type a) topic)))
+      (setf (%topic-name-scope c) (copy-list (%topic-name-scope a))
+            (%topic-name-item-identifiers c) (set-union (%topic-name-item-identifiers a) (%topic-name-item-identifiers b) #'string=)
+            (%topic-name-variants c) (set-union (%topic-name-variants a) (%topic-name-variants b) #'eq)
+            (%topic-names topic) (replace-pair (%topic-names topic) a b c)
+            (%topic-name-forward a) c (%topic-name-forward b) c)
+      (dolist (v (%topic-name-variants c)) (setf (%variant-parent v) c))
+      (merge-item-reifiers c a b)))
+  (normalize-variants map))
 
 (defun merge-two-topics (map a b)
   ;; TMDM 6.2: create C, union supported properties, replace model references.
 
 (defun merge-two-topics (map a b)
   ;; TMDM 6.2: create C, union supported properties, replace model references.
-  ;; Replace Scope sets too; Reification remains outside the supported model.
+  ;; TMDM 6.2 conflict is checked against current targets BEFORE replacement.
+  (check-core-subject-identifiers (append (%topic-subject-identifiers a) (%topic-subject-identifiers b)))
+  (let ((left (topic-reified a)) (right (topic-reified b)))
+    (when (and left right (not (eq left right)))
+      (reification-problem :reifier-conflict "Topics reify different information items.")))
+  (check-variant-merge-constraints map :pair (list a b))
   (let ((c (%make-topic map)))
     (setf (%topic-item-identifiers c) (set-union (%topic-item-identifiers a) (%topic-item-identifiers b) #'string=)
           (%topic-subject-identifiers c) (set-union (%topic-subject-identifiers a) (%topic-subject-identifiers b) #'string=)
   (let ((c (%make-topic map)))
     (setf (%topic-item-identifiers c) (set-union (%topic-item-identifiers a) (%topic-item-identifiers b) #'string=)
           (%topic-subject-identifiers c) (set-union (%topic-subject-identifiers a) (%topic-subject-identifiers b) #'string=)
         ;; Parent is the computed containing topic (TMDM 5.4).
         (setf (%topic-name-parent name) topic
               (%topic-name-type name) (canonical-topic (%topic-name-type name))
         ;; Parent is the computed containing topic (TMDM 5.4).
         (setf (%topic-name-parent name) topic
               (%topic-name-type name) (canonical-topic (%topic-name-type name))
-              (%topic-name-scope name) (canonical-scope map (%topic-name-scope name)))))
+              (%topic-name-scope name) (canonical-scope map (%topic-name-scope name)))
+        (dolist (item (%topic-name-variants name))
+          (setf (%variant-scope item) (canonical-scope map (%variant-scope item))))))
     (dolist (topic (%topic-map-topics map))
       (dolist (occurrence (%topic-occurrences topic))
         (setf (%occurrence-parent occurrence) topic
     (dolist (topic (%topic-map-topics map))
       (dolist (occurrence (%topic-occurrences topic))
         (setf (%occurrence-parent occurrence) topic
       (dolist (role (%association-roles association))
         (setf (%association-role-type role) (canonical-topic (%association-role-type role))
               (%association-role-player role) (canonical-topic (%association-role-player role)))))
       (dolist (role (%association-roles association))
         (setf (%association-role-type role) (canonical-topic (%association-role-type role))
               (%association-role-player role) (canonical-topic (%association-role-player role)))))
+    (dolist (item (map-information-items map))
+      (when (and (reifiable-p item) (%item-reifier item))
+        (setf (%item-reifier item) (canonical-topic (%item-reifier item)))))
     (normalize-names map)
     (normalize-occurrences map)
     (normalize-associations map)
     (normalize-names map)
     (normalize-occurrences map)
     (normalize-associations map)
-    c))
+    (canonical-topic c)))
 
 (defun normalize-topic-map (map)
 
 (defun normalize-topic-map (map)
-  "Apply same-map identity merging, then remove equal Names, Occurrences, Roles and Associations.
-Only implemented properties participate; no cross-map merging."
+  "Reach a fixed point of same-map Topic and structural item merging."
   (check-type map topic-map)
   (check-type map topic-map)
-  (loop for pair = (equal-pair (%topic-map-topics map) #'equal-topics-p)
-        while pair do (merge-two-topics map (first pair) (second pair)))
-  (normalize-names map)
-  (normalize-occurrences map)
-  (normalize-associations map)
-  map)
+  (when (needs-merge-preflight-p map)
+    (return-from normalize-topic-map
+      (preflight-merge-mutation map (lambda (ref) (normalize-topic-map (funcall ref map))))))
+  (validate-map-core-subjects map)
+  (check-variant-merge-constraints map)
+  (loop for before = (length (map-information-items map)) do
+    (loop for pair = (equal-pair (%topic-map-topics map) #'equal-topics-p)
+          while pair do
+      ;; A staged import may contain equal owners/types AND equal Reifier
+      ;; Topics whose Names/Occurrences/etc. have not merged yet. First perform
+      ;; available nonconflicting Topic merges; their ordinary item merging can
+      ;; resolve those Reified references. Do not let collection order invent a
+      ;; conflict. If none can proceed, MERGE-TWO-TOPICS still signals TMDM 6.2's
+      ;; conflict; no equality or conflict rule is relaxed.
+      (let ((ready (equal-pair (%topic-map-topics map)
+                     (lambda (a b)
+                       (and (equal-topics-p a b)
+                            (let ((left (topic-reified a)) (right (topic-reified b)))
+                              (not (and left right (not (eq left right))))))))))
+        (let ((next (or ready pair)))
+          (merge-two-topics map (first next) (second next)))))
+    (normalize-names map) (normalize-occurrences map) (normalize-associations map)
+    when (= before (length (map-information-items map))) return map))
 
 (defun live-topic (topic)
   (let ((current (canonical-topic topic)))
 
 (defun live-topic (topic)
   (let ((current (canonical-topic topic)))
@@ -129,6 +161,9 @@ Explicit merging may assert shared subject identity beyond locator equality.
 Cross-map merging is rejected before mutation. Obsolete handles remain resolvable."
   (let* ((a (live-topic a)) (b (live-topic b)) (map (%topic-parent a)))
     (unless (eq map (%topic-parent b)) (error "Cross-map Topic merging is outside this API."))
 Cross-map merging is rejected before mutation. Obsolete handles remain resolvable."
   (let* ((a (live-topic a)) (b (live-topic b)) (map (%topic-parent a)))
     (unless (eq map (%topic-parent b)) (error "Cross-map Topic merging is outside this API."))
+    (when (needs-merge-preflight-p map)
+      (return-from merge-topics (preflight-merge-mutation map
+        (lambda (ref) (merge-topics (funcall ref a) (funcall ref b))))))
     (unless (eq a b) (setf a (merge-two-topics map a b)))
     (normalize-topic-map map)
     (canonical-topic a)))
     (unless (eq a b) (setf a (merge-two-topics map a b)))
     (normalize-topic-map map)
     (canonical-topic a)))
@@ -139,9 +174,16 @@ KIND is :ITEM-IDENTIFIER, :SUBJECT-IDENTIFIER or :SUBJECT-LOCATOR.
 IRI is an already resolved, nonempty absolute locator string; CTM validates it."
   (unless (and (stringp iri) (plusp (length iri))) (error "A nonempty locator string is required."))
   (let ((topic (live-topic topic)))
 IRI is an already resolved, nonempty absolute locator string; CTM validates it."
   (unless (and (stringp iri) (plusp (length iri))) (error "A nonempty locator string is required."))
   (let ((topic (live-topic topic)))
+    (when (needs-merge-preflight-p (%topic-parent topic))
+      (return-from add-topic-identifier (preflight-merge-mutation (%topic-parent topic)
+        (lambda (ref) (add-topic-identifier (funcall ref topic) kind iri)))))
+    (when (eq kind :subject-identifier)
+      (check-core-subject-identifiers (cons iri (%topic-subject-identifiers topic))))
     (check-topic-occurrence-identifiers (%topic-parent topic) kind iri)
     (check-topic-occurrence-identifiers (%topic-parent topic) kind iri)
+    ;; Check the entire prospective identity closure before adding the locator.
+    (check-variant-merge-constraints (%topic-parent topic) :identifier (list topic kind iri))
     (ecase kind
     (ecase kind
-      (:item-identifier (pushnew iri (%topic-item-identifiers topic) :test #'string=))
+      (:item-identifier (pushnew (copy-seq iri) (%topic-item-identifiers topic) :test #'string=))
       (:subject-identifier (pushnew iri (%topic-subject-identifiers topic) :test #'string=))
       (:subject-locator (pushnew iri (%topic-subject-locators topic) :test #'string=)))
     (normalize-topic-map (%topic-parent topic))
       (:subject-identifier (pushnew iri (%topic-subject-identifiers topic) :test #'string=))
       (:subject-locator (pushnew iri (%topic-subject-locators topic) :test #'string=)))
     (normalize-topic-map (%topic-parent topic))
@@ -154,6 +196,11 @@ IRI is an already resolved, nonempty absolute locator string; CTM validates it."
   ;; Validate the category even for an empty map, before adding a Topic.
   (ecase kind ((:item-identifier :subject-identifier :subject-locator)))
   (check-topic-occurrence-identifiers map kind iri)
   ;; Validate the category even for an empty map, before adding a Topic.
   (ecase kind ((:item-identifier :subject-identifier :subject-locator)))
   (check-topic-occurrence-identifiers map kind iri)
+  ;; Preflight BEFORE creating a new Topic, so rejected cascades cannot leave
+  ;; even an empty newly interned item in the original Map.
+  (when (needs-merge-preflight-p map)
+    (return-from intern-topic (preflight-merge-mutation map
+      (lambda (ref) (intern-topic (funcall ref map) kind iri)))))
   (let ((topic (or (find-if (lambda (topic) (identifier-matches-p topic kind iri))
                            (%topic-map-topics map))
                    (let ((new (%make-topic map)))
   (let ((topic (or (find-if (lambda (topic) (identifier-matches-p topic kind iri))
                            (%topic-map-topics map))
                    (let ((new (%make-topic map)))