X-Git-Url: https://dreyeck.freedombox.rocks/gitweb/topic-maps.git/blobdiff_plain/17dd58775e25ab495e3c83deb4b5a36f5e5a124b..aeb017e4f4846f2e6a701e581d20eef84a8b83d9:/src/cxtm/canonicalizer.lisp diff --git a/src/cxtm/canonicalizer.lisp b/src/cxtm/canonicalizer.lisp index ab3800f..19c57ef 100644 --- a/src/cxtm/canonicalizer.lisp +++ b/src/cxtm/canonicalizer.lisp @@ -47,7 +47,9 @@ (let ((c (string-order (nfc (topic-name-value a)) (nfc (topic-name-value b))))) (if (not (zerop c)) c (let ((type (topic-order (topic-name-type a) (topic-name-type b)))) - (if (not (zerop type)) type (topic-order (topic-name-parent a) (topic-name-parent b))))))) + (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. ;; CXTM 4.2 orders strings before locators, independent of their spelling. @@ -61,7 +63,7 @@ (list (occurrence-value-order a b) (locator-order (occurrence-datatype a) (occurrence-datatype b)) (topic-order (occurrence-type a) (occurrence-type b)) - ;; Both scopes are empty; parent comparison is the final key. + (set-order (occurrence-scope a) (occurrence-scope b) #'topic-order) (topic-order (occurrence-parent a) (occurrence-parent b))) when (not (zerop comparison)) return comparison finally (return 0))) @@ -74,7 +76,10 @@ (defun association-order (a b) (let ((c (topic-order (association-type a) (association-type b)))) (if (not (zerop c)) c - (set-order (association-roles a) (association-roles b) #'role-signature-order)))) + (let ((roles (set-order (association-roles a) (association-roles b) #'role-signature-order))) + ;; Parent Map is equal for all associations compared by this serializer. + (if (not (zerop roles)) roles + (set-order (association-scope a) (association-scope b) #'topic-order)))))) (defun role-order (a b) (let ((c (role-signature-order a b))) (if (not (zerop c)) c @@ -85,7 +90,7 @@ (unless (eq (type-of item) type) (fail :unsupported-information-item (format nil "Expected exact ~S, received ~S." type (type-of item)) t))) (defun require-set (items) - (unless (and (listp items) (= (length items) (length (remove-duplicates items :test #'eq)))) + (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-map (map) "Validate current model schema and referential integrity; never normalize/mutate." @@ -111,25 +116,35 @@ (require-exact-item name 'topic-name) (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))) + (reference (topic-name-type name)) + (require-set (topic-name-scope name)) (mapc #'reference (topic-name-scope name))) (require-set (topic-occurrences topic)) (dolist (item (topic-occurrences topic)) (require-exact-item item 'occurrence) (unless (and (eq item (canonical-occurrence item)) (eq (occurrence-parent item) topic)) (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.")) - ;; CXTM 3.16: these three remaining datatypes need additional canonical - ;; lexical rules. Unknown datatypes use Other, without interpretation. - (when (member (occurrence-datatype item) - '("http://www.w3.org/2001/XMLSchema#decimal" - "http://www.w3.org/2001/XMLSchema#date" - "http://www.w3.org/2001/XMLSchema#dateTime") :test #'string=) - (fail :unsupported-occurrence-datatype "CXTM 3.16 numeric/date canonicalization is not implemented." t)) + (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.")) @@ -141,7 +156,9 @@ (require-exact-item a 'association) (unless (and (eq a (canonical-association a)) (eq (association-parent a) map)) (fail :invalid-parent "Association must be current and owned by this map.")) - (reference (association-type a)) (require-set (association-roles a)) + (reference (association-type a)) + (require-set (association-scope a)) (mapc #'reference (association-scope a)) + (require-set (association-roles a)) (unless (association-roles a) (fail :empty-association "Association requires Roles.")) (dolist (r (association-roles a)) (require-exact-item r 'association-role) @@ -165,9 +182,17 @@ (format out "~%")) (format out "~%" tag))) +(defun emit-scope (themes topic-numbers out) + ;; CXTM 3.12/4.4/4.6. Empty Scope has no XML representation. + (when themes + (format out "~%") + (dolist (theme (sorted themes #'topic-order)) + (format out "~%" (gethash theme topic-numbers))) + (format out "~%"))) + (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: empty Scope, null Reifier, no +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." (unless (and (stringp base-locator) (position #\: base-locator) (let ((scheme (subseq base-locator 0 (position #\: base-locator)))) @@ -209,18 +234,26 @@ Variants or non-Topic identifiers other than Occurrence identifiers. Does not mu (loop for name in (sorted (topic-names topic) #'name-order) for index from 1 do (format out "~%" index) (xml-text (topic-name-value name) out) (format out "~%") - (type-ref (topic-name-type name)) (format out "~%")) + (type-ref (topic-name-type name)) + (emit-scope (topic-name-scope name) topic-numbers out) (format out "~%")) (loop for item in (sorted (topic-occurrences topic) #'occurrence-order) for index from 1 do (format out "~%" 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) (format out "~%") ;; CXTM 3.14: datatype locator is NOT base-normalized. (xml-text (occurrence-datatype item) out) (format out "~%") (type-ref (occurrence-type item)) + (emit-scope (occurrence-scope item) topic-numbers out) (emit-locators "itemIdentifiers" (occurrence-item-identifiers item) out) (format out "~%")) (dolist (r (sorted (topic-roles-played topic) #'role-order)) (role-ref r)) @@ -232,6 +265,7 @@ Variants or non-Topic identifiers other than Occurrence identifiers. Does not mu (format out "~%~%" index (gethash (association-role-player r) topic-numbers)) (type-ref (association-role-type r)) (format out "~%")) + (emit-scope (association-scope a) topic-numbers out) (format out "~%")) ;; CXTM 3.1 step 2 appends LF after EVERY element, including the root. (format out "~%")))))