|
1 | 1 | package de.uhd.ifi.se.decision.management.jira.git;
|
2 | 2 |
|
3 | 3 | import java.util.List;
|
| 4 | +import java.util.stream.Collectors; |
4 | 5 |
|
5 | 6 | import org.eclipse.jgit.diff.DiffEntry;
|
6 | 7 |
|
|
27 | 28 | * improvement and maintenance by developers. Developers can manually change
|
28 | 29 | * links. 3) Automatic trace link maintenance during git fetch based on recent
|
29 | 30 | * changes.
|
| 31 | + * |
| 32 | + * @issue Which files should be integrated into the knowledge graph? |
| 33 | + * @decision Integrate all Java files into the knowledge graph and link them to |
| 34 | + * the respective Jira issues (e.g., work items or requirements)! |
30 | 35 | */
|
31 | 36 | public class CodeFileExtractorAndMaintainer {
|
32 | 37 |
|
33 |
| - private String projectKey; |
34 | 38 | private CodeClassPersistenceManager codeFilePersistenceManager;
|
| 39 | + private KnowledgeGraph graph; |
35 | 40 |
|
36 | 41 | public CodeFileExtractorAndMaintainer(String projectKey) {
|
37 |
| - this.projectKey = projectKey; |
38 | 42 | this.codeFilePersistenceManager = KnowledgePersistenceManager.getInstance(projectKey)
|
39 | 43 | .getCodeClassPersistenceTextManager();
|
40 |
| - } |
41 |
| - |
42 |
| - /** |
43 |
| - * Extracts all code files and the decision knowledge from code comments within |
44 |
| - * the {@link Diff}. Links the files to the respective Jira Jira issues (e.g., |
45 |
| - * work items or requirements). Extracting means: 1) Adding code files to the |
46 |
| - * {@link CodeClassInDatabase}, 2) adding links to the {@link LinkInDatabase}, |
47 |
| - * 3) adding code files and links to the {@link KnowledgeGraph}. |
48 |
| - * |
49 |
| - * @param diff |
50 |
| - * {@link Diff} object with added, updated, or deleted |
51 |
| - * {@link ChangedFile}s. |
52 |
| - * |
53 |
| - * @issue Which files should be integrated into the knowledge graph? |
54 |
| - * @decision Integrate all Java files into the knowledge graph and link them to |
55 |
| - * the respective Jira issues (e.g., work items or requirements)! |
56 |
| - */ |
57 |
| - public void extractAllChangedFiles(Diff diff) { |
58 |
| - KnowledgeGraph graph = KnowledgeGraph.getInstance(projectKey); |
59 |
| - for (ChangedFile changedFile : diff.getChangedFiles()) { |
60 |
| - if (!changedFile.isCodeFileToExtract()) { |
61 |
| - continue; |
62 |
| - } |
63 |
| - List<DecisionKnowledgeElementInCodeComment> decisionKnowledgeInCodeComments = changedFile |
64 |
| - .getRationaleElementsFromCodeComments(); |
65 |
| - KnowledgeElement source = codeFilePersistenceManager.insertKnowledgeElement(changedFile, null); |
66 |
| - graph.updateElement(source); |
67 |
| - graph.addElementsNotInDatabase(source, decisionKnowledgeInCodeComments); |
68 |
| - } |
| 44 | + this.graph = KnowledgeGraph.getInstance(projectKey); |
69 | 45 | }
|
70 | 46 |
|
71 | 47 | /**
|
72 | 48 | * Either inserts, updates, or deletes the code files in the diff in the
|
73 | 49 | * database depending on its change type (see {@link ChangedFile#getDiffEntry()}
|
74 |
| - * and {@link DiffEntry#getChangeType()}. |
| 50 | + * and {@link DiffEntry#getChangeType()}. Also extracts the decision knowledge |
| 51 | + * from code comments within the {@link Diff}. Links the files to the respective |
| 52 | + * Jira issues (e.g., work items or requirements). |
| 53 | + * |
| 54 | + * Code extraction means: 1) Adding code files to the |
| 55 | + * {@link CodeClassInDatabase}, 2) adding links to the {@link LinkInDatabase}, |
| 56 | + * 3) adding code files and links to the {@link KnowledgeGraph}. |
75 | 57 | *
|
76 | 58 | * @param diff
|
77 | 59 | * {@link Diff} object with recently added, updated, or deleted
|
@@ -105,22 +87,49 @@ public void addUpdateOrDeleteChangedFileInDatabase(ChangedFile changedFile) {
|
105 | 87 | if (!changedFile.isCodeFileToExtract()) {
|
106 | 88 | return;
|
107 | 89 | }
|
| 90 | + |
108 | 91 | DiffEntry diffEntry = changedFile.getDiffEntry();
|
109 | 92 | switch (diffEntry.getChangeType()) {
|
110 |
| - case ADD: |
111 |
| - codeFilePersistenceManager.insertKnowledgeElement(changedFile, null); |
| 93 | + case DELETE: |
| 94 | + codeFilePersistenceManager.deleteKnowledgeElement(changedFile, null); |
112 | 95 | break;
|
113 |
| - case MODIFY: |
114 |
| - // new links could have been added |
115 |
| - // same as rename, thus, no break after add to fall through |
116 | 96 | case RENAME:
|
117 | 97 | codeFilePersistenceManager.updateKnowledgeElement(changedFile, null);
|
118 | 98 | break;
|
119 |
| - case DELETE: |
120 |
| - codeFilePersistenceManager.deleteKnowledgeElement(changedFile, null); |
121 |
| - break; |
122 | 99 | default:
|
| 100 | + case MODIFY: |
| 101 | + // rationale elements in code comments could have been added |
| 102 | + // no break after modify to fall through |
| 103 | + case ADD: |
| 104 | + List<DecisionKnowledgeElementInCodeComment> decisionKnowledgeInCodeComments = changedFile |
| 105 | + .getRationaleElementsFromCodeComments(); |
| 106 | + KnowledgeElement source = codeFilePersistenceManager.insertKnowledgeElement(changedFile, null); |
| 107 | + if (!graph.updateElement(source)) { |
| 108 | + graph.addVertex(source); |
| 109 | + } |
| 110 | + graph.addElementsNotInDatabase(source, decisionKnowledgeInCodeComments); |
123 | 111 | break;
|
124 | 112 | }
|
125 | 113 | }
|
| 114 | + |
| 115 | + /** |
| 116 | + * Deletes code files from database and the knowledge graph that do not exist in |
| 117 | + * the latest version (tagged with HEAD) in the git repository anymore. |
| 118 | + * |
| 119 | + * @param diff |
| 120 | + * for the current version in git. |
| 121 | + * @return true if any old code file was deleted. |
| 122 | + */ |
| 123 | + public boolean deleteOldFiles(Diff diff) { |
| 124 | + List<String> fileNamesInDiff = diff.getChangedFiles().stream().map(file -> file.getName()) |
| 125 | + .collect(Collectors.toList()); |
| 126 | + boolean isAnyFileDeleted = false; |
| 127 | + for (KnowledgeElement codeFileInDatabase : codeFilePersistenceManager.getKnowledgeElements()) { |
| 128 | + if (!fileNamesInDiff.contains(codeFileInDatabase.getSummary())) { |
| 129 | + codeFilePersistenceManager.deleteKnowledgeElement(codeFileInDatabase, null); |
| 130 | + isAnyFileDeleted = true; |
| 131 | + } |
| 132 | + } |
| 133 | + return isAnyFileDeleted; |
| 134 | + } |
126 | 135 | }
|
0 commit comments