1 | ;;+----------------------------------------------------------------------------- |
---|
2 | ;;+ Isidorus |
---|
3 | ;;+ (c) 2008-2010 Marc Kuester, Christoph Ludwig, Lukas Georgieff |
---|
4 | ;;+ |
---|
5 | ;;+ Isidorus is freely distributable under the LLGPL license. |
---|
6 | ;;+ You can find a detailed description in trunk/docs/LLGPL-LICENSE.txt and |
---|
7 | ;;+ trunk/docs/LGPL-LICENSE.txt. |
---|
8 | ;;+----------------------------------------------------------------------------- |
---|
9 | |
---|
10 | |
---|
11 | ;; TODOs (in descending priority): |
---|
12 | ;; * resolve non-local topicRefs |
---|
13 | ;; * either check the input document for XTM2.0 conformance in advance or |
---|
14 | ;; raise some kind of error (--> condition) if something goes wrong. |
---|
15 | |
---|
16 | (defpackage :xtm-importer |
---|
17 | (:use :cl :cxml :elephant :datamodel :isidorus-threading :base-tools) |
---|
18 | (:import-from :constants |
---|
19 | *type-instance-psi* |
---|
20 | *type-psi* |
---|
21 | *instance-psi* |
---|
22 | *XTM2.0-NS* |
---|
23 | *XTM1.0-NS* |
---|
24 | *XTM1.0-XLINK* |
---|
25 | *XML-STRING* |
---|
26 | *XML-URI* |
---|
27 | *topic-name-psi*) |
---|
28 | (:import-from :xml-constants |
---|
29 | *core_psis.xtm*) |
---|
30 | (:import-from :xml-tools |
---|
31 | get-attribute |
---|
32 | xpath-fn-string |
---|
33 | xpath-child-elems-by-qname |
---|
34 | xpath-single-child-elem-by-qname |
---|
35 | xpath-select-location-path |
---|
36 | xpath-select-single-location-path) |
---|
37 | (:import-from :exceptions |
---|
38 | missing-reference-error |
---|
39 | duplicate-identifier-error) |
---|
40 | (:export :create-instanceof-association |
---|
41 | :from-association-elem |
---|
42 | :from-name-elem |
---|
43 | :from-occurrence-elem |
---|
44 | :from-role-elem |
---|
45 | :from-scope-elem |
---|
46 | :from-topic-elem-to-stub |
---|
47 | :from-type-elem |
---|
48 | :get-topicref-uri |
---|
49 | :import-only-topics |
---|
50 | :import-from-xtm |
---|
51 | :importer |
---|
52 | :init-isidorus |
---|
53 | :merge-topic-elem |
---|
54 | :setup-repository |
---|
55 | :get-topicid-by-psi |
---|
56 | :get-topic-id-xtm1.0 |
---|
57 | :from-resourceRef-elem-xtm1.0 |
---|
58 | :from-baseName-elem-xtm1.0 |
---|
59 | :from-variant-elem-xtm1.0 |
---|
60 | :from-topicRef-elem-xtm1.0 |
---|
61 | :from-resourceX-elem-xtm1.0 |
---|
62 | :from-variant-elem-xtm1.0 |
---|
63 | :from-parameters-elem-xtm1.0 |
---|
64 | :get-xlink-attribute |
---|
65 | :get-instanceOf-refs-xtm1.0 |
---|
66 | :from-roleSpec-elem-xtm1.0 |
---|
67 | :from-scope-elem-xtm1.0 |
---|
68 | :from-occurrence-elem-xtm1.0 |
---|
69 | :from-subjectIdentity-elem-xtm1.0 |
---|
70 | :from-member-elem-xtm1.0 |
---|
71 | :from-topic-elem-to-stub-xtm1.0 |
---|
72 | :merge-topic-elem-xtm1.0 |
---|
73 | :from-association-elem-xtm1.0 |
---|
74 | :importer-xtm1.0 |
---|
75 | :get-uuid |
---|
76 | :with-tm)) |
---|
77 | |
---|
78 | (in-package :xtm-importer) |
---|
79 | |
---|
80 | |
---|
81 | (defun get-topicref-uri (topicref-elem) |
---|
82 | "Extract the uri from a topicref" |
---|
83 | ;TODO: at present, this resolves only local topicRefs by cutting off |
---|
84 | ;the first character ('#') |
---|
85 | (declare (dom:element topicref-elem)) |
---|
86 | (let |
---|
87 | ((topicref (get-attribute topicref-elem "href"))) |
---|
88 | (unless (char= (elt topicref 0) #\#) |
---|
89 | (error "cannot handle topicrefs that don't start with #")) |
---|
90 | (subseq topicref 1))) |
---|
91 | |
---|
92 | (defun get-topicid-by-psi (uri &key (xtm-id d:*current-xtm*) (revision *TM-REVISION*)) |
---|
93 | (when uri |
---|
94 | (loop for item in |
---|
95 | (topic-identifiers |
---|
96 | (identified-construct (elephant:get-instance-by-value 'PersistentIdC 'uri uri)) :revision revision) |
---|
97 | when (string= xtm-id (xtm-id item)) |
---|
98 | return (uri item)))) |
---|
99 | |
---|
100 | |
---|
101 | (defmacro with-tm ((revision xtm-id tm-id) &body body) |
---|
102 | "creates a topic map object called tm and puts it into the local scope" |
---|
103 | `(let ((ii (make-construct 'ItemIdentifierC |
---|
104 | :uri ,tm-id |
---|
105 | :start-revision ,revision))) |
---|
106 | (let ((tm |
---|
107 | (make-construct 'TopicMapC |
---|
108 | :start-revision ,revision |
---|
109 | :xtm-id ,xtm-id |
---|
110 | :item-identifiers (list ii)))) |
---|
111 | (declare (ItemIdentifierC ii)) |
---|
112 | (declare (TopicMapC tm)) |
---|
113 | ,@body))) |
---|
114 | |
---|
115 | |
---|
116 | (defun init-isidorus (&optional (revision (get-revision))) |
---|
117 | "Initiatlize the database with the stubs of the core topics + PSIs |
---|
118 | defined in the XTM 1.0 spec. This includes a topic that represents the |
---|
119 | core TM" |
---|
120 | (with-writer-lock |
---|
121 | (with-tm (revision "core.xtm" "http://www.topicmaps.org/xtm/1.0/core.xtm") |
---|
122 | (let |
---|
123 | ((core-dom |
---|
124 | (cxml:parse-file *core_psis.xtm* (cxml-dom:make-dom-builder)))) |
---|
125 | (loop for top-elem across |
---|
126 | (xpath-child-elems-by-qname (dom:document-element core-dom) |
---|
127 | *xtm2.0-ns* "topic") |
---|
128 | do |
---|
129 | (let |
---|
130 | ((top |
---|
131 | (from-topic-elem-to-stub top-elem revision :xtm-id "core.xtm"))) |
---|
132 | (add-to-tm tm top))))))) |
---|
133 | |
---|
134 | |
---|
135 | ;TODO: replace the two importers with this macro |
---|
136 | (defmacro importer-mac |
---|
137 | (get-topic-elems get-association-elems |
---|
138 | from-topic-elem-to-stub merge-topic-elem from-association-elem) |
---|
139 | `(lambda (xtm-dom &key (xtm-id d:*current-xtm*) (revision (get-revision))) |
---|
140 | (declare (dom:element xtm-dom)) |
---|
141 | (declare (integer revision)) ;all topics that are imported in one go share the same revision |
---|
142 | |
---|
143 | (let |
---|
144 | ((topic-vector (,get-topic-elems xtm-dom)) |
---|
145 | (assoc-vector (,get-association-elems xtm-dom))) |
---|
146 | (loop for top-elem across topic-vector do |
---|
147 | (,from-topic-elem-to-stub top-elem revision :xtm-id xtm-id)) |
---|
148 | (loop for top-elem across topic-vector do |
---|
149 | (format t "t") |
---|
150 | (,merge-topic-elem top-elem revision :xtm-id xtm-id)) |
---|
151 | (loop for assoc-elem across assoc-vector do |
---|
152 | (,from-association-elem assoc-elem revision :xtm-id xtm-id))))) |
---|
153 | |
---|
154 | |
---|
155 | (defun create-instanceof-association (topicid-of-supertype player2-obj start-revision |
---|
156 | &key |
---|
157 | tm |
---|
158 | (xtm-id *current-xtm*)) |
---|
159 | "handle the instanceOf element. The instanceOf element is different |
---|
160 | from all the others in that it is not modelled one to one, but |
---|
161 | following the suggestion of the XTM 2.0 spec (4.9) and the |
---|
162 | TMDM (7.2) as an association" |
---|
163 | ;instanceOf = element instanceOf { topicRef+ } |
---|
164 | (declare (string topicid-of-supertype)) |
---|
165 | (declare (TopicC player2-obj)) |
---|
166 | (declare (TopicMapC tm)) |
---|
167 | (let |
---|
168 | ((associationtype |
---|
169 | (get-item-by-psi *type-instance-psi* :revision start-revision)) |
---|
170 | (roletype1 |
---|
171 | (get-item-by-psi *type-psi* :revision start-revision)) |
---|
172 | (roletype2 |
---|
173 | (get-item-by-psi *instance-psi* :revision start-revision)) |
---|
174 | (player1 |
---|
175 | (get-item-by-id topicid-of-supertype |
---|
176 | :xtm-id xtm-id |
---|
177 | :revision start-revision))) |
---|
178 | (unless (and associationtype roletype1 roletype2) |
---|
179 | (error "Error in the creation of an instanceof association: core topics are missing")) |
---|
180 | (unless player1 |
---|
181 | (error |
---|
182 | (make-condition 'missing-reference-error |
---|
183 | :message "could not find type topic (first player)" |
---|
184 | :reference topicid-of-supertype))) |
---|
185 | (add-to-tm tm associationtype) |
---|
186 | (add-to-tm tm roletype1) |
---|
187 | (add-to-tm tm roletype2) |
---|
188 | (add-to-tm |
---|
189 | tm |
---|
190 | (make-construct |
---|
191 | 'AssociationC |
---|
192 | :item-identifiers nil |
---|
193 | :themes nil |
---|
194 | :start-revision start-revision |
---|
195 | :instance-of associationtype |
---|
196 | :roles (list (list :start-revision start-revision |
---|
197 | :instance-of roletype1 |
---|
198 | :player player1) |
---|
199 | (list :start-revision start-revision |
---|
200 | :instance-of roletype2 |
---|
201 | :player player2-obj)))))) |
---|