Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/prov/graph.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 from __future__ import (absolute_import, division, print_function, | |
2 unicode_literals) | |
3 | |
4 import networkx as nx | |
5 from prov.model import ( | |
6 ProvDocument, ProvRecord, ProvElement, ProvEntity, ProvActivity, ProvAgent, | |
7 ProvRelation, PROV_ATTR_ENTITY, PROV_ATTR_ACTIVITY, PROV_ATTR_AGENT, | |
8 PROV_ATTR_TRIGGER, PROV_ATTR_GENERATED_ENTITY, PROV_ATTR_USED_ENTITY, | |
9 PROV_ATTR_DELEGATE, PROV_ATTR_RESPONSIBLE, PROV_ATTR_SPECIFIC_ENTITY, | |
10 PROV_ATTR_GENERAL_ENTITY, PROV_ATTR_ALTERNATE1, PROV_ATTR_ALTERNATE2, | |
11 PROV_ATTR_COLLECTION, PROV_ATTR_INFORMED, PROV_ATTR_INFORMANT | |
12 ) | |
13 | |
14 __author__ = 'Trung Dong Huynh' | |
15 __email__ = 'trungdong@donggiang.com' | |
16 | |
17 | |
18 INFERRED_ELEMENT_CLASS = { | |
19 PROV_ATTR_ENTITY: ProvEntity, | |
20 PROV_ATTR_ACTIVITY: ProvActivity, | |
21 PROV_ATTR_AGENT: ProvAgent, | |
22 PROV_ATTR_TRIGGER: ProvEntity, | |
23 PROV_ATTR_GENERATED_ENTITY: ProvEntity, | |
24 PROV_ATTR_USED_ENTITY: ProvEntity, | |
25 PROV_ATTR_DELEGATE: ProvAgent, | |
26 PROV_ATTR_RESPONSIBLE: ProvAgent, | |
27 PROV_ATTR_SPECIFIC_ENTITY: ProvEntity, | |
28 PROV_ATTR_GENERAL_ENTITY: ProvEntity, | |
29 PROV_ATTR_ALTERNATE1: ProvEntity, | |
30 PROV_ATTR_ALTERNATE2: ProvEntity, | |
31 PROV_ATTR_COLLECTION: ProvEntity, | |
32 PROV_ATTR_INFORMED: ProvActivity, | |
33 PROV_ATTR_INFORMANT: ProvActivity | |
34 } | |
35 | |
36 | |
37 def prov_to_graph(prov_document): | |
38 """ | |
39 Convert a :class:`~prov.model.ProvDocument` to a `MultiDiGraph | |
40 <https://networkx.readthedocs.io/en/stable/reference/classes.multigraph.html>`_ | |
41 instance of the `NetworkX <https://networkx.github.io/>`_ library. | |
42 | |
43 :param prov_document: The :class:`~prov.model.ProvDocument` instance to convert. | |
44 """ | |
45 g = nx.MultiDiGraph() | |
46 unified = prov_document.unified() | |
47 node_map = dict() | |
48 for element in unified.get_records(ProvElement): | |
49 g.add_node(element) | |
50 node_map[element.identifier] = element | |
51 | |
52 for relation in unified.get_records(ProvRelation): | |
53 # taking the first two elements of a relation | |
54 attr_pair_1, attr_pair_2 = relation.formal_attributes[:2] | |
55 # only need the QualifiedName (i.e. the value of the attribute) | |
56 qn1, qn2 = attr_pair_1[1], attr_pair_2[1] | |
57 if qn1 and qn2: # only proceed if both ends of the relation exist | |
58 try: | |
59 if qn1 not in node_map: | |
60 node_map[qn1] = \ | |
61 INFERRED_ELEMENT_CLASS[attr_pair_1[0]](None, qn1) | |
62 if qn2 not in node_map: | |
63 node_map[qn2] = \ | |
64 INFERRED_ELEMENT_CLASS[attr_pair_2[0]](None, qn2) | |
65 except KeyError: | |
66 # Unsupported attribute; cannot infer the type of the element | |
67 continue # skipping this relation | |
68 g.add_edge(node_map[qn1], node_map[qn2], relation=relation) | |
69 return g | |
70 | |
71 | |
72 def graph_to_prov(g): | |
73 """ | |
74 Convert a `MultiDiGraph | |
75 <https://networkx.readthedocs.io/en/stable/reference/classes.multigraph.html>`_ | |
76 that was previously produced by :func:`prov_to_graph` back to a | |
77 :class:`~prov.model.ProvDocument`. | |
78 | |
79 :param g: The graph instance to convert. | |
80 """ | |
81 prov_doc = ProvDocument() | |
82 for n in g.nodes_iter(): | |
83 if isinstance(n, ProvRecord) and n.bundle is not None: | |
84 prov_doc.add_record(n) | |
85 for _, _, edge_data in g.edges_iter(data=True): | |
86 try: | |
87 relation = edge_data['relation'] | |
88 if isinstance(relation, ProvRecord): | |
89 prov_doc.add_record(relation) | |
90 except KeyError: | |
91 pass | |
92 | |
93 return prov_doc |