Skip to content

Commit e63e16d

Browse files
committed
Merge branch 'satra-enh/extensible' into dev
2 parents cfd7e9f + 4f83144 commit e63e16d

3 files changed

Lines changed: 58 additions & 41 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cache:
2020

2121
# Install packages
2222
install:
23-
- pip install --upgrade setuptools
23+
- pip install --upgrade "setuptools<40.0.0"
2424
- pip install -r requirements.txt
2525
- pip install coverage coveralls
2626

src/prov/serializers/provrdf.py

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ def get_anon_id(self, obj, local_prefix="id"):
6262
if six.integer_types[-1] not in LITERAL_XSDTYPE_MAP:
6363
LITERAL_XSDTYPE_MAP[six.integer_types[-1]] = XSD['long']
6464

65+
relation_mapper = {URIRef(PROV['alternateOf'].uri): 'alternate',
66+
URIRef(PROV['actedOnBehalfOf'].uri): 'delegation',
67+
URIRef(PROV['specializationOf'].uri): 'specialization',
68+
URIRef(PROV['mentionOf'].uri): 'mention',
69+
URIRef(PROV['wasAssociatedWith'].uri): 'association',
70+
URIRef(PROV['wasDerivedFrom'].uri): 'derivation',
71+
URIRef(PROV['wasAttributedTo'].uri): 'attribution',
72+
URIRef(PROV['wasInformedBy'].uri): 'communication',
73+
URIRef(PROV['wasGeneratedBy'].uri): 'generation',
74+
URIRef(PROV['wasInfluencedBy'].uri): 'influence',
75+
URIRef(PROV['wasInvalidatedBy'].uri): 'invalidation',
76+
URIRef(PROV['wasEndedBy'].uri): 'end',
77+
URIRef(PROV['wasStartedBy'].uri): 'start',
78+
URIRef(PROV['hadMember'].uri): 'membership',
79+
URIRef(PROV['used'].uri): 'usage',
80+
}
81+
predicate_mapper = {RDFS.label: pm.PROV['label'],
82+
URIRef(PROV['atLocation'].uri): PROV_LOCATION,
83+
URIRef(PROV['startedAtTime'].uri): PROV_ATTR_STARTTIME,
84+
URIRef(PROV['endedAtTime'].uri): PROV_ATTR_ENDTIME,
85+
URIRef(PROV['atTime'].uri): PROV_ATTR_TIME,
86+
URIRef(PROV['hadRole'].uri): PROV_ROLE,
87+
URIRef(PROV['hadPlan'].uri): pm.PROV_ATTR_PLAN,
88+
URIRef(PROV['hadUsage'].uri): pm.PROV_ATTR_USAGE,
89+
URIRef(PROV['hadGeneration'].uri): pm.PROV_ATTR_GENERATION,
90+
URIRef(PROV['hadActivity'].uri): pm.PROV_ATTR_ACTIVITY,
91+
}
92+
6593

6694
def attr2rdf(attr):
6795
return URIRef(PROV[PROV_ID_ATTRIBUTES_MAP[attr].split('prov:')[1]].uri)
@@ -79,15 +107,16 @@ class ProvRDFSerializer(Serializer):
79107
PROV-O serializer for :class:`~prov.model.ProvDocument`
80108
"""
81109

82-
def serialize(self, stream=None, rdf_format='trig', **kwargs):
110+
def serialize(self, stream=None, rdf_format='trig', PROV_N_MAP=PROV_N_MAP,
111+
**kwargs):
83112
"""
84113
Serializes a :class:`~prov.model.ProvDocument` instance to
85114
`PROV-O <https://www.w3.org/TR/prov-o/>`_.
86115
87116
:param stream: Where to save the output.
88117
:param rdf_format: The RDF format of the output, default to TRiG.
89118
"""
90-
container = self.encode_document(self.document)
119+
container = self.encode_document(self.document, PROV_N_MAP=PROV_N_MAP)
91120
newargs = kwargs.copy()
92121
newargs['format'] = rdf_format
93122

@@ -120,7 +149,9 @@ def serialize(self, stream=None, rdf_format='trig', **kwargs):
120149
finally:
121150
buf.close()
122151

123-
def deserialize(self, stream, rdf_format='trig', **kwargs):
152+
def deserialize(self, stream, rdf_format='trig',
153+
relation_mapper=relation_mapper,
154+
predicate_mapper=predicate_mapper, **kwargs):
124155
"""
125156
Deserialize from the `PROV-O <https://www.w3.org/TR/prov-o/>`_
126157
representation to a :class:`~prov.model.ProvDocument` instance.
@@ -134,7 +165,9 @@ def deserialize(self, stream, rdf_format='trig', **kwargs):
134165
container.parse(stream, **newargs)
135166
document = pm.ProvDocument()
136167
self.document = document
137-
self.decode_document(container, document)
168+
self.decode_document(container, document,
169+
relation_mapper=relation_mapper,
170+
predicate_mapper=predicate_mapper)
138171
return document
139172

140173
def valid_identifier(self, value):
@@ -192,15 +225,17 @@ def decode_rdf_representation(self, literal, graph):
192225
# simple type, just return it
193226
return literal
194227

