]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/occurrences.lisp
test(ctm): register original empty2 prolog contract
[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 (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 ;; TMDM 5.3.5's SI/II cross-identity rule concerns two Topics, not an
20 ;; implicit Reification between a Topic and a non-Topic construct.
21 (when (eq kind :item-identifier)
22 (handler-case (validate-item-identifiers map (list iri) :topic-p t)
23 (item-identifier-error (c) (legacy-identifier-problem c)))))
24 (defun validate-occurrence-identifiers (map topic type value datatype identifiers scope)
25 (handler-case
26 (validate-item-identifiers map identifiers
27 :allowed (remove-if-not (lambda (other) (same-occurrence-properties-p other value datatype type scope))
28 (%topic-occurrences topic)))
29 (item-identifier-error (c) (legacy-identifier-problem c))))
30 (defun normalize-occurrences (map)
31 "TMDM 5.6/6.5: rescan live owners after Reifier-induced merges."
32 (loop for entry = (loop for topic in (%topic-map-topics map)
33 for pair = (equal-pair (%topic-occurrences topic)
34 (lambda (a b) (same-occurrence-properties-p a (%occurrence-value b)
35 (%occurrence-datatype b) (%occurrence-type b) (%occurrence-scope b))))
36 when pair return (cons topic pair))
37 while entry do
38 (let* ((topic (first entry)) (a (second entry)) (b (third entry))
39 (c (%make-occurrence (%occurrence-value a) (%occurrence-datatype a) (%occurrence-type a) topic)))
40 (setf (%occurrence-scope c) (copy-list (%occurrence-scope a))
41 (%occurrence-item-identifiers c) (set-union (%occurrence-item-identifiers a) (%occurrence-item-identifiers b) #'string=)
42 (%topic-occurrences topic) (replace-pair (%topic-occurrences topic) a b c)
43 (%occurrence-forward a) c (%occurrence-forward b) c)
44 (merge-item-reifiers c a b)))
45 map)
46 (defun add-occurrence (topic type value &key (datatype (string-datatype)) item-identifiers scope)
47 "Create an Occurrence with a Scope Topic set; TYPE must be a same-map Topic. VALUE and DATATYPE
48 are strings; DATATYPE is any absolute locator (TMDM 4.4). Supported Integer/Decimal/Date/DateTime
49 lexical forms are validated; stored strings are never rewritten.
50 anyURI VALUE must already be absolute, never resolved here.
51 Validate before mutation, return current representative after duplicate replacement."
52 (check-type value string) (check-type datatype string)
53 ;; TMDM 4.4/5.6: datatype is a locator, never a Topic or a whitelist.
54 (unless (absolute-locator-p datatype)
55 (occurrence-problem :invalid-datatype "Datatype must be a resolved absolute locator."))
56 (when (and (string= datatype (integer-datatype)) (not (integer-lexical-p value)))
57 (occurrence-problem :invalid-integer-value "Value is not an xsd:integer lexical form."))
58 (when (and (string= datatype (decimal-datatype)) (not (decimal-lexical-p value)))
59 (occurrence-problem :invalid-decimal-value "Value is not an xsd:decimal lexical form."))
60 (when (and (string= datatype (date-datatype)) (not (date-lexical-p value)))
61 (occurrence-problem :invalid-date-value "Value is not an XSD 1.0 date lexical form or valid Gregorian date."))
62 (when (and (string= datatype (datetime-datatype)) (not (datetime-lexical-p value)))
63 (occurrence-problem :invalid-datetime-value "Value is not an XSD 1.0 dateTime lexical form."))
64 (when (and (string= datatype (iri-datatype)) (not (absolute-locator-p value)))
65 (occurrence-problem :relative-uri-value "TMDM IRI-valued Occurrences require an absolute locator."))
66 (let* ((topic (live-topic topic)) (type (live-topic type)) (map (%topic-parent topic))
67 (scope (canonical-scope map scope)))
68 (unless (eq map (%topic-parent type))
69 (occurrence-problem :foreign-type "Occurrence Parent and Type must belong to the same map."))
70 (validate-occurrence-identifiers map topic type value datatype item-identifiers scope)
71 (let ((item (%make-occurrence value datatype type topic)))
72 (setf (%occurrence-scope item) scope
73 (%occurrence-item-identifiers item) (mapcar #'copy-seq (remove-duplicates item-identifiers :test #'string= :from-end t))
74 (%topic-occurrences topic) (append (%topic-occurrences topic) (list item)))
75 (normalize-occurrences map)
76 (canonical-occurrence item))))