]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/items.lisp
fix(ctm): distinguish malformed names from unsupported typed names
[topic-maps.git] / src / tmdm / items.lisp
1 (in-package #:topic-maps.tmdm)
2
3 ;;; Only the information-item properties used by Slice 1 are materialized.
4 ;;; Omitted set-valued properties are implicitly empty, single-valued ones null
5 ;;; (CTM 3.2). In particular all supported names have empty scope and variants.
6 (defstruct topic-map topics)
7 (defstruct (topic (:constructor %make-topic (parent)))
8 (parent nil :read-only t) item-identifiers subject-identifiers subject-locators names)
9 (defstruct (topic-name (:constructor %make-topic-name (value type parent)))
10 (value "" :type string :read-only t)
11 (type nil :type topic :read-only t)
12 (parent nil :type topic :read-only t))
13
14 ;; Avoid recursive printing through parent/type links; DESCRIBE remains useful.
15 (defmethod print-object ((item topic-map) stream)
16 (print-unreadable-object (item stream :type t :identity t)
17 (format stream "~D topics" (length (topic-map-topics item)))))
18 (defmethod print-object ((item topic) stream)
19 (print-unreadable-object (item stream :type t :identity t)
20 (format stream "II=~S SI=~S SL=~S" (topic-item-identifiers item)
21 (topic-subject-identifiers item) (topic-subject-locators item))))
22 (defmethod print-object ((item topic-name) stream)
23 (print-unreadable-object (item stream :type t :identity t)
24 (format stream "~S" (topic-name-value item))))
25
26 (defun identifiers (topic kind)
27 (ecase kind
28 (:item-identifier (topic-item-identifiers topic))
29 (:subject-identifier (topic-subject-identifiers topic))
30 (:subject-locator (topic-subject-locators topic))))
31
32 (defun intern-topic (map kind iri)
33 "Within one map, reuse an equal identity reference (TMDM 5.3.5).
34 This restricted interning does not implement general merge or cross-map merge."
35 (unless (and (typep map 'topic-map) (stringp iri) (plusp (length iri)))
36 (error "A map and a nonempty locator string are required."))
37 (let* ((kinds (ecase kind
38 ((:item-identifier :subject-identifier)
39 '(:item-identifier :subject-identifier))
40 (:subject-locator '(:subject-locator))))
41 (existing
42 (find-if (lambda (topic)
43 (some (lambda (property)
44 (member iri (identifiers topic property) :test #'string=))
45 kinds))
46 (topic-map-topics map)))
47 (topic (or existing (%make-topic map))))
48 (unless existing
49 ;; Private storage mutation: list order is observational, not TMDM semantics.
50 (setf (topic-map-topics map) (append (topic-map-topics map) (list topic))))
51 (ecase kind
52 (:item-identifier (pushnew iri (topic-item-identifiers topic) :test #'string=))
53 (:subject-identifier (pushnew iri (topic-subject-identifiers topic) :test #'string=))
54 (:subject-locator (pushnew iri (topic-subject-locators topic) :test #'string=)))
55 topic))
56
57 (defun default-name-type-identifier ()
58 "http://psi.topicmaps.org/iso13250/model/topic-name")
59
60 (defun add-default-name (topic value)
61 "CTM 3.11 default type; unscoped name equality per TMDM 5.4."
62 (check-type topic topic)
63 (check-type value string)
64 (let* ((type (intern-topic (topic-parent topic) :subject-identifier
65 (default-name-type-identifier)))
66 (existing (find-if (lambda (name)
67 (and (string= value (topic-name-value name))
68 (eq type (topic-name-type name))))
69 (topic-names topic))))
70 (or existing
71 (let ((name (%make-topic-name value type topic)))
72 (setf (topic-names topic) (append (topic-names topic) (list name)))
73 name))))