-
Notifications
You must be signed in to change notification settings - Fork 15
/
util-networks.R
1860 lines (1544 loc) · 83 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 <[email protected]>
## Copyright 2017 by Raphael Nömmer <[email protected]>
## Copyright 2017-2018 by Christian Hechtl <[email protected]>
## Copyright 2017-2019 by Thomas Bock <[email protected]>
## Copyright 2021, 2023-2024 by Thomas Bock <[email protected]>
## Copyright 2018 by Barbara Eckl <[email protected]>
## Copyright 2018-2019 by Jakob Kronawitter <[email protected]>
## Copyright 2020 by Anselm Fehnker <[email protected]>
## Copyright 2021 by Niklas Schneider <[email protected]>
## Copyright 2022 by Jonathan Baumann <[email protected]>
## Copyright 2023-2024 by Maximilian Löffler <[email protected]>
## 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 = function(relation) sort(unique(relation)),
## 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)
}
if (private$proj.data$get.project.conf()$get.entry("issues.only.comments")) {
logging::logwarn(paste(
"Create an edge-less artifact network as 'issues.only.comments' is set.",
"Comments in issues cannot create issue edges."
))
}
## construct edge list based on issue-artifact data
artifacts.net.data.raw = private$proj.data[[DATASOURCE.TO.ARTIFACT.FUNCTION[["issues"]]]]()
## obtain issue-connecting events
add.links = artifacts.net.data.raw[artifacts.net.data.raw$event.name == "add_link" &
artifacts.net.data.raw$event.info.2 == "issue", ]
referenced.bys = artifacts.net.data.raw[artifacts.net.data.raw$event.name == "referenced_by" &
artifacts.net.data.raw$event.info.2 == "issue", ]
## the codeface extraction for jira issues creates duplicate events, linking the referenced issue
## to the referencing issue, in addition to the correct events, linking the referencing issue to
## the referenced issue. We can only deduplicate them, if we build an undirected network, as otherwise,
## we would need to guess the correct direction.
if (!private$network.conf$get.entry("artifact.directed")) {
## obtain 'add_link' events from jira
jira.add.links = add.links[add.links$issue.source == "jira", ]
matched = list()
## iterate over all add_link events from jira
for (i in 1:nrow(jira.add.links)) {
add.link = jira.add.links[i, ]
## ensure not to remove both duplicate edges
if (any(sapply(matched, function(entry) identical(entry, add.link)))) {
next
}
## match any 'add_link' events, that are the reverse direction of 'add.link',
## but have the same timestamp and author information
match = jira.add.links[(
jira.add.links$issue.id == add.link$event.info.1 &
jira.add.links$event.info.1 == add.link$issue.id &
jira.add.links$date == add.link$date &
jira.add.links$author.name == add.link$author.name), ]
## if a match is found, remove 'add.link' and its corresponding 'referenced_by' event
if (nrow(match) > 0) {
add.links = add.links[!(
add.links$issue.id == match$issue.id &
add.links$event.info.1 == match$event.info.1 &
add.links$date == match$date &
add.links$author.name == match$author.name), ]
referenced.bys = referenced.bys[!(
referenced.bys$issue.id == add.link$issue.id &
referenced.bys$event.info.1 == add.link$event.info.1 &
referenced.bys$date == add.link$date &
referenced.bys$author.name == add.link$author.name), ]
matched = append(matched, list(match))
}
}
}
if (nrow(add.links) != nrow(referenced.bys)) {
logging::logwarn("Inconsistent issue data. Unequally many 'add_link' and 'referenced_by' issue-events.")
}
vertices = unique(artifacts.net.data.raw["issue.id"])
edge.list = data.frame()
# edges in artifact networks can not have the 'artifact' attribute but should instead have
# the 'author.name' attribute as events caused by authors connect issues
edge.attributes = private$network.conf$get.value("edge.attributes")
artifact.index = match("artifact", edge.attributes, nomatch = NA)
if (!is.na(artifact.index)) {
edge.attributes = edge.attributes[-artifact.index]
edge.attributes = c(edge.attributes, c("author.name"))
}
## connect corresponding add_link and referenced_by issue-events
edge.list = plyr::rbind.fill(parallel::mclapply(split(add.links, seq_along(add.links)), function(from) {
## get edge attributes
cols.which = edge.attributes %in% colnames(from)
edge.attrs = from[, edge.attributes[cols.which], drop = FALSE]
## construct edge
to = subset(referenced.bys,
event.info.1 == from[["issue.id"]] &
author.name == from[["author.name"]] &
date == from[["date"]])
if (!all(is.na(to))) {
combination = list("from" = from[["issue.id"]], "to" = to[["issue.id"]])
combination = cbind(combination, edge.attrs, row.names = NULL) # add edge attributes
return(combination) # return the combination for this row
}
}))
artifacts.net.data = list(
vertices = data.frame(
name = vertices
),
edges = edge.list
)
## 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 = private$network.conf$get.value("artifact.directed"),
available.edge.attributes = private$proj.data$get.data.columns.for.data.source("issues")
)
## 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
## simplify network if wanted
if (private$network.conf$get.value("simplify")) {
net = simplify.network(net, simplify.multiple.relations =
private$network.conf$get.value("simplify.multiple.relations"))
}
## 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
## simplify network if wanted
if (private$network.conf$get.value("simplify")) {
net = simplify.network(net, simplify.multiple.relations =
private$network.conf$get.value("simplify.multiple.relations"))
}
## 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)
}
## simplify network if wanted
if (private$network.conf$get.value("simplify")) {
network = simplify.network(network, simplify.multiple.relations =
private$network.conf$get.value("simplify.multiple.relations"))
}
## 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