WRApplication
External kratos "application" for multiscale time integration.
PatternUtility.hpp
Go to the documentation of this file.
1 
3 #pragma once
4 
5 // --- WRApp Includes ---
7 
8 // --- Core Includes ---
9 #include "includes/kratos_export_api.h"
10 #include "includes/model_part.h"
11 
12 // --- STL Includes ---
13 #include <regex>
14 #include <vector>
15 #include <map>
16 #include <filesystem>
17 #include <optional>
18 
19 
20 namespace Kratos {
21 
22 
27 
28 
36 {
37 public:
40 
52  static std::pair<std::string,std::regex> Integer();
53 
64  static std::pair<std::string,std::regex> UnsignedInteger();
65 
81  static std::pair<std::string,std::regex> FloatingPoint();
82 
84 
85 private:
88 
90  RegexUtility() = delete;
91 
93  RegexUtility(RegexUtility&& rOther) = delete;
94 
96  RegexUtility(const RegexUtility& rOther) = delete;
97 
99 }; // class RegexUtility
100 
101 
123 class KRATOS_API(WR_APPLICATION) PlaceholderPattern
124 {
125 public:
128 
129  using PlaceholderMap = std::map<std::string,std::string>;
130 
131  using PlaceholderGroupMap = std::map<std::string,std::optional<std::vector<std::size_t>>>;
132 
133  using MatchType = std::map<std::string,std::vector<std::string>>;
134 
135  using PathType = std::filesystem::path;
136 
138 
142 
143  PlaceholderPattern() = default;
144 
154  Ref<const PlaceholderMap> rPlaceholderMap);
155 
157 
158  PlaceholderPattern(const PlaceholderPattern& rOther) = default;
159 
160  virtual ~PlaceholderPattern() = default;
161 
165 
167 
169 
173 
175  bool IsAMatch(Ref<const std::string> rString) const;
176 
187 
195  std::string Apply(Ref<const PlaceholderMap> rPlaceholderValueMap) const;
196 
202  template <class TOutputIterator>
203  void Glob(TOutputIterator it) const;
204 
208 
210  bool IsConst() const;
211 
214 
217 
220 
223 
225 
226 private:
229 
230  std::string mPattern;
231 
232  // Placeholders and their assigned group indices in the pattern
233  PlaceholderGroupMap mPlaceholderGroupMap;
234 
235  std::string mRegexString;
236 
237  std::regex mRegex;
238 
242 
244  static std::string FormatRegexLiteral(const std::string& rLiteral);
245 
247 }; // class PlaceholderPattern
248 
249 
261 class KRATOS_API(WR_APPLICATION) ModelPartPattern : public PlaceholderPattern
262 {
263 public:
266 
268 
272 
273  ModelPartPattern() = default;
274 
275  ModelPartPattern(const std::string& rPattern);
276 
277  ModelPartPattern(ModelPartPattern&& rOther) = default;
278 
279  ModelPartPattern(const ModelPartPattern& rOther) = default;
280 
284 
285  using PlaceholderPattern::operator=;
286 
290 
292 
303  std::string Apply(const ModelPart& rModelPart) const;
304 
306 
307 protected:
310 
312  ModelPartPattern(const std::string& rPattern, const PlaceholderMap& rPlaceholderMap);
313 
315  virtual void PopulatePlaceholderMap(PlaceholderMap& rMap, const ModelPart& rModelPart) const;
316 
318 
320 }; // class ModelPartPattern
321 
322 
336 class KRATOS_API(WR_APPLICATION) CheckpointPattern : public ModelPartPattern
337 {
338 public:
341 
343 
347 
348  CheckpointPattern() = default;
349 
350  CheckpointPattern(const std::string& rPattern);
351 
353 
354  CheckpointPattern(const CheckpointPattern& rOther) = default;
355 
359 
360  using ModelPartPattern::operator=;
361 
362 protected:
365 
367 
369  virtual void PopulatePlaceholderMap(PlaceholderMap& rMap, const ModelPart& rModelPart) const override;
370 
372 }; // class CheckpointPattern
373 
374 
377 
378 
379 } // namespace Kratos
380 
381 #include "wrapp/utils/impl/PatternUtility_impl.hpp"
A class for working with formatted strings related to Checkpoint.
Definition: PatternUtility.hpp:337
KRATOS_CLASS_POINTER_DEFINITION(CheckpointPattern)
CheckpointPattern(CheckpointPattern &&rOther)=default
CheckpointPattern(const CheckpointPattern &rOther)=default
static PlaceholderMap GetPlaceholderMap()
CheckpointPattern(const std::string &rPattern)
virtual void PopulatePlaceholderMap(PlaceholderMap &rMap, const ModelPart &rModelPart) const override
Populate a key-value map of registered placeholders from a ModelPart.
A class for working with formatted strings related to ModelParts.
Definition: PatternUtility.hpp:262
KRATOS_CLASS_POINTER_DEFINITION(ModelPartPattern)
ModelPartPattern(const std::string &rPattern, const PlaceholderMap &rPlaceholderMap)
Forwarding constructor for derived classes.
ModelPartPattern(const std::string &rPattern)
virtual void PopulatePlaceholderMap(PlaceholderMap &rMap, const ModelPart &rModelPart) const
Populate a key-value map of registered placeholders from a ModelPart.
ModelPartPattern(ModelPartPattern &&rOther)=default
static PlaceholderMap GetPlaceholderMap()
ModelPartPattern(const ModelPartPattern &rOther)=default
std::string Apply(const ModelPart &rModelPart) const
Substitute values from the specified ModelPart in the stored pattern.
A class for interfacing placeholders and regular expressions.
Definition: PatternUtility.hpp:124
PlaceholderPattern(const PlaceholderPattern &rOther)=default
Ref< const std::string > GetPatternString() const
Get the pattern with placeholders.
KRATOS_CLASS_POINTER_DEFINITION(PlaceholderPattern)
Ref< const std::string > GetRegexString() const
Get the string representation of the regex.
PlaceholderPattern & operator=(PlaceholderPattern &&rOther)=default
std::string Apply(Ref< const PlaceholderMap > rPlaceholderValueMap) const
Substitute values in the stored pattern.
std::map< std::string, std::vector< std::string > > MatchType
Definition: PatternUtility.hpp:133
virtual ~PlaceholderPattern()=default
PlaceholderPattern & operator=(const PlaceholderPattern &rOther)=default
bool IsConst() const
Return true if the input pattern contains no placeholders.
std::filesystem::path PathType
Definition: PatternUtility.hpp:135
PlaceholderPattern(PlaceholderPattern &&rOther)=default
std::map< std::string, std::optional< std::vector< std::size_t > >> PlaceholderGroupMap
Definition: PatternUtility.hpp:131
MatchType Match(Ref< const std::string > rString) const
Find all placeholders' values in the input string.
bool IsAMatch(Ref< const std::string > rString) const
Check whether a string satisfies the pattern.
Ref< const std::regex > GetRegex() const
Get the regex for the input pattern.
std::map< std::string, std::string > PlaceholderMap
Definition: PatternUtility.hpp:129
PlaceholderPattern(Ref< const std::string > rPattern, Ref< const PlaceholderMap > rPlaceholderMap)
Construct from a placeholder pattern and its associated map.
void Glob(TOutputIterator it) const
Collect all file/directory paths that match the pattern.
PlaceholderMap GetPlaceholderMap() const
Get a map of all placeholders and their associated regex representations.
Static-only utility class for common regexes.
Definition: PatternUtility.hpp:36
static std::pair< std::string, std::regex > UnsignedInteger()
Get a string-regex pair representing an unsigned integer.
static std::pair< std::string, std::regex > Integer()
Get a string-regex pair representing an integer.
static std::pair< std::string, std::regex > FloatingPoint()
Get a string-regex pair representing an integer.
Definition: AddIOToPython.hpp:12
T & Ref
Definition: common.hpp:9