1 (in-package #:topic-maps.tmdm)
3 ;;; Only Topic Map, Topic, Name/Occurrence, 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). Scope is a materialized Topic set; Variants belong to Names.
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 occurrences
10 ;; Implementation bookkeeping, not a TMDM property or another information item.
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 scope 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 scope variants forward)
24 (defstruct (variant (:constructor %make-variant (value datatype parent)) (:conc-name %variant-))
25 (value "" :type string :read-only t) (datatype "" :type string :read-only t)
26 (parent nil :type topic-name) scope forward)
28 (defstruct (occurrence (:constructor %make-occurrence (value datatype type parent))
29 (:conc-name %occurrence-))
30 (value "" :type string :read-only t) (datatype "" :type string :read-only t)
31 (type nil :type topic) (parent nil :type topic) item-identifiers scope forward)
33 (defun canonical-occurrence (occurrence)
34 "Resolve a retained Occurrence replaced by TMDM duplicate removal; no compression."
35 (check-type occurrence occurrence)
36 (loop for current = occurrence then (%occurrence-forward current)
37 unless (%occurrence-forward current) return current))
38 (defun string-datatype () "http://www.w3.org/2001/XMLSchema#string")
39 (defun iri-datatype () "http://www.w3.org/2001/XMLSchema#anyURI")
41 (defun canonical-topic (topic)
42 "Resolve a retained reference to a Topic replaced by same-map merging.
43 EQ on obsolete objects does not change; compare their canonical representatives."
44 (check-type topic topic)
45 (loop for current = topic then (%topic-forward current)
46 unless (%topic-forward current) return current))
47 (defun canonical-topic-name (name)
48 "Resolve a retained reference to a Name replaced during duplicate removal."
49 (check-type name topic-name)
50 (loop for current = name then (%topic-name-forward current)
51 unless (%topic-name-forward current) return current))
53 (defun canonical-variant (item)
54 "Resolve a retained Variant replaced by duplicate removal, without mutation."
55 (check-type item variant)
56 (loop for current = item then (%variant-forward current)
57 unless (%variant-forward current) return current))
58 (defun topic-name-variants (name) (copy-list (%topic-name-variants (canonical-topic-name name))))
59 (defun variant-value (item) (%variant-value (canonical-variant item)))
60 (defun variant-datatype (item) (%variant-datatype (canonical-variant item)))
61 (defun variant-parent (item) (%variant-parent (canonical-variant item)))
62 (defun variant-scope (item) (copy-list (%variant-scope (canonical-variant item))))
63 (defun variant-item-identifiers (item)
64 "Item identifiers are implicitly empty in the supported unreified Variant subset."
65 (canonical-variant item) nil)
67 (defun canonical-association (association)
68 "Resolve a retained Association handle after duplicate association merging."
69 (check-type association association)
70 (loop for current = association then (%association-forward current)
71 unless (%association-forward current) return current))
72 (defun canonical-association-role (role)
73 "Resolve a retained Role handle after role/association duplicate removal."
74 (check-type role association-role)
75 (loop for current = role then (%association-role-forward current)
76 unless (%association-role-forward current) return current))
78 ;; Public readers resolve obsolete handles and expose copies of set storage.
79 ;; Mutations must use the identity/merge API to maintain model invariants.
80 (defun topic-map-topics (map) (copy-list (%topic-map-topics map)))
81 (defun topic-map-associations (map) (copy-list (%topic-map-associations map)))
82 (defun topic-parent (topic) (%topic-parent (canonical-topic topic)))
83 (defun topic-item-identifiers (topic) (copy-list (%topic-item-identifiers (canonical-topic topic))))
84 (defun topic-subject-identifiers (topic) (copy-list (%topic-subject-identifiers (canonical-topic topic))))
85 (defun topic-subject-locators (topic) (copy-list (%topic-subject-locators (canonical-topic topic))))
86 (defun topic-names (topic) (copy-list (%topic-names (canonical-topic topic))))
87 (defun topic-occurrences (topic) (copy-list (%topic-occurrences (canonical-topic topic))))
88 (defun occurrence-value (item) (%occurrence-value (canonical-occurrence item)))
89 (defun occurrence-datatype (item) (%occurrence-datatype (canonical-occurrence item)))
90 (defun occurrence-parent (item) (%occurrence-parent (canonical-occurrence item)))
91 (defun occurrence-type (item) (%occurrence-type (canonical-occurrence item)))
92 (defun occurrence-item-identifiers (item) (copy-list (%occurrence-item-identifiers (canonical-occurrence item))))
93 (defun topic-name-value (name) (%topic-name-value (canonical-topic-name name)))
94 (defun topic-name-type (name) (%topic-name-type (canonical-topic-name name)))
95 (defun topic-name-scope (name) (copy-list (%topic-name-scope (canonical-topic-name name))))
96 (defun occurrence-scope (item) (copy-list (%occurrence-scope (canonical-occurrence item))))
97 (defun association-scope (item) (copy-list (%association-scope (canonical-association item))))
98 (defun topic-name-parent (name) (%topic-name-parent (canonical-topic-name name)))
99 (defun association-parent (association) (%association-parent (canonical-association association)))
100 (defun association-type (association) (%association-type (canonical-association association)))
101 (defun association-roles (association) (copy-list (%association-roles (canonical-association association))))
102 (defun association-role-parent (role) (%association-role-parent (canonical-association-role role)))
103 (defun association-role-type (role) (%association-role-type (canonical-association-role role)))
104 (defun association-role-player (role) (%association-role-player (canonical-association-role role)))
105 (defun topic-roles-played (topic)
106 "Computed TMDM 5.3.5 property; no independently maintained inverse index."
107 (let ((topic (canonical-topic topic)))
108 (loop for association in (%topic-map-associations (%topic-parent topic)) append
109 (remove-if-not (lambda (role) (eq topic (%association-role-player role)))
110 (%association-roles association)))))
112 ;; Avoid recursive printing through parent/type links; DESCRIBE remains useful.
113 (defmethod print-object ((item topic-map) stream)
114 (print-unreadable-object (item stream :type t :identity t)
115 (format stream "~D topics, ~D associations" (length (topic-map-topics item))
116 (length (topic-map-associations item)))))
117 (defmethod print-object ((item topic) stream)
118 (print-unreadable-object (item stream :type t :identity t)
119 (format stream "II=~S SI=~S SL=~S" (topic-item-identifiers item)
120 (topic-subject-identifiers item) (topic-subject-locators item))))
121 (defmethod print-object ((item topic-name) stream)
122 (print-unreadable-object (item stream :type t :identity t)
123 (format stream "~S" (topic-name-value item))))
124 (defmethod print-object ((item variant) stream)
125 (print-unreadable-object (item stream :type t :identity t)
126 (format stream "~S datatype=~S" (variant-value item) (variant-datatype item))))
127 (defmethod print-object ((item association) stream)
128 (print-unreadable-object (item stream :type t :identity t)
129 (format stream "~D roles" (length (association-roles item)))))
130 (defmethod print-object ((item association-role) stream)
131 (print-unreadable-object (item stream :type t :identity t)))
133 (defun default-name-type-identifier ()
134 "http://psi.topicmaps.org/iso13250/model/topic-name")
136 (defmethod print-object ((item occurrence) stream)
137 (print-unreadable-object (item stream :type t :identity t)
138 (format stream "~S datatype=~S" (occurrence-value item) (occurrence-datatype item))))