Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4585cd0f0891b1abd26c110e2fcdaf16fdf0b844 (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
/*******************************************************************************
 * 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.framework.core.dsl.integration;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.data.BranchToken;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.dsl.integration.internal.Activator;
import org.eclipse.osee.framework.core.dsl.integration.util.OseeUtil;
import org.eclipse.osee.framework.core.dsl.oseeDsl.AddEnum;
import org.eclipse.osee.framework.core.dsl.oseeDsl.OseeDsl;
import org.eclipse.osee.framework.core.dsl.oseeDsl.OseeDslFactory;
import org.eclipse.osee.framework.core.dsl.oseeDsl.OverrideOption;
import org.eclipse.osee.framework.core.dsl.oseeDsl.RemoveEnum;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XArtifactType;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XAttributeType;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XAttributeTypeRef;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XOseeEnumEntry;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XOseeEnumOverride;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XOseeEnumType;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XRelationType;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.RelationTypeMultiplicity;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.model.OseeEnumEntry;
import org.eclipse.osee.framework.core.model.cache.AttributeTypeCache;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.core.model.type.OseeEnumType;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.core.services.IOseeModelFactoryService;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Ryan D. Brooks
 * @author Roberto E. Escobar
 */
public class XTextToOseeTypeOperation extends AbstractOperation {
   private final IOseeModelFactoryService provider;
   private final OseeDsl model;
   private final OseeTypeCache typeCache;
   private final BranchCache branchCache;

   public XTextToOseeTypeOperation(IOseeModelFactoryService provider, OseeTypeCache typeCache, BranchCache branchCache, OseeDsl model) {
      super("OSEE Text Model to OSEE", Activator.PLUGIN_ID);
      this.provider = provider;
      this.typeCache = typeCache;
      this.branchCache = branchCache;
      this.model = model;
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws Exception {
      double workAmount = 1.0;

      int workTotal = model.getArtifactTypes().size();
      workTotal += model.getAttributeTypes().size();
      workTotal += model.getRelationTypes().size();
      workTotal += model.getEnumTypes().size();
      workTotal += model.getEnumOverrides().size();

      if (workTotal > 0) {
         double workPercentage = workAmount / workTotal;

         for (XArtifactType xArtifactType : model.getArtifactTypes()) {
            translateXArtifactType(xArtifactType);
            monitor.worked(calculateWork(workPercentage));
         }

         for (XOseeEnumOverride xEnumOverride : model.getEnumOverrides()) {
            translateXEnumOverride(xEnumOverride);
            monitor.worked(calculateWork(workPercentage));
         }

         for (XOseeEnumType xEnumType : model.getEnumTypes()) {
            translateXEnumType(xEnumType);
            monitor.worked(calculateWork(workPercentage));
         }

         for (XAttributeType xAttributeType : model.getAttributeTypes()) {
            translateXAttributeType(xAttributeType);
            monitor.worked(calculateWork(workPercentage));
         }

         for (XArtifactType xArtifactType : model.getArtifactTypes()) {
            handleXArtifactTypeCrossRef(xArtifactType);
            monitor.worked(calculateWork(workPercentage));
         }

         for (XRelationType xRelationType : model.getRelationTypes()) {
            translateXRelationType(xRelationType);
            monitor.worked(calculateWork(workPercentage));
         }
      }
   }

   private void handleXArtifactTypeCrossRef(XArtifactType xArtifactType) throws OseeCoreException {
      ArtifactType targetArtifactType = typeCache.getArtifactTypeCache().getByGuid(xArtifactType.getTypeGuid());
      translateSuperTypes(targetArtifactType, xArtifactType);
      Map<IOseeBranch, Collection<AttributeType>> validAttributesPerBranch = getOseeAttributes(xArtifactType);
      targetArtifactType.setAllAttributeTypes(validAttributesPerBranch);
   }

   private void translateSuperTypes(ArtifactType targetArtifactType, XArtifactType xArtifactType) throws OseeCoreException {
      Set<ArtifactType> oseeSuperTypes = new HashSet<ArtifactType>();
      for (XArtifactType xSuperType : xArtifactType.getSuperArtifactTypes()) {
         String superTypeName = xSuperType.getName();
         ArtifactType oseeSuperType = typeCache.getArtifactTypeCache().getUniqueByName(superTypeName);
         oseeSuperTypes.add(oseeSuperType);
      }

      if (!oseeSuperTypes.isEmpty()) {
         targetArtifactType.setSuperTypes(oseeSuperTypes);
      }
   }

   private Map<IOseeBranch, Collection<AttributeType>> getOseeAttributes(XArtifactType xArtifactType) throws OseeCoreException {
      Map<IOseeBranch, Collection<AttributeType>> validAttributes =
         new HashMap<IOseeBranch, Collection<AttributeType>>();
      for (XAttributeTypeRef xAttributeTypeRef : xArtifactType.getValidAttributeTypes()) {
         XAttributeType xAttributeType = xAttributeTypeRef.getValidAttributeType();
         IOseeBranch branch = getAttributeBranch(xAttributeTypeRef);
         AttributeType oseeAttributeType = typeCache.getAttributeTypeCache().getByGuid(xAttributeType.getTypeGuid());
         if (oseeAttributeType != null) {
            Collection<AttributeType> listOfAllowedAttributes = validAttributes.get(branch);
            if (listOfAllowedAttributes == null) {
               listOfAllowedAttributes = new HashSet<AttributeType>();
               validAttributes.put(branch, listOfAllowedAttributes);
            }
            listOfAllowedAttributes.add(oseeAttributeType);
         } else {
            System.out.println(String.format("Type was null for \"%s\"", xArtifactType.getName()));
         }
      }
      return validAttributes;
   }

   private IOseeBranch getAttributeBranch(XAttributeTypeRef xAttributeTypeRef) throws OseeCoreException {
      String branchGuid = xAttributeTypeRef.getBranchGuid();
      if (branchGuid == null) {
         return CoreBranches.SYSTEM_ROOT;
      } else {
         IOseeBranch branch = branchCache.getByGuid(branchGuid);
         if (branch == null) {
            branch = new BranchToken(branchGuid, branchGuid);
         }
         return branch;
      }
   }

   private void translateXArtifactType(XArtifactType xArtifactType) throws OseeCoreException {
      String artifactTypeName = xArtifactType.getName();

      ArtifactType oseeArtifactType =
         provider.getArtifactTypeFactory().createOrUpdate(typeCache.getArtifactTypeCache(),
            xArtifactType.getTypeGuid(), xArtifactType.isAbstract(), artifactTypeName);
      xArtifactType.setTypeGuid(oseeArtifactType.getGuid());
   }

   private void translateXEnumType(XOseeEnumType xEnumType) throws OseeCoreException {
      String enumTypeName = xEnumType.getName();

      OseeEnumType oseeEnumType =
         provider.getOseeEnumTypeFactory().createOrUpdate(typeCache.getEnumTypeCache(), xEnumType.getTypeGuid(),
            enumTypeName);

      int lastOrdinal = 0;
      List<OseeEnumEntry> oseeEnumEntries = new ArrayList<OseeEnumEntry>();
      for (XOseeEnumEntry xEnumEntry : xEnumType.getEnumEntries()) {
         String entryName = xEnumEntry.getName();
         String ordinal = xEnumEntry.getOrdinal();
         if (Strings.isValid(ordinal)) {
            lastOrdinal = Integer.parseInt(ordinal);
         }

         String entryGuid = xEnumEntry.getEntryGuid();
         oseeEnumEntries.add(provider.getOseeEnumTypeFactory().createEnumEntry(entryGuid, entryName, lastOrdinal));
         lastOrdinal++;
      }
      oseeEnumType.setEntries(oseeEnumEntries);
   }

   private void translateXEnumOverride(XOseeEnumOverride xEnumOverride) throws OseeCoreException {
      XOseeEnumType xEnumType = xEnumOverride.getOverridenEnumType();
      if (!xEnumOverride.isInheritAll()) {
         xEnumType.getEnumEntries().clear();
      }
      OseeDslFactory factory = OseeDslFactory.eINSTANCE;
      for (OverrideOption xOverrideOption : xEnumOverride.getOverrideOptions()) {
         if (xOverrideOption instanceof AddEnum) {
            String entryName = ((AddEnum) xOverrideOption).getEnumEntry();
            String entryGuid = ((AddEnum) xOverrideOption).getEntryGuid();
            XOseeEnumEntry xEnumEntry = factory.createXOseeEnumEntry();
            xEnumEntry.setName(entryName);
            xEnumEntry.setEntryGuid(entryGuid);
            xEnumType.getEnumEntries().add(xEnumEntry);
         } else if (xOverrideOption instanceof RemoveEnum) {
            XOseeEnumEntry enumEntry = ((RemoveEnum) xOverrideOption).getEnumEntry();
            xEnumType.getEnumEntries().remove(enumEntry);
         } else {
            throw new OseeStateException("Unsupported Override Operation");
         }
      }
   }

   private void translateXAttributeType(XAttributeType xAttributeType) throws OseeCoreException {
      int min = Integer.parseInt(xAttributeType.getMin());
      int max = Integer.MAX_VALUE;
      if (!xAttributeType.getMax().equals("unlimited")) {
         max = Integer.parseInt(xAttributeType.getMax());
      }
      XOseeEnumType xEnumType = xAttributeType.getEnumType();
      OseeEnumType oseeEnumType = null;
      if (xEnumType != null) {
         oseeEnumType = typeCache.getEnumTypeCache().getByGuid(xEnumType.getTypeGuid());
      }

      AttributeTypeCache cache = typeCache.getAttributeTypeCache();
      AttributeType oseeAttributeType = provider.getAttributeTypeFactory().createOrUpdate(cache, //
         xAttributeType.getTypeGuid(), //
         xAttributeType.getName(), //
         getQualifiedTypeName(xAttributeType.getBaseAttributeType()), //
         getQualifiedTypeName(xAttributeType.getDataProvider()), //
         xAttributeType.getFileExtension(), //
         xAttributeType.getDefaultValue(), //
         oseeEnumType, //
         min, //
         max, //
         xAttributeType.getDescription(), //
         xAttributeType.getTaggerId()//
      );
      xAttributeType.setTypeGuid(oseeAttributeType.getGuid());
   }

   private String getQualifiedTypeName(String id) {
      String value = id;
      if (!value.contains(".")) {
         value = "org.eclipse.osee.framework.skynet.core." + id;
      }
      return value;
   }

   private void translateXRelationType(XRelationType xRelationType) throws OseeCoreException {
      RelationTypeMultiplicity multiplicity =
         RelationTypeMultiplicity.getFromString(xRelationType.getMultiplicity().name());

      String sideATypeName = xRelationType.getSideAArtifactType().getName();
      String sideBTypeName = xRelationType.getSideBArtifactType().getName();

      ArtifactType sideAType = typeCache.getArtifactTypeCache().getUniqueByName(sideATypeName);
      ArtifactType sideBType = typeCache.getArtifactTypeCache().getUniqueByName(sideBTypeName);

      RelationType oseeRelationType =
         provider.getRelationTypeFactory().createOrUpdate(typeCache.getRelationTypeCache(), //
            xRelationType.getTypeGuid(), //
            xRelationType.getName(), //
            xRelationType.getSideAName(), //
            xRelationType.getSideBName(), //
            sideAType, //
            sideBType, //
            multiplicity, //
            OseeUtil.orderTypeNameToGuid(xRelationType.getDefaultOrderType()) //
         );

      xRelationType.setTypeGuid(oseeRelationType.getGuid());
   }

}

Back to the top