-
Notifications
You must be signed in to change notification settings - Fork 15
/
util-networks.R
1734 lines (1438 loc) · 76.2 KB
/
util-networks.R
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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## This file is part of coronet, which is free software: you
## can redistribute it and/or modify it under the terms of the GNU General
## Public License as published by the Free Software Foundation, version 2.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
##
## Copyright 2016-2019 by Claus Hunsen <hunsen@fim.uni-passau.de>
## Copyright 2017 by Raphael Nömmer <noemmer@fim.uni-passau.de>
## Copyright 2017-2018 by Christian Hechtl <hechtl@fim.uni-passau.de>
## Copyright 2017-2019 by Thomas Bock <bockthom@fim.uni-passau.de>
## Copyright 2021, 2023 by Thomas Bock <bockthom@cs.uni-saarland.de>
## Copyright 2018 by Barbara Eckl <ecklbarb@fim.uni-passau.de>
## Copyright 2018-2019 by Jakob Kronawitter <kronawij@fim.uni-passau.de>
## Copyright 2020 by Anselm Fehnker <anselm@muenster.de>
## Copyright 2021 by Niklas Schneider <s8nlschn@stud.uni-saarland.de>
## Copyright 2022 by Jonathan Baumann <joba00002@stud.uni-saarland.de>
## Copyright 2023 by Maximilian Löffler <s8maloef@stud.uni-saarland.de>
## All Rights Reserved.
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Libraries ---------------------------------------------------------------
requireNamespace("R6") # for R6 classes
requireNamespace("logging") # for logging
requireNamespace("parallel") # for parallel computation
requireNamespace("plyr") # for dlply function
requireNamespace("igraph") # networks
requireNamespace("lubridate") # for date conversion
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Vertex and edge types ---------------------------------------------------
## vertex types
TYPE.AUTHOR = "Author"
TYPE.ARTIFACT = "Artifact"
## edge types
TYPE.EDGES.INTRA = "Unipartite"
TYPE.EDGES.INTER = "Bipartite"
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Edge-attribute handling during simplification ---------------------------
## Edge-attribute contraction: configure handling of attributes by name
EDGE.ATTR.HANDLING = list(
## network-analytic data
weight = "sum",
type = "first",
relation = "first",
## commit data
changed.files = "sum",
added.lines = "sum",
deleted.lines = "sum",
diff.size = "sum",
artifact.diff.size = "sum",
## everything else
"concat"
)
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Constants ---------------------------------------------------------------
## mapping of relation to data source
RELATION.TO.DATASOURCE = list(
"cochange" = "commits",
"callgraph" = "commits",
"mail" = "mails",
"issue" = "issues"
)
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## NetworkBuilder ----------------------------------------------------------
#' The class \code{NetworkBuilder} provides convenient network construction in a
#' lazy fashion.
#'
#' To configure an object of this network-construction class, a configuration
#' object of class \code{NetworkConf} and a data-carrying object of class
#' \code{ProjectData} must be passed to the initialization function.
#'
#' The following types of networks can be constructed:
#' - author networks,
#' - artifact networks,
#' - bipartite networks, and
#' - multi networks (a combination of all other types of networks).
#'
#' @seealso NetworkConf
#' @seealso ProjectData
NetworkBuilder = R6::R6Class("NetworkBuilder",
## * private -----------------------------------------------------------
private = list(
## * * data and configuration --------------------------------------
proj.data = NULL,
proj.data.original = NULL,
network.conf = NULL,
## * * network caching ---------------------------------------------
authors.network.mail = NULL, # igraph
authors.network.cochange = NULL, # igraph
authors.network.issue = NULL, #igraph
artifacts.network.cochange = NULL, # igraph
artifacts.network.callgraph = NULL, # igraph
artifacts.network.mail = NULL, # igraph
artifacts.network.issue = NULL, # igraph
## * * relation-to-vertex-kind mapping -----------------------------
#' Determine which vertex kind should be chosen for the vertex depending on the relation
#' between the vertices.
#'
#' @param relation the given relation
#'
#' @return the vertex kind to be used
get.vertex.kind.for.relation = function(relation) {
vertex.kind = switch(relation,
cochange = private$proj.data$get.project.conf.entry("artifact.codeface"),
callgraph = private$proj.data$get.project.conf.entry("artifact.codeface"),
mail = "MailThread",
issue = "Issue"
)
return(vertex.kind)
},
## * * data cutting ------------------------------------------------
#' Cut the data sources of the data object to the same date ranges.
cut.data.to.same.timestamps = function() {
cut.data = private$proj.data$get.data.cut.to.same.date(data.sources = private$get.data.sources())
private$proj.data = cut.data
},
#' Determine which data sources should be cut depending on the artifact and author relation.
#'
#' @return the data sources to be cut
get.data.sources = function() {
author.relation = private$network.conf$get.value("author.relation")
artifact.relation = private$network.conf$get.value("artifact.relation")
data.sources = unique(c(RELATION.TO.DATASOURCE[[author.relation]],
RELATION.TO.DATASOURCE[[artifact.relation]]))
return(data.sources)
},
## * * author networks ---------------------------------------------
#' Get the co-change-based author relation as network.
#' If it does not already exist build it first.
#'
#' @return the author network with cochange relation
get.author.network.cochange = function() {
logging::logdebug("get.author.network.cochange: starting.")
## do not compute anything more than once
if (!is.null(private$authors.network.cochange)) {
logging::logdebug("get.author.network.cochange: finished. (already existing)")
return(private$authors.network.cochange)
}
## Get a list of all artifacts extracted from the commit data. Each artifact in this group is again a list
## of all authors that were involved in making changes to this artifact. In the following two steps, some of
## the artifacts are filtered from this list, which removes all information (including author information)
## about these artifacts. Since we only want to lose the edge information and not the information about
## authors, they will explicitly be added in a later step.
author.groups = private$proj.data$group.authors.by.data.column("commits", "artifact")
## 1) if configured in the 'NetworkConf, remove the base artifact
if (!private$network.conf$get.value("edges.for.base.artifacts")) {
author.groups = author.groups[!(names(author.groups) %in% BASE.ARTIFACTS)]
}
## 2) in any case, remove the untracked files
author.groups = author.groups[names(author.groups) != UNTRACKED.FILE.EMPTY.ARTIFACT]
## construct edge list based on artifact2author data
author.net.data = construct.edge.list.from.key.value.list(
author.groups,
network.conf = private$network.conf,
directed = private$network.conf$get.value("author.directed"),
respect.temporal.order = private$network.conf$get.value("author.respect.temporal.order")
)
## Add author vertices back into the graph. Previously, commit information on untracked files
## ('UNTRACKED.FILE') and, if configured, the base artifact ('BASE.ARTIFACTS') has been removed and, hence,
## also corresponding author information. Re-add author vertices back to the network now by accessing the
## complete author list:
## 1) get all authors on commits
authors = private$proj.data$get.authors.by.data.source(data.sources = "commits")
## 2) only select author names
authors = authors["author.name"]
## 3) rename single column to "name" to correct mapping to vertex attribute "name"
colnames(authors) = "name"
## 4) set author list as vertices
author.net.data[["vertices"]] = authors
## construct network from obtained data
author.net = construct.network.from.edge.list(
author.net.data[["vertices"]],
author.net.data[["edges"]],
network.conf = private$network.conf,
directed = private$network.conf$get.value("author.directed"),
available.edge.attributes = private$proj.data$get.data.columns.for.data.source("commits")
)
## store network
private$authors.network.cochange = author.net
logging::logdebug("get.author.network.cochange: finished.")
return(author.net)
},
#' Get the thread-based author relation as network.
#' If it does not already exist build it first.
#'
#' @return the author network with mail relation
get.author.network.mail = function() {
logging::logdebug("get.author.network.mail: starting.")
## do not compute anything more than once
if (!is.null(private$authors.network.mail)) {
logging::logdebug("get.author.network.mail: finished. (already existing)")
return(private$authors.network.mail)
}
## construct edge list based on thread2author data
author.net.data = construct.edge.list.from.key.value.list(
private$proj.data$group.authors.by.data.column("mails", "thread"),
network.conf = private$network.conf,
directed = private$network.conf$get.value("author.directed"),
respect.temporal.order = private$network.conf$get.value("author.respect.temporal.order")
)
## construct network from obtained data
author.net = construct.network.from.edge.list(
author.net.data[["vertices"]],
author.net.data[["edges"]],
network.conf = private$network.conf,
directed = private$network.conf$get.value("author.directed"),
available.edge.attributes = private$proj.data$get.data.columns.for.data.source("mails")
)
## store network
private$authors.network.mail = author.net
logging::logdebug("get.author.network.mail: finished.")
return(author.net)
},
##get the issue based author relation as network
get.author.network.issue = function() {
logging::logdebug("get.author.network.issue: starting.")
if (!is.null(private$authors.network.issue)) {
logging::logdebug("get.author.network.issue: finished. (already existing)")
return(private$authors.network.issue)
}
## construct edge list based on issue2author data
author.net.data = construct.edge.list.from.key.value.list(
private$proj.data$group.authors.by.data.column("issues", "issue.id"),
network.conf = private$network.conf,
directed = private$network.conf$get.value("author.directed"),
respect.temporal.order = private$network.conf$get.value("author.respect.temporal.order")
)
## construct network from obtained data
author.net = construct.network.from.edge.list(
author.net.data[["vertices"]],
author.net.data[["edges"]],
network.conf = private$network.conf,
directed = private$network.conf$get.value("author.directed"),
available.edge.attributes = private$proj.data$get.data.columns.for.data.source("issues")
)
private$authors.network.issue = author.net
logging::logdebug("get.author.network.issue: finished.")
return(author.net)
},
## * * artifact networks -------------------------------------------
#' Get the co-change-based artifact network,
#' If it does not already exist build it first.
#'
#' @return the artifact network with cochange realtion
get.artifact.network.cochange = function() {
logging::logdebug("get.artifact.network.cochange: starting.")
## do not compute anything more than once
if (!is.null(private$artifacts.network.cochange)) {
logging::logdebug("get.artifact.network.cochange: finished. (already existing)")
return(private$artifacts.network.cochange)
}
## construct edge list based on commit--artifact data
artifacts.net.data.raw = private$proj.data$group.artifacts.by.data.column("commits", "hash")
artifacts.net.data = construct.edge.list.from.key.value.list(
artifacts.net.data.raw,
network.conf = private$network.conf,
directed = FALSE,
respect.temporal.order = TRUE,
artifact.edges = TRUE
)
## construct network from obtained data
artifacts.net = construct.network.from.edge.list(
artifacts.net.data[["vertices"]],
artifacts.net.data[["edges"]],
network.conf = private$network.conf,
directed = FALSE,
available.edge.attributes = private$proj.data$get.data.columns.for.data.source("commits")
)
## remove the artifact vertices stemming from untracked files if existing
if ("name" %in% igraph::list.vertex.attributes(artifacts.net) &&
length(igraph::V(artifacts.net)[name == UNTRACKED.FILE.EMPTY.ARTIFACT]) > 0) {
artifacts.net = igraph::delete.vertices(artifacts.net, UNTRACKED.FILE.EMPTY.ARTIFACT)
}
## store network
private$artifacts.network.cochange = artifacts.net
logging::logdebug("get.artifact.network.cochange: finished.")
return(artifacts.net)
},
#' Get the call-graph-based artifact network.
#' If it does not already exist build it first.
#' IMPORTANT: This only works for range-level analyses!
#'
#' @return the artifact network with callgraph relation
get.artifact.network.callgraph = function() {
logging::logdebug("get.artifact.network.callgraph: starting.")
## do not compute anything more than once
if (!is.null(private$artifacts.network.callgraph)) {
logging::logdebug("get.artifact.network.callgraph: finished. (already existing)")
return(private$artifacts.network.callgraph)
}
## check if revision for call-graphs is set
if (!("RangeData" %in% class(private$proj.data)) ||
is.na(private$proj.data$get.revision.callgraph())) {
logging::logerror("The call-graph revision is not set. Aborting...")
logging::logerror("This may be due to project-level analysis.
The call-graph data is only available in range-level analysis.")
stop("Trying to get call-graph data before setting a revision.")
}
## construct path and file
file.dir = private$proj.data$get.data.path.callgraph()
file.name = sprintf("cg_nw_%s_%s.net",
private$proj.data$get.project.conf.entry("artifact.short"),
private$proj.data$get.revision.callgraph())
file = file.path(file.dir, file.name)
## read network from disk
artifacts.net = igraph::read.graph(file, format = "pajek")
# set vertex labels properly (copy "id" attribute to "name" attribute)
artifacts.net = igraph::set.vertex.attribute(
artifacts.net,
"name",
igraph::V(artifacts.net),
igraph::get.vertex.attribute(artifacts.net, "id")
)
## process vertex names in artifact networks for consistent names:
## Since feature and file networks may have unique naming structures, the names
## need to be processed in order to match the ones coming from other analyses
## (e.g. Codeface):
## (1) retrieve parameters for processing
names = igraph::get.vertex.attribute(artifacts.net, "name")
artifact = private$proj.data$get.project.conf.entry("artifact")
## (2) different replacings for different artifacts
## feature
if (artifact == "feature") {
names = gsub("^CONFIG_", "ENABLE_", names) # BusyBox
names = gsub("^1$", "Base_Feature", names) # Base feature
}
## file
else if (artifact == "file" || artifact == "function") {
## transform to relative paths
names = gsub("^/local/bockthom/TypeChef-BusyboxAnalysis/gitbusybox/", "", names) # BusyBox
names = gsub("^/local/bockthom/openssl/", "", names) # OpenSSL
names = gsub("^/local/bockthom/sqlite/\\./", "", names) # SQLite
names = gsub("^/local/bockthom/sqlite/", "", names) # SQLite
## remove call-graph extension
names = gsub(".cg", "", names, fixed = TRUE)
}
## (3) set processed names inside graph object
artifacts.net = igraph::set.vertex.attribute(artifacts.net, "name", value = names)
## set edge attribute 'artifact.type' as the raw data do not contain this!
artifacts.net = igraph::set.edge.attribute(
artifacts.net, "artifact.type",
value = private$proj.data$get.project.conf.entry("artifact.codeface")
)
## store network
private$artifacts.network.callgraph = artifacts.net
logging::logdebug("get.artifact.network.callgraph: finished.")
return(artifacts.net)
},
#' Get the mail-based artifact network.
#' If it does not already exist build it first.
#'
#' @return the artifact network with mail relation
get.artifact.network.mail = function() {
logging::logdebug("get.artifact.network.mail: starting.")
## do not compute anything more than once
if (!is.null(private$artifacts.network.mail)) {
logging::logdebug("get.artifact.network.mail: finished. (already existing)")
return(private$artifacts.network.mail)
}
## log warning as we do not have relations among threads right now
logging::logwarn(paste(
"There exist no actual artifact network with the relation 'mail'.",
"Return an edge-less network now."
))
## construct edgeless network with mandatory edge and vertex attributes
directed = private$network.conf$get.value("artifact.directed")
artifacts = private$proj.data$get.artifacts("mails") # thread IDs
artifacts.net = create.empty.network(directed = directed, add.attributes = TRUE) +
igraph::vertices(artifacts)
## store network
private$artifacts.network.mail = artifacts.net
logging::logdebug("get.artifact.network.mail: finished.")
return(artifacts.net)
},
#' Get the issue-based artifact network.
#' If it does not already exist build it first.
#'
#' @return the artifact network with issue relation
get.artifact.network.issue = function() {
logging::logdebug("get.artifact.network.issue: starting.")
## do not compute anything more than once
if (!is.null(private$artifacts.network.issue)) {
logging::logdebug("get.artifact.network.issue: finished. (already existing)")
return(private$artifacts.network.issue)
}
## log warning as we do not have relations among issues right now
logging::logwarn(paste(
"There exist no actual artifact network with the relation 'issue'.",
"Return an edge-less network now."
))
## construct edgeless network with mandatory edge and vertex attributes
directed = private$network.conf$get.value("artifact.directed")
artifacts = private$proj.data$get.artifacts("issues") # issue IDs
artifacts.net = create.empty.network(directed = directed, add.attributes = TRUE) +
igraph::vertices(artifacts)
## store network
private$artifacts.network.issue = artifacts.net
logging::logdebug("get.artifact.network.issue: finished.")
return(artifacts.net)
},
## * * bipartite relations ------------------------------------------
#' Get the key-value data for the bipartite relations,
#' which are implied by the "artifact.relation" from the network configuration.
#'
#' @return a named list of data for the bipartite relations, each with the attributes
#' 'vertex.kind' denoting the artifact type for the relations
#' and 'relation' denoting the respective network-configuration entry; the names
#' are the relations configured by the attribute 'artifact.relation' of the
#' network configuration
get.bipartite.relations = function() {
logging::logdebug("get.bipartite.relations: starting.")
relations = private$network.conf$get.variable("artifact.relation")
logging::logdebug("Using bipartite relations '%s'.", relations)
bip.relations = lapply(relations, function(relation) {
## get data for current bipartite relation
data.source = RELATION.TO.DATASOURCE[[relation]]
bip.relation = private$proj.data$group.artifacts.by.data.column(data.source, "author.name")
## set vertex.kind and relation attributes
attr(bip.relation, "vertex.kind") = private$get.vertex.kind.for.relation(relation)
attr(bip.relation, "relation") = relation
return (bip.relation)
})
names(bip.relations) = relations
logging::logdebug("get.bipartite.relations: finished.")
return(bip.relations)
}
),
## * * public ----------------------------------------------------------
public = list(
#' Constructor of the class. Constructs a new instance based on the
#' given data object and the network configuration
#'
#' @param project.data the given data object
#' @param network.conf the network configuration
initialize = function(project.data, network.conf) {
## check arguments
private$proj.data.original = verify.argument.for.parameter(project.data, "ProjectData", "NetworkBuilder$new")
private$proj.data = project.data$clone()
private$network.conf = verify.argument.for.parameter(network.conf, "NetworkConf", "NetworkBuilder$new")
## cut data if needed
if (private$network.conf$get.value("unify.date.ranges")) {
private$cut.data.to.same.timestamps()
}
if (class(self)[1] == "NetworkBuilder") {
logging::loginfo("Initialized network builder for data %s.",
private$proj.data$get.class.name())
}
},
## * * resetting environment ---------------------------------------
#' Reset the current environment in order to rebuild it.
#' Has to be called whenever the data or configuration get changed.
reset.environment = function() {
private$authors.network.cochange = NULL
private$authors.network.issue = NULL
private$authors.network.mail = NULL
private$artifacts.network.callgraph = NULL
private$artifacts.network.cochange = NULL
private$artifacts.network.issue = NULL
private$artifacts.network.mail = NULL
private$proj.data = private$proj.data.original
if (private$network.conf$get.value("unify.date.ranges")) {
private$cut.data.to.same.timestamps()
}
},
## * * configuration -----------------------------------------------
#' Get the current network configuration.
#'
#' @return the 'network.conf' of the current instance of the class
get.network.conf = function() {
return(private$network.conf)
},
#' Set the current network configuration to the given one.
#'
#' @param network.conf the new network configuration.
#' @param reset.environment parameter to determine whether the environment
#' has to be reset or not [default: FALSE]
set.network.conf = function(network.conf, reset.environment = FALSE) {
private$network.conf = network.conf
if (reset.environment) {
self$reset.environment()
}
},
#' Get a value of the network configuration.
#'
#' @return the value of the given entry name
get.network.conf.entry = function(entry) {
return(private$network.conf$get.value(entry))
},
#' Set a value of the network configuration and reset the environment.
#'
#' @param entry the configuration option to set
#' @param value the new value that is assigned to the configuration parameter
set.network.conf.entry = function(entry, value) {
private$network.conf$update.value(entry, value)
self$reset.environment()
},
#' Update the network configuration based on the given list
#' of values and reset the environment afterwards.
#'
#' @param updated.values the new values for the network configuration [default: list()]
update.network.conf = function(updated.values = list()) {
private$network.conf$update.values(updated.values = updated.values)
self$reset.environment()
},
#' Get the ProjectData object of the NetworkBuilder.
#' This method is mainly used for testing purposes at the moment.
#'
#' @return the project data object of the NetworkBuilder
get.project.data = function() {
return(private$proj.data)
},
## * * networks ----------------------------------------------------
#' Get the generic author network.
#'
#' @return the generic author network
get.author.network = function() {
logging::loginfo("Constructing author network.")
## construct network
relations = private$network.conf$get.value("author.relation")
networks = lapply(relations, function(relation) {
network = switch(
relation,
cochange = private$get.author.network.cochange(),
mail = private$get.author.network.mail(),
issue = private$get.author.network.issue(),
stop(sprintf("The author relation '%s' does not exist.", rel))
## TODO construct edge lists here and merge those (inline the private methods)
)
## set edge attributes on all edges
igraph::E(network)$type = TYPE.EDGES.INTRA
igraph::E(network)$relation = relation
return(network)
})
net = merge.networks(networks)
## add all missing authors to the network if wanted
if (private$network.conf$get.value("author.all.authors")) {
authors.all = private$proj.data$get.authors()[[ "author.name" ]]
authors.net = igraph::get.vertex.attribute(net, "name")
net = net + igraph::vertices(setdiff(authors.all, authors.net))
}
## remove all authors from the corresponding network who do not have touched any artifact
if (private$network.conf$get.value("author.only.committers")) {
## authors-artifact relation
authors.from.net = igraph::get.vertex.attribute(net, "name")
authors.from.artifacts = lapply(private$get.bipartite.relations(), function(bipartite.relation) {
return(names(bipartite.relation))
})
authors.from.artifacts = unlist(authors.from.artifacts)
if (!is.null(authors.from.artifacts)) {
net = igraph::delete.vertices(net, setdiff(authors.from.net, authors.from.artifacts))
}
}
## set vertex attributes for identifaction
igraph::V(net)$kind = TYPE.AUTHOR
igraph::V(net)$type = TYPE.AUTHOR
## add range attribute for later analysis (if available)
if ("RangeData" %in% class(private$proj.data)) {
attr(net, "range") = private$proj.data$get.range()
}
return(net)
},
#' Get the generic artifact network.
#'
#' @return the generic artifact network
get.artifact.network = function() {
logging::loginfo("Constructing artifact network.")
## construct network
relations = private$network.conf$get.value("artifact.relation")
networks = lapply(relations, function(relation) {
network = switch(
relation,
cochange = private$get.artifact.network.cochange(),
callgraph = private$get.artifact.network.callgraph(),
mail = private$get.artifact.network.mail(),
issue = private$get.artifact.network.issue(),
stop(sprintf("The artifact relation '%s' does not exist.", relation))
)
## set edge attributes on all edges
igraph::E(network)$type = TYPE.EDGES.INTRA
igraph::E(network)$relation = relation
## set vertex attribute 'kind' on all edges, corresponding to relation
vertex.kind = private$get.vertex.kind.for.relation(relation)
network = igraph::set.vertex.attribute(network, "kind", value = vertex.kind)
return(network)
})
net = merge.networks(networks)
## set vertex and edge attributes for identifaction
igraph::V(net)$type = TYPE.ARTIFACT
## add range attribute for later analysis (if available)
if ("RangeData" %in% class(private$proj.data)) {
attr(net, "range") = private$proj.data$get.range()
}
return(net)
},
#' Get the (real) bipartite network.
#'
#' @return the bipartite network
get.bipartite.network = function() {
## get data by the chosen relation
bipartite.relation.data = private$get.bipartite.relations()
directed = private$network.conf$get.value("author.directed")
vertex.data = lapply(bipartite.relation.data, function(net.to.net) {
## extract vertices for author network
if (private$network.conf$get.value("author.all.authors")) {
author.vertices = private$proj.data$get.authors()[[ "author.name" ]]
} else {
author.vertices = names(net.to.net)
}
## select all artifact vertices
artifact.vertices = unique(unlist(lapply(net.to.net, function(df) {
return(df[["data.vertices"]])
})))
## get vertex.kind from bipartite relation
vertex.kind = attr(net.to.net, "vertex.kind")
## join author and artifact vertices to the list of all vertices in the network
vertices = data.frame(
name = c(author.vertices, artifact.vertices),
kind = c(
rep(TYPE.AUTHOR, length(author.vertices)),
rep(vertex.kind , length(artifact.vertices))
),
type = c(
rep(TYPE.AUTHOR, length(author.vertices)),
rep(TYPE.ARTIFACT, length(artifact.vertices))
)
)
return(vertices)
})
## Merge network data and construct a vertex-only network for now:
## 1) construct vertex data (without edges)
vertex.data = merge.network.data(vertex.data = vertex.data, edge.data = NULL)[["vertices"]]
## 2) remove empty artifact, if names are available
if ("name" %in% colnames(vertex.data)) {
vertex.data = subset(vertex.data, !(name == UNTRACKED.FILE.EMPTY.ARTIFACT & type == TYPE.ARTIFACT))
}
## 3) obtain all possible data columns, i.e., edge attributes
available.edge.attributes = lapply(
private$network.conf$get.variable("artifact.relation"),
function(relation) {
data.source = RELATION.TO.DATASOURCE[[relation]]
data.cols = private$proj.data$get.data.columns.for.data.source(data.source)
return(data.cols)
}
)
available.edge.attributes = unlist(available.edge.attributes, recursive = FALSE)
available.edge.attributes = available.edge.attributes[
!duplicated(names(available.edge.attributes)) # remove duplicates based on names
]
## 4) construct network without edges
vertex.network = construct.network.from.edge.list(
vertices = vertex.data,
edge.list = create.empty.edge.list(),
network.conf = private$network.conf,
directed = directed,
available.edge.attributes = available.edge.attributes
)
## explicitly add vertex attributes if vertex data was empty
if (nrow(vertex.data) == 0) {
vertex.network = add.attributes.to.network(vertex.network, "vertex", attributes = list(
name = "character", kind = "character", type = "character"
))
}
## add edges to the vertex network
network = add.edges.for.bipartite.relation(
vertex.network,
bipartite.relations = bipartite.relation.data,
private$network.conf,
available.edge.attributes = available.edge.attributes
)
## remove vertices that are not committers if wanted
if (private$network.conf$get.value("author.only.committers")) {
committers = unique(private$proj.data$get.commits.unfiltered()[["author.name"]])
authors = igraph::get.vertex.attribute(network, "name", igraph::V(network)[ type == TYPE.AUTHOR ])
authors.to.remove = setdiff(authors, committers)
network = igraph::delete.vertices(network, authors.to.remove)
}
## add range attribute for later analysis (if available)
if ("RangeData" %in% class(private$proj.data)) {
attr(network, "range") = private$proj.data$get.range()
}
return(network)
},
#' Get all networks as list.
#' Build unification to avoid null-pointers.
#'
#' @return all networks in a list
get.networks = function() {
logging::loginfo("Constructing all networks.")
## author-artifact relation
authors.to.artifacts = private$get.bipartite.relations()
## bipartite network
bipartite.net = self$get.bipartite.network()
## author relation
authors.net = self$get.author.network()
## artifact relation
artifacts.net = self$get.artifact.network()
return(list(
"authors.to.artifacts" = authors.to.artifacts,
"bipartite.net" = bipartite.net,
"authors.net" = authors.net,
"artifacts.net" = artifacts.net
))
},
#' Get the multi network.
#' The multi network is basically the result of 'get.networks' combined
#' in one network.
#'
#' @return the multi network
get.multi.network = function() {
logging::loginfo("Constructing multi network.")
## construct the network parts we need for the multi network
networks = self$get.networks()
authors.to.artifacts = networks[["authors.to.artifacts"]]
authors.net = networks[["authors.net"]]
igraph::V(authors.net)$kind = TYPE.AUTHOR
artifacts.net = networks[["artifacts.net"]]
## remove authors from author-artifact relation, if needed:
## we only add bipartite edges for authors already present in the author network (this can be
## configured by 'author.only.committers', for example), thus, we need to remove any authors
## from the author--artifact relation that are superfluous
authors.from.net = igraph::get.vertex.attribute(authors.net, "name")
## save relation and intersect the author vertices from the author network and the
## bipartite networks
authors.to.artifacts = mapply(function(a2a.rel, relation.type) {
authors.from.artifacts = names(a2a.rel)
a2a = a2a.rel[ intersect(authors.from.net, authors.from.artifacts) ]
attr(a2a, "relation") = relation.type
return(a2a)
}, authors.to.artifacts, names(authors.to.artifacts), SIMPLIFY = FALSE)
## unify vertices with artifacts from bipartite and artifact relation:
## when the source for artifacts is different for the bipartite relation and the artifact relation (e.g.,
## "cochange" and "callgraph"), then the vertex names need to be unified so that all vertices are
## represented properly and, more important here, all bipartite relations can be added
artifacts = lapply(authors.to.artifacts, function(a2a.rel) {
artifact.list = plyr::rbind.fill(a2a.rel)[ c("data.vertices", "artifact.type") ]
artifact.list = unique(artifact.list)
return(artifact.list)
})
artifacts.all = plyr::rbind.fill(artifacts)
artifacts.from.net = igraph::get.vertex.attribute(artifacts.net, "name")
artifacts.to.add = setdiff(artifacts.all[["data.vertices"]], artifacts.from.net)
artifacts.to.add.kind = artifacts.all[
artifacts.all[["data.vertices"]] %in% artifacts.to.add, "artifact.type"
]
artifacts.net = artifacts.net + igraph::vertices(artifacts.to.add, type = TYPE.ARTIFACT,
kind = artifacts.to.add.kind)
## check directedness and adapt artifact network if needed
if (igraph::is.directed(authors.net) && !igraph::is.directed(artifacts.net)) {
logging::logwarn(paste0("Author network is directed, but artifact network is not.",
"Converting artifact network..."))
artifacts.net = igraph::as.directed(artifacts.net, mode = "mutual")
} else if (!igraph::is.directed(authors.net) && igraph::is.directed(artifacts.net)) {
logging::logwarn(paste0("Author network is undirected, but artifact network is not.",
"Converting artifact network..."))
artifacts.net = igraph::as.undirected(artifacts.net, mode = "each",
edge.attr.comb = EDGE.ATTR.HANDLING)
}
## reduce memory consumption by removing temporary data
rm(networks)
gc()
## combine the networks:
## 1) merge the existing networks
u = igraph::disjoint_union(authors.net, artifacts.net)
## As there is a bug in 'igraph::disjoint_union' in igraph versions 1.4.0, 1.4.1, and 1.4.2
## (see https://github.com/igraph/rigraph/issues/761), we need to adjust the type of the date attribute
## of the outcome of 'igraph::disjoint_union'.
## Note: The following temporary fix only considers the 'date' attribute. However, this problem could also
## affect several other attributes, whose classes are not adjusted in our temporary fix.
## The following code block should be redundant as soon as igraph has fixed their bug.
u.actual.edge.attribute.date = igraph::get.edge.attribute(u, "date")
if (!is.null(u.actual.edge.attribute.date)) {
if (is.list(u.actual.edge.attribute.date)) {
u.expected.edge.attribute.date = lapply(u.actual.edge.attribute.date, get.date.from.unix.timestamp)
} else {
u.expected.edge.attribute.date = get.date.from.unix.timestamp(u.actual.edge.attribute.date)
}
u = igraph::set.edge.attribute(u, "date", value = u.expected.edge.attribute.date)
}
## 2) add the bipartite edges
u = add.edges.for.bipartite.relation(u, authors.to.artifacts, private$network.conf)
## add range attribute for later analysis (if available)
if ("RangeData" %in% class(private$proj.data)) {
attr(u, "range") = private$proj.data$get.range()
}
return(u)
}
)
)
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Network and edge construction -------------------------------------------
#' Construct a edge list from the given list of lists.
#' For example for a list of authors per thread, where all authors are connected if they are
#' in the same thread (sublist).
#'
#' Important: The input needs to be compatible with the function \code{get.key.to.value.from.df}.
#'
#' If \code{directed}, as default, the order of things in the sublist is respected (if
#' \code{respect.temporal.order} is not set manually) and the 'edge.attr's hold the
#' vector of possible edge attributes in the given list.
#'
#' @param list the list of lists with data
#' @param network.conf the network configuration
#' @param directed whether or not the network should be directed [default: FALSE]
#' @param respect.temporal.order whether to respect the temporal order when constructing edges,
#' i.e., whether to only add edges from the later event to the previous one.
#' If \code{NA} is passed, the default value is taken.
#' [default: directed]
#' @param artifact.edges whether the key value data represents edges in an artifact network based
#' on the cochange relation
#' [default: FALSE]
#'
#' @return a list of two data.frames named 'vertices' and 'edges' (compatible with return value
#' of \code{igraph::as.data.frame})
construct.edge.list.from.key.value.list = function(list, network.conf, directed = FALSE,
respect.temporal.order = directed, artifact.edges = FALSE) {
logging::loginfo("Create edges.")
logging::logdebug("construct.edge.list.from.key.value.list: starting.")
## if no value for respect.temporal.order is specified (indicated by an NA value
## coming from the network configuration), use the value of directed parameter instead
if (is.na(respect.temporal.order)) {
respect.temporal.order = directed
}
## initialize an edge list to fill and the set of vertices
vertices.processed = c()
edge.list = data.frame()
keys = names(list)
keys.number = length(list)