test_tagrules.cxx
Go to the documentation of this file.
1 /* Test TagRules
2 
3  This has some built-in testing data but can also be used to test
4  from the command line.
5 
6  test_tagrules file.jsonnet tag1 tag2 ...
7 
8  The file (can be JSON also) must follow schema:
9  [array
10  {category1:
11  match1a: tagset1a
12  match1b: tagset1b
13  },
14  {category2:
15  match2: tagset2
16  },
17  ]
18 
19  */
20 
21 #include "WireCellUtil/TagRules.h"
22 #include "WireCellUtil/Persist.h"
23 
24 #include <iostream>
25 
26 using namespace std;
27 using namespace WireCell;
28 
29 int main(int argc, char* argv[])
30 {
31  std::string jtext = R"(
32 [
33  {
34  frame: {
35  tagXY: ["tagX%d"%n, "tagY%d"%n],
36  "is-tagged\\d": "was-tagged",
37  ["is-tagged%d"%n]: "was-tagged%d"%n,
38  },
39  // Output traces are tagged based on a map from an
40  // input to one or more output tags.
41  trace: {
42  intagA: "outtagA%d"%n,
43  intagB: ["outtagB%d"%n, "outtagBB%d"%n],
44  }
45  } for n in std.range(0,5)
46 ]
47 )";
48 
49  tagrules::tagset_t tags;
51  if (argc == 2) {
52  cfg = Persist::load(argv[1]); // .jsonnet or .json file assumed
53  }
54  else {
55  cfg = Persist::loads(jtext);
56  }
57  if (argc > 2) {
58  for (int ind=2; ind<argc; ++ind) {
59  tags.insert(argv[ind]);
60  }
61  }
62  else {
63  tags.insert("tagXY");
64  tags.insert("tagXY"); // intenional duplicate
65  tags.insert("intagA");
66  tags.insert("dontmatchme"); // a tag that shouldn't be matched
67  tags.insert("is-tagged2");
68  }
69 
70  int iport = 0;
71  for (auto jport : cfg) {
72  for (auto cat : jport.getMemberNames()) {
73  auto jrules = jport[cat];
74  auto rs = convert<tagrules::ruleset_t>(jrules);
75  auto newtags = tagrules::transform(tags, rs);
76  std::cout << "port:"<<iport<<", categ:\""<<cat<<"\": have tags:[";
77  string comma="";
78  for (auto t: tags) {
79  std::cout << comma << t;
80  comma = ", ";
81  }
82  std::cout << "], got tags: [";
83  comma="";
84  for (auto t: newtags) {
85  std::cout << comma << t;
86  comma = ", ";
87  }
88  std::cout << "], with rules:" << std::endl;
89  std::cout << jrules << std::endl;
90  }
91  ++iport;
92  std::cout << std::endl;
93  }
94  return 0;
95 }
96 
std::unordered_set< tag_t > tagset_t
Definition: TagRules.h:48
std::string string
Definition: nybbler.cc:12
int main(int argc, char *argv[])
STL namespace.
cfg
Definition: dbjson.py:29
tagset_t transform(const tagset_t &ts, const ruleset_t &rs, bool all_rules=true)
Definition: TagRules.cxx:43
Definition: Main.h:22
Json::Value Configuration
Definition: Configuration.h:50
QTextStream & endl(QTextStream &s)
def load(filename, jpath="depos")
Definition: depos.py:34