]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/items.lisp
feat(tmdm): merge same-map topic identities and retarget name references
[topic-maps.git] / src / tmdm / items.lisp
1 (in-package #:topic-maps.tmdm)
2
3 ;;; Only Topic Map, Topic and unscoped Name properties are materialized.
4 ;;; Omitted set-valued properties are implicitly empty, single-valued ones null
5 ;;; (CTM 3.2). In particular all supported names have empty scope and variants.
6 (defstruct (topic-map (:constructor make-topic-map ()) (:conc-name %topic-map-)) topics)
7 (defstruct (topic (:constructor %make-topic (parent)) (:conc-name %topic-))
8 (parent nil :read-only t) item-identifiers subject-identifiers subject-locators names
9 ;; Implementation bookkeeping, not a TMDM property or another information item.
10 forward)
11 (defstruct (topic-name (:constructor %make-topic-name (value type parent)) (:conc-name %topic-name-))
12 (value "" :type string :read-only t)
13 (type nil :type topic)
14 (parent nil :type topic)
15 forward)
16
17 (defun canonical-topic (topic)
18 "Resolve a retained reference to a Topic replaced by same-map merging.
19 EQ on obsolete objects does not change; compare their canonical representatives."
20 (check-type topic topic)
21 (loop for current = topic then (%topic-forward current)
22 unless (%topic-forward current) return current))
23 (defun canonical-topic-name (name)
24 "Resolve a retained reference to a Name replaced during duplicate removal."
25 (check-type name topic-name)
26 (loop for current = name then (%topic-name-forward current)
27 unless (%topic-name-forward current) return current))
28
29 ;; Public readers resolve obsolete handles and expose copies of set storage.
30 ;; Mutations must use the identity/merge API to maintain model invariants.
31 (defun topic-map-topics (map) (copy-list (%topic-map-topics map)))
32 (defun topic-parent (topic) (%topic-parent (canonical-topic topic)))
33 (defun topic-item-identifiers (topic) (copy-list (%topic-item-identifiers (canonical-topic topic))))
34 (defun topic-subject-identifiers (topic) (copy-list (%topic-subject-identifiers (canonical-topic topic))))
35 (defun topic-subject-locators (topic) (copy-list (%topic-subject-locators (canonical-topic topic))))
36 (defun topic-names (topic) (copy-list (%topic-names (canonical-topic topic))))
37 (defun topic-name-value (name) (%topic-name-value (canonical-topic-name name)))
38 (defun topic-name-type (name) (%topic-name-type (canonical-topic-name name)))
39 (defun topic-name-parent (name) (%topic-name-parent (canonical-topic-name name)))
40
41 ;; Avoid recursive printing through parent/type links; DESCRIBE remains useful.
42 (defmethod print-object ((item topic-map) stream)
43 (print-unreadable-object (item stream :type t :identity t)
44 (format stream "~D topics" (length (topic-map-topics item)))))
45 (defmethod print-object ((item topic) stream)
46 (print-unreadable-object (item stream :type t :identity t)
47 (format stream "II=~S SI=~S SL=~S" (topic-item-identifiers item)
48 (topic-subject-identifiers item) (topic-subject-locators item))))
49 (defmethod print-object ((item topic-name) stream)
50 (print-unreadable-object (item stream :type t :identity t)
51 (format stream "~S" (topic-name-value item))))
52
53 (defun default-name-type-identifier ()
54 "http://psi.topicmaps.org/iso13250/model/topic-name")