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 (defun absolute-locator-p (value)
9 ;; Model inputs are already validated/resolved locators, not CTM source.
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))))
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))))
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; anyURI VALUE must already be absolute (TMDM 4.3), never resolved here.
58 Validate before mutation, return current representative after duplicate replacement."
59 (check-type value string) (check-type datatype string)
60 (unless (member datatype (list (string-datatype) (iri-datatype)) :test #'string=)
61 (occurrence-problem :unsupported-datatype "Only xsd:string and xsd:anyURI are supported."))
62 (when (and (string= datatype (iri-datatype)) (not (absolute-locator-p value)))
63 (occurrence-problem :relative-uri-value "TMDM IRI-valued Occurrences require an absolute locator."))
64 (let* ((topic (live-topic topic)) (type (live-topic type)) (map (%topic-parent topic)))
65 (unless (eq map (%topic-parent type))
66 (occurrence-problem :foreign-type "Occurrence Parent and Type must belong to the same map."))
67 (validate-occurrence-identifiers map topic type value datatype item-identifiers)
68 (let ((item (%make-occurrence value datatype type topic)))
69 (setf (%occurrence-item-identifiers item) (remove-duplicates (copy-list item-identifiers) :test #'string= :from-end t)
70 (%topic-occurrences topic) (append (%topic-occurrences topic) (list item)))
71 (normalize-occurrences map)
72 (canonical-occurrence item))))