Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2403a19b4a1e64be7ccdbc7c1b43e99c568ab7e5 (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
/*******************************************************************************
 * 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.relation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IRelationType;
import org.eclipse.osee.framework.core.enums.RelationSide;
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.util.Conditions;
import org.eclipse.osee.orcs.core.internal.util.MultiplicityState;
import org.eclipse.osee.orcs.data.RelationTypes;

/**
 * @author Roberto E. Escobar
 * @author Megumi Telles
 */
public class RelationTypeValidity {

   private final RelationTypes relationTypes;

   public RelationTypeValidity(RelationTypes relationTypes) {
      super();
      this.relationTypes = relationTypes;
   }

   public void checkRelationTypeMultiplicity(IRelationType type, RelationNode node, RelationSide side, int count) throws OseeCoreException {
      MultiplicityState state = getRelationMultiplicityState(type, side, count);
      switch (state) {
         case MAX_VIOLATION:
            throw new OseeStateException("Relation type [%s] on [%s] exceeds max occurrence rule on [%s]", type, side,
               node.getExceptionString());
         case MIN_VIOLATION:
            throw new OseeStateException("Relation type [%s] on [%s] is less than min occurrence rule on [%s]", type,
               side, node.getExceptionString());
         default:
            break;
      }
   }

   public void checkRelationTypeValid(IRelationType type, RelationNode node, RelationSide side) throws OseeCoreException {
      Conditions.checkNotNull(type, "type");
      Conditions.checkNotNull(node, "node");
      Conditions.checkNotNull(side, "relationSide");

      IArtifactType artifactType = node.getArtifactType();
      boolean isValid = isRelationTypeValid(type, artifactType, side);
      Conditions.checkExpressionFailOnTrue(
         !isValid,
         "Relation validity error for [%s] - ArtifactType [%s] does not belong on side [%s] of relation [%s] - only items of type [%s] are allowed",
         node.getExceptionString(), artifactType, side.name(), type, relationTypes.getArtifactType(type, side));
   }

   public int getMaximumRelationsAllowed(IRelationType type, IArtifactType artifactType, RelationSide side) throws OseeCoreException {
      Conditions.checkNotNull(type, "relationType");
      Conditions.checkNotNull(artifactType, "artifactType");
      Conditions.checkNotNull(side, "relationSide");
      checkTypeExists(type);

      int toReturn = 0;
      if (relationTypes.isArtifactTypeAllowed(type, side, artifactType)) {
         toReturn = relationTypes.getMultiplicity(type).getLimit(side);
      }
      return toReturn;
   }

   public MultiplicityState getRelationMultiplicityState(IRelationType type, RelationSide side, int count) throws OseeCoreException {      
      Conditions.checkNotNull(type, "type");
      Conditions.checkNotNull(side, "relationSide");
      checkTypeExists(type);

      RelationTypeMultiplicity multiplicity = relationTypes.getMultiplicity(type);

      MultiplicityState toReturn = MultiplicityState.IS_VALID;
      int limit = multiplicity.getLimit(side);
      if (count > limit) {
         toReturn = MultiplicityState.MAX_VIOLATION;
      }
      return toReturn;
   }

   public boolean isRelationTypeValid(IRelationType relationType, IArtifactType artifactType, RelationSide relationSide) throws OseeCoreException {
      checkTypeExists(relationType);
      Conditions.checkNotNull(artifactType, "artifactType");
      Conditions.checkNotNull(relationSide, "relationSide");
      return getRelationSideMax(relationType, artifactType, relationSide) > 0;
   }

   public List<IRelationType> getValidRelationTypes(IArtifactType artifactType) throws OseeCoreException {
      Conditions.checkNotNull(artifactType, "artifactType");
      Collection<? extends IRelationType> types = relationTypes.getAll();
      List<IRelationType> toReturn = new ArrayList<IRelationType>();
      for (IRelationType relationType : types) {
         if (isTypeAllowed(artifactType, relationType)) {
            toReturn.add(relationType);
         }
      }
      return toReturn;
   }

   private boolean isTypeAllowed(IArtifactType artifactType, IRelationType relationType) throws OseeCoreException {
      boolean result = false;
      for (RelationSide side : RelationSide.values()) {
         int sideMax = getRelationSideMax(relationType, artifactType, side);
         if (sideMax > 0) {
            result = true;
            break;
         }
      }
      return result;
   }

   private void checkTypeExists(IRelationType type) throws OseeCoreException {
      boolean exists = relationTypes.exists(type);
      Conditions.checkExpressionFailOnTrue(!exists, "relationType [%s] does not exist", type);
   }

   private int getRelationSideMax(IRelationType relationType, IArtifactType artifactType, RelationSide relationSide) throws OseeCoreException {
      int toReturn = 0;
      if (relationTypes.isArtifactTypeAllowed(relationType, relationSide, artifactType)) {
         toReturn = relationTypes.getMultiplicity(relationType).getLimit(relationSide);
      }
      return toReturn;
   }

}

Back to the top