]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/occurrences.lisp
Implement Date Occurrences with lexical validation and CXTM canonicalization
[topic-maps.git] / src / tmdm / occurrences.lisp
1 (in-package #:topic-maps.tmdm)
2
3 (define-condition occurrence-error (error)
4 ((code :initarg :code :reader occurrence-error-code)
5 (detail :initarg :detail :reader occurrence-error-detail))
6 (:report (lambda (c out) (format out "Occurrence ~A: ~A" (occurrence-error-code c) (occurrence-error-detail c)))))
7 (defun occurrence-problem (code detail) (error 'occurrence-error :code code :detail detail))
8 (defun absolute-locator-p (value)
9 ;; Model inputs are already validated/resolved locators, not CTM source.
10 (and (stringp value)
11 (let ((colon (position #\: value)))
12 (and colon (plusp colon)
13 (let ((head (subseq value 0 colon)))
14 (and (or (char<= #\a (char head 0) #\z) (char<= #\A (char head 0) #\Z))
15 (every (lambda (c) (or (char<= #\a c #\z) (char<= #\A c #\Z)
16 (char<= #\0 c #\9) (find c "+-."))) head)))))))
17 (defun same-occurrence-properties-p (a value datatype type)
18 ;; Parent is the containing topic; scope is empty throughout this subset.
19 (and (string= (%occurrence-value a) value) (string= (%occurrence-datatype a) datatype)
20 (eq (%occurrence-type a) type)))
21 (defun check-topic-occurrence-identifiers (map kind iri)
22 (when (member kind '(:item-identifier :subject-identifier))
23 (dolist (topic (%topic-map-topics map))
24 (dolist (occurrence (%topic-occurrences topic))
25 (when (member iri (%occurrence-item-identifiers occurrence) :test #'string=)
26 (occurrence-problem (if (eq kind :item-identifier) :duplicate-item-identifier :unsupported-reification-identity)
27 "Topic identity conflicts with an existing Occurrence item identifier."))))))
28 (defun validate-occurrence-identifiers (map topic type value datatype identifiers)
29 (unless (and (listp identifiers) (every #'absolute-locator-p identifiers))
30 (occurrence-problem :invalid-item-identifiers "Item identifiers must be a list of resolved absolute locators."))
31 (dolist (id identifiers)
32 (dolist (owner (%topic-map-topics map))
33 (when (member id (%topic-item-identifiers owner) :test #'string=)
34 (occurrence-problem :duplicate-item-identifier "An Item Identifier is already assigned to a Topic."))
35 (when (member id (%topic-subject-identifiers owner) :test #'string=)
36 (occurrence-problem :unsupported-reification-identity "Topic/Occurrence reification identity is outside this model."))
37 (dolist (other (%topic-occurrences owner))
38 (when (and (member id (%occurrence-item-identifiers other) :test #'string=)
39 (not (and (eq owner topic) (same-occurrence-properties-p other value datatype type))))
40 (occurrence-problem :duplicate-item-identifier "An Item Identifier is already assigned to a different Occurrence."))))))
41 (defun normalize-occurrences (map)
42 "TMDM 5.6/6.5, empty scope/null reifier; replace duplicates and union identifiers."
43 (dolist (topic (%topic-map-topics map))
44 (loop for pair = (equal-pair (%topic-occurrences topic)
45 (lambda (a b) (same-occurrence-properties-p a (%occurrence-value b)
46 (%occurrence-datatype b) (%occurrence-type b))))
47 while pair do
48 (let* ((a (first pair)) (b (second pair))
49 (c (%make-occurrence (%occurrence-value a) (%occurrence-datatype a) (%occurrence-type a) topic)))
50 (setf (%occurrence-item-identifiers c)
51 (set-union (%occurrence-item-identifiers a) (%occurrence-item-identifiers b) #'string=)
52 (%topic-occurrences topic) (replace-pair (%topic-occurrences topic) a b c)
53 (%occurrence-forward a) c (%occurrence-forward b) c))))
54 map)
55 (defun add-occurrence (topic type value &key (datatype (string-datatype)) item-identifiers)
56 "Create an unscoped Occurrence; TYPE must be a same-map Topic. VALUE and DATATYPE
57 are strings; DATATYPE is any absolute locator (TMDM 4.4). Supported Integer/Decimal/Date
58 lexical forms are validated; stored strings are never rewritten.
59 anyURI VALUE must already be absolute, never resolved here.
60 Validate before mutation, return current representative after duplicate replacement."
61 (check-type value string) (check-type datatype string)
62 ;; TMDM 4.4/5.6: datatype is a locator, never a Topic or a whitelist.
63 (unless (absolute-locator-p datatype)
64 (occurrence-problem :invalid-datatype "Datatype must be a resolved absolute locator."))
65 (when (and (string= datatype (integer-datatype)) (not (integer-lexical-p value)))
66 (occurrence-problem :invalid-integer-value "Value is not an xsd:integer lexical form."))
67 (when (and (string= datatype (decimal-datatype)) (not (decimal-lexical-p value)))
68 (occurrence-problem :invalid-decimal-value "Value is not an xsd:decimal lexical form."))
69 (when (and (string= datatype (date-datatype)) (not (date-lexical-p value)))
70 (occurrence-problem :invalid-date-value "Value is not an XSD 1.0 date lexical form or valid Gregorian date."))
71 (when (and (string= datatype (iri-datatype)) (not (absolute-locator-p value)))
72 (occurrence-problem :relative-uri-value "TMDM IRI-valued Occurrences require an absolute locator."))
73 (let* ((topic (live-topic topic)) (type (live-topic type)) (map (%topic-parent topic)))
74 (unless (eq map (%topic-parent type))
75 (occurrence-problem :foreign-type "Occurrence Parent and Type must belong to the same map."))
76 (validate-occurrence-identifiers map topic type value datatype item-identifiers)
77 (let ((item (%make-occurrence value datatype type topic)))
78 (setf (%occurrence-item-identifiers item) (remove-duplicates (copy-list item-identifiers) :test #'string= :from-end t)
79 (%topic-occurrences topic) (append (%topic-occurrences topic) (list item)))
80 (normalize-occurrences map)
81 (canonical-occurrence item))))