-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathprovone.py
More file actions
907 lines (720 loc) · 28.2 KB
/
Copy pathprovone.py
File metadata and controls
907 lines (720 loc) · 28.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
"""Python implementation of the ProvONE Model Specification
References:
ProvONE: http://vcvcomputing.com/provone/provone.html
"""
import logging
import os
import io
import shutil
import tempfile
from six.moves.urllib.parse import urlparse
import nidm.core.serializers
from prov.constants import PROV_N_MAP, PROV_ATTR_STARTTIME, PROV_ATTR_ENDTIME, \
PROV_ATTR_TIME, PROV_DERIVATION, PROV_GENERATION, PROV_USAGE, \
PROV_COMMUNICATION, PROV_ASSOCIATION, PROV_ATTRIBUTION, PROV_ATTR_COLLECTION, \
PROV_ATTR_USAGE, PROV_MEMBERSHIP
from prov.model import ProvEntity, ProvAgent, ProvDocument, ProvAttribution, \
PROV_REC_CLS, ProvActivity, _ensure_datetime, ProvAssociation, \
ProvCommunication, ProvDerivation, ProvRelation, ProvGeneration, ProvUsage, \
ProvMembership, ProvRecord
from .Constants import PROVONE_N_MAP, PROVONE_PROCESS, PROVONE_INPUTPORT, \
PROVONE_OUTPUTPORT, PROVONE_DATA, PROVONE_DATALINK, PROVONE_SEQCTRLLINK, \
PROVONE_USER, PROVONE_PROCESSEXEC, PROVONE_ATTR_PROCESS, PROVONE_ATTR_USER, \
PROVONE_ATTR_PROCESSEXEC, PROVONE_ATTR_PLAN, \
PROVONE_ATTR_INFORMED, PROVONE_ATTR_INFORMANT, \
PROVONE_ATTR_GENERATED_DATA, PROVONE_ATTR_USED_DATA, \
PROVONE_ATTR_GENERATION, \
PROVONE_ATTR_DATA, PROVONE_ATTR_INPUTPORT, \
PROVONE_HASINPORT, PROVONE_ATTR_OUTPUTPORT, PROVONE_HASOUTPORT, \
PROVONE_HASSUBPROCESS, PROVONE_ATTR_DATALINK, PROVONE_INPORTTODL, \
PROVONE_OUTPORTTODL, PROVONE_DLTOOUTPORT, PROVONE_DLTOINPORT, \
PROVONE_ATTR_SEQCTRLLINK, PROVONE_CLTODESTP, PROVONE_SOURCEPTOCL, \
PROVONE_DATAONLINK, PROVONE_HASDEFAULTPARAM, \
PROVONE_ATTR_GENERATED_PROCESS, PROVONE_ATTR_USED_PROCESS, PROVONE_ISPARTOF, \
PROVONE_ATTR_RELATED_PREXEC, PROVONE_ATTR_USED_PREXEC, \
PROVONE_ATTR_CHILD_PREXEC, PROVONE_MEMBERSHIP
__author__ = 'Sanu Ann Abraham'
__email__ = 'sanuann@mit.edu'
logger = logging.getLogger(__name__)
# update ProvOne Notation mapping with PROV_N_MAP
PROVONE_N_MAP.update(PROV_N_MAP)
class ProvOneRecord(ProvRecord):
"""Base class for PROVONE records."""
def copy(self):
"""
Return an exact copy of this record.
"""
return PROVONE_REC_CLS[self.get_type()](
self._bundle, self.identifier, self.attributes
)
class ProvPlan(ProvEntity):
"""
ProvONE Plan element
"""
pass
class Process(ProvEntity):
"""
ProvONE Process element """
_prov_type = PROVONE_PROCESS
class InputPort(ProvEntity):
""" ProvONE Input Port element """
_prov_type = PROVONE_INPUTPORT
class OutputPort(ProvEntity):
""" ProvONE Output Port element"""
_prov_type = PROVONE_OUTPUTPORT
class Data(ProvEntity):
"""
basic unit of information consumed or produced by a Process. Multiple Data items may be grouped into a Collection.
"""
_prov_type = PROVONE_DATA
class DataLink(ProvEntity):
""" ProvONE DataLink Element """
_prov_type = PROVONE_DATALINK
class SeqCtrlLink(ProvEntity):
""" ProvONE SeqCtrlLink Element """
_prov_type = PROVONE_SEQCTRLLINK
class User(ProvAgent):
"""ProvONE User element."""
_prov_type = PROVONE_USER
class ProcessExec(ProvActivity):
""" ProvONE Process Execution element. """
_prov_type = PROVONE_PROCESSEXEC
class Attribution(ProvAttribution):
"""ProvONE Attribution relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_PROCESS, PROVONE_ATTR_USER)
_prov_type = PROV_ATTRIBUTION
class Association(ProvAssociation):
"""Provenance Association relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_PROCESSEXEC, PROVONE_ATTR_PROCESS,
PROVONE_ATTR_PLAN)
_prov_type = PROV_ASSOCIATION
class Communication(ProvCommunication):
"""Provenance Communication relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_INFORMED, PROVONE_ATTR_INFORMANT)
_prov_type = PROV_COMMUNICATION
class Derivation(ProvDerivation):
"""Provenance Derivation relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_GENERATED_DATA, PROVONE_ATTR_USED_DATA,
PROVONE_ATTR_PROCESSEXEC, PROVONE_ATTR_GENERATION,
PROV_ATTR_USAGE)
_prov_type = PROV_DERIVATION
class Generation(ProvGeneration):
"""Provenance Generation relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_DATA, PROVONE_ATTR_PROCESSEXEC, PROV_ATTR_TIME)
_prov_type = PROV_GENERATION
class Usage(ProvUsage):
"""Provenance Usage relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_PROCESSEXEC, PROVONE_ATTR_DATA, PROV_ATTR_TIME)
_prov_type = PROV_USAGE
class Partnership(ProvRelation):
"""Provenance Membership relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_USED_PREXEC,
PROVONE_ATTR_CHILD_PREXEC)
_prov_type = PROVONE_ISPARTOF
# Component 6: Collections
class Membership(ProvMembership):
"""Provenance Membership relationship."""
FORMAL_ATTRIBUTES = (PROV_ATTR_COLLECTION, PROVONE_ATTR_DATA)
#_prov_type = PROV_MEMBERSHIP
class HasInput(ProvRelation):
"""ProvONE HasInput Port relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_PROCESS,
PROVONE_ATTR_INPUTPORT)
_prov_type = PROVONE_HASINPORT
class HasOutput(ProvRelation):
"""ProvONE HasOutput Port relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_PROCESS, PROVONE_ATTR_OUTPUTPORT)
_prov_type = PROVONE_HASOUTPORT
class HasSubProcess(ProvRelation):
"""ProvONE Has SubProcess relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_USED_PROCESS, PROVONE_ATTR_GENERATED_PROCESS)
_prov_type = PROVONE_HASSUBPROCESS
class InToDL(ProvRelation):
""" ProvONE InPort to DL relationship """
FORMAL_ATTRIBUTES = (PROVONE_ATTR_INPUTPORT, PROVONE_ATTR_DATALINK)
_prov_type = PROVONE_INPORTTODL
class OutToDL(ProvRelation):
""" ProvONE Output port to DL relationship """
FORMAL_ATTRIBUTES = (PROVONE_ATTR_OUTPUTPORT, PROVONE_ATTR_DATALINK)
_prov_type = PROVONE_OUTPORTTODL
class DLtoOutPort(ProvRelation):
""" ProvONE DL to Output port relationship """
FORMAL_ATTRIBUTES = (PROVONE_ATTR_DATALINK, PROVONE_ATTR_OUTPUTPORT)
_prov_type = PROVONE_DLTOOUTPORT
class DLtoInPort(ProvRelation):
""" ProvONE DL to Input port relationship """
FORMAL_ATTRIBUTES = (PROVONE_ATTR_DATALINK, PROVONE_ATTR_INPUTPORT)
_prov_type = PROVONE_DLTOINPORT
class CLtoDestP(ProvRelation):
"""ProvONE CLtoDestP relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_SEQCTRLLINK, PROVONE_ATTR_PROCESS)
_prov_type = PROVONE_CLTODESTP
class SourcePtoCL(ProvRelation):
"""ProvONE SourcePtoCL relationship."""
FORMAL_ATTRIBUTES = (PROVONE_ATTR_PROCESS, PROVONE_ATTR_SEQCTRLLINK)
_prov_type = PROVONE_SOURCEPTOCL
class DataLinkage(ProvRelation):
""" ProvONE dataOnLink relationship """
FORMAL_ATTRIBUTES = (PROVONE_ATTR_DATA, PROVONE_ATTR_DATALINK,
PROVONE_ATTR_PROCESS)
_prov_type = PROVONE_DATAONLINK
class Parameterization(ProvRelation):
""" ProvONE hasDefaultParam relationship. """
FORMAL_ATTRIBUTES = (PROVONE_ATTR_INPUTPORT, PROVONE_ATTR_DATA)
_prov_type = PROVONE_HASDEFAULTPARAM
class Workflow(Process, ):
pass
# Class mappings from PROVONE record type
PROVONE_REC_CLS = {
PROVONE_PROCESS: Process,
PROVONE_PROCESSEXEC: ProcessExec,
PROVONE_DATA: Data,
PROVONE_INPUTPORT: InputPort,
PROVONE_HASINPORT: HasInput,
PROVONE_OUTPUTPORT: OutputPort,
PROVONE_HASOUTPORT: HasOutput,
PROVONE_HASSUBPROCESS: HasSubProcess,
PROVONE_DATALINK: DataLink,
PROVONE_INPORTTODL: InToDL,
PROVONE_SEQCTRLLINK: SeqCtrlLink,
PROVONE_CLTODESTP: CLtoDestP,
PROVONE_SOURCEPTOCL: SourcePtoCL,
PROVONE_OUTPORTTODL: OutToDL,
PROVONE_DLTOOUTPORT: DLtoOutPort,
PROVONE_DLTOINPORT: DLtoInPort,
PROVONE_DATAONLINK: DataLinkage,
PROVONE_HASDEFAULTPARAM: Parameterization,
PROVONE_USER: User,
PROVONE_ISPARTOF: Partnership,
}
PROVONE_REC_CLS.update(PROV_REC_CLS)
class ProvONEDocument(ProvDocument):
""" ProvONE Document"""
def __repr__(self):
return '<ProvONEDocument>'
def process(self, identifier, other_attributes=None):
"""
Creates a new process.
:param identifier: Identifier for new process.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(PROVONE_PROCESS, identifier, None,
other_attributes)
def user(self, identifier, other_attributes=None):
"""
Creates a new user.
:param identifier: Identifier for new user.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(PROVONE_USER, identifier, None, other_attributes)
def data(self, identifier, other_attributes=None):
"""
Creates a new data.
:param identifier: Identifier for new data.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(PROVONE_DATA, identifier, None,
other_attributes)
def attribution(self, process_spec, user, identifier=None,
other_attributes=None):
"""
Creates a new attribution record between a process specification and an user.
:param process_spec: ProcessSpecification or a string identifier for the process spec (relationship
source).
:param user: User or string identifier of the user involved in the
attribution (relationship destination).
:param identifier: Identifier for new attribution record.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROV_ATTRIBUTION, identifier, {
PROVONE_ATTR_PROCESS: process_spec,
PROVONE_ATTR_USER: user
},
other_attributes
)
def processExec(self, identifier, startTime=None, endTime=None,
other_attributes=None):
"""
Creates a new process execution.
:param identifier: Identifier for new process execution.
:param startTime: Optional start time for the process execution (default:
None).
Either a :py:class:`datetime.datetime` object or a string that can be
parsed by :py:func:`dateutil.parser`.
:param endTime: Optional end time for the process execution (default: None).
Either a :py:class:`datetime.datetime` object or a string that can be
parsed by :py:func:`dateutil.parser`.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_PROCESSEXEC, identifier, {
PROV_ATTR_STARTTIME: _ensure_datetime(startTime),
PROV_ATTR_ENDTIME: _ensure_datetime(endTime)
},
other_attributes
)
def association(self, process_exec, process_spec=None, plan=None,
identifier=None, other_attributes=None):
"""
Creates a new association record for a process execution.
:param process_exec: Process Execution or a string identifier for the
process execution.
:param process_spec: Process Spec or string identifier of the process
involved in the association (default: None).
:param plan: Optionally extra entity to state qualified association through
an internal plan (default: None).
:param identifier: Identifier for new association record.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROV_ASSOCIATION, identifier, {
PROVONE_ATTR_PROCESSEXEC: process_exec,
PROVONE_ATTR_PROCESS: process_spec,
PROVONE_ATTR_PLAN: plan
},
other_attributes
)
def derivation(self, generatedData, usedData, process_exec=None,
generation=None, usage=None,
identifier=None, other_attributes=None):
"""
Creates a new derivation record for a generated data from a used data.
:param generatedData: Data or a string identifier for the generated
data (relationship source).
:param usedData: Data or a string identifier for the used data
(relationship destination).
:param process_exec: Process execution or string identifier of the
processExec involved in the derivation (default: None).
:param generation: Optionally extra activity to state qualified generation
through a generation (default: None).
:param usage: XXX (default: None).
:param identifier: Identifier for new derivation record.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
attributes = {PROVONE_ATTR_GENERATED_DATA: generatedData,
PROVONE_ATTR_USED_DATA: usedData,
PROVONE_ATTR_PROCESSEXEC: process_exec,
PROVONE_ATTR_GENERATION: generation,
PROV_ATTR_USAGE: usage}
return self.new_record(
PROV_DERIVATION, identifier, attributes, other_attributes
)
def generation(self, data, process_exec=None, time=None, identifier=None,
other_attributes=None):
"""
Creates a new generation record for a data.
:param data: Data or a string identifier for the data.
:param process_exec: Process execution or string identifier of the
process_exec involved in the generation (default: None).
:param time: Optional time for the generation (default: None).
Either a :py:class:`datetime.datetime` object or a string that can be
parsed by :py:func:`dateutil.parser`.
:param identifier: Identifier for new generation record.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROV_GENERATION, identifier, {
PROVONE_ATTR_DATA: data,
PROVONE_ATTR_PROCESSEXEC: process_exec,
PROV_ATTR_TIME: _ensure_datetime(time)
},
other_attributes
)
def usage(self, process_exec, data=None, time=None, identifier=None,
other_attributes=None):
"""
Creates a new usage record for a process execution.
:param process_exec: Process Execution or a string identifier for the
processExec.
:param data: Data or string identifier of the data involved in
the usage relationship (default: None).
:param time: Optional time for the usage (default: None).
Either a :py:class:`datetime.datetime` object or a string that can be
parsed by :py:func:`dateutil.parser`.
:param identifier: Identifier for new usage record.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROV_USAGE, identifier, {
PROVONE_ATTR_PROCESSEXEC: process_exec,
PROVONE_ATTR_DATA: data,
PROV_ATTR_TIME: _ensure_datetime(time)},
other_attributes
)
def communication(self, informed, informant, identifier=None,
other_attributes=None):
"""
Creates a new communication record for a process exec.
:param informed: The informed processExec (relationship destination).
:param informant: The informing processExec (relationship source).
:param identifier: Identifier for new communication record.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROV_COMMUNICATION, identifier, {
PROVONE_ATTR_INFORMED: informed,
PROVONE_ATTR_INFORMANT: informant
},
other_attributes
)
def input_port(self, identifier, other_attributes=None):
"""
Creates a new input port.
:param identifier: Identifier for new input port.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(PROVONE_INPUTPORT, identifier, None,
other_attributes)
def has_in_ports(self, process, in_ports, identifier=None,
other_attributes=None):
"""
Creates a new input port record for a process.
:param process: Process or a string identifier for the
process(relationship source).
:param in_ports: Input Port or string identifier for the used input port (
relationship destination).
:param identifier: Identifier for new input port membership.
:param other_attributes: Optional other attributes as a dictionary or
list of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_HASINPORT, identifier, {
PROVONE_ATTR_PROCESS: process,
PROVONE_ATTR_INPUTPORT: in_ports,
},
other_attributes
)
def output_port(self, identifier, other_attributes=None):
"""
Creates a new output port.
:param identifier: Identifier for new output port.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(PROVONE_OUTPUTPORT, identifier, None,
other_attributes)
def has_out_ports(self, process, out_ports, identifier=None,
other_attributes=None):
"""
Creates a new input port record for a process.
:param process: Process or a string identifier for the
process(relationship source).
:param out_ports: Output Port or string identifier for the used output
port (relationship destination).
:param identifier: Identifier for new output port membership.
:param other_attributes: Optional other attributes as a dictionary or
list of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_HASOUTPORT, identifier, {
PROVONE_ATTR_PROCESS: process,
PROVONE_ATTR_OUTPUTPORT: out_ports,
},
other_attributes
)
def has_sub_process(self, used_process, generated_process, identifier=None,
other_attributes=None ):
"""
Creates a new has-sub-process record for a generated process from a
used process.
:param used_process: Process or a string identifier for the
used process (relationship source).
:param generated_process: Process or a string identifier for the
generated process (relationship destination).
:param identifier: Identifier for new sub-process record.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
attributes = {PROVONE_ATTR_USED_PROCESS: used_process,
PROVONE_ATTR_GENERATED_PROCESS: generated_process}
return self.new_record(
PROVONE_HASSUBPROCESS, identifier, attributes, other_attributes
)
def dataLink(self, identifier, other_attributes=None):
"""
Creates a new data link.
:param identifier: Identifier for new data link.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(PROVONE_DATALINK, identifier, None,
other_attributes)
def inPortToDL(self, in_port, dt_link, identifier=None,
other_attributes=None):
"""
:param in_port: Input port or a string identifier for the
in_port(relationship source).
:param dt_link: Data Link or string identifier for the used data link (
relationship destination).
:param identifier: Identifier for new data link membership.
:param other_attributes: Optional other attributes as a dictionary or
list of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_INPORTTODL, identifier, {
PROVONE_ATTR_INPUTPORT: in_port,
PROVONE_ATTR_DATALINK: dt_link,
},
other_attributes
)
def outPortToDL(self, out_port, dt_link, identifier=None,
other_attributes=None):
"""
:param out_port: Output port or a string identifier for the
out_port(relationship source).
:param dt_link: Data Link or string identifier for the used data link (
relationship destination).
:param identifier: Identifier for new output-data link membership.
:param other_attributes: Optional other attributes as a dictionary or
list of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_OUTPORTTODL, identifier, {
PROVONE_ATTR_OUTPUTPORT: out_port,
PROVONE_ATTR_DATALINK: dt_link,
},
other_attributes
)
def DLToOutPort(self, dt_link, out_port, identifier=None,
other_attributes=None):
"""
:param dt_link: Data Link or string identifier for the used data link (
relationship source).
:param out_port: Output port or a string identifier for the
out_port(relationship destination).
:param identifier: Identifier for new data link-output membership.
:param other_attributes: Optional other attributes as a dictionary or
list of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_DLTOOUTPORT, identifier, {
PROVONE_ATTR_DATALINK: dt_link,
PROVONE_ATTR_OUTPUTPORT: out_port,
},
other_attributes
)
def DLToInPort(self, dt_link, in_port, identifier=None,
other_attributes=None):
"""
:param dt_link: Data Link or string identifier for the used data link (
relationship source).
:param in_port: Input port or a string identifier for the in_port (
relationship destination).
:param identifier: Identifier for new data link-output membership.
:param other_attributes: Optional other attributes as a dictionary or
list of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_DLTOINPORT, identifier, {
PROVONE_ATTR_DATALINK: dt_link,
PROVONE_ATTR_INPUTPORT: in_port,
},
other_attributes
)
def seqCtrlLink(self, identifier, other_attributes=None):
"""
Creates a new seq ctrl link.
:param identifier: Identifier for new seq ctrl link.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(PROVONE_SEQCTRLLINK, identifier, None,
other_attributes)
def control_link_to_process(self, used_cntrl_link, used_process,
identifier=None, other_attributes=None):
"""
:param used_cntrl_link: Control Link or a string identifier for the used
control link (relationship source).
:param used_process: Data Link or string identifier for the used process
(relationship destination).
:param identifier: Identifier for new control link to process relation.
:param other_attributes: Optional other attributes as a dictionary or
list of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_CLTODESTP, identifier, {
PROVONE_ATTR_SEQCTRLLINK: used_cntrl_link,
PROVONE_ATTR_PROCESS: used_process,
},
other_attributes)
def process_to_control_link(self, used_process, used_cntrl_link,
identifier=None, other_attributes=None):
"""
:param used_process: Process or string identifier for the used process
(relationship source).
:param used_cntrl_link: Control Link or a string identifier for the used
control link (relationship destination).
:param identifier: Identifier for new process to control link relation.
:param other_attributes: Optional other attributes as a dictionary or
list of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_SOURCEPTOCL, identifier, {
PROVONE_ATTR_PROCESS: used_process,
PROVONE_ATTR_SEQCTRLLINK: used_cntrl_link,
},
other_attributes)
def linkage(self, data_item, dl_link, related_process=None,
identifier=None,
other_attributes=None):
"""
:param data_item: Data or string identifier for the associated data (
relationship source).
:param dl_link: Data link or string identifier for the data link (
relationship destination).
:param related_process: Process or string identifier of the data link (default=None)
:param identifier: Identifier for new data-on-link relation.
:param other_attributes: Optional other attributes as a dictionary or
list of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_DATAONLINK, identifier, {
PROVONE_ATTR_DATA: data_item,
PROVONE_ATTR_DATALINK: dl_link,
PROVONE_ATTR_PROCESS: related_process,
},
other_attributes)
def parameterization(self, in_port, data_item, identifier=None,
other_attributes=None):
"""
:param in_port: InputPort or string identifier for the associated in port (
relationship source).
:param data_item: Data item or string identifier for the associated
default parameter (relationship destination).
:param identifier: Identifier for new default parameter relation.
:param other_attributes: Optional other attributes as a dictionary or
list of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_HASDEFAULTPARAM, identifier, {
PROVONE_ATTR_INPUTPORT: in_port,
PROVONE_ATTR_DATA: data_item,
},
other_attributes)
def serialize(self, destination=None, format='json', **args):
"""
Serialize the :py:class:`ProvDocument` to the destination.
Available serializers can be queried by the value of
`:py:attr:~prov.serializers.Registry.serializers` after loading them via
`:py:func:~prov.serializers.Registry.load_serializers()`.
:param destination: Stream object to serialize the output to. Default is
`None`, which serializes as a string.
:param format: Serialization format (default: 'json'), defaulting to
PROV-JSON.
:return: Serialization in a string if no destination was given,
None otherwise.
"""
serializer = nidm.core.serializers.get(format)(self)
if destination is None:
stream = io.StringIO()
serializer.serialize(stream, **args)
return stream.getvalue()
if hasattr(destination, "write"):
stream = destination
serializer.serialize(stream, **args)
else:
location = destination
scheme, netloc, path, params, _query, fragment = urlparse(location)
if netloc != "":
print("WARNING: not saving as location " +
"is not a local file reference")
return
fd, name = tempfile.mkstemp()
stream = os.fdopen(fd, "wb")
serializer.serialize(stream, **args)
stream.close()
if hasattr(shutil, "move"):
shutil.move(name, path)
else:
shutil.copy(name, path)
os.remove(name)
def is_part_of(self, used_processex, child_processex, identifier=None,
other_attributes=None):
"""
Creates a new is-part-of record for a process exec.
:param used_processex: The parent processExec (relationship source).
:param child_processex: The child processExec (relationship destination).
:param identifier: Identifier for new is-part-of record.
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
return self.new_record(
PROVONE_ISPARTOF, identifier, {
PROVONE_ATTR_USED_PREXEC: used_processex,
PROVONE_ATTR_CHILD_PREXEC: child_processex
},
other_attributes
)
def membership(self, collection, data):
"""
Creates a new membership record for data to a collection.
:param collection: Collection the data is to be added to.
:param data: Data to be added to the collection.
"""
return self.new_record(
PROV_MEMBERSHIP, None, {
PROV_ATTR_COLLECTION: collection,
PROVONE_ATTR_DATA: data
}
)
# same method as in prov/model.py with just the modification of PROVONE
# constants. Need to re-architect prov and then make necessary changes in
# provone.
def new_record(self, record_type, identifier, attributes=None,
other_attributes=None):
"""
Creates a new record.
:param record_type: Type of record (one of :py:const:`PROVONE_REC_CLS`).
:param identifier: Identifier for new record.
:param attributes: Attributes as a dictionary or list of tuples to be added
to the record optionally (default: None).
:param other_attributes: Optional other attributes as a dictionary or list
of tuples to be added to the record optionally (default: None).
"""
attr_list = []
if attributes:
if isinstance(attributes, dict):
attr_list.extend(
(attr, value) for attr, value in attributes.items()
)
else:
# expecting a list of attributes here
attr_list.extend(attributes)
if other_attributes:
attr_list.extend(
other_attributes.items() if isinstance(other_attributes, dict)
else other_attributes
)
new_record = PROVONE_REC_CLS[record_type](
self, self.valid_qualified_name(identifier), attr_list
)
self._add_record(new_record)
return new_record
# Aliases
wasAttributedTo = attribution
wasAssociatedWith = association
wasDerivedFrom = derivation
wasGeneratedBy = generation
wasInformedBy = communication
used = usage
hasInPort = has_in_ports
hasOutPort = has_out_ports
hasSubProcess = has_sub_process
CLtoDestP = control_link_to_process
sourcePToCL = process_to_control_link
hasDefaultParam = parameterization
dataOnLink = linkage
isPartOf = is_part_of
hadMember = membership