ATLAS Offline Software
Logic.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "TrigConfData/Logic.h"
6 
7 #include <iostream>
8 #include <regex>
9 
10 #include "boost/lexical_cast.hpp"
11 
12 const std::regex re( "([-\\w.]+)(?:\\[x(\\d+)\\])?$" );
13 
15  m_nodeType(nodeType)
16 {}
17 
18 void
20  m_negate = negate;
21 }
22 
23 bool
25  return m_negate;
26 }
27 
28 void
29 TrigConf::Logic::setExpression(const std::string & expr) {
30  m_expression = expr;
31 }
32 
33 const std::string &
35  return m_expression;
36 }
37 
38 void
39 TrigConf::Logic::print(std::ostream & o) const {
40  if(m_expression.size()>0) {
41  o << m_expression << std::endl;
42  o << std::string(m_expression.size(), '-') << std::endl;
43  }
44  print(o, 4, 0);
45 }
46 
47 
49  Logic(Logic::LEAF)
50 {
52 }
53 
54 
55 void
57  m_content = content;
58  std::smatch sm;
59  std::regex_match(content, sm, re);
60  m_name = sm[1];
61  m_count = sm[2].length()>0 ? boost::lexical_cast<unsigned int,std::string>(sm[2]) : 1;
62 }
63 
64 
65 bool
66 TrigConf::LogicLeaf::evaluate(const std::map<std::string,bool> & elementsState) const {
67  auto stateForThisLeaf = elementsState.find(m_content);
68  if( stateForThisLeaf == elementsState.end() ) {
69  throw LogicParsingException(std::string("Can't evaluate logic ") + m_content + " as it is missing in the state map");
70  }
71  return negate() ? !stateForThisLeaf->second : stateForThisLeaf->second;
72 }
73 
74 
75 bool
76 TrigConf::LogicLeaf::evaluate(const std::map<std::string, unsigned int> & elementsCount) const {
77  auto stateForThisLeaf = elementsCount.find(m_name);
78  if( stateForThisLeaf == elementsCount.end() ) {
79  throw LogicParsingException(std::string("Can't evaluate logic ") + m_content + " as " + m_name + " is missing in the counts map");
80  }
81  bool pass = stateForThisLeaf->second >= m_count;
82  return negate() ? !pass : pass;
83 }
84 
85 std::map<std::string, bool>
87  return {{m_name, true}};
88 }
89 
90 
91 std::map<std::string, unsigned int>
93  return {{m_name, m_count}};
94 }
95 
96 
97 void
98 TrigConf::LogicLeaf::print(std::ostream & o, size_t indSize, size_t indLevel) const {
99  o << std::string(indSize*indLevel, ' ');
100  if( negate() ) { o << "NOT "; }
101  o << m_content << std::endl;
102 }
103 
104 
106  Logic(nodeType)
107 {}
108 
109 TrigConf::LogicOPS::LogicOPS(NodeType nodeType, std::unique_ptr<Logic> && left) :
110  Logic(nodeType)
111 {
112  m_subs.push_back(std::move(left));
113 }
114 
115 void
116 TrigConf::LogicOPS::addSubLogic(std::unique_ptr<Logic>&& right)
117 {
118  m_subs.push_back(std::move(right));
119 }
120 
121 const std::vector<std::unique_ptr<TrigConf::Logic>> &
123  return m_subs;
124 }
125 
126 std::vector<std::unique_ptr<TrigConf::Logic>>
128  auto tmp = std::move(m_subs);
129  m_subs.clear();
130  return tmp;
131 }
132 
133 void
134 TrigConf::LogicOPS::print(std::ostream & o, size_t indSize, size_t indLevel) const {
135  o << std::string(indSize*indLevel, ' ');
136  if( negate() ) { o << "NOT "; }
137  o << (nodeType()==Logic::AND ? "AND (" : "OR (") << std::endl;
138  for( auto & subLogic : m_subs ) {
139  subLogic->print(o,indSize, indLevel+1);
140  }
141  o << std::string(indSize*indLevel, ' ') << ")" << std::endl;
142 }
143 
144 
145 
146 
148  LogicOPS(Logic::AND)
149 {}
150 
151 TrigConf::LogicAND::LogicAND(std::unique_ptr<Logic>&& left) :
152  LogicOPS(Logic::AND, std::move(left))
153 {}
154 
155 bool
156 TrigConf::LogicAND::evaluate(const std::map<std::string,bool> & elementsState) const {
157  for( auto & subLogic : subLogics() ) {
158  if(! subLogic->evaluate(elementsState) ) { // if one evaluates to false, we can stop
159  return negate() ? true : false;
160  }
161  }
162  return negate() ? false : true;
163 }
164 
165 bool
166 TrigConf::LogicAND::evaluate(const std::map<std::string, unsigned int> & elementsCount) const {
167  for( auto & subLogic : subLogics() ) {
168  if(! subLogic->evaluate(elementsCount) ) { // if one evaluates to false, we can stop
169  return negate() ? true : false;
170  }
171  }
172  return negate() ? false : true;
173 }
174 
175 std::map<std::string, bool>
177  std::map<std::string, bool> elements;
178  for( auto & subLogic : subLogics() ) {
179  for ( const auto& el : subLogic->elements() ) {
180  elements.insert(el);
181  }
182  }
183  return elements;
184 }
185 
186 std::map<std::string, unsigned int>
188  std::map<std::string, unsigned int> elementsCount;
189  for( auto & subLogic : subLogics() ) {
190  for ( const auto& el : subLogic->elementsCount() ) {
191  elementsCount.insert(el);
192  }
193  }
194  return elementsCount;
195 }
196 
198  LogicOPS(Logic::OR)
199 {}
200 
201 TrigConf::LogicOR::LogicOR(std::unique_ptr<Logic>&& left) :
202  LogicOPS(Logic::OR, std::move(left))
203 {}
204 
205 bool
206 TrigConf::LogicOR::evaluate(const std::map<std::string,bool> & elementsState) const {
207  for( auto & subLogic : subLogics() ) {
208  if( subLogic->evaluate(elementsState) ) { // if one evaluates to true, we can stop
209  return negate() ? false : true;
210  }
211  }
212  return negate() ? true : false;
213 }
214 
215 bool
216 TrigConf::LogicOR::evaluate(const std::map<std::string, unsigned int> & elementsCount) const {
217  for( auto & subLogic : subLogics() ) {
218  if( subLogic->evaluate(elementsCount) ) { // if one evaluates to true, we can stop
219  return negate() ? false : true;
220  }
221  }
222  return negate() ? true : false;
223 }
224 
225 std::map<std::string, bool>
227  std::map<std::string, bool> elements;
228  for( auto & subLogic : subLogics() ) {
229  for ( const auto& el : subLogic->elements() ) {
230  elements.insert(el);
231  }
232  }
233  return elements;
234 }
235 
236 std::map<std::string, unsigned int>
238  std::map<std::string, unsigned int> elementsCount;
239  for( auto & subLogic : subLogics() ) {
240  for ( const auto& el : subLogic->elementsCount() ) {
241  elementsCount.insert(el);
242  }
243  }
244  return elementsCount;
245 }
TrigConf::LogicLeaf::setContent
void setContent(const std::string &content)
Definition: Logic.cxx:56
TrigConf::LogicLeaf::evaluate
bool evaluate(const std::map< std::string, bool > &elements) const override
Definition: Logic.cxx:66
TrigConf::LogicOPS::print
virtual void print(std::ostream &, size_t indSize, size_t indLevel) const override
Definition: Logic.cxx:134
TrigConf::LogicOPS::m_subs
std::vector< std::unique_ptr< Logic > > m_subs
Definition: Logic.h:97
TrigConf::LogicOR::elements
std::map< std::string, bool > elements() const override
Definition: Logic.cxx:226
TrigConf::LogicOPS::subLogics
const std::vector< std::unique_ptr< Logic > > & subLogics() const
Definition: Logic.cxx:122
TrigConf::LogicAND::elements
std::map< std::string, bool > elements() const override
Definition: Logic.cxx:176
TrigConf::Logic::setExpression
void setExpression(const std::string &)
Definition: Logic.cxx:29
TrigConf::LogicAND::elementsCount
std::map< std::string, unsigned int > elementsCount() const override
Definition: Logic.cxx:187
TrigConf::LogicLeaf::print
virtual void print(std::ostream &, size_t indSize, size_t indLevel) const override
Definition: Logic.cxx:98
TrigConf::LogicLeaf::elements
std::map< std::string, bool > elements() const override
Definition: Logic.cxx:86
Root::AND
@ AND
Definition: TGRLCollection.h:32
TrigConf::Logic::Logic
Logic()=delete
TrigConf::Logic::setNegate
void setNegate(bool negate=true)
Definition: Logic.cxx:19
TrigConf::Logic::negate
bool negate() const
Definition: Logic.cxx:24
TrigConf::LogicAND::LogicAND
LogicAND()
Definition: Logic.cxx:147
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
TrigConf::LogicOR::LogicOR
LogicOR()
Definition: Logic.cxx:197
grepfile.content
string content
Definition: grepfile.py:56
TrigConf::Logic::AND
@ AND
Definition: Logic.h:33
re
const std::regex re("([-\\w.]+)(?:\\[x(\\d+)\\])?$")
TrigConf::LogicOPS
Definition: Logic.h:87
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
TrigConf::Logic::expression
const std::string & expression() const
Definition: Logic.cxx:34
TrigConf::LogicLeaf::elementsCount
std::map< std::string, unsigned int > elementsCount() const override
Definition: Logic.cxx:92
TrigConf::Logic
Class to hold the logic structure representing a logical expression and evaluate it for a given state...
Definition: Logic.h:31
Logic.h
TrigConf::LogicLeaf::LogicLeaf
LogicLeaf()=delete
TrigConf::LogicOR::elementsCount
std::map< std::string, unsigned int > elementsCount() const override
Definition: Logic.cxx:237
TrigConf::Logic::print
void print(std::ostream &=std::cout) const
Definition: Logic.cxx:39
TrigConf::LogicOPS::LogicOPS
LogicOPS(NodeType nodeType)
Definition: Logic.cxx:105
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
Root::OR
@ OR
Definition: TGRLCollection.h:32
TrigConf::Logic::NodeType
NodeType
Definition: Logic.h:33
TrigConf::LogicOPS::takeSubLogics
std::vector< std::unique_ptr< Logic > > takeSubLogics()
Definition: Logic.cxx:127
TrigConf::LogicOPS::addSubLogic
void addSubLogic(std::unique_ptr< Logic > &&right)
Definition: Logic.cxx:116
TrigConf::LogicParsingException
Definition: Logic.h:16
TrigConf::LogicOR::evaluate
bool evaluate(const std::map< std::string, bool > &elementsState) const override
Definition: Logic.cxx:206
TrigConf::LogicAND::evaluate
bool evaluate(const std::map< std::string, bool > &elementsState) const override
Definition: Logic.cxx:156