Skip to main content
summaryrefslogtreecommitdiffstats
blob: cb33e4bbe0cf082ca4929e9e83f468914a90efe2 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.define.traceability.operations;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.nebula.widgets.xviewer.XViewerColumn;
import org.eclipse.nebula.widgets.xviewer.XViewerColumn.SortDataType;
import org.eclipse.osee.define.DefinePlugin;
import org.eclipse.osee.define.traceability.TraceabilityExtractor;
import org.eclipse.osee.define.traceability.data.CodeUnitData;
import org.eclipse.osee.define.traceability.data.RequirementData;
import org.eclipse.osee.define.traceability.data.TestUnitData;
import org.eclipse.osee.define.traceability.data.TraceMark;
import org.eclipse.osee.define.traceability.data.TraceUnit;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.enums.IRelationEnumeration;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.ArtifactType;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.plugin.core.util.IExceptionableRunnable;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.skynet.core.OseeSystemArtifacts;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.utility.Requirements;
import org.eclipse.osee.framework.ui.skynet.results.IResultsEditorProvider;
import org.eclipse.osee.framework.ui.skynet.results.IResultsEditorTab;
import org.eclipse.osee.framework.ui.skynet.results.ResultsEditor;
import org.eclipse.osee.framework.ui.skynet.results.html.ResultsEditorHtmlTab;
import org.eclipse.osee.framework.ui.skynet.results.table.IResultsXViewerRow;
import org.eclipse.osee.framework.ui.skynet.results.table.ResultsEditorTableTab;
import org.eclipse.osee.framework.ui.skynet.results.table.ResultsXViewerRow;
import org.eclipse.swt.SWT;

/**
 * @author Roberto E. Escobar
 */
public class TraceUnitToArtifactProcessor implements ITraceUnitProcessor {
   private RequirementData requirementData;

   private CodeUnitData codeUnitData;
   private TestUnitData testUnitData;

   private final Branch importIntoBranch;
   private SkynetTransaction transaction;

   private final HashCollection<TraceUnit, TraceMark> reportTraceNotFound;
   private final HashCollection<String, String> unknownRelationError;
   private final Set<String> unRelatedUnits;

   public TraceUnitToArtifactProcessor(Branch importIntoBranch) {
      this.importIntoBranch = importIntoBranch;
      this.reportTraceNotFound = new HashCollection<TraceUnit, TraceMark>(false, HashSet.class);
      this.unknownRelationError = new HashCollection<String, String>(false, HashSet.class);
      this.unRelatedUnits = new HashSet<String>();
   }

   @Override
   public void clear() {
      transaction = null;
      if (requirementData != null) {
         requirementData.reset();
         requirementData = null;
      }
      if (testUnitData != null) {
         testUnitData.reset();
         testUnitData = null;
      }
      if (codeUnitData != null) {
         codeUnitData.reset();
         codeUnitData = null;
      }
   }

   @Override
   public void initialize(IProgressMonitor monitor) {
      transaction = null;
      requirementData = new RequirementData(importIntoBranch);
      if (!monitor.isCanceled()) {
         requirementData.initialize(monitor);
      }
   }

   private Artifact getArtifactFromCache(IProgressMonitor monitor, String artifactTypeName, String name) throws OseeCoreException {
      ArtifactType typeValue = ArtifactTypeManager.getType(artifactTypeName);
      if (typeValue.inheritsFrom(CoreArtifactTypes.TestUnit)) {
         if (testUnitData == null) {
            testUnitData = new TestUnitData(importIntoBranch);
            if (!monitor.isCanceled()) {
               testUnitData.initialize(monitor);
            }
         }
         return testUnitData.getTestUnitByName(name);
      } else if (typeValue.inheritsFrom(CoreArtifactTypes.CodeUnit)) {
         if (codeUnitData == null) {
            codeUnitData = new CodeUnitData(importIntoBranch);
            if (!monitor.isCanceled()) {
               codeUnitData.initialize(monitor);
            }
         }
         return codeUnitData.getCodeUnitByName(name);
      }
      return null;
   }

