Skip to content

Commit 123982e

Browse files
committed
[tools/rdfwriter] Add save_doc method
1 parent 52eb531 commit 123982e

1 file changed

Lines changed: 42 additions & 1 deletion

File tree

odml/tools/rdf_converter.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,52 @@ def save_repository_node(self, parent_node, rdf_predicate, leaf_value):
161161

162162
self.graph.add((parent_node, rdf_predicate, terminology_node))
163163

164+
def save_document(self, doc, curr_node=None):
165+
"""
166+
Add the current odML Document to the RDF graph and handle all child
167+
elements recursively.
168+
169+
:param doc: An odml Document that should be added to the RDF graph.
170+
:param curr_node: An RDF node that is used to append the current odml element
171+
to the Hub node of the current RDF graph.
172+
"""
173+
fmt = doc.format()
174+
175+
if not curr_node:
176+
curr_node = URIRef(ODML_NS + unicode(doc.id))
177+
178+
self.graph.add((curr_node, RDF.type, URIRef(fmt.rdf_type)))
179+
self.graph.add((self.hub_root, ODML_NS.hasDocument, curr_node))
180+
181+
# If available, add the documents' filename to the document node
182+
# so we can identify where the data came from.
183+
if hasattr(doc, "_origin_file_name"):
184+
curr_lit = Literal(doc._origin_file_name)
185+
self.graph.add((curr_node, ODML_NS.hasFileName, curr_lit))
186+
187+
for k in fmt.rdf_map_keys:
188+
curr_pred = fmt.rdf_map(k)
189+
curr_val = getattr(doc, k)
190+
191+
# Ignore an "id" entry, it has already been used to create the node itself.
192+
if k == "id" or not curr_val:
193+
continue
194+
elif k == "repository":
195+
self.save_repository_node(curr_node, curr_pred, curr_val)
196+
elif k == "sections":
197+
# generating nodes for child sections
198+
self.save_odml_list(curr_val, curr_node, curr_pred)
199+
elif k == "date":
200+
curr_lit = Literal(curr_val, datatype=XSD.date)
201+
self.graph.add((curr_node, curr_pred, curr_lit))
202+
else:
203+
curr_lit = Literal(curr_val)
204+
self.graph.add((curr_node, curr_pred, curr_lit))
205+
164206
def save_element(self, odml_elem, node=None):
165207
"""
166208
Save the current odml element to the RDF graph and handle all child
167209
elements of the current odml element recursively.
168-
169210
:param odml_elem: An odml element that should be added to the RDF graph.
170211
:param node: An RDF node that is used to append the current odml element
171212
to the RDF graph. If None, a new node will be created and

0 commit comments

Comments
 (0)