X-Git-Url: https://dreyeck.freedombox.rocks/gitweb/topic-maps.git/blobdiff_plain/1805023b5c5690f224a2bb556af46a4af7c36687..aeb017e4f4846f2e6a701e581d20eef84a8b83d9:/src/tmdm/items.lisp diff --git a/src/tmdm/items.lisp b/src/tmdm/items.lisp index 65fc13a..f9142d1 100644 --- a/src/tmdm/items.lisp +++ b/src/tmdm/items.lisp @@ -1,20 +1,101 @@ (in-package #:topic-maps.tmdm) -;;; Only the information-item properties used by Slice 1 are materialized. +;;; Only Topic Map, Topic, Name/Occurrence, Association and Role properties used +;;; by the current subset are materialized. ;;; Omitted set-valued properties are implicitly empty, single-valued ones null -;;; (CTM 3.2). In particular all supported names have empty scope and variants. -(defstruct topic-map topics) -(defstruct (topic (:constructor %make-topic (parent))) - (parent nil :read-only t) item-identifiers subject-identifiers subject-locators names) -(defstruct (topic-name (:constructor %make-topic-name (value type parent))) +;;; (CTM 3.2). Variants remain implicitly empty; Scope is a materialized Topic set. +(defstruct (topic-map (:constructor make-topic-map ()) (:conc-name %topic-map-)) topics associations) +(defstruct (topic (:constructor %make-topic (parent)) (:conc-name %topic-)) + (parent nil :read-only t) item-identifiers subject-identifiers subject-locators names occurrences + ;; Implementation bookkeeping, not a TMDM property or another information item. + forward) + +(defstruct (association (:constructor %make-association (parent type)) (:conc-name %association-)) + (parent nil :type topic-map :read-only t) (type nil :type topic) roles scope forward) +(defstruct (association-role (:constructor %make-association-role (parent type player)) + (:conc-name %association-role-)) + (parent nil :type association) (type nil :type topic) (player nil :type topic) forward) +(defstruct (topic-name (:constructor %make-topic-name (value type parent)) (:conc-name %topic-name-)) (value "" :type string :read-only t) - (type nil :type topic :read-only t) - (parent nil :type topic :read-only t)) + (type nil :type topic) + (parent nil :type topic) + scope forward) + +(defstruct (occurrence (:constructor %make-occurrence (value datatype type parent)) + (:conc-name %occurrence-)) + (value "" :type string :read-only t) (datatype "" :type string :read-only t) + (type nil :type topic) (parent nil :type topic) item-identifiers scope forward) + +(defun canonical-occurrence (occurrence) + "Resolve a retained Occurrence replaced by TMDM duplicate removal; no compression." + (check-type occurrence occurrence) + (loop for current = occurrence then (%occurrence-forward current) + unless (%occurrence-forward current) return current)) +(defun string-datatype () "http://www.w3.org/2001/XMLSchema#string") +(defun iri-datatype () "http://www.w3.org/2001/XMLSchema#anyURI") + +(defun canonical-topic (topic) + "Resolve a retained reference to a Topic replaced by same-map merging. +EQ on obsolete objects does not change; compare their canonical representatives." + (check-type topic topic) + (loop for current = topic then (%topic-forward current) + unless (%topic-forward current) return current)) +(defun canonical-topic-name (name) + "Resolve a retained reference to a Name replaced during duplicate removal." + (check-type name topic-name) + (loop for current = name then (%topic-name-forward current) + unless (%topic-name-forward current) return current)) + +(defun canonical-association (association) + "Resolve a retained Association handle after duplicate association merging." + (check-type association association) + (loop for current = association then (%association-forward current) + unless (%association-forward current) return current)) +(defun canonical-association-role (role) + "Resolve a retained Role handle after role/association duplicate removal." + (check-type role association-role) + (loop for current = role then (%association-role-forward current) + unless (%association-role-forward current) return current)) + +;; Public readers resolve obsolete handles and expose copies of set storage. +;; Mutations must use the identity/merge API to maintain model invariants. +(defun topic-map-topics (map) (copy-list (%topic-map-topics map))) +(defun topic-map-associations (map) (copy-list (%topic-map-associations map))) +(defun topic-parent (topic) (%topic-parent (canonical-topic topic))) +(defun topic-item-identifiers (topic) (copy-list (%topic-item-identifiers (canonical-topic topic)))) +(defun topic-subject-identifiers (topic) (copy-list (%topic-subject-identifiers (canonical-topic topic)))) +(defun topic-subject-locators (topic) (copy-list (%topic-subject-locators (canonical-topic topic)))) +(defun topic-names (topic) (copy-list (%topic-names (canonical-topic topic)))) +(defun topic-occurrences (topic) (copy-list (%topic-occurrences (canonical-topic topic)))) +(defun occurrence-value (item) (%occurrence-value (canonical-occurrence item))) +(defun occurrence-datatype (item) (%occurrence-datatype (canonical-occurrence item))) +(defun occurrence-parent (item) (%occurrence-parent (canonical-occurrence item))) +(defun occurrence-type (item) (%occurrence-type (canonical-occurrence item))) +(defun occurrence-item-identifiers (item) (copy-list (%occurrence-item-identifiers (canonical-occurrence item)))) +(defun topic-name-value (name) (%topic-name-value (canonical-topic-name name))) +(defun topic-name-type (name) (%topic-name-type (canonical-topic-name name))) +(defun topic-name-scope (name) (copy-list (%topic-name-scope (canonical-topic-name name)))) +(defun occurrence-scope (item) (copy-list (%occurrence-scope (canonical-occurrence item)))) +(defun association-scope (item) (copy-list (%association-scope (canonical-association item)))) +(defun topic-name-parent (name) (%topic-name-parent (canonical-topic-name name))) +(defun association-parent (association) (%association-parent (canonical-association association))) +(defun association-type (association) (%association-type (canonical-association association))) +(defun association-roles (association) (copy-list (%association-roles (canonical-association association)))) +(defun association-role-parent (role) (%association-role-parent (canonical-association-role role))) +(defun association-role-type (role) (%association-role-type (canonical-association-role role))) +(defun association-role-player (role) (%association-role-player (canonical-association-role role))) +(defun topic-roles-played (topic) + "Computed TMDM 5.3.5 property; no independently maintained inverse index." + (let ((topic (canonical-topic topic))) + (loop for association in (%topic-map-associations (%topic-parent topic)) append + (remove-if-not (lambda (role) (eq topic (%association-role-player role))) + (%association-roles association))))) ;; Avoid recursive printing through parent/type links; DESCRIBE remains useful. (defmethod print-object ((item topic-map) stream) (print-unreadable-object (item stream :type t :identity t) - (format stream "~D topics" (length (topic-map-topics item))))) + (format stream "~D topics, ~D associations" (length (topic-map-topics item)) + (length (topic-map-associations item))))) (defmethod print-object ((item topic) stream) (print-unreadable-object (item stream :type t :identity t) (format stream "II=~S SI=~S SL=~S" (topic-item-identifiers item) @@ -22,52 +103,15 @@ (defmethod print-object ((item topic-name) stream) (print-unreadable-object (item stream :type t :identity t) (format stream "~S" (topic-name-value item)))) - -(defun identifiers (topic kind) - (ecase kind - (:item-identifier (topic-item-identifiers topic)) - (:subject-identifier (topic-subject-identifiers topic)) - (:subject-locator (topic-subject-locators topic)))) - -(defun intern-topic (map kind iri) - "Within one map, reuse an equal identity reference (TMDM 5.3.5). -This restricted interning does not implement general merge or cross-map merge." - (unless (and (typep map 'topic-map) (stringp iri) (plusp (length iri))) - (error "A map and a nonempty locator string are required.")) - (let* ((kinds (ecase kind - ((:item-identifier :subject-identifier) - '(:item-identifier :subject-identifier)) - (:subject-locator '(:subject-locator)))) - (existing - (find-if (lambda (topic) - (some (lambda (property) - (member iri (identifiers topic property) :test #'string=)) - kinds)) - (topic-map-topics map))) - (topic (or existing (%make-topic map)))) - (unless existing - ;; Private storage mutation: list order is observational, not TMDM semantics. - (setf (topic-map-topics map) (append (topic-map-topics map) (list topic)))) - (ecase kind - (:item-identifier (pushnew iri (topic-item-identifiers topic) :test #'string=)) - (:subject-identifier (pushnew iri (topic-subject-identifiers topic) :test #'string=)) - (:subject-locator (pushnew iri (topic-subject-locators topic) :test #'string=))) - topic)) +(defmethod print-object ((item association) stream) + (print-unreadable-object (item stream :type t :identity t) + (format stream "~D roles" (length (association-roles item))))) +(defmethod print-object ((item association-role) stream) + (print-unreadable-object (item stream :type t :identity t))) (defun default-name-type-identifier () "http://psi.topicmaps.org/iso13250/model/topic-name") -(defun add-default-name (topic value) - "CTM 3.11 default type; unscoped name equality per TMDM 5.4." - (check-type topic topic) - (check-type value string) - (let* ((type (intern-topic (topic-parent topic) :subject-identifier - (default-name-type-identifier))) - (existing (find-if (lambda (name) - (and (string= value (topic-name-value name)) - (eq type (topic-name-type name)))) - (topic-names topic)))) - (or existing - (let ((name (%make-topic-name value type topic))) - (setf (topic-names topic) (append (topic-names topic) (list name))) - name)))) +(defmethod print-object ((item occurrence) stream) + (print-unreadable-object (item stream :type t :identity t) + (format stream "~S datatype=~S" (occurrence-value item) (occurrence-datatype item))))