]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/cxtm/canonicalizer.lisp
Implement general TMDM item identifiers and canonical output
[topic-maps.git] / src / cxtm / canonicalizer.lisp
index 19c57ef75df39986e35ba4de471ecd20d6822844..6b40783e0292861d4ea7742175541a7a3308ac41 100644 (file)
           (if (not (zerop type)) type
               (let ((scope (set-order (topic-name-scope a) (topic-name-scope b) #'topic-order)))
                 (if (not (zerop scope)) scope (topic-order (topic-name-parent a) (topic-name-parent b)))))))))
-(defun occurrence-value-order (a b)
-  ;; TMDM 4.3/5.6: anyURI values are locators; other supported values are strings.
+(defun literal-value-order (value-a datatype-a value-b datatype-b)
+  ;; TMDM 4.3/5.5/5.6: anyURI values are locators; other supported values are strings.
   ;; CXTM 4.2 orders strings before locators, independent of their spelling.
-  (let ((iri-a (string= (occurrence-datatype a) (iri-datatype)))
-        (iri-b (string= (occurrence-datatype b) (iri-datatype))))
-    (cond ((and iri-a iri-b) (locator-order (occurrence-value a) (occurrence-value b)))
+  (let ((iri-a (string= datatype-a (iri-datatype)))
+        (iri-b (string= datatype-b (iri-datatype))))
+    (cond ((and iri-a iri-b) (locator-order value-a value-b))
           (iri-a 1) (iri-b -1)
-          (t (string-order (nfc (occurrence-value a)) (nfc (occurrence-value b)))))))
+          (t (string-order (nfc value-a) (nfc value-b))))))
+(defun occurrence-value-order (a b)
+  (literal-value-order (occurrence-value a) (occurrence-datatype a)
+                       (occurrence-value b) (occurrence-datatype b)))
+(defun variant-order (a b)
+  (loop for comparison in
+        (list (literal-value-order (variant-value a) (variant-datatype a) (variant-value b) (variant-datatype b))
+              (locator-order (variant-datatype a) (variant-datatype b))
+              (set-order (variant-scope a) (variant-scope b) #'topic-order)
+              (name-order (variant-parent a) (variant-parent b)))
+        when (not (zerop comparison)) return comparison finally (return 0)))
+
+(defun canonical-literal-value (value datatype)
+  "CXTM 3.16: shared Occurrence/Variant representation; never rewrite model data."
+  (cond ((string= datatype (iri-datatype)) (normalized-locator value))
+        ((string= datatype (integer-datatype)) (canonical-integer-lexical-form value))
+        ((string= datatype (decimal-datatype)) (canonical-decimal-lexical-form value))
+        ((string= datatype (date-datatype)) (canonical-date-lexical-form value))
+        ((string= datatype (datetime-datatype)) (canonical-datetime-lexical-form value))
+        (t value)))
 (defun occurrence-order (a b)
   (loop for comparison in
         (list (occurrence-value-order a b)
 (defun require-set (items)
   (unless (and (topic-maps.tmdm::proper-list-p items) (= (length items) (length (remove-duplicates items :test #'eq))))
     (fail :invalid-set "An information-item collection must be a proper set.")))
+(defun validate-literal-value (value datatype)
+  "Validate the shared Variant/Occurrence literal representation without mutation."
+  (unless (and (stringp value) (stringp datatype)) (fail :invalid-literal "Value and datatype must be strings."))
+  (unless (topic-maps.tmdm::absolute-locator-p datatype)
+    (fail :invalid-occurrence-datatype "Datatype must be an absolute locator."))
+  (when (and (string= datatype (integer-datatype))
+             (not (integer-lexical-p value)))
+    (fail :invalid-integer-value "Literal value is not an xsd:integer lexical form."))
+  (when (and (string= datatype (decimal-datatype))
+             (not (decimal-lexical-p value)))
+    (fail :invalid-decimal-value "Literal value is not an xsd:decimal lexical form."))
+  (when (string= datatype (date-datatype))
+    (unless (date-lexical-p value)
+      (fail :invalid-date-value "Literal value is not a valid XSD 1.0 date."))
+    (unless (canonical-date-lexical-form value)
+      (fail :date-timezone-canonicalization "XSD date recoverable-timezone canonicalization is outside this slice." t)))
+  (when (string= datatype (datetime-datatype))
+    (unless (datetime-lexical-p value)
+      (fail :invalid-datetime-value "Literal value is not a valid XSD 1.0 dateTime."))
+    (multiple-value-bind (value reason) (canonical-datetime-lexical-form value)
+      (unless value (fail (if (eq reason :unsupported-bce-timezone-rollover)
+                             :datetime-bce-timezone-rollover :datetime-leap-second-context)
+                          "DateTime BCE rollover/leap-second context is outside this canonical subset." t))))
+  (when (and (string= datatype (iri-datatype))
+             (not (topic-maps.tmdm::absolute-locator-p value)))
+    (fail :invalid-occurrence-value "IRI-valued literals must store an absolute locator."))
+  t)
 (defun validate-map (map)
   "Validate current model schema and referential integrity; never normalize/mutate."
   (require-exact-item map 'topic-map)
           (unless (and (eq name (canonical-topic-name name)) (eq (topic-name-parent name) topic))
             (fail :invalid-parent "Name must be current and owned by its containing Topic."))
           (reference (topic-name-type name))
-          (require-set (topic-name-scope name)) (mapc #'reference (topic-name-scope name)))
+          (require-set (topic-name-scope name)) (mapc #'reference (topic-name-scope name))
+          (require-set (topic-name-variants name))
+          (dolist (item (topic-name-variants name))
+            (require-exact-item item 'variant)
+            (unless (and (eq item (canonical-variant item)) (eq (variant-parent item) name))
+              (fail :invalid-parent "Variant must be current and owned by its containing Name."))
+            (require-set (variant-scope item)) (mapc #'reference (variant-scope item))
+            (unless (topic-maps.tmdm::variant-scope-valid-p (variant-scope item) (topic-name-scope name))
+              (fail :invalid-variant-scope "Variant scope must properly contain its Parent Name scope."))
+            (validate-literal-value (variant-value item) (variant-datatype item))))
         (require-set (topic-occurrences topic))
         (dolist (item (topic-occurrences topic))
           (require-exact-item item 'occurrence)
             (fail :invalid-parent "Occurrence must be current and owned by its containing Topic."))
           (reference (occurrence-type item))
           (require-set (occurrence-scope item)) (mapc #'reference (occurrence-scope item))
-          (unless (topic-maps.tmdm::absolute-locator-p (occurrence-datatype item))
-            (fail :invalid-occurrence-datatype "Datatype must be an absolute locator."))
-          (when (and (string= (occurrence-datatype item) (integer-datatype))
-                     (not (integer-lexical-p (occurrence-value item))))
-            (fail :invalid-integer-value "Occurrence value is not an xsd:integer lexical form."))
-          (when (and (string= (occurrence-datatype item) (decimal-datatype))
-                     (not (decimal-lexical-p (occurrence-value item))))
-            (fail :invalid-decimal-value "Occurrence value is not an xsd:decimal lexical form."))
-          (when (string= (occurrence-datatype item) (date-datatype))
-            (unless (date-lexical-p (occurrence-value item))
-              (fail :invalid-date-value "Occurrence value is not a valid XSD 1.0 date."))
-            (unless (canonical-date-lexical-form (occurrence-value item))
-              (fail :date-timezone-canonicalization "XSD date recoverable-timezone canonicalization is outside this slice." t)))
-          (when (string= (occurrence-datatype item) (datetime-datatype))
-            (unless (datetime-lexical-p (occurrence-value item))
-              (fail :invalid-datetime-value "Occurrence value is not a valid XSD 1.0 dateTime."))
-            (multiple-value-bind (value reason) (canonical-datetime-lexical-form (occurrence-value item))
-              (unless value (fail (if (eq reason :unsupported-bce-timezone-rollover)
-                                     :datetime-bce-timezone-rollover :datetime-leap-second-context)
-                                  "DateTime BCE rollover/leap-second context is outside this canonical subset." t))))
-          (when (and (string= (occurrence-datatype item) (iri-datatype))
-                     (not (topic-maps.tmdm::absolute-locator-p (occurrence-value item))))
-            (fail :invalid-occurrence-value "IRI-valued Occurrences must store an absolute locator."))
+          (validate-literal-value (occurrence-value item) (occurrence-datatype item))
           (let ((ids (occurrence-item-identifiers item)))
             (unless (and (every #'topic-maps.tmdm::absolute-locator-p ids)
                          (= (length ids) (length (remove-duplicates ids :test #'string=))))
           (require-exact-item r 'association-role)
           (unless (and (eq r (canonical-association-role r)) (eq (association-role-parent r) a))
             (fail :invalid-parent "Role must be current and owned by its Association."))
-          (reference (association-role-type r)) (reference (association-role-player r)))))))
+          (reference (association-role-type r)) (reference (association-role-player r))))))
+  (handler-case (topic-maps.tmdm::validate-map-item-identifiers map)
+    (topic-maps.tmdm:item-identifier-error (c)
+      (fail (topic-maps.tmdm:item-identifier-error-code c) (princ-to-string c)))))
 
 (defun xml-text (text out)
   (loop for c across (nfc text) for n = (char-code c) do
 
 (defun canonicalize (map &key base-locator)
   "Return CXTM as a string. BASE-LOCATOR is an explicit absolute IRI (CXTM 3.1).
-Only the existing TMDM schema is supported: Topic-set Scope, null Reifier, no
-Variants or non-Topic identifiers other than Occurrence identifiers. Does not mutate MAP or handles."
+Supports Scope, Variants and Item Identifiers on all seven item kinds; Reifier
+remains null. Does not mutate MAP or retained handles."
   (unless (and (stringp base-locator) (position #\: base-locator)
                (let ((scheme (subseq base-locator 0 (position #\: base-locator))))
                  (and (plusp (length scheme))
@@ -226,6 +262,7 @@ Variants or non-Topic identifiers other than Occurrence identifiers. Does not mu
                          (gethash (association-role-parent r) association-numbers)
                          (gethash r role-numbers))))
         (format out "<topicMap>~%")
+        (emit-locators "itemIdentifiers" (item-identifiers map) out)
         (loop for topic in topics for number from 1 do
           (format out "<topic number=\"~D\">~%" number)
           (emit-locators "subjectIdentifiers" (topic-subject-identifiers topic) out)
@@ -235,20 +272,19 @@ Variants or non-Topic identifiers other than Occurrence identifiers. Does not mu
             (format out "<name number=\"~D\">~%<value>" index)
             (xml-text (topic-name-value name) out) (format out "</value>~%")
             (type-ref (topic-name-type name))
-            (emit-scope (topic-name-scope name) topic-numbers out) (format out "</name>~%"))
+            (emit-scope (topic-name-scope name) topic-numbers out)
+            (loop for item in (sorted (topic-name-variants name) #'variant-order) for number from 1 do
+              (format out "<variant number=\"~D\">~%<value>" number)
+              (xml-text (canonical-literal-value (variant-value item) (variant-datatype item)) out)
+              (format out "</value>~%<datatype>") (xml-text (variant-datatype item) out)
+              (format out "</datatype>~%") (emit-scope (variant-scope item) topic-numbers out)
+              (emit-locators "itemIdentifiers" (item-identifiers item) out)
+              (format out "</variant>~%"))
+            (emit-locators "itemIdentifiers" (item-identifiers name) out)
+            (format out "</name>~%"))
           (loop for item in (sorted (topic-occurrences topic) #'occurrence-order) for index from 1 do
             (format out "<occurrence number=\"~D\">~%<value>" index)
-            (xml-text (cond ((string= (occurrence-datatype item) (iri-datatype))
-                             (normalized-locator (occurrence-value item)))
-                            ((string= (occurrence-datatype item) (integer-datatype))
-                             (canonical-integer-lexical-form (occurrence-value item)))
-                            ((string= (occurrence-datatype item) (decimal-datatype))
-                             (canonical-decimal-lexical-form (occurrence-value item)))
-                            ((string= (occurrence-datatype item) (date-datatype))
-                             (canonical-date-lexical-form (occurrence-value item)))
-                            ((string= (occurrence-datatype item) (datetime-datatype))
-                             (canonical-datetime-lexical-form (occurrence-value item)))
-                            (t (occurrence-value item))) out)
+            (xml-text (canonical-literal-value (occurrence-value item) (occurrence-datatype item)) out)
             (format out "</value>~%<datatype>")
             ;; CXTM 3.14: datatype locator is NOT base-normalized.
             (xml-text (occurrence-datatype item) out) (format out "</datatype>~%")
@@ -264,8 +300,11 @@ Variants or non-Topic identifiers other than Occurrence identifiers. Does not mu
           (loop for r in (sorted (association-roles a) #'role-order) for index from 1 do
             (format out "<role number=\"~D\">~%<player topicref=\"~D\"></player>~%"
                     index (gethash (association-role-player r) topic-numbers))
-            (type-ref (association-role-type r)) (format out "</role>~%"))
+            (type-ref (association-role-type r))
+            (emit-locators "itemIdentifiers" (item-identifiers r) out)
+            (format out "</role>~%"))
           (emit-scope (association-scope a) topic-numbers out)
+          (emit-locators "itemIdentifiers" (item-identifiers a) out)
           (format out "</association>~%"))
         ;; CXTM 3.1 step 2 appends LF after EVERY element, including the root.
         (format out "</topicMap>~%")))))