]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/tmdm/merging.lisp
Support explicitly typed Topic Names with merge and CXTM evidence
[topic-maps.git] / src / tmdm / merging.lisp
index 3221610e1245c3aee3dcd2cac75fdc42425251c6..c03a833ed13b5f1b8b994d42290c5893da1248e0 100644 (file)
 
 (defun merge-two-topics (map a b)
   ;; TMDM 6.2: create C, union supported properties, replace model references.
-  ;; Unimplemented associations/scope/reifiers cannot contain hidden references.
+  ;; Scope/reifiers remain unimplemented; Association/Role refs are materialized.
   (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=)
           (%topic-subject-locators c) (set-union (%topic-subject-locators a) (%topic-subject-locators b) #'string=)
           (%topic-names c) (set-union (%topic-names a) (%topic-names b) #'eq)
+          (%topic-occurrences c) (set-union (%topic-occurrences a) (%topic-occurrences b) #'eq)
           (%topic-map-topics map) (replace-pair (%topic-map-topics map) a b c)
           (%topic-forward a) c (%topic-forward b) c)
     (dolist (topic (%topic-map-topics map))
         ;; 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)))))
+    (dolist (topic (%topic-map-topics map))
+      (dolist (occurrence (%topic-occurrences topic))
+        (setf (%occurrence-parent occurrence) topic
+              (%occurrence-type occurrence) (canonical-topic (%occurrence-type occurrence)))))
+    (dolist (association (%topic-map-associations map))
+      (setf (%association-type association) (canonical-topic (%association-type association)))
+      (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)))))
     (normalize-names map)
+    (normalize-occurrences map)
+    (normalize-associations map)
     c))
 
 (defun normalize-topic-map (map)
-  "Apply same-map identity merging to a fixed point, then remove equal Names.
-Only implemented Topic/Name properties participate; no cross-map merging."
+  "Apply same-map identity merging, then remove equal Names, Occurrences, Roles and Associations.
+Only implemented properties participate; no cross-map merging."
   (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)
 
 (defun live-topic (topic)
@@ -96,6 +110,7 @@ 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)))
+    (check-topic-occurrence-identifiers (%topic-parent topic) kind iri)
     (ecase kind
       (:item-identifier (pushnew iri (%topic-item-identifiers topic) :test #'string=))
       (:subject-identifier (pushnew iri (%topic-subject-identifiers topic) :test #'string=))
@@ -109,6 +124,7 @@ 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."))
   ;; 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)
   (let ((topic (or (find-if (lambda (topic) (identifier-matches-p topic kind iri))
                            (%topic-map-topics map))
                    (let ((new (%make-topic map)))
@@ -116,17 +132,31 @@ IRI is an already resolved, nonempty absolute locator string; CTM validates it."
                      new))))
     (add-topic-identifier topic kind iri)))
 
-(defun add-default-name (topic value)
-  "CTM 3.11 default type; equal unscoped names share one information item."
+(defun add-name (topic value &key (type nil type-supplied-p))
+  "Add an unscoped Name with VALUE to TOPIC (TMDM 5.4).
+TYPE, when supplied, must be a Topic in the same map. When omitted, use the
+CTM 3.11 default name type. Resolve retained Topic handles before storing
+references. Equal value/type/parent returns the existing Name."
   (check-type value string)
   (let* ((topic (live-topic topic))
-         (type (intern-topic (%topic-parent topic) :subject-identifier (default-name-type-identifier)))
+         (type (if type-supplied-p
+                   (live-topic type)
+                   (intern-topic (%topic-parent topic) :subject-identifier
+                                 (default-name-type-identifier))))
+         ;; Interning the default type may itself merge the parent Topic.
          (topic (canonical-topic topic))
-         (existing (find-if (lambda (name)
-                             (and (string= value (%topic-name-value name))
-                                  (eq type (%topic-name-type name))))
-                           (%topic-names topic))))
-    (or existing
-        (let ((name (%make-topic-name value type topic)))
-          (setf (%topic-names topic) (append (%topic-names topic) (list name)))
-          name))))
+         (type (canonical-topic type)))
+    (unless (eq (%topic-parent topic) (%topic-parent type))
+      (error "Name type belongs to another map."))
+    (let ((existing (find-if (lambda (name)
+                               (and (string= value (%topic-name-value name))
+                                    (eq type (%topic-name-type name))))
+                             (%topic-names topic))))
+      (or existing
+          (let ((name (%make-topic-name value type topic)))
+            (setf (%topic-names topic) (append (%topic-names topic) (list name)))
+            name)))))
+
+(defun add-default-name (topic value)
+  "Compatibility entry point for ADD-NAME with the CTM 3.11 default type."
+  (add-name topic value))