Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8223f77763f4dbc6f539810fb199b8b5e9c36671 (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
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
/*******************************************************************************
 * Copyright (c) 2015 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.skynet.core.internal;

import com.google.common.io.InputSupplier;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.emf.common.util.EList;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.dsl.OseeDslResource;
import org.eclipse.osee.framework.core.dsl.OseeDslResourceUtil;
import org.eclipse.osee.framework.core.dsl.oseeDsl.AddAttribute;
import org.eclipse.osee.framework.core.dsl.oseeDsl.AddEnum;
import org.eclipse.osee.framework.core.dsl.oseeDsl.AttributeOverrideOption;
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.RemoveAttribute;
import org.eclipse.osee.framework.core.dsl.oseeDsl.RemoveEnum;
import org.eclipse.osee.framework.core.dsl.oseeDsl.UpdateAttribute;
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.XOseeArtifactTypeOverride;
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.dsl.oseeDsl.util.OseeDslSwitch;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.RelationOrderBaseTypes;
import org.eclipse.osee.framework.core.enums.RelationTypeMultiplicity;
import org.eclipse.osee.framework.core.model.IOseeStorable;
import org.eclipse.osee.framework.core.model.OseeEnumEntry;
import org.eclipse.osee.framework.core.model.cache.ArtifactTypeCache;
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.cache.IOseeCache;
import org.eclipse.osee.framework.core.model.cache.OseeEnumTypeCache;
import org.eclipse.osee.framework.core.model.cache.RelationTypeCache;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.ArtifactTypeFactory;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.core.model.type.AttributeTypeFactory;
import org.eclipse.osee.framework.core.model.type.OseeEnumType;
import org.eclipse.osee.framework.core.model.type.OseeEnumTypeFactory;
import org.eclipse.osee.framework.core.model.type.RelationTypeFactory;
import org.eclipse.osee.framework.core.services.IOseeCachingService;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.HexUtil;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.internal.ClientCachingServiceProxy.TypesLoader;

/**
 * @author Roberto E. Escobar
 */
public class DslToTypeLoader implements TypesLoader {

   private final ArtifactTypeFactory artTypeFactory = new ArtifactTypeFactory();
   private final AttributeTypeFactory attrTypeFactory = new AttributeTypeFactory();
   private final OseeEnumTypeFactory enumTypeFactory = new OseeEnumTypeFactory();
   private final RelationTypeFactory relTypeFactory = new RelationTypeFactory();

   private final BranchCache branchCache;

   public DslToTypeLoader(BranchCache branchCache) {
      super();
      this.branchCache = branchCache;
   }

   @Override
   public void loadTypes(IOseeCachingService caches, InputSupplier<? extends InputStream> supplier) {
      OseeDslResource loadModel;
      InputStream inputStream = null;
      try {
         inputStream = supplier.getInput();
         loadModel = OseeDslResourceUtil.loadModel("osee:/text.osee", inputStream);
      } catch (Exception ex) {
         throw OseeCoreException.wrap(ex);
      } finally {
         Lib.close(inputStream);
      }

      TypeBuffer buffer = new TypeBuffer();

      OseeDsl model = loadModel.getModel();
      if (model != null) {
         loadTypes(buffer, branchCache, model);
      }

      buffer.copyEnumTypes(caches.getEnumTypeCache());
      buffer.copyAttrTypes(caches.getAttributeTypeCache());
      buffer.copyArtTypes(caches.getArtifactTypeCache());
      buffer.copyRelTypes(caches.getRelationTypeCache());
   }

   private void loadTypes(TypeBuffer buffer, BranchCache branchCache, OseeDsl model) {
      for (XOseeArtifactTypeOverride xArtifactTypeOverride : model.getArtifactTypeOverrides()) {
         translateXArtifactTypeOverride(xArtifactTypeOverride);
      }

      for (XArtifactType xArtifactType : model.getArtifactTypes()) {
         translateXArtifactType(buffer, xArtifactType);
      }

      for (XOseeEnumOverride xEnumOverride : model.getEnumOverrides()) {
         translateXEnumOverride(xEnumOverride);
      }

      for (XOseeEnumType xEnumType : model.getEnumTypes()) {
         translateXEnumType(buffer, xEnumType);
      }

      for (XAttributeType xAttributeType : model.getAttributeTypes()) {
         translateXAttributeType(buffer, xAttributeType);
      }

      for (XArtifactType xArtifactType : model.getArtifactTypes()) {
         handleXArtifactTypeCrossRef(buffer, branchCache, xArtifactType);
      }

      for (XRelationType xRelationType : model.getRelationTypes()) {
         translateXRelationType(buffer, xRelationType);
      }
   }

   private void handleXArtifactTypeCrossRef(TypeBuffer buffer, BranchCache branchCache, XArtifactType xArtifactType) throws OseeCoreException {
      ArtifactType targetArtifactType = buffer.getArtTypes().getByGuid(HexUtil.toLong(xArtifactType.getUuid()));
      translateSuperTypes(buffer, targetArtifactType, xArtifactType);
      Map<BranchId, Collection<AttributeType>> validAttributesPerBranch =
         getOseeAttributes(buffer, branchCache, xArtifactType);
      targetArtifactType.setAllAttributeTypes(validAttributesPerBranch);
   }

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

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

   private Map<BranchId, Collection<AttributeType>> getOseeAttributes(TypeBuffer buffer, BranchCache branchCache, XArtifactType xArtifactType) throws OseeCoreException {
      Map<BranchId, Collection<AttributeType>> validAttributes =
         new HashMap<BranchId, Collection<AttributeType>>();
      for (XAttributeTypeRef xAttributeTypeRef : xArtifactType.getValidAttributeTypes()) {
         XAttributeType xAttributeType = xAttributeTypeRef.getValidAttributeType();
         BranchId branch = getAttributeBranch(branchCache, xAttributeTypeRef);
         Long attrUuid = HexUtil.toLong(xAttributeType.getUuid());
         AttributeType oseeAttributeType = buffer.getAttrTypes().getByGuid(attrUuid);
         if (oseeAttributeType != null) {
            Collection<AttributeType> listOfAllowedAttributes = validAttributes.get(branch);
            if (listOfAllowedAttributes == null) {
               listOfAllowedAttributes = new HashSet<>();
               validAttributes.put(branch, listOfAllowedAttributes);
            }
            listOfAllowedAttributes.add(oseeAttributeType);
         } else {
            OseeLog.logf(Activator.class, Level.WARNING, "Type was null for \"%s\"", xArtifactType.getName());
         }
      }
      return validAttributes;
   }

   private BranchId getAttributeBranch(BranchCache branchCache, XAttributeTypeRef xAttributeTypeRef) throws OseeCoreException {
      String branchIdStr = xAttributeTypeRef.getBranchUuid();
      if (branchIdStr == null) {
         return CoreBranches.SYSTEM_ROOT;
      } else {
         return TokenFactory.createBranch(Long.valueOf(branchIdStr));
      }
   }

   private void translateXArtifactTypeOverride(XOseeArtifactTypeOverride xArtTypeOverride) {
      XArtifactType xArtifactType = xArtTypeOverride.getOverridenArtifactType();
      final EList<XAttributeTypeRef> validAttributeTypes = xArtifactType.getValidAttributeTypes();
      if (!xArtTypeOverride.isInheritAll()) {
         validAttributeTypes.clear();
      }

      OseeDslSwitch<Void> overrideVisitor = new OseeDslSwitch<Void>() {

         @Override
         public Void caseAddAttribute(AddAttribute addOption) {
            XAttributeTypeRef attributeRef = addOption.getAttribute();
            validAttributeTypes.add(attributeRef);
            return super.caseAddAttribute(addOption);
         }

         @Override
         public Void caseRemoveAttribute(RemoveAttribute removeOption) {
            XAttributeType attribute = removeOption.getAttribute();
            String guidToMatch = attribute.getUuid();
            List<XAttributeTypeRef> toRemove = new LinkedList<>();
            for (XAttributeTypeRef xAttributeTypeRef : validAttributeTypes) {
               String itemGuid = xAttributeTypeRef.getValidAttributeType().getUuid();
               if (guidToMatch.equals(itemGuid)) {
                  toRemove.add(xAttributeTypeRef);
               }
            }
            validAttributeTypes.removeAll(toRemove);
            return super.caseRemoveAttribute(removeOption);
         }

         @Override
         public Void caseUpdateAttribute(UpdateAttribute updateAttribute) {
            XAttributeTypeRef refToUpdate = updateAttribute.getAttribute();
            String guidToMatch = refToUpdate.getValidAttributeType().getUuid();
            List<XAttributeTypeRef> toRemove = new LinkedList<>();
            for (XAttributeTypeRef xAttributeTypeRef : validAttributeTypes) {
               String itemGuid = xAttributeTypeRef.getValidAttributeType().getUuid();
               if (guidToMatch.equals(itemGuid)) {
                  toRemove.add(xAttributeTypeRef);
               }
            }
            validAttributeTypes.removeAll(toRemove);
            validAttributeTypes.add(refToUpdate);
            return super.caseUpdateAttribute(updateAttribute);
         }

      };

      for (AttributeOverrideOption xOverrideOption : xArtTypeOverride.getOverrideOptions()) {
         overrideVisitor.doSwitch(xOverrideOption);
      }
   }

   private void translateXArtifactType(TypeBuffer buffer, XArtifactType xArtifactType) throws OseeCoreException {
      String artifactTypeName = xArtifactType.getName();
      Long artUuid = HexUtil.toLong(xArtifactType.getUuid());
      artTypeFactory.createOrUpdate(buffer.getArtTypes(), artUuid, xArtifactType.isAbstract(), artifactTypeName);
   }

   private void translateXEnumType(TypeBuffer buffer, XOseeEnumType xEnumType) throws OseeCoreException {
      String enumTypeName = xEnumType.getName();
      Long enumUuid = HexUtil.toLong(xEnumType.getUuid());
      OseeEnumType oseeEnumType = enumTypeFactory.createOrUpdate(buffer.getEnumTypes(), enumUuid, enumTypeName);

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

   private void translateXEnumOverride(XOseeEnumOverride xEnumOverride) {
      XOseeEnumType xEnumType = xEnumOverride.getOverridenEnumType();
      final EList<XOseeEnumEntry> enumEntries = xEnumType.getEnumEntries();
      if (!xEnumOverride.isInheritAll()) {
         enumEntries.clear();
      }

      OseeDslSwitch<Void> overrideVisitor = new OseeDslSwitch<Void>() {

         @Override
         public Void caseAddEnum(AddEnum addEnum) {
            String entryName = addEnum.getEnumEntry();
            String entryGuid = addEnum.getEntryGuid();
            String description = addEnum.getDescription();
            XOseeEnumEntry xEnumEntry = OseeDslFactory.eINSTANCE.createXOseeEnumEntry();
            xEnumEntry.setName(entryName);
            xEnumEntry.setEntryGuid(entryGuid);
            xEnumEntry.setDescription(description);
            enumEntries.add(xEnumEntry);
            return super.caseAddEnum(addEnum);
         }

         @Override
         public Void caseRemoveEnum(RemoveEnum removeEnum) {
            XOseeEnumEntry enumEntry = removeEnum.getEnumEntry();
            String guidToMatch = enumEntry.getEntryGuid();
            List<XOseeEnumEntry> toRemove = new LinkedList<>();
            for (XOseeEnumEntry item : enumEntries) {
               String itemGuid = item.getEntryGuid();
               if (guidToMatch.equals(itemGuid)) {
                  toRemove.add(item);
               }
            }
            enumEntries.removeAll(toRemove);
            return super.caseRemoveEnum(removeEnum);
         }

      };

      for (OverrideOption xOverrideOption : xEnumOverride.getOverrideOptions()) {
         overrideVisitor.doSwitch(xOverrideOption);
      }
   }

   private void translateXAttributeType(TypeBuffer buffer, 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) {
         Long enumUuid = HexUtil.toLong(xEnumType.getUuid());
         oseeEnumType = buffer.getEnumTypes().getByGuid(enumUuid);
      }

      Long attrUuid = HexUtil.toLong(xAttributeType.getUuid());
      attrTypeFactory.createOrUpdate(buffer.getAttrTypes(), //
         attrUuid, //
         xAttributeType.getName(), //
         getQualifiedTypeName(xAttributeType.getBaseAttributeType()), //
         getQualifiedTypeName(xAttributeType.getDataProvider()), //
         xAttributeType.getFileExtension(), //
         xAttributeType.getDefaultValue(), //
         oseeEnumType, //
         min, //
         max, //
         xAttributeType.getDescription(), //
         xAttributeType.getTaggerId(), //
         xAttributeType.getMediaType());
   }

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

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

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

      ArtifactType sideAType = buffer.getArtTypes().getUniqueByName(sideATypeName);
      ArtifactType sideBType = buffer.getArtTypes().getUniqueByName(sideBTypeName);

      Long relUuid = HexUtil.toLong(xRelationType.getUuid());
      relTypeFactory.createOrUpdate(buffer.getRelTypes(), //
         relUuid, //
         xRelationType.getName(), //
         xRelationType.getSideAName(), //
         xRelationType.getSideBName(), //
         sideAType, //
         sideBType, //
         multiplicity, //
         orderTypeNameToGuid(xRelationType.getDefaultOrderType()) //
      );
   }

   private String orderTypeNameToGuid(String orderTypeName) throws OseeCoreException {
      Conditions.checkNotNull(orderTypeName, "orderTypeName");
      return RelationOrderBaseTypes.getFromOrderTypeName(orderTypeName.replaceAll("_", " ")).getGuid();
   }

   private static final class TypeBuffer {
      private final ArtifactTypeCache artTypes = new ArtifactTypeCache();
      private final OseeEnumTypeCache enumTypes = new OseeEnumTypeCache();
      private final AttributeTypeCache attrTypes = new AttributeTypeCache();
      private final RelationTypeCache relTypes = new RelationTypeCache();

      public ArtifactTypeCache getArtTypes() {
         return artTypes;
      }

      public OseeEnumTypeCache getEnumTypes() {
         return enumTypes;
      }

      public AttributeTypeCache getAttrTypes() {
         return attrTypes;
      }

      public RelationTypeCache getRelTypes() {
         return relTypes;
      }

      public void copyArtTypes(ArtifactTypeCache dest) {
         copy(artTypes, dest);
      }

      public void copyEnumTypes(OseeEnumTypeCache dest) {
         copy(enumTypes, dest);
      }

      public void copyAttrTypes(AttributeTypeCache dest) {
         copy(attrTypes, dest);
      }

      public void copyRelTypes(RelationTypeCache dest) {
         copy(relTypes, dest);
      }

      private <T extends IOseeStorable> void copy(IOseeCache<T> src, IOseeCache<T> dest) {
         synchronized (dest) {
            dest.decacheAll();
            for (T type : src.getAll()) {
               type.clearDirty();
               dest.cache(type);
            }
         }
      }

   }
}

Back to the top