   @Override
   public void process(IProgressMonitor monitor, TraceUnit traceUnit) throws OseeCoreException {
      if (transaction == null) {
         transaction = new SkynetTransaction(importIntoBranch, "Importing Trace Unit(s)");
      }
      boolean hasChange = false;
      boolean artifactWasCreated = false;
      boolean wasRelated = false;
      String traceUnitType = traceUnit.getTraceUnitType();

      Artifact traceUnitArtifact = getArtifactFromCache(monitor, traceUnitType, traceUnit.getName());
      if (traceUnitArtifact == null) {
         traceUnitArtifact =
               ArtifactTypeManager.addArtifact(traceUnit.getTraceUnitType(), transaction.getBranch(),
                     traceUnit.getName());
         artifactWasCreated = true;
      }

      for (TraceMark traceMark : traceUnit.getTraceMarks()) {
         if (monitor.isCanceled()) {
            break;
         }

         Artifact requirementArtifact = getRequirementArtifact(traceMark.getRawTraceMark(), requirementData);
         if (requirementArtifact != null) {
            IRelationEnumeration relationType = getRelationFromTraceType(traceUnitArtifact, traceMark.getTraceType());
            if (relationType == null) {
               unknownRelationError.put(traceUnitArtifact.getArtifactTypeName(), traceMark.getTraceType());
            } else if (!requirementArtifact.isRelated(relationType, traceUnitArtifact)) {
               requirementArtifact.addRelation(relationType, traceUnitArtifact);
               hasChange = true;
               wasRelated = true;
            } else {
               wasRelated = true;
            }
         } else {
            reportTraceNotFound.put(traceUnit, traceMark);
         }
      }

      // TODO Report Items that were not used from the TEST UNIT DATA Structure
      // Not Part of this class though
      if (!wasRelated) {
         unRelatedUnits.add(traceUnitArtifact.getName());
      }

      if (hasChange || artifactWasCreated) {
         HierarchyHandler.addArtifact(transaction, traceUnitArtifact);
         if (traceUnitArtifact.isOfType(Requirements.ABSTRACT_TEST_UNIT)) {
            TestRunHandler.linkWithTestUnit(transaction, traceUnitArtifact);
         }
         traceUnitArtifact.persist(transaction);
      }
   }

   private boolean isUsesTraceType(String traceType) {
      return traceType.equalsIgnoreCase("USES");
   }

   private IRelationEnumeration getRelationFromTraceType(Artifact traceUnitArtifact, String traceType) throws OseeCoreException {
      if (traceUnitArtifact.isOfType(CoreArtifactTypes.TestUnit)) {
         if (isUsesTraceType(traceType)) {
            return CoreRelationTypes.Uses__TestUnit;
         } else {
            return CoreRelationTypes.Verification__Verifier;
         }
      } else if (traceUnitArtifact.isOfType(CoreArtifactTypes.CodeUnit)) {
         return CoreRelationTypes.CodeRequirement_CodeUnit;
      }
      return null;
   }

   private Artifact getRequirementArtifact(String traceMark, RequirementData requirementData) {
      Artifact toReturn = requirementData.getRequirementFromTraceMark(traceMark);
      if (toReturn == null) {
         Pair<String, String> structuredRequirement =
               TraceabilityExtractor.getInstance().getStructuredRequirement(traceMark);
         if (structuredRequirement != null) {
            toReturn = requirementData.getRequirementFromTraceMark(structuredRequirement.getFirst());
         }
      }
      return toReturn;
   }

   @Override
   public void onComplete(IProgressMonitor monitor) throws OseeCoreException {
      try {
         if (!monitor.isCanceled()) {
            if (transaction != null) {
               transaction.execute();
            }
         }
      } finally {
         openReport();
      }
   }

