Skip to main content
summaryrefslogtreecommitdiffstats
blob: be4f6c64a77d74ba8906c4165a4e12334de3be72 (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
/*******************************************************************************
 * Copyright (c) 2011 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;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osee.define.internal.Activator;
import org.eclipse.osee.define.traceability.data.RequirementData;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;

/**
 * @author Roberto E. Escobar
 */
public class RequirementTraceabilityData {
   private final IOseeBranch testProcedureBranch;
   private final TraceabilityProviderOperation traceabilityProvider;
   private RequirementData requirementData;
   private HashCollection<Artifact, String> requirementsToCodeUnits;
   private final HashCollection<String, Artifact> requirementNameToTestProcedures =
      new HashCollection<String, Artifact>();
   private final Set<String> codeUnits = new TreeSet<>();
   private final Map<String, Artifact> testProcedures = new HashMap<>();
   private File testProcedureFilter;

   public RequirementTraceabilityData(IOseeBranch testProcedureBranch, TraceabilityProviderOperation traceabilityProvider) {
      this.testProcedureBranch = testProcedureBranch;
      this.traceabilityProvider = traceabilityProvider;
      this.testProcedureFilter = null;
      reset();
   }

   private void reset() {
      this.codeUnits.clear();
      this.testProcedures.clear();
      this.requirementsToCodeUnits = null;
      this.requirementData = null;
      this.requirementsToCodeUnits = null;
   }

   public IStatus initialize(IProgressMonitor monitor) {
      IStatus status = Status.CANCEL_STATUS;
      try {
         reset();
         monitor.subTask("Loading traceability data...");
         status = Operations.executeWork(traceabilityProvider, monitor);

         this.requirementData = traceabilityProvider.getRequirementData();
         this.requirementsToCodeUnits = traceabilityProvider.getRequirementToCodeUnitsMap();
         this.codeUnits.addAll(traceabilityProvider.getCodeUnits());

         if (monitor.isCanceled() != true) {
            if (status.getSeverity() == IStatus.OK) {
               getTestProcedureTraceability(testProcedureBranch);
            }
         } else {
            status = Status.CANCEL_STATUS;
         }
      } catch (Exception ex) {
         status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error gathering traceability data", ex);
      }
      return status;
   }

   private void getTestProcedureTraceability(IOseeBranch testProcedureBranch) throws OseeCoreException {
      // Map Software Requirements from TestProcedure IOseeBranch to Requirements IOseeBranch
      Map<String, Artifact> testProcedureBranchReqsToReqsBranchMap = new HashMap<>();
      for (Artifact tpRequirement : ArtifactQuery.getArtifactListFromType(CoreArtifactTypes.SoftwareRequirement,
         testProcedureBranch)) {
         testProcedureBranchReqsToReqsBranchMap.put(tpRequirement.getName(), tpRequirement);
      }

      Set<String> testProceduresFilter = getAllowedTestProcedures();
      for (Entry<String, Artifact> entry : testProcedureBranchReqsToReqsBranchMap.entrySet()) {
         Artifact requirement = entry.getValue();
         Set<Artifact> foundProcedures =
            new HashSet<Artifact>(requirement.getRelatedArtifacts(CoreRelationTypes.Validation__Validator));
         Set<Artifact> toAdd = new HashSet<>();
         if (testProceduresFilter.isEmpty() != true) {
            for (Artifact artifact : foundProcedures) {
               if (testProceduresFilter.contains(artifact.getName())) {
                  toAdd.add(artifact);
               }
            }
         } else {
            toAdd = foundProcedures;
         }
         requirementNameToTestProcedures.put(entry.getKey(), toAdd);
         for (Artifact artifact : toAdd) {
            this.testProcedures.put(artifact.getName(), artifact);
         }
      }
   }

   public Collection<Artifact> getDirectSwRequirements() {
      return requirementData.getDirectRequirements();
   }

   public Collection<Artifact> getAllSwRequirements() {
      return requirementData.getAllRequirements();
   }

   /**
    * Get Requirement Artifact based on traceMark mark if it fails, check if trace mark is a structured requirement and
    * try again
    * 
    * @return requirement artifact
    */
   public Artifact getRequirementFromTraceMark(String traceMark) {
      return this.requirementData.getRequirementFromTraceMarkIncludeStructuredRequirements(traceMark);
   }

   /**
    * @return the requirementsToCodeUnits
    */
   public HashCollection<Artifact, String> getRequirementsToCodeUnits() {
      return requirementsToCodeUnits;
   }

   /**
    * @return the codeUnits
    */
   public Set<String> getCodeUnits() {
      return codeUnits;
   }

   /**
    * @return the testProcedures
    */
   public Set<String> getTestProcedures() {
      return testProcedures.keySet();
   }

   /**
    * TODO need to use persisted test script NOTE: These artifact should not be persisted since they are temporary items
    * artifacts
    * 
    * @param artifact requirement to find
    * @return the test scripts associated with this requirement
    * @throws OseeCoreException
    */
   public Collection<Artifact> getTestScriptsForRequirement(Artifact artifact) throws OseeCoreException {
      return traceabilityProvider.getTestUnitArtifacts(artifact);
   }

   public Artifact getTestProcedureByName(String name) {
      return testProcedures.get(name);
   }

   public Artifact getTestScriptByName(String name) {
      return traceabilityProvider.getTestUnitByName(name);
   }

   public void setTestProcedureFilterFile(File filter) {
      this.testProcedureFilter = filter;
   }

   private File getTestProcedureFilterFile() {
      return testProcedureFilter;
   }

   private Set<String> getAllowedTestProcedures() {
      Set<String> toReturn = new HashSet<>();
      File filter = getTestProcedureFilterFile();
      if (filter != null && filter.exists() && filter.canRead()) {
         BufferedReader reader = null;
         try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(filter)));
            String line = "";
            while ((line = reader.readLine()) != null) {
               toReturn.add(line.trim());
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         } finally {
            if (reader != null) {
               try {
                  reader.close();
               } catch (IOException ex) {
                  OseeLog.log(Activator.class, Level.SEVERE, ex);
               }
            }
         }
      }
      return toReturn;
   }
}

Back to the top