]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/items.lisp
feat(ctm): decode ISO-8859-5 with qualified charset aliases
[topic-maps.git] / src / tmdm / items.lisp
1 (in-package #:topic-maps.tmdm)
2
3 ;;; All seven information items carry Item Identifier sets (TMDM 5.1-5.8).
4 ;;; Reifier belongs to non-Topic constructs; Topic Reified is computed.
5 (defstruct (topic-map (:constructor make-topic-map ()) (:conc-name %topic-map-)) topics associations item-identifiers reifier)
6 (defstruct (topic (:constructor %make-topic (parent)) (:conc-name %topic-))
7 (parent nil :read-only t) item-identifiers subject-identifiers subject-locators names occurrences
8 ;; Implementation bookkeeping, not a TMDM property or another information item.
9 forward)
10
11 (defstruct (association (:constructor %make-association (parent type)) (:conc-name %association-))
12 (parent nil :type topic-map :read-only t) (type nil :type topic) roles scope item-identifiers reifier forward)
13 (defstruct (association-role (:constructor %make-association-role (parent type player))
14 (:conc-name %association-role-))
15 (parent nil :type association) (type nil :type topic) (player nil :type topic) item-identifiers reifier forward)
16 (defstruct (topic-name (:constructor %make-topic-name (value type parent)) (:conc-name %topic-name-))
17 (value "" :type string :read-only t)
18 (type nil :type topic)
19 (parent nil :type topic)
20 scope variants item-identifiers reifier forward)
21
22 (defstruct (variant (:constructor %make-variant (value datatype parent)) (:conc-name %variant-))
23 (value "" :type string :read-only t) (datatype "" :type string :read-only t)
24 (parent nil :type topic-name) scope item-identifiers reifier forward)
25
26 (defstruct (occurrence (:constructor %make-occurrence (value datatype type parent))
27 (:conc-name %occurrence-))
28 (value "" :type string :read-only t) (datatype "" :type string :read-only t)
29 (type nil :type topic) (parent nil :type topic) item-identifiers scope reifier forward)
30
31 (defun canonical-occurrence (occurrence)
32 "Resolve a retained Occurrence replaced by TMDM duplicate removal; no compression."
33 (check-type occurrence occurrence)
34 (loop for current = occurrence then (%occurrence-forward current)
35 unless (%occurrence-forward current) return current))
36 (defun string-datatype () "http://www.w3.org/2001/XMLSchema#string")
37 (defun iri-datatype () "http://www.w3.org/2001/XMLSchema#anyURI")
38
39 (defun canonical-topic (topic)
40 "Resolve a retained reference to a Topic replaced by same-map merging.
41 EQ on obsolete objects does not change; compare their canonical representatives."
42 (check-type topic topic)
43 (loop for current = topic then (%topic-forward current)
44 unless (%topic-forward current) return current))
45 (defun canonical-topic-name (name)
46 "Resolve a retained reference to a Name replaced during duplicate removal."
47 (check-type name topic-name)
48 (loop for current = name then (%topic-name-forward current)
49 unless (%topic-name-forward current) return current))
50
51 (defun canonical-variant (item)
52 "Resolve a retained Variant replaced by duplicate removal, without mutation."
53 (check-type item variant)
54 (loop for current = item then (%variant-forward current)
55 unless (%variant-forward current) return current))
56 (defun topic-name-variants (name) (copy-list (%topic-name-variants (canonical-topic-name name))))
57 (defun variant-value (item) (%variant-value (canonical-variant item)))
58 (defun variant-datatype (item) (%variant-datatype (canonical-variant item)))
59 (defun variant-parent (item) (%variant-parent (canonical-variant item)))
60 (defun variant-scope (item) (copy-list (%variant-scope (canonical-variant item))))
61 (defun variant-item-identifiers (item)
62 (item-identifiers (canonical-variant item)))
63
64 (defun canonical-association (association)
65 "Resolve a retained Association handle after duplicate association merging."
66 (check-type association association)
67 (loop for current = association then (%association-forward current)
68 unless (%association-forward current) return current))
69 (defun canonical-association-role (role)
70 "Resolve a retained Role handle after role/association duplicate removal."
71 (check-type role association-role)
72 (loop for current = role then (%association-role-forward current)
73 unless (%association-role-forward current) return current))
74
75 ;; Public readers resolve obsolete handles and expose copies of set storage.
76 ;; Mutations must use the identity/merge API to maintain model invariants.
77 (defun topic-map-topics (map) (copy-list (%topic-map-topics map)))
78 (defun topic-map-associations (map) (copy-list (%topic-map-associations map)))
79 (defun topic-parent (topic) (%topic-parent (canonical-topic topic)))
80 (defun topic-item-identifiers (topic) (item-identifiers (canonical-topic topic)))
81 (defun topic-map-item-identifiers (map) (check-type map topic-map) (item-identifiers map))
82 (defun topic-name-item-identifiers (name) (item-identifiers (canonical-topic-name name)))
83 (defun association-item-identifiers (item) (item-identifiers (canonical-association item)))
84 (defun association-role-item-identifiers (item) (item-identifiers (canonical-association-role item)))
85 (defun topic-subject-identifiers (topic) (copy-list (%topic-subject-identifiers (canonical-topic topic))))
86 (defun topic-subject-locators (topic) (copy-list (%topic-subject-locators (canonical-topic topic))))
87 (defun topic-names (topic) (copy-list (%topic-names (canonical-topic topic))))
88 (defun topic-occurrences (topic) (copy-list (%topic-occurrences (canonical-topic topic))))
89 (defun occurrence-value (item) (%occurrence-value (canonical-occurrence item)))
90 (defun occurrence-datatype (item) (%occurrence-datatype (canonical-occurrence item)))
91 (defun occurrence-parent (item) (%occurrence-parent (canonical-occurrence item)))
92 (defun occurrence-type (item) (%occurrence-type (canonical-occurrence item)))
93 (defun occurrence-item-identifiers (item) (item-identifiers (canonical-occurrence item)))
94 (defun topic-name-value (name) (%topic-name-value (canonical-topic-name name)))
95 (defun topic-name-type (name) (%topic-name-type (canonical-topic-name name)))
96 (defun topic-name-scope (name) (copy-list (%topic-name-scope (canonical-topic-name name))))
97 (defun occurrence-scope (item) (copy-list (%occurrence-scope (canonical-occurrence item))))
98 (defun association-scope (item) (copy-list (%association-scope (canonical-association item))))
99 (defun topic-name-parent (name) (%topic-name-parent (canonical-topic-name name)))
100 (defun association-parent (association) (%association-parent (canonical-association association)))
101 (defun association-type (association) (%association-type (canonical-association association)))
102 (defun association-roles (association) (copy-list (%association-roles (canonical-association association))))
103 (defun association-role-parent (role) (%association-role-parent (canonical-association-role role)))
104 (defun association-role-type (role) (%association-role-type (canonical-association-role role)))
105 (defun association-role-player (role) (%association-role-player (canonical-association-role role)))
106 (defun topic-roles-played (topic)
107 "Computed TMDM 5.3.5 property; no independently maintained inverse index."
108 (let ((topic (canonical-topic topic)))
109 (loop for association in (%topic-map-associations (%topic-parent topic)) append
110 (remove-if-not (lambda (role) (eq topic (%association-role-player role)))
111 (%association-roles association)))))
112
113 ;; Avoid recursive printing through parent/type links; DESCRIBE remains useful.
114 (defmethod print-object ((item topic-map) stream)
115 (print-unreadable-object (item stream :type t :identity t)
116 (format stream "~D topics, ~D associations" (length (topic-map-topics item))
117 (length (topic-map-associations item)))))
118 (defmethod print-object ((item topic) stream)
119 (print-unreadable-object (item stream :type t :identity t)
120 (format stream "II=~S SI=~S SL=~S" (topic-item-identifiers item)
121 (topic-subject-identifiers item) (topic-subject-locators item))))
122 (defmethod print-object ((item topic-name) stream)
123 (print-unreadable-object (item stream :type t :identity t)
124 (format stream "~S" (topic-name-value item))))
125 (defmethod print-object ((item variant) stream)
126 (print-unreadable-object (item stream :type t :identity t)
127 (format stream "~S datatype=~S" (variant-value item) (variant-datatype item))))
128 (defmethod print-object ((item association) stream)
129 (print-unreadable-object (item stream :type t :identity t)
130 (format stream "~D roles" (length (association-roles item)))))
131 (defmethod print-object ((item association-role) stream)
132 (print-unreadable-object (item stream :type t :identity t)))
133
134 (defun default-name-type-identifier ()
135 "http://psi.topicmaps.org/iso13250/model/topic-name")
136
137 (defmethod print-object ((item occurrence) stream)
138 (print-unreadable-object (item stream :type t :identity t)
139 (format stream "~S datatype=~S" (occurrence-value item) (occurrence-datatype item))))