]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/items.lisp
Implement CXTM canonicalization for supported TMDM items
[topic-maps.git] / src / tmdm / items.lisp
1 (in-package #:topic-maps.tmdm)
2
3 ;;; Only Topic Map, Topic, unscoped Name, Association and Role properties used
4 ;;; by the current subset are materialized.
5 ;;; Omitted set-valued properties are implicitly empty, single-valued ones null
6 ;;; (CTM 3.2). In particular all supported names have empty scope and variants.
7 (defstruct (topic-map (:constructor make-topic-map ()) (:conc-name %topic-map-)) topics associations)
8 (defstruct (topic (:constructor %make-topic (parent)) (:conc-name %topic-))
9 (parent nil :read-only t) item-identifiers subject-identifiers subject-locators names
10 ;; Implementation bookkeeping, not a TMDM property or another information item.
11 forward)
12
13 (defstruct (association (:constructor %make-association (parent type)) (:conc-name %association-))
14 (parent nil :type topic-map :read-only t) (type nil :type topic) roles forward)
15 (defstruct (association-role (:constructor %make-association-role (parent type player))
16 (:conc-name %association-role-))
17 (parent nil :type association) (type nil :type topic) (player nil :type topic) forward)
18 (defstruct (topic-name (:constructor %make-topic-name (value type parent)) (:conc-name %topic-name-))
19 (value "" :type string :read-only t)
20 (type nil :type topic)
21 (parent nil :type topic)
22 forward)
23
24 (defun canonical-topic (topic)
25 "Resolve a retained reference to a Topic replaced by same-map merging.
26 EQ on obsolete objects does not change; compare their canonical representatives."
27 (check-type topic topic)
28 (loop for current = topic then (%topic-forward current)
29 unless (%topic-forward current) return current))
30 (defun canonical-topic-name (name)
31 "Resolve a retained reference to a Name replaced during duplicate removal."
32 (check-type name topic-name)
33 (loop for current = name then (%topic-name-forward current)
34 unless (%topic-name-forward current) return current))
35
36 (defun canonical-association (association)
37 "Resolve a retained Association handle after duplicate association merging."
38 (check-type association association)
39 (loop for current = association then (%association-forward current)
40 unless (%association-forward current) return current))
41 (defun canonical-association-role (role)
42 "Resolve a retained Role handle after role/association duplicate removal."
43 (check-type role association-role)
44 (loop for current = role then (%association-role-forward current)
45 unless (%association-role-forward current) return current))
46
47 ;; Public readers resolve obsolete handles and expose copies of set storage.
48 ;; Mutations must use the identity/merge API to maintain model invariants.
49 (defun topic-map-topics (map) (copy-list (%topic-map-topics map)))
50 (defun topic-map-associations (map) (copy-list (%topic-map-associations map)))
51 (defun topic-parent (topic) (%topic-parent (canonical-topic topic)))
52 (defun topic-item-identifiers (topic) (copy-list (%topic-item-identifiers (canonical-topic topic))))
53 (defun topic-subject-identifiers (topic) (copy-list (%topic-subject-identifiers (canonical-topic topic))))
54 (defun topic-subject-locators (topic) (copy-list (%topic-subject-locators (canonical-topic topic))))
55 (defun topic-names (topic) (copy-list (%topic-names (canonical-topic topic))))
56 (defun topic-name-value (name) (%topic-name-value (canonical-topic-name name)))
57 (defun topic-name-type (name) (%topic-name-type (canonical-topic-name name)))
58 (defun topic-name-parent (name) (%topic-name-parent (canonical-topic-name name)))
59 (defun association-parent (association) (%association-parent (canonical-association association)))
60 (defun association-type (association) (%association-type (canonical-association association)))
61 (defun association-roles (association) (copy-list (%association-roles (canonical-association association))))
62 (defun association-role-parent (role) (%association-role-parent (canonical-association-role role)))
63 (defun association-role-type (role) (%association-role-type (canonical-association-role role)))
64 (defun association-role-player (role) (%association-role-player (canonical-association-role role)))
65 (defun topic-roles-played (topic)
66 "Computed TMDM 5.3.5 property; no independently maintained inverse index."
67 (let ((topic (canonical-topic topic)))
68 (loop for association in (%topic-map-associations (%topic-parent topic)) append
69 (remove-if-not (lambda (role) (eq topic (%association-role-player role)))
70 (%association-roles association)))))
71
72 ;; Avoid recursive printing through parent/type links; DESCRIBE remains useful.
73 (defmethod print-object ((item topic-map) stream)
74 (print-unreadable-object (item stream :type t :identity t)
75 (format stream "~D topics, ~D associations" (length (topic-map-topics item))
76 (length (topic-map-associations item)))))
77 (defmethod print-object ((item topic) stream)
78 (print-unreadable-object (item stream :type t :identity t)
79 (format stream "II=~S SI=~S SL=~S" (topic-item-identifiers item)
80 (topic-subject-identifiers item) (topic-subject-locators item))))
81 (defmethod print-object ((item topic-name) stream)
82 (print-unreadable-object (item stream :type t :identity t)
83 (format stream "~S" (topic-name-value item))))
84 (defmethod print-object ((item association) stream)
85 (print-unreadable-object (item stream :type t :identity t)
86 (format stream "~D roles" (length (association-roles item)))))
87 (defmethod print-object ((item association-role) stream)
88 (print-unreadable-object (item stream :type t :identity t)))
89
90 (defun default-name-type-identifier ()
91 "http://psi.topicmaps.org/iso13250/model/topic-name")