Skip to content

Commit 9a615c0

Browse files
committed
[tools/rdfwriter] Refactor save element method
1 parent 29b1bc7 commit 9a615c0

1 file changed

Lines changed: 36 additions & 36 deletions

File tree

odml/tools/rdf_converter.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -150,69 +150,69 @@ def save_repository_node(self, parent_node, rdf_predicate, leaf_value):
150150

151151
def save_element(self, odml_elem, node=None):
152152
"""
153-
Save the current element to the RDF graph
154-
:param odml_elem: current element
155-
:param node: A node to pass the earlier created node to inner elements
156-
:return: the RDF graph
153+
Save the current odml element to the RDF graph and handle all child
154+
elements of the current odml element recursively.
155+
156+
:param odml_elem: An odml element that should be added to the RDF graph.
157+
:param node: An RDF node that is used to append the current odml element
158+
to the RDF graph. If None, a new node will be created and
159+
added to the 'Hub' node of the RDF graph.
157160
"""
158161
fmt = odml_elem.format()
159162

160-
is_doc = isinstance(fmt, Document.__class__)
161-
is_sec = isinstance(fmt, Section.__class__)
162-
is_prop = isinstance(fmt, Property.__class__)
163+
is_doc = fmt.name == Document.name
164+
is_sec = fmt.name == Section.name
165+
is_prop = fmt.name == Property.name
163166

164-
if not node:
167+
curr_node = node
168+
if not curr_node:
165169
curr_node = URIRef(ODML_NS + unicode(odml_elem.id))
166-
else:
167-
curr_node = node
168170

169-
if fmt.name == "section":
171+
# Add type of current node to the RDF graph
172+
curr_type = fmt.rdf_type
173+
# Handle section subclass types
174+
if is_sec:
170175
sub_sec = self._get_section_subclass(odml_elem)
171-
sec_type = sub_sec if sub_sec else fmt.rdf_type
172-
self.graph.add((curr_node, RDF.type, URIRef(sec_type)))
173-
else:
174-
self.graph.add((curr_node, RDF.type, URIRef(fmt.rdf_type)))
176+
if sub_sec:
177+
curr_type = sub_sec
178+
self.graph.add((curr_node, RDF.type, URIRef(curr_type)))
175179

176-
# adding doc to the hub
180+
# Add a new document to the RDF Hub node
177181
if is_doc:
178182
self.graph.add((self.hub_root, ODML_NS.hasDocument, curr_node))
179183

180-
# If available add the documents filename to the document node
184+
# If available, add the documents' filename to the document node
181185
# so we can identify where the data came from.
182186
if hasattr(odml_elem, "_origin_file_name"):
183187
curr_lit = Literal(odml_elem._origin_file_name)
184188
self.graph.add((curr_node, ODML_NS.hasFileName, curr_lit))
185189

186190
for k in fmt.rdf_map_keys:
187-
if k == 'id':
191+
if k == "id" or \
192+
(k == "value" and not getattr(odml_elem, fmt.map(k))) or \
193+
(not getattr(odml_elem, k)):
188194
continue
189-
elif (is_doc or is_sec) and k == "repository" and getattr(odml_elem, k):
195+
196+
if (is_doc or is_sec) and k == "repository":
190197
self.save_repository_node(curr_node, fmt.rdf_map(k),
191198
getattr(odml_elem, k))
192199

193-
# generating nodes for entities: sections, properties and bags of values
194-
elif (is_doc or is_sec) and k == 'sections' and getattr(odml_elem, k):
195-
self.save_odml_list(getattr(odml_elem, k), curr_node, fmt.rdf_map(k))
196-
197-
elif is_sec and k == 'properties' and getattr(odml_elem, k):
200+
# generating nodes for sections and properties
201+
elif ((is_doc or is_sec) and k == "sections") or \
202+
(is_sec and k == "properties"):
198203
self.save_odml_list(getattr(odml_elem, k), curr_node, fmt.rdf_map(k))
199204

200-
elif is_prop and k == 'value' and getattr(odml_elem, fmt.map(k)):
205+
# generating nodes for Property values
206+
elif is_prop and k == "value":
201207
# 'value' needs to be mapped to its appropriate odml Property attribute.
202208
self.save_odml_values(curr_node, fmt.rdf_map(k),
203209
getattr(odml_elem, fmt.map(k)))
204-
205-
# adding entities' properties
210+
elif k == "date":
211+
curr_lit = Literal(getattr(odml_elem, k), datatype=XSD.date)
212+
self.graph.add((curr_node, fmt.rdf_map(k), curr_lit))
206213
else:
207-
val = getattr(odml_elem, k)
208-
if val is None or not val:
209-
continue
210-
elif k == 'date':
211-
curr_lit = Literal(val, datatype=XSD.date)
212-
self.graph.add((curr_node, fmt.rdf_map(k), curr_lit))
213-
else:
214-
self.graph.add((curr_node, fmt.rdf_map(k), Literal(val)))
215-
return self.graph
214+
curr_lit = Literal(getattr(odml_elem, k))
215+
self.graph.add((curr_node, fmt.rdf_map(k), curr_lit))
216216

217217
def _get_section_subclass(self, elem):
218218
"""

0 commit comments

Comments
 (0)