2525import org .jooq .lambda .tuple .Tuple2 ;
2626
2727import java .util .Collection ;
28- import java .util .Comparator ;
2928import java .util .List ;
3029import java .util .Map ;
3130import java .util .Optional ;
3635import static org .finos .waltz .common .Checks .checkNotNull ;
3736import static org .finos .waltz .common .CollectionUtilities .head ;
3837import static org .finos .waltz .common .CollectionUtilities .sort ;
38+ import static org .finos .waltz .common .ListUtilities .asList ;
3939import static org .finos .waltz .common .MapUtilities .groupAndThen ;
4040import static org .finos .waltz .common .MapUtilities .isEmpty ;
4141import static org .finos .waltz .service .flow_classification_rule .FlowClassificationRuleUtilities .flowClassificationRuleVantagePointComparator ;
@@ -76,6 +76,22 @@ public FlowClassificationRuleResolver(FlowDirection direction, List<FlowClassifi
7676 }
7777
7878
79+ /**
80+ * Given a collection of vantages points (maybe) return the first
81+ * after sorting them in (descending) rank order.
82+ *
83+ * @param vantagePoints
84+ * @return
85+ */
86+ public static Optional <FlowClassificationRuleVantagePoint > getMostSpecificRanked (Collection <FlowClassificationRuleVantagePoint > vantagePoints ) {
87+ List <FlowClassificationRuleVantagePoint > sorted = sort (
88+ vantagePoints ,
89+ flowClassificationRuleVantagePointComparator );
90+ return head (
91+ sorted ); //note the reversal of parameters because we want descending order
92+ }
93+
94+
7995 /**
8096 * Given a vantage point, a supplier and a data type this method will give back
8197 * an authoritativeness rating.
@@ -102,21 +118,41 @@ public Tuple2<AuthoritativenessRatingValue, Optional<FlowClassificationRuleVanta
102118 return tuple (AuthoritativenessRatingValue .NO_OPINION , Optional .empty ());
103119 }
104120
121+ Map <EntityReference , Optional <FlowClassificationRuleVantagePoint >> allDataTypeEntityRule = vpEntity .getOrDefault (null , emptyMap ());
105122 Map <EntityReference , Optional <FlowClassificationRuleVantagePoint >> dtEntity = vpEntity .getOrDefault (dataTypeId , emptyMap ());
123+ Map <EntityReference , Optional <FlowClassificationRuleVantagePoint >> allDataTypeGroupRule = vpGroup .getOrDefault (null , emptyMap ());
106124 Map <EntityReference , Optional <FlowClassificationRuleVantagePoint >> dataTypeGroup = vpGroup .getOrDefault (dataTypeId , emptyMap ());
107125
108- if (isEmpty (dataTypeGroup ) && isEmpty (dtEntity )) {
126+ if (isEmpty (dataTypeGroup ) && isEmpty (dtEntity ) && isEmpty ( allDataTypeEntityRule ) && isEmpty ( allDataTypeGroupRule ) ) {
109127 return tuple (AuthoritativenessRatingValue .NO_OPINION , Optional .empty ());
110128 }
111129
112- Optional <FlowClassificationRuleVantagePoint > maybeRule = isEmpty (dtEntity )
113- ? dataTypeGroup .getOrDefault (
114- subject ,
115- Optional .empty ())
116- : dtEntity .getOrDefault (
117- subject ,
130+ List <Map <EntityReference , Optional <FlowClassificationRuleVantagePoint >>> dataTypeCoverage = asList (dtEntity , dataTypeGroup , allDataTypeEntityRule , allDataTypeGroupRule );
131+
132+ Optional <FlowClassificationRuleVantagePoint > pointToPointRule = dtEntity .getOrDefault (
133+ subject ,
134+ Optional .empty ());
135+
136+ Optional <FlowClassificationRuleVantagePoint > vantagePointRule = dataTypeGroup .getOrDefault (
137+ subject ,
138+ Optional .empty ());
139+
140+ Optional <FlowClassificationRuleVantagePoint > allDtToPointRule = allDataTypeEntityRule .getOrDefault (
141+ subject ,
142+ Optional .empty ());
143+
144+ Optional <FlowClassificationRuleVantagePoint > alLDtToGroupRule = allDataTypeGroupRule .getOrDefault (
145+ subject ,
118146 Optional .empty ());
119147
148+ List <Optional <FlowClassificationRuleVantagePoint >> rules = asList (pointToPointRule , vantagePointRule , allDtToPointRule , alLDtToGroupRule );
149+
150+ Optional <FlowClassificationRuleVantagePoint > maybeRule = rules
151+ .stream ()
152+ .filter (Optional ::isPresent )
153+ .map (Optional ::get )
154+ .findFirst ();
155+
120156 AuthoritativenessRatingValue defaultRating = direction == FlowDirection .OUTBOUND
121157 ? AuthoritativenessRatingValue .DISCOURAGED // at least one rule covering this scope and data type to reach this point
122158 : AuthoritativenessRatingValue .NO_OPINION ;
@@ -125,35 +161,20 @@ public Tuple2<AuthoritativenessRatingValue, Optional<FlowClassificationRuleVanta
125161 .map (r -> AuthoritativenessRatingValue .of (r .classificationCode ()))
126162 .orElse (defaultRating );
127163
128- Optional <FlowClassificationRuleVantagePoint > discouragedRule = determineDefaultRuleId (dataTypeGroup );
164+ Optional <FlowClassificationRuleVantagePoint > discouragedRule = determineDefaultRuleId (dataTypeCoverage );
129165
130166 return tuple (ratingValue , ofNullable (maybeRule .orElse (discouragedRule .orElse (null ))));
131167 }
132168
133- private Optional <FlowClassificationRuleVantagePoint > determineDefaultRuleId (Map <EntityReference , Optional <FlowClassificationRuleVantagePoint >> dataTypeGroup ) {
134- return dataTypeGroup
135- .values ()
169+ private Optional <FlowClassificationRuleVantagePoint > determineDefaultRuleId (List <Map <EntityReference , Optional <FlowClassificationRuleVantagePoint >>> dtCoverageRules ) {
170+ return dtCoverageRules
136171 .stream ()
172+ .flatMap (d -> d .values ().stream ())
137173 .filter (Optional ::isPresent )
138174 .map (Optional ::get )
139175 .sorted (flowClassificationRuleVantagePointComparator )
140176 .findFirst ();
141177 }
142178
143179
144- /**
145- * Given a collection of vantages points (maybe) return the first
146- * after sorting them in (descending) rank order.
147- *
148- * @param vantagePoints
149- * @return
150- */
151- public static Optional <FlowClassificationRuleVantagePoint > getMostSpecificRanked (Collection <FlowClassificationRuleVantagePoint > vantagePoints ) {
152- List <FlowClassificationRuleVantagePoint > sorted = sort (
153- vantagePoints ,
154- flowClassificationRuleVantagePointComparator );
155- return head (
156- sorted ); //note the reversal of parameters because we want descending order
157- }
158-
159180}
0 commit comments