TagRules.h
Go to the documentation of this file.
1 /*
2 
3 A tag rule is an association of a match rule and a single string or an
4 array of strings. The match is in general a regular expression
5 (regex). Multiple tag rules are collected into a "tag rule set" which
6 is expressed as an associative array aka dict aka object aka map with
7 the match as the key. Tag rule sets may be further collected into
8 contexts.
9 
10 For example, the FrameFanin/FrameFanout pair from gen allows Jsonnet
11 expressions like:
12 
13 tags_rules : [
14  {
15  frame: {
16  tagXY: ["tagX%d"%n, "tagY%d"%n],
17  ["is-tagged%d"%n]: "was-tagged",
18  "*": "fanned",
19  "*": "saw-pipeline-%d"%n,
20  },
21  // Output traces are tagged based on a map from an
22  // input to one or more output tags.
23  trace: {
24  intagA: "outtagA%d"%n,
25  intagB: ["outtagB%d"%n, "outtagBB%d"%n],
26  }
27  }
28 } for n in std.range(0,self.multiplicity-1)],
29 
30 In C++ this file provides utilities to parse and query this structure.
31 
32 */
33 
35 
36 #include <unordered_set>
37 #include <unordered_map>
38 #include <string>
39 #include <vector>
40 #include <regex>
41 
42 namespace WireCell {
43 
44  namespace tagrules {
45 
46  typedef std::string tag_t;
48  typedef std::unordered_set<tag_t> tagset_t;
49  typedef std::pair<std::regex, tagset_t> rule_t;
50  typedef std::vector<rule_t> ruleset_t;
51 
52  // return rule's tagset if rule matches tag else empty set
53  tagset_t match(const tag_t& tag, const rule_t& rs);
54 
55  // Compare a rule set to a tag and fill ret with first
56  // matching rule value and return true if match found. Else
57  // return false. If "all_rules" is false, then stop checking
58  // rules after the first is found.
59  bool match(const tag_t& tag, const ruleset_t& rs, tagset_t& ret, bool all_rules = true);
60 
61  // Given a tagset and a ruleset return a tagset which
62  // transforms all input tags according to rules.
63  tagset_t transform(const tagset_t& ts, const ruleset_t& rs, bool all_rules = true);
64 
65 
66  /* A tagrule::Context represents a collection of tag
67  * transforms, each at an index and with a name.
68  */
69  class Context {
70 
71  std::unordered_map< std::string, std::vector<ruleset_t> > m_rulesets;
72 
73  public:
74  // This should be an array of objects each keyed by a
75  // context name and with values providing a ruleset.
76  void configure(const Configuration& cfg);
77 
78  // Transform tag in context and return set of produced tags.
79  tagset_t transform(size_t ind, const std::string& name, const tag_t& tag);
80 
81  // Transform a collection of tags in a context.
82  template<typename Tags>
83  Tags transform(size_t ind, const std::string& name, const Tags& tags) {
84  const auto& rsv = m_rulesets[name];
85  if (rsv.empty() or ind >= rsv.size()) {
86  return Tags();
87  }
88  tagrules::tagset_t ts(tags.begin(), tags.end());
89  const auto& rs = rsv[ind];
90  auto out = tagrules::transform(ts, rs);
91  return Tags(out.begin(), out.end());
92  }
93  };
94 
95  }
96 
97  // Some Configuration cverter helpers for targules types.
98 
99  template<>
100  inline
102  tagrules::tagset_t ret;
103  if (cfg.isString()) {
104  ret.insert(cfg.asString());
105  return ret;
106  }
107  if (cfg.isArray()) {
108  for (auto one : cfg) {
109  ret.insert(one.asString());
110  }
111  return ret;
112  }
113  return def;
114  }
115 
116  template<>
117  inline
120  for (auto key : cfg.getMemberNames()) {
121  auto ts = convert<tagrules::tagset_t>(cfg[key]);
122  if (ts.empty()) {
123  continue;
124  }
125  ret.push_back(make_pair(std::regex(key), ts));
126  }
127  if (ret.empty()) {
128  return def;
129  }
130  return ret;
131  }
132 
133 
134 }
static QCString name
Definition: declinfo.cpp:673
std::pair< std::regex, tagset_t > rule_t
Definition: TagRules.h:49
tagrules::ruleset_t convert< tagrules::ruleset_t >(const Configuration &cfg, const tagrules::ruleset_t &def)
Definition: TagRules.h:118
std::unordered_set< tag_t > tagset_t
Definition: TagRules.h:48
std::string string
Definition: nybbler.cc:12
cfg
Definition: dbjson.py:29
tagset_t match(const tag_t &tag, const rule_t &rs)
Definition: TagRules.cxx:16
tagrules::tagset_t convert< tagrules::tagset_t >(const Configuration &cfg, const tagrules::tagset_t &def)
Definition: TagRules.h:101
tagset_t transform(const tagset_t &ts, const ruleset_t &rs, bool all_rules=true)
Definition: TagRules.cxx:43
std::vector< rule_t > ruleset_t
Definition: TagRules.h:50
std::unordered_map< std::string, std::vector< ruleset_t > > m_rulesets
Definition: TagRules.h:71
def key(type, name=None)
Definition: graph.py:13
Definition: Main.h:22
tagset_t transform(size_t ind, const std::string &name, const tag_t &tag)
Definition: TagRules.cxx:80
std::string match_t
Definition: TagRules.h:47
Tags transform(size_t ind, const std::string &name, const Tags &tags)
Definition: TagRules.h:83
Json::Value Configuration
Definition: Configuration.h:50
void configure(const Configuration &cfg)
Definition: TagRules.cxx:61
std::string tag_t
Definition: TagRules.h:46