1 (in-package #:topic-maps.tmdm)
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 (define-condition occurrence-identifier-error (occurrence-error item-identifier-error) ())
9 (defun legacy-identifier-problem (condition)
10 ;; Existing callers may still catch OCCURRENCE-ERROR; the uniform API also
11 ;; exposes ITEM-IDENTIFIER-ERROR without breaking that condition contract.
12 (error 'occurrence-identifier-error :code (item-identifier-error-code condition)
13 :detail (item-identifier-error-detail condition)))
14 (defun same-occurrence-properties-p (a value datatype type scope)
15 ;; Parent is the containing topic; Scope is compared as a Topic set.
16 (and (string= (%occurrence-value a) value) (string= (%occurrence-datatype a) datatype)
17 (eq (%occurrence-type a) type) (same-scope-p (%occurrence-scope a) scope)))
18 (defun check-topic-occurrence-identifiers (map kind iri)
19 (when (eq kind :item-identifier)
20 (handler-case (validate-item-identifiers map (list iri) :topic-p t)
21 (item-identifier-error (c) (legacy-identifier-problem c))))
22 (when (eq kind :subject-identifier)
23 ;; Historical boundary only: this is NOT automatic Reification. Its
24 ;; treatment is deferred to Slice 16B; do not extend it to other item types.
25 (dolist (topic (%topic-map-topics map))
26 (dolist (occurrence (%topic-occurrences topic))
27 (when (member iri (%occurrence-item-identifiers occurrence) :test #'string=)
28 (error 'occurrence-identifier-error :code :unsupported-reification-identity
29 :detail "Topic SI / Occurrence II handling is deferred to Slice 16B."))))))
30 (defun validate-occurrence-identifiers (map topic type value datatype identifiers scope)
32 (validate-item-identifiers map identifiers
33 :allowed (remove-if-not (lambda (other) (same-occurrence-properties-p other value datatype type scope))
34 (%topic-occurrences topic)))
35 (item-identifier-error (c) (legacy-identifier-problem c)))
36 (dolist (id identifiers)
37 (dolist (owner (%topic-map-topics map))
38 (when (member id (%topic-subject-identifiers owner) :test #'string=)
39 (error 'occurrence-identifier-error :code :unsupported-reification-identity
40 :detail "Topic SI / Occurrence II handling is deferred to Slice 16B.")))))
41 (defun normalize-occurrences (map)
42 "TMDM 5.6/6.5, 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) (%occurrence-scope b))))
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-scope c) (copy-list (%occurrence-scope a))
51 (%occurrence-item-identifiers c)
52 (set-union (%occurrence-item-identifiers a) (%occurrence-item-identifiers b) #'string=)
53 (%topic-occurrences topic) (replace-pair (%topic-occurrences topic) a b c)
54 (%occurrence-forward a) c (%occurrence-forward b) c))))
56 (defun add-occurrence (topic type value &key (datatype (string-datatype)) item-identifiers scope)
57 "Create an Occurrence with a Scope Topic set; TYPE must be a same-map Topic. VALUE and DATATYPE
58 are strings; DATATYPE is any absolute locator (TMDM 4.4). Supported Integer/Decimal/Date/DateTime
59 lexical forms are validated; stored strings are never rewritten.
60 anyURI VALUE must already be absolute, never resolved here.
61 Validate before mutation, return current representative after duplicate replacement."
62 (check-type value string) (check-type datatype string)
63 ;; TMDM 4.4/5.6: datatype is a locator, never a Topic or a whitelist.
64 (unless (absolute-locator-p datatype)
65 (occurrence-problem :invalid-datatype "Datatype must be a resolved absolute locator."))
66 (when (and (string= datatype (integer-datatype)) (not (integer-lexical-p value)))
67 (occurrence-problem :invalid-integer-value "Value is not an xsd:integer lexical form."))
68 (when (and (string= datatype (decimal-datatype)) (not (decimal-lexical-p value)))
69 (occurrence-problem :invalid-decimal-value "Value is not an xsd:decimal lexical form."))
70 (when (and (string= datatype (date-datatype)) (not (date-lexical-p value)))
71 (occurrence-problem :invalid-date-value "Value is not an XSD 1.0 date lexical form or valid Gregorian date."))
72 (when (and (string= datatype (datetime-datatype)) (not (datetime-lexical-p value)))
73 (occurrence-problem :invalid-datetime-value "Value is not an XSD 1.0 dateTime lexical form."))
74 (when (and (string= datatype (iri-datatype)) (not (absolute-locator-p value)))
75 (occurrence-problem :relative-uri-value "TMDM IRI-valued Occurrences require an absolute locator."))
76 (let* ((topic (live-topic topic)) (type (live-topic type)) (map (%topic-parent topic))
77 (scope (canonical-scope map scope)))
78 (unless (eq map (%topic-parent type))
79 (occurrence-problem :foreign-type "Occurrence Parent and Type must belong to the same map."))
80 (validate-occurrence-identifiers map topic type value datatype item-identifiers scope)
81 (let ((item (%make-occurrence value datatype type topic)))
82 (setf (%occurrence-scope item) scope
83 (%occurrence-item-identifiers item) (mapcar #'copy-seq (remove-duplicates item-identifiers :test #'string= :from-end t))
84 (%topic-occurrences topic) (append (%topic-occurrences topic) (list item)))
85 (normalize-occurrences map)
86 (canonical-occurrence item))))