   private void openReport() {
      IExceptionableRunnable runnable = new IExceptionableRunnable() {

         @Override
         public IStatus run(IProgressMonitor monitor) throws Exception {
            ResultsEditor.open(new ResultEditorProvider());
            return Status.OK_STATUS;
         }
      };
      Jobs.runInJob("Trace Unit to Artifact Report", runnable, DefinePlugin.class, DefinePlugin.PLUGIN_ID);
   }

   private static final class TestRunHandler {

      private static void createGuidLink(Artifact testCase, Artifact testRun) throws OseeCoreException {
         if (testCase != null) {
            testRun.setSoleAttributeValue("Test Script GUID", testCase.getGuid());
         }
      }

      public static void linkWithTestUnit(SkynetTransaction transaction, Artifact testCase) throws OseeCoreException {
         if (testCase.isOfType(Requirements.TEST_CASE)) {
            List<Artifact> testRuns =
                  ArtifactQuery.getArtifactListFromTypeAndName(Requirements.TEST_RUN, testCase.getName(),
                        transaction.getBranch());

            for (Artifact testRun : testRuns) {
               createGuidLink(testCase, testRun);
            }
         }
      }
   }

   private static final class HierarchyHandler {

      public static void addArtifact(SkynetTransaction transaction, Artifact testUnit) throws OseeCoreException {
         Artifact folder = null;
         if (testUnit.isOfType(Requirements.TEST_CASE)) {
            folder = getOrCreateTestCaseFolder(transaction);
         } else if (testUnit.isOfType(Requirements.TEST_SUPPORT)) {
            folder = getOrCreateTestSupportFolder(transaction);
         } else if (testUnit.isOfType(CoreArtifactTypes.CodeUnit)) {
            folder = getOrCreateCodeUnitFolder(transaction);
         } else {
            folder = getOrCreateUnknownTestUnitFolder(transaction);
         }

         if (folder != null && !folder.isRelated(CoreRelationTypes.Default_Hierarchical__Child, testUnit)) {
            folder.addChild(testUnit);
            folder.persist(transaction);
         }
      }

      private static Artifact getOrCreateUnknownTestUnitFolder(SkynetTransaction transaction) throws OseeCoreException {
         return getOrCreateTestUnitSubFolder(transaction, "Unknown Test Unit Type");
      }

      private static Artifact getOrCreateTestSupportFolder(SkynetTransaction transaction) throws OseeCoreException {
         return getOrCreateTestUnitSubFolder(transaction, Requirements.TEST_SUPPORT_UNITS);
      }

      private static Artifact getOrCreateTestCaseFolder(SkynetTransaction transaction) throws OseeCoreException {
         return getOrCreateTestUnitSubFolder(transaction, "Test Cases");
      }

      private static Artifact getOrCreateCodeUnitFolder(SkynetTransaction transaction) throws OseeCoreException {
         Artifact codeUnitFolder = getOrCreateFolder(transaction, "Code Units");
         Artifact root = OseeSystemArtifacts.getDefaultHierarchyRootArtifact(transaction.getBranch());
         if (!root.isRelated(CoreRelationTypes.Default_Hierarchical__Child, codeUnitFolder)) {
            root.addChild(codeUnitFolder);
            root.persist(transaction);
         }
         return codeUnitFolder;
      }

      private static Artifact getOrCreateTestUnitSubFolder(SkynetTransaction transaction, String folderName) throws OseeCoreException {
         Artifact subFolder = getOrCreateFolder(transaction, folderName);
         Artifact testUnits = getOrCreateTestUnitsFolder(transaction);
         if (!testUnits.isRelated(CoreRelationTypes.Default_Hierarchical__Child, subFolder)) {
            testUnits.addChild(subFolder);
            testUnits.persist(transaction);
         }
         return subFolder;
      }