195-
def encode_document(self, document):
228+
def encode_document(self, document, PROV_N_MAP=PROV_N_MAP):
196229
container = self.encode_container(document)
197230
for item in document.bundles:
198231
# encoding the sub-bundle
199-
bundle = self.encode_container(item, identifier=item.identifier.uri)
232+
bundle = self.encode_container(item, identifier=item.identifier.uri,
233+
PROV_N_MAP=PROV_N_MAP)
200234
container.addN(bundle.quads())
201235
return container
202236

203-
def encode_container(self, bundle, container=None, identifier=None):
237+
def encode_container(self, bundle, PROV_N_MAP=PROV_N_MAP,
238+
container=None, identifier=None):
204239
if container is None:
205240
container = ConjunctiveGraph(identifier=identifier)
206241
nm = container.namespace_manager
@@ -385,54 +420,36 @@ def encode_container(self, bundle, container=None, identifier=None):
385420
container.add((identifier, pred, obj))
386421
return container
387422

388-
def decode_document(self, content, document):
423+
def decode_document(self, content, document,
424+
relation_mapper=relation_mapper,
425+
predicate_mapper=predicate_mapper):
389426
for prefix, url in content.namespaces():
390427
document.add_namespace(prefix, six.text_type(url))
391428
if hasattr(content, 'contexts'):
392429
for graph in content.contexts():
393430
if isinstance(graph.identifier, BNode):
394-
self.decode_container(graph, document)
431+
self.decode_container(graph, document,
432+
relation_mapper=relation_mapper,
433+
predicate_mapper=predicate_mapper)
395434
else:
396435
bundle_id = six.text_type(graph.identifier)
397436
bundle = document.bundle(bundle_id)
398-
self.decode_container(graph, bundle)
437+
self.decode_container(graph, bundle,
438+
relation_mapper=relation_mapper,
439+
predicate_mapper=predicate_mapper)
399440
else:
400-
self.decode_container(content, document)
441+
self.decode_container(content, document,
442+
relation_mapper=relation_mapper,
443+
predicate_mapper=predicate_mapper)
401444

402-
def decode_container(self, graph, bundle):
445+
def decode_container(self, graph, bundle, relation_mapper=relation_mapper,
446+
predicate_mapper=predicate_mapper):
403447
ids = {}
404448
PROV_CLS_MAP = {}
405449
formal_attributes = {}
406450
unique_sets = {}
407451
for key, val in PROV_BASE_CLS.items():
408452
PROV_CLS_MAP[key.uri] = PROV_BASE_CLS[key]
409-
relation_mapper = {URIRef(PROV['alternateOf'].uri): 'alternate',
410-
URIRef(PROV['actedOnBehalfOf'].uri): 'delegation',
411-
URIRef(PROV['specializationOf'].uri): 'specialization',
412-
URIRef(PROV['mentionOf'].uri): 'mention',
413-
URIRef(PROV['wasAssociatedWith'].uri): 'association',
414-
URIRef(PROV['wasDerivedFrom'].uri): 'derivation',
415-
URIRef(PROV['wasAttributedTo'].uri): 'attribution',
416-
URIRef(PROV['wasInformedBy'].uri): 'communication',
417-
URIRef(PROV['wasGeneratedBy'].uri): 'generation',
418-
URIRef(PROV['wasInfluencedBy'].uri): 'influence',
419-
URIRef(PROV['wasInvalidatedBy'].uri): 'invalidation',
420-
URIRef(PROV['wasEndedBy'].uri): 'end',
421-
URIRef(PROV['wasStartedBy'].uri): 'start',
422-
URIRef(PROV['hadMember'].uri): 'membership',
423-
URIRef(PROV['used'].uri): 'usage',
424-
}
425-
predicate_mapper = {RDFS.label: pm.PROV['label'],
426-
URIRef(PROV['atLocation'].uri): PROV_LOCATION,
427-
URIRef(PROV['startedAtTime'].uri): PROV_ATTR_STARTTIME,
428-
URIRef(PROV['endedAtTime'].uri): PROV_ATTR_ENDTIME,
429-
URIRef(PROV['atTime'].uri): PROV_ATTR_TIME,
430-
URIRef(PROV['hadRole'].uri): PROV_ROLE,
431-
URIRef(PROV['hadPlan'].uri): pm.PROV_ATTR_PLAN,
432-
URIRef(PROV['hadUsage'].uri): pm.PROV_ATTR_USAGE,
433-
URIRef(PROV['hadGeneration'].uri): pm.PROV_ATTR_GENERATION,
434-
URIRef(PROV['hadActivity'].uri): pm.PROV_ATTR_ACTIVITY,
435-
}
436453
other_attributes = {}
437454
for stmt in graph.triples((None, RDF.type, None)):
438455
id = six.text_type(stmt[0])

src/prov/tests/test_dot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class SVGDotOutputTest(DocumentBaseTestCase, AllTestsBase):
2525

2626
def do_tests(self, prov_doc, msg=None):
2727
dot = prov_to_dot(prov_doc)
28-
svg_content = dot.create(format="svg")
28+
svg_content = dot.create(format="svg", encoding="utf-8")
2929
# Very naive check of the returned SVG content as we have no way to check the graphical content
3030
self.assertGreater(
3131
len(svg_content), self.MIN_SVG_SIZE,

0 commit comments

Comments
 (0)