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