      private static Artifact getOrCreateTestUnitsFolder(SkynetTransaction transaction) throws OseeCoreException {
         Artifact testUnitFolder = getOrCreateFolder(transaction, "Test Units");
         Artifact root = OseeSystemArtifacts.getDefaultHierarchyRootArtifact(transaction.getBranch());
         if (!root.isRelated(CoreRelationTypes.Default_Hierarchical__Child, testUnitFolder)) {
            root.addChild(testUnitFolder);
            root.persist(transaction);
         }
         return testUnitFolder;
      }

      private static Artifact getOrCreateFolder(SkynetTransaction transaction, String folderName) throws OseeCoreException {
         return OseeSystemArtifacts.getOrCreateArtifact(CoreArtifactTypes.Folder, folderName, transaction.getBranch());
      }
   }

   private final class ResultEditorProvider implements IResultsEditorProvider {

      @Override
      public String getEditorName() throws OseeCoreException {
         return "Trace Units To Artifacts Report";
      }

      private List<XViewerColumn> createColumns(String... columnNames) {
         List<XViewerColumn> columns = new ArrayList<XViewerColumn>();
         for (String name : columnNames) {
            columns.add(new XViewerColumn(name, name, 80, SWT.LEFT, true, SortDataType.String, false, ""));
         }
         return columns;
      }

      private void addUnRelatedTraceUnit(List<IResultsEditorTab> toReturn) {
         if (!unRelatedUnits.isEmpty()) {
            List<XViewerColumn> columns = createColumns("Trace Unit Name");
            List<IResultsXViewerRow> rows = new ArrayList<IResultsXViewerRow>();
            for (String artifactName : unRelatedUnits) {
               rows.add(new ResultsXViewerRow(new String[] {artifactName}));
            }
            toReturn.add(new ResultsEditorTableTab("Trace Units Created But Had No Relations", columns, rows));
         }
      }

      private void addTraceNotFoundTab(List<IResultsEditorTab> toReturn) {
         if (!reportTraceNotFound.isEmpty()) {
            List<XViewerColumn> columns =
                  createColumns("Trace Unit Name", "Trace Unit Type", "Trace Mark Type", "Trace Mark");

            List<IResultsXViewerRow> rows = new ArrayList<IResultsXViewerRow>();
            for (TraceUnit unit : reportTraceNotFound.keySet()) {
               Collection<TraceMark> traceMarks = reportTraceNotFound.getValues(unit);
               for (TraceMark traceMark : traceMarks) {
                  rows.add(new ResultsXViewerRow(new String[] {unit.getName(), unit.getTraceUnitType(),
                        traceMark.getTraceType(), traceMark.getRawTraceMark()}));
               }
            }
            toReturn.add(new ResultsEditorTableTab("Trace Marks Not Found", columns, rows));
         }
      }

      private void addRelationTypeNotFoundTab(List<IResultsEditorTab> toReturn) {
         if (!unknownRelationError.isEmpty()) {
            List<XViewerColumn> columns = createColumns("Artifact Type", "Trace Mark Type");
            List<IResultsXViewerRow> rows = new ArrayList<IResultsXViewerRow>();
            for (String artifactType : unknownRelationError.keySet()) {
               Collection<String> traceTypes = unknownRelationError.getValues(artifactType);
               for (String traceType : traceTypes) {
                  rows.add(new ResultsXViewerRow(new String[] {artifactType, traceType}));
               }
            }
            toReturn.add(new ResultsEditorTableTab("Invalid Artifact Type to Trace Relation", columns, rows));
         }
      }

      @Override
      public List<IResultsEditorTab> getResultsEditorTabs() throws OseeCoreException {
         List<IResultsEditorTab> toReturn = new ArrayList<IResultsEditorTab>();
         addTraceNotFoundTab(toReturn);
         addUnRelatedTraceUnit(toReturn);
         addRelationTypeNotFoundTab(toReturn);
         if (toReturn.isEmpty()) {
            toReturn.add(new ResultsEditorHtmlTab("Trace Unit Import Status", "Import Status", "All Items Linked"));
         }
         return toReturn;
      }
   }
}

Back to the top