]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/items.lisp
Support explicitly typed Topic Names with merge and CXTM evidence
[topic-maps.git] / src / tmdm / items.lisp
1 (in-package #:topic-maps.tmdm)
2
3 ;;; Only Topic Map, Topic, unscoped 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). 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 occurrences
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 (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 forward)
28
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")
36
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))
48
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))
59
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-parent (name) (%topic-name-parent (canonical-topic-name name)))
78 (defun association-parent (association) (%association-parent (canonical-association association)))
79 (defun association-type (association) (%association-type (canonical-association association)))
80 (defun association-roles (association) (copy-list (%association-roles (canonical-association association))))
81 (defun association-role-parent (role) (%association-role-parent (canonical-association-role role)))
82 (defun association-role-type (role) (%association-role-type (canonical-association-role role)))
83 (defun association-role-player (role) (%association-role-player (canonical-association-role role)))
84 (defun topic-roles-played (topic)
85 "Computed TMDM 5.3.5 property; no independently maintained inverse index."
86 (let ((topic (canonical-topic topic)))
87 (loop for association in (%topic-map-associations (%topic-parent topic)) append
88 (remove-if-not (lambda (role) (eq topic (%association-role-player role)))
89 (%association-roles association)))))
90
91 ;; Avoid recursive printing through parent/type links; DESCRIBE remains useful.
92 (defmethod print-object ((item topic-map) stream)
93 (print-unreadable-object (item stream :type t :identity t)
94 (format stream "~D topics, ~D associations" (length (topic-map-topics item))
95 (length (topic-map-associations item)))))
96 (defmethod print-object ((item topic) stream)
97 (print-unreadable-object (item stream :type t :identity t)
98 (format stream "II=~S SI=~S SL=~S" (topic-item-identifiers item)
99 (topic-subject-identifiers item) (topic-subject-locators item))))
100 (defmethod print-object ((item topic-name) stream)
101 (print-unreadable-object (item stream :type t :identity t)
102 (format stream "~S" (topic-name-value item))))
103 (defmethod print-object ((item association) stream)
104 (print-unreadable-object (item stream :type t :identity t)
105 (format stream "~D roles" (length (association-roles item)))))
106 (defmethod print-object ((item association-role) stream)
107 (print-unreadable-object (item stream :type t :identity t)))
108
109 (defun default-name-type-identifier ()
110 "http://psi.topicmaps.org/iso13250/model/topic-name")
111
112 (defmethod print-object ((item occurrence) stream)
113 (print-unreadable-object (item stream :type t :identity t)
114 (format stream "~S datatype=~S" (occurrence-value item) (occurrence-datatype item))))