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