Skip to content

Commit

Permalink
Export as RDF using prov library
Browse files Browse the repository at this point in the history
new feature from: trungdong/prov#49
  • Loading branch information
cmaumet committed Oct 12, 2016
1 parent 630be00 commit 0b78abf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 45 deletions.
4 changes: 2 additions & 2 deletions nidmresults/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ def save_prov_to_files(self, showattributes=False):
# provn_fid.close()

ttl_file = os.path.join(self.export_dir, 'nidm.ttl')
ttl_txt = self.g.serialize(format='turtle')
ttl_txt = self.doc.serialize(format='rdf', rdf_format='turtle')

ttl_txt = self.use_prefixes(ttl_txt.decode("utf-8"))
ttl_txt = self.use_prefixes(ttl_txt)
with open(ttl_file, 'w') as ttl_fid:
ttl_fid.write(ttl_txt)

Expand Down
86 changes: 43 additions & 43 deletions nidmresults/objects/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,47 +52,47 @@ def __repr__(self):
str(self.id).replace("niiri:", "").replace(NIIRI._uri, "")[0:8]\
+ '>'

def _rdf_add_attributes(self, attributes):
if self.type is not None:
self._rdf_add(self.id, RDF.type, self.type)
self._rdf_add(self.id, RDF.type, self.prov_type)

# If attributes is a dictionnary, convert to list
if isinstance(attributes, dict):
attributes = [(k, v) for k, v in attributes.items()]

for p, o in attributes:
self._rdf_add(self.id, p, o)

def _rdf_add(self, s, p, o):
if isinstance(o, prov.identifier.QualifiedName):
o = rdflib.URIRef(o.uri)
elif isinstance(o, prov.identifier.Identifier):
o = rdflib.Literal(o, datatype=XSD.anyURI)
else:
if (isinstance(o, str) or isinstance(o, str)) and \
not str(p) == "prov:label":
o = rdflib.Literal(o, datatype=XSD.string)
elif isinstance(o, float):
o = rdflib.Literal(o, datatype=XSD.float)
elif isinstance(o, bool):
o = rdflib.Literal(o, datatype=XSD.boolean)
elif isinstance(o, int):
o = rdflib.Literal(o, datatype=XSD.int)
else:
o = rdflib.Literal(str(o))

if not isinstance(p, rdflib.URIRef):
if str(p) == "prov:type":
p = RDF.type
elif str(p) == "prov:label":
p = RDFS.label
elif str(p) == "prov:location":
p = rdflib.URIRef(p.uri.replace("location", "atLocation"))
else:
p = rdflib.URIRef(p.uri)

self.g.add((rdflib.URIRef(s.uri), p, o))
# def _rdf_add_attributes(self, attributes):
# if self.type is not None:
# self._rdf_add(self.id, RDF.type, self.type)
# self._rdf_add(self.id, RDF.type, self.prov_type)

# # If attributes is a dictionnary, convert to list
# if isinstance(attributes, dict):
# attributes = [(k, v) for k, v in attributes.items()]

# for p, o in attributes:
# self._rdf_add(self.id, p, o)

# def _rdf_add(self, s, p, o):
# if isinstance(o, prov.identifier.QualifiedName):
# o = rdflib.URIRef(o.uri)
# elif isinstance(o, prov.identifier.Identifier):
# o = rdflib.Literal(o, datatype=XSD.anyURI)
# else:
# if (isinstance(o, str) or isinstance(o, str)) and \
# not str(p) == "prov:label":
# o = rdflib.Literal(o, datatype=XSD.string)
# elif isinstance(o, float):
# o = rdflib.Literal(o, datatype=XSD.float)
# elif isinstance(o, bool):
# o = rdflib.Literal(o, datatype=XSD.boolean)
# elif isinstance(o, int):
# o = rdflib.Literal(o, datatype=XSD.int)
# else:
# o = rdflib.Literal(str(o))

# if not isinstance(p, rdflib.URIRef):
# if str(p) == "prov:type":
# p = RDF.type
# elif str(p) == "prov:label":
# p = RDFS.label
# elif str(p) == "prov:location":
# p = rdflib.URIRef(p.uri.replace("location", "atLocation"))
# else:
# p = rdflib.URIRef(p.uri)

# self.g.add((rdflib.URIRef(s.uri), p, o))

def add_object(self, nidm_object, nidm_version):
nidm_object.export(nidm_version)
Expand Down Expand Up @@ -136,7 +136,7 @@ def _add_prov_relation(self, relation, nidm_object):
else:
raise Exception('Unrecognised prov relation')

self._rdf_add(self.id, relation, object_id)
# self._rdf_add(self.id, relation, object_id)

def add_attributes(self, attributes):
if self.prov_type == PROV['Activity']:
Expand All @@ -146,7 +146,7 @@ def add_attributes(self, attributes):
elif self.prov_type == PROV['Agent']:
self.p.agent(self.id, other_attributes=attributes)

self._rdf_add_attributes(attributes)
# self._rdf_add_attributes(attributes)


# class NIDMBundle(NIDMObject):
Expand Down
3 changes: 3 additions & 0 deletions nidmresults/test/test_results_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ def __init__(self, name, ttl_file, gt_ttl_files,
self.gt_ttl_files = gt_ttl_files
self.exact_comparison = exact_comparison
self.graph = Graph()

print(ttl_file)

self.graph.parse(ttl_file, format='turtle')

# Get NIDM-Results version for each example
Expand Down

0 comments on commit 0b78abf

Please sign in to comment.