X-Git-Url: https://dreyeck.freedombox.rocks/gitweb/topic-maps.git/blobdiff_plain/8036a2b1ce009218f33d300edcc5f715ec1d1124..6a2f8446afca0915882f0d4a06cb891ccfcdc96d:/src/cxtm/canonicalizer.lisp diff --git a/src/cxtm/canonicalizer.lisp b/src/cxtm/canonicalizer.lisp index 8f56cc9..6d544ef 100644 --- a/src/cxtm/canonicalizer.lisp +++ b/src/cxtm/canonicalizer.lisp @@ -48,6 +48,23 @@ (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))))))) +(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. + (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))) + (iri-a 1) (iri-b -1) + (t (string-order (nfc (occurrence-value a)) (nfc (occurrence-value b))))))) +(defun occurrence-order (a b) + (loop for comparison in + (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. + (topic-order (occurrence-parent a) (occurrence-parent b))) + when (not (zerop comparison)) return comparison finally (return 0))) + (defun role-signature-order (a b) ;; In a single parent's roles set, [parent] is equal. During association ;; comparison equal player/type pairs also have equal parent-comparison @@ -94,7 +111,40 @@ (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-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)) + (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.")) + (let ((ids (occurrence-item-identifiers item))) + (unless (and (every #'topic-maps.tmdm::absolute-locator-p ids) + (= (length ids) (length (remove-duplicates ids :test #'string=)))) + (fail :invalid-locators "Occurrence identifiers must be an absolute locator set."))))) (dolist (a associations) (require-exact-item a 'association) (unless (and (eq a (canonical-association a)) (eq (association-parent a) map)) @@ -126,7 +176,7 @@ (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 -Occurrences/Variants or non-Topic identifiers. Does not mutate MAP or handles." +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)))) (and (plusp (length scheme)) @@ -168,6 +218,25 @@ Occurrences/Variants or non-Topic identifiers. Does not mutate MAP or handles." (format out "~%" index) (xml-text (topic-name-value name) out) (format out "~%") (type-ref (topic-name-type name)) (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-locators "itemIdentifiers" (occurrence-item-identifiers item) out) + (format out "~%")) (dolist (r (sorted (topic-roles-played topic) #'role-order)) (role-ref r)) (format out "~%")) (loop for a in associations for number from 1 do