]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/tmdm/import.lisp
feat(xtm): support resource references on name variants
[topic-maps.git] / src / tmdm / import.lisp
1 (in-package #:topic-maps.tmdm)
2
3 (define-condition map-import-error (error)
4 ((code :initarg :code :reader map-import-error-code)
5 (detail :initarg :detail :reader map-import-error-detail))
6 (:report (lambda (condition out)
7 (format out "Map content import ~A: ~A" (map-import-error-code condition)
8 (map-import-error-detail condition)))))
9 (defun import-problem (code detail)
10 (error 'map-import-error :code code :detail detail))
11
12 (defun validate-import-map (map)
13 "Check the current graph, without repairing or modifying caller objects.
14 Public model operations maintain these invariants; raw structure-slot edits are
15 not an alternative import API. Semantic condition classes remain unchanged."
16 (let ((items (map-information-items map)) (topics (%topic-map-topics map)))
17 (labels ((ref (item)
18 (unless (and (eq item (canonical-item item))
19 (member item items :test #'eq) (eq map (item-topic-map item)))
20 (import-problem :invalid-reference "Expected a current same-map information item.")))
21 (topic-ref (topic)
22 (unless (and (typep topic 'topic) (member topic topics :test #'eq))
23 (import-problem :invalid-reference "Expected a current same-map Topic."))
24 (ref topic))
25 (scope (themes)
26 (unless (and (proper-list-p themes)
27 (= (length themes) (length (remove-duplicates themes :test #'eq))))
28 (import-problem :invalid-scope "Scope must be a set of current Topic references."))
29 (mapc #'topic-ref themes)))
30 (unless (= (length items) (length (remove-duplicates items :test #'eq)))
31 (import-problem :invalid-reference "An information item occurs in multiple collections."))
32 (dolist (item items)
33 (ref item)
34 (typecase item
35 (topic
36 (unless (or (%topic-item-identifiers item) (%topic-subject-identifiers item)
37 (%topic-subject-locators item))
38 (import-problem :unidentified-topic "TMDM 5.3.5 requires a Topic identifier."))
39 (dolist (name (%topic-names item))
40 (unless (eq item (%topic-name-parent name))
41 (import-problem :invalid-reference "Name Parent differs from its containing Topic.")))
42 (dolist (occurrence (%topic-occurrences item))
43 (unless (eq item (%occurrence-parent occurrence))
44 (import-problem :invalid-reference "Occurrence Parent differs from its containing Topic."))))
45 (topic-name
46 (topic-ref (%topic-name-type item)) (scope (%topic-name-scope item))
47 (dolist (variant (%topic-name-variants item))
48 (unless (eq item (%variant-parent variant))
49 (import-problem :invalid-reference "Variant Parent differs from its containing Name."))))
50 (variant (scope (%variant-scope item)))
51 (occurrence (topic-ref (%occurrence-type item)) (scope (%occurrence-scope item)))
52 (association
53 (topic-ref (%association-type item)) (scope (%association-scope item))
54 (unless (%association-roles item)
55 (import-problem :invalid-reference "Association must have Roles."))
56 (dolist (role (%association-roles item))
57 (unless (eq item (%association-role-parent role))
58 (import-problem :invalid-reference "Role Parent differs from its containing Association."))))
59 (association-role (topic-ref (%association-role-type item))
60 (topic-ref (%association-role-player item)))))))
61 (validate-map-item-identifiers map)
62 (validate-map-core-subjects map)
63 (validate-map-reification map)
64 (check-variant-merge-constraints map)
65 map)
66
67 (defun import-content-into (target source)
68 ;; Only called after validation, or on a disposable preflight graph. The copy
69 ;; table rewrites references, never decides subject identity. Normalization
70 ;; uses the ordinary same-map TMDM merging operations on the completed graph.
71 (multiple-value-bind (map ref copies) (copy-map-graph source target :map-item-p nil)
72 (declare (ignore ref))
73 (normalize-topic-map map)
74 (validate-import-map map)
75 copies))
76
77 (defun import-map-content (target source)
78 "Copy SOURCE's Topics, Associations and their children into TARGET.
79 Return (values TARGET RESOLVE), where (funcall RESOLVE SOURCE-ITEM) returns its
80 current canonical target representative, including after later target merges.
81 The mapping describes this import's source snapshot: known old source objects
82 remain resolvable; new source content is not imported automatically. Retained
83 source aliases may resolve through a snapshot representative. SOURCE itself,
84 foreign objects and unimported objects signal MAP-IMPORT-ERROR :NOT-IMPORTED.
85
86 Both arguments must be different valid Topic Maps. Their root items are not
87 merged: SOURCE's map identifiers and map reifier are not copied to TARGET.
88 SOURCE's objects, forward links and strings are never changed or shared with
89 new copies. Existing target items may merge, preserving canonical handles.
90 All semantic rejection is preflighted on a disposable copy of TARGET using
91 the same TMDM structures/normalization; resource exhaustion and concurrent
92 external mutation are outside this single-threaded API guarantee."
93 (check-type target topic-map) (check-type source topic-map)
94 (when (eq target source)
95 (import-problem :same-map "Source and target must be different Topic Maps."))
96 (validate-import-map target) (validate-import-map source)
97 (let ((*merge-mutation-active* t))
98 ;; Unconditional preflight is needed even when neither input currently has
99 ;; Reifiers/Core Subjects: the combined graph may create identifier conflicts.
100 (import-content-into (copy-map-for-preflight target) source)
101 (let ((copies (import-content-into target source)))
102 (remhash source copies)
103 (values target
104 (lambda (item)
105 (let ((copy (or (gethash item copies)
106 (and (typep item '(or topic topic-name variant occurrence association association-role))
107 (gethash (canonical-item item) copies)))))
108 (unless copy
109 (import-problem :not-imported "Object is not content of this import's source snapshot."))
110 (canonical-item copy)))))))