Skip to content

Commit 38429e4

Browse files
committed
[tools/rdfwriter] Refactor save_elem usage
To reduce complexity, the RDFWriter.save_elem method is removed and its usage replaced by the new methods RDFWriter.save_document, .save_section and .save_property.
1 parent 7fa5dc3 commit 38429e4

1 file changed

Lines changed: 7 additions & 71 deletions

File tree

odml/tools/rdf_converter.py

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def convert_to_rdf(self):
9090
if isinstance(doc, BaseDocument):
9191
# make sure links and includes are resolved before conversion
9292
doc.finalize()
93-
self.save_element(doc)
93+
self.save_document(doc)
9494

9595
return self.graph
9696

@@ -137,7 +137,12 @@ def save_odml_list(self, odml_list, parent_node, rdf_predicate):
137137
for curr_item in odml_list:
138138
node = URIRef(ODML_NS + unicode(curr_item.id))
139139
self.graph.add((parent_node, rdf_predicate, node))
140-
self.save_element(curr_item, node)
140+
141+
fmt = curr_item.format()
142+
if fmt.name == Section.name:
143+
self.save_section(curr_item, node)
144+
elif fmt.name == Property.name:
145+
self.save_property(curr_item, node)
141146

142147
def save_repository_node(self, parent_node, rdf_predicate, leaf_value):
143148
"""
@@ -272,75 +277,6 @@ def save_property(self, prop, curr_node):
272277
curr_lit = Literal(curr_val)
273278
self.graph.add((curr_node, curr_pred, curr_lit))
274279

275-
def save_element(self, odml_elem, node=None):
276-
"""
277-
Save the current odml element to the RDF graph and handle all child
278-
elements of the current odml element recursively.
279-
:param odml_elem: An odml element that should be added to the RDF graph.
280-
:param node: An RDF node that is used to append the current odml element
281-
to the RDF graph. If None, a new node will be created and
282-
added to the 'Hub' node of the RDF graph.
283-
"""
284-
fmt = odml_elem.format()
285-
286-
is_doc = fmt.name == Document.name
287-
is_sec = fmt.name == Section.name
288-
is_prop = fmt.name == Property.name
289-
290-
curr_node = node
291-
if not curr_node:
292-
curr_node = URIRef(ODML_NS + unicode(odml_elem.id))
293-
294-
# Add type of current node to the RDF graph
295-
curr_type = fmt.rdf_type
296-
# Handle section subclass types
297-
if is_sec:
298-
sub_sec = self._get_section_subclass(odml_elem)
299-
if sub_sec:
300-
curr_type = sub_sec
301-
self.graph.add((curr_node, RDF.type, URIRef(curr_type)))
302-
303-
# Add a new document to the RDF Hub node
304-
if is_doc:
305-
self.graph.add((self.hub_root, ODML_NS.hasDocument, curr_node))
306-
307-
# If available, add the documents' filename to the document node
308-
# so we can identify where the data came from.
309-
if hasattr(odml_elem, "_origin_file_name"):
310-
curr_lit = Literal(odml_elem._origin_file_name)
311-
self.graph.add((curr_node, ODML_NS.hasFileName, curr_lit))
312-
313-
for k in fmt.rdf_map_keys:
314-
# Ignore "id" and empty values, but make sure the content of "value"
315-
# is only accessed via its non deprecated property "values".
316-
if k == "id" or k == "value" and not getattr(odml_elem, fmt.map(k)):
317-
continue
318-
elif k != "value" and not getattr(odml_elem, k):
319-
continue
320-
321-
if (is_doc or is_sec) and k == "repository":
322-
self.save_repository_node(curr_node, fmt.rdf_map(k),
323-
getattr(odml_elem, k))
324-
325-
# generating nodes for sections and properties
326-
elif ((is_doc or is_sec) and k == "sections") or \
327-
(is_sec and k == "properties"):
328-
self.save_odml_list(getattr(odml_elem, k), curr_node, fmt.rdf_map(k))
329-
330-
# generating nodes for Property values
331-
elif is_prop and k == "value":
332-
# 'value' needs to be mapped to its appropriate odml Property attribute.
333-
self.save_odml_values(curr_node, fmt.rdf_map(k),
334-
getattr(odml_elem, fmt.map(k)))
335-
336-
elif k == "date":
337-
curr_lit = Literal(getattr(odml_elem, k), datatype=XSD.date)
338-
self.graph.add((curr_node, fmt.rdf_map(k), curr_lit))
339-
340-
else:
341-
curr_lit = Literal(getattr(odml_elem, k))
342-
self.graph.add((curr_node, fmt.rdf_map(k), curr_lit))
343-
344280
def _get_section_subclass(self, elem):
345281
"""
346282
_get_section_subclass checks whether the current odML element

0 commit comments

Comments
 (0)