Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0751beeb2143c4de0454c2ad5f9a49b5315f636a (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
/*******************************************************************************
 * Copyright (c) 2013 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.orcs.core.internal.types.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XArtifactType;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.orcs.core.internal.types.BranchHierarchyProvider;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * @author Roberto E. Escobar
 */
public class ArtifactTypeIndex extends TokenTypeIndex<Long, IArtifactType, XArtifactType> {

   private final Map<IArtifactType, ArtifactTypeMetaData> tokenToTypeData;
   private final BranchHierarchyProvider hierarchyProvider;

   public ArtifactTypeIndex(BranchHierarchyProvider hierarchyProvider) {
      super();
      this.tokenToTypeData = Maps.newHashMap();
      this.hierarchyProvider = hierarchyProvider;
   }

   public void put(IArtifactType type, Set<IArtifactType> superTypes) {
      ArtifactTypeMetaData metaData = getOrCreateData(type);
      metaData.setSuperTypes(superTypes);
   }

   public void put(IArtifactType type, Map<IOseeBranch, Collection<IAttributeType>> attributes) {
      ArtifactTypeMetaData metaData = getOrCreateData(type);
      metaData.setAttributeTypes(attributes);
   }

   private ArtifactTypeMetaData getOrCreateData(IArtifactType type) {
      ArtifactTypeMetaData metaData = tokenToTypeData.get(type);
      if (metaData == null) {
         metaData = new ArtifactTypeMetaData(type);
         tokenToTypeData.put(type, metaData);
      }
      return metaData;
   }

   public Collection<IArtifactType> getSuperTypes(IArtifactType artifactType) {
      ArtifactTypeMetaData metaData = tokenToTypeData.get(artifactType);
      return metaData != null ? metaData.getSuperTypes() : Collections.<IArtifactType> emptyList();
   }

   public Collection<IArtifactType> getDescendantTypes(IArtifactType artifactType) {
      ArtifactTypeMetaData metaData = tokenToTypeData.get(artifactType);
      return metaData != null ? metaData.getDescendantTypes() : Collections.<IArtifactType> emptyList();
   }

   public boolean hasSuperArtifactTypes(IArtifactType artType) {
      return !getSuperTypes(artType).isEmpty();
   }

   public boolean inheritsFrom(IArtifactType thisType, IArtifactType... otherTypes) {
      boolean result = false;
      for (IArtifactType otherType : otherTypes) {
         if (inheritsFromSingle(thisType, otherType)) {
            result = true;
            break;
         }
      }
      return result;
   }

   private boolean inheritsFromSingle(IArtifactType thisType, IArtifactType otherType) {
      boolean result = false;
      if (thisType.equals(otherType)) {
         result = true;
      } else {
         for (IArtifactType superType : getSuperTypes(thisType)) {
            if (inheritsFrom(superType, otherType)) {
               result = true;
               break;
            }
         }
      }
      return result;
   }

   public Collection<IAttributeType> getAttributeTypes(IArtifactType artType, IOseeBranch branch) throws OseeCoreException {
      Set<IAttributeType> attributeTypes = Sets.newLinkedHashSet();
      getAttributeTypes(attributeTypes, artType, branch);
      return attributeTypes;
   }

   private void getAttributeTypes(Set<IAttributeType> attributeTypes, IArtifactType artifactType, IOseeBranch branch) throws OseeCoreException {
      ArtifactTypeMetaData metaData = tokenToTypeData.get(artifactType);
      if (metaData != null) {
         Map<IOseeBranch, Collection<IAttributeType>> validityMap = metaData.getAttributeTypes();

         Iterable<? extends IOseeBranch> branches = hierarchyProvider.getParentHierarchy(branch);
         for (IOseeBranch parent : branches) {
            Collection<IAttributeType> items = validityMap.get(parent);
            if (items != null) {
               attributeTypes.addAll(items);
            }
         }
      }
      for (IArtifactType superType : getSuperTypes(artifactType)) {
         getAttributeTypes(attributeTypes, superType, branch);
      }
   }

   private final class ArtifactTypeMetaData {
      private final IArtifactType type;
      private Set<IArtifactType> superTypes;
      private final Set<IArtifactType> descendantTypes;
      private Map<IOseeBranch, Collection<IAttributeType>> attributeTypes;

      public ArtifactTypeMetaData(IArtifactType type) {
         super();
         this.type = type;
         superTypes = Collections.emptySet();
         descendantTypes = Sets.newLinkedHashSet();
         attributeTypes = Collections.emptyMap();
      }

      public void setSuperTypes(Set<IArtifactType> newSuperTypes) {
         Set<IArtifactType> originals = Sets.newHashSet(superTypes);
         superTypes = Sets.newHashSet(newSuperTypes);
         for (IArtifactType superType : superTypes) {
            ArtifactTypeMetaData metaData = tokenToTypeData.get(superType);
            if (metaData != null) {
               metaData.getDescendantTypes().add(type);
            }
         }
         for (IArtifactType oldValue : originals) {
            ArtifactTypeMetaData metaData = tokenToTypeData.get(oldValue);
            if (metaData != null) {
               metaData.getDescendantTypes().remove(type);
            }
         }
      }

      public void setAttributeTypes(Map<IOseeBranch, Collection<IAttributeType>> attributes) {
         this.attributeTypes = attributes;
      }

      public Set<IArtifactType> getSuperTypes() {
         return superTypes;
      }

      public Set<IArtifactType> getDescendantTypes() {
         return descendantTypes;
      }

      public Map<IOseeBranch, Collection<IAttributeType>> getAttributeTypes() {
         return attributeTypes;
      }

   }
}

Back to the top