66 */
77package com .powsybl .openrao .data .crac .io .commons .ucte ;
88
9+ import com .powsybl .openrao .commons .OpenRaoException ;
910import com .powsybl .openrao .data .crac .io .commons .ConnectableType ;
1011import com .google .common .collect .Ordering ;
1112import com .google .common .collect .TreeMultimap ;
@@ -39,7 +40,7 @@ class UcteConnectableCollection {
3940 addHvdcs (network );
4041 }
4142
42- UcteMatchingResult lookForConnectable (String fromNodeId , String toNodeId , String suffix , UcteNetworkAnalyzerProperties . BusIdMatchPolicy policy , ConnectableType ... connectableTypes ) {
43+ UcteMatchingResult lookForConnectable (String fromNodeId , String toNodeId , String suffix , UcteNetworkAnalyzerProperties ucteNetworkAnalyzerProperties , ConnectableType ... connectableTypes ) {
4344
4445 /*
4546 priority is given to the search with the from/to direction given in argument
@@ -55,17 +56,17 @@ UcteMatchingResult lookForConnectable(String fromNodeId, String toNodeId, String
5556 method returns the connectable with the id in the same order as the ones given in argument of the method.
5657 */
5758
58- UcteMatchingResult ucteMatchingResult = lookForMatch (fromNodeId , toNodeId , suffix , connectableTypes );
59+ UcteMatchingResult ucteMatchingResult = lookForMatch (fromNodeId , toNodeId , suffix , ucteNetworkAnalyzerProperties , connectableTypes );
5960
6061 if (!ucteMatchingResult .getStatus ().equals (UcteMatchingResult .MatchStatus .NOT_FOUND )) {
6162 return ucteMatchingResult ;
6263 }
6364
6465 // if no result has been found in the direction in argument, look for an inverted one
65- ucteMatchingResult = lookForMatch (toNodeId , fromNodeId , suffix , connectableTypes );
66+ ucteMatchingResult = lookForMatch (toNodeId , fromNodeId , suffix , ucteNetworkAnalyzerProperties , connectableTypes );
6667
6768 if (!ucteMatchingResult .getStatus ().equals (UcteMatchingResult .MatchStatus .NOT_FOUND )
68- || !policy .equals (UcteNetworkAnalyzerProperties .BusIdMatchPolicy .REPLACE_8TH_CHARACTER_WITH_WILDCARD )) {
69+ || !ucteNetworkAnalyzerProperties . getBusIdMatchPolicy () .equals (UcteNetworkAnalyzerProperties .BusIdMatchPolicy .REPLACE_8TH_CHARACTER_WITH_WILDCARD )) {
6970 return ucteMatchingResult .invert ();
7071 }
7172
@@ -75,24 +76,25 @@ UcteMatchingResult lookForConnectable(String fromNodeId, String toNodeId, String
7576 String toWildcard = String .format ("%1$-7s" , toNodeId ).substring (0 , 7 ) + UcteUtils .WILDCARD_CHARACTER ;
7677
7778 // with the direction in argument ...
78- ucteMatchingResult = lookForMatch (fromWildcard , toWildcard , suffix , connectableTypes );
79+ ucteMatchingResult = lookForMatch (fromWildcard , toWildcard , suffix , ucteNetworkAnalyzerProperties , connectableTypes );
7980
8081 if (!ucteMatchingResult .getStatus ().equals (UcteMatchingResult .MatchStatus .NOT_FOUND )) {
8182 return ucteMatchingResult ;
8283 }
8384
8485 // or, if not found, with the inverted direction
85- return lookForMatch (toWildcard , fromWildcard , suffix , connectableTypes ).invert ();
86+ return lookForMatch (toWildcard , fromWildcard , suffix , ucteNetworkAnalyzerProperties , connectableTypes ).invert ();
8687
8788 }
8889
89- private UcteMatchingResult lookForMatch (String fromNodeId , String toNodeId , String suffix , ConnectableType ... types ) {
90+ private UcteMatchingResult lookForMatch (String fromNodeId , String toNodeId , String suffix ,
91+ UcteNetworkAnalyzerProperties ucteNetworkAnalyzerProperties , ConnectableType ... types ) {
9092
9193 if (!fromNodeId .endsWith (UcteUtils .WILDCARD_CHARACTER )) {
9294
9395 // if the from node contains no wildcard, directly look for the entry of the TreeMultimap with the fromNode id
9496 Collection <UcteConnectable > ucteElements = connectables .asMap ().getOrDefault (fromNodeId , Collections .emptyList ());
95- return lookForMatchWithinCollection (fromNodeId , toNodeId , suffix , ucteElements , types );
97+ return lookForMatchWithinCollection (fromNodeId , toNodeId , suffix , ucteElements , ucteNetworkAnalyzerProperties , types );
9698
9799 } else {
98100
@@ -106,37 +108,71 @@ private UcteMatchingResult lookForMatch(String fromNodeId, String toNodeId, Stri
106108 .flatMap (Collection ::stream )
107109 .toList ();
108110
109- return lookForMatchWithinCollection (fromNodeId , toNodeId , suffix , ucteElements , types );
111+ return lookForMatchWithinCollection (fromNodeId , toNodeId , suffix , ucteElements , ucteNetworkAnalyzerProperties , types );
110112 }
111113 }
112114
113- private UcteMatchingResult lookForMatchWithinCollection (String fromNodeId , String toNodeId , String suffix , Collection <UcteConnectable > ucteConnectables , ConnectableType ... connectableTypes ) {
115+ private UcteMatchingResult lookForMatchWithinCollection (String fromNodeId , String toNodeId , String suffix ,
116+ Collection <UcteConnectable > ucteConnectables ,
117+ UcteNetworkAnalyzerProperties ucteNetworkAnalyzerProperties ,
118+ ConnectableType ... connectableTypes ) {
114119
115- if (fromNodeId . endsWith ( UcteUtils . WILDCARD_CHARACTER ) || toNodeId . endsWith ( UcteUtils . WILDCARD_CHARACTER )) {
116- // if the nodes contains wildCards, we have to look for all possible match
120+ List < UcteMatchingResult > matchedConnectables = getUcteMatchingResultsWithPriority (fromNodeId , toNodeId , suffix ,
121+ ucteConnectables , ucteNetworkAnalyzerProperties . getSuffixMatchPriority (), connectableTypes );
117122
118- List <UcteMatchingResult > matchedConnectables = ucteConnectables .stream ()
119- .filter (ucteConnectable -> ucteConnectable .doesMatch (fromNodeId , toNodeId , suffix , connectableTypes ))
120- .map (ucteConnectable -> ucteConnectable .getUcteMatchingResult (fromNodeId , toNodeId , suffix , connectableTypes ))
121- .toList ();
122-
123- if (matchedConnectables .size () == 1 ) {
124- return matchedConnectables .get (0 );
125- } else if (matchedConnectables .size () == 2 ) {
126- return UcteMatchingResult .severalPossibleMatch ();
127- } else if (matchedConnectables .size () > 2 ) {
128- return UcteMatchingResult .severalPossibleMatch ();
129- } else {
130- return UcteMatchingResult .notFound ();
131- }
123+ if (matchedConnectables .size () == 1 ) {
124+ return matchedConnectables .get (0 );
125+ } else if (matchedConnectables .size () == 2 ) {
126+ return UcteMatchingResult .severalPossibleMatch ();
127+ } else if (matchedConnectables .size () > 2 ) {
128+ return UcteMatchingResult .severalPossibleMatch ();
132129 } else {
130+ return UcteMatchingResult .notFound ();
131+ }
132+ }
133133
134- // if the nodes contains no wildCards, speed up the search by using findAny() instead of looking for all possible matches
135- return ucteConnectables .stream ()
136- .filter (ucteConnectable -> ucteConnectable .doesMatch (fromNodeId , toNodeId , suffix , connectableTypes ))
137- .map (ucteConnectable -> ucteConnectable .getUcteMatchingResult (fromNodeId , toNodeId , suffix , connectableTypes ))
138- .findAny ().orElse (UcteMatchingResult .notFound ());
134+ private static List <UcteMatchingResult > getUcteMatchingResultsWithPriority (String fromNodeId , String toNodeId , String suffix ,
135+ Collection <UcteConnectable > ucteConnectables ,
136+ UcteNetworkAnalyzerProperties .SuffixMatchPriority suffixMatchPriority ,
137+ ConnectableType [] connectableTypes ) {
138+ List <UcteMatchingResult > matchedConnectables ;
139+
140+ switch (suffixMatchPriority ) {
141+ case ALL :
142+ matchedConnectables = ucteConnectables .stream ()
143+ .filter (ucteConnectable -> ucteConnectable .doesMatchWithElementName (fromNodeId , toNodeId , suffix , connectableTypes )
144+ || ucteConnectable .doesMatchWithOrderCode (fromNodeId , toNodeId , suffix , connectableTypes ))
145+ .map (UcteConnectable ::getUcteMatchingResult )
146+ .toList ();
147+ break ;
148+ case ORDERCODE_BEFORE_NAME :
149+ matchedConnectables = ucteConnectables .stream ()
150+ .filter (ucteConnectable -> ucteConnectable .doesMatchWithOrderCode (fromNodeId , toNodeId , suffix , connectableTypes ))
151+ .map (UcteConnectable ::getUcteMatchingResult )
152+ .toList ();
153+ if (matchedConnectables .isEmpty ()) {
154+ matchedConnectables = ucteConnectables .stream ()
155+ .filter (ucteConnectable -> ucteConnectable .doesMatchWithElementName (fromNodeId , toNodeId , suffix , connectableTypes ))
156+ .map (UcteConnectable ::getUcteMatchingResult )
157+ .toList ();
158+ }
159+ break ;
160+ case NAME_BEFORE_ORDERCODE :
161+ matchedConnectables = ucteConnectables .stream ()
162+ .filter (ucteConnectable -> ucteConnectable .doesMatchWithElementName (fromNodeId , toNodeId , suffix , connectableTypes ))
163+ .map (UcteConnectable ::getUcteMatchingResult )
164+ .toList ();
165+ if (matchedConnectables .isEmpty ()) {
166+ matchedConnectables = ucteConnectables .stream ()
167+ .filter (ucteConnectable -> ucteConnectable .doesMatchWithOrderCode (fromNodeId , toNodeId , suffix , connectableTypes ))
168+ .map (UcteConnectable ::getUcteMatchingResult )
169+ .toList ();
170+ }
171+ break ;
172+ default :
173+ throw new OpenRaoException (String .format ("SuffixMatchPriority %s is not handled" , suffixMatchPriority ));
139174 }
175+ return matchedConnectables ;
140176 }
141177
142178 private void addBranches (Network network ) {
0 commit comments