ATLAS Offline Software
Loading...
Searching...
No Matches
Logic.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include <iostream>
8#include <regex>
9
10static const 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 << "\n";
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 if(!std::regex_match(content, sm, re)){
58 throw std::invalid_argument("LogicLeaf::setContent: Invalid content '" + content + "'");
59 }
60 m_name = sm[1];
61 m_count = sm[2].length() > 0 ? static_cast<unsigned int>(std::stoul(sm[2])) : 1;
62}
63
64
65bool
66TrigConf::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
75bool
76TrigConf::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
85std::map<std::string, bool>
87 return {{m_name, true}};
88}
89
90
91std::map<std::string, unsigned int>
95
96
97void
98TrigConf::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
108
109TrigConf::LogicOPS::LogicOPS(NodeType nodeType, std::unique_ptr<Logic> && left) :
111{
112 m_subs.push_back(std::move(left));
113}
114
115void
116TrigConf::LogicOPS::addSubLogic(std::unique_ptr<Logic>&& right)
117{
118 m_subs.push_back(std::move(right));
119}
120
121const std::vector<std::unique_ptr<TrigConf::Logic>> &
123 return m_subs;
124}
125
126std::vector<std::unique_ptr<TrigConf::Logic>>
128 auto tmp = std::move(m_subs);
129 m_subs.clear();
130 return tmp;
131}
132
133void
134TrigConf::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
150
151TrigConf::LogicAND::LogicAND(std::unique_ptr<Logic>&& left) :
152 LogicOPS(Logic::AND, std::move(left))
153{}
154
155bool
156TrigConf::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
165bool
166TrigConf::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
175std::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
186std::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
200
201TrigConf::LogicOR::LogicOR(std::unique_ptr<Logic>&& left) :
202 LogicOPS(Logic::OR, std::move(left))
203{}
204
205bool
206TrigConf::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
215bool
216TrigConf::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
225std::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
236std::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}
const boost::regex re(r_e)
static const std::regex re("([-\\w.]+)(?:\\[x(\\d+)\\])?$")
void print(char *figname, TCanvas *c1)
std::map< std::string, bool > elements() const override
Definition Logic.cxx:176
bool evaluate(const std::map< std::string, bool > &elementsState) const override
Definition Logic.cxx:156
std::map< std::string, unsigned int > elementsCount() const override
Definition Logic.cxx:187
bool evaluate(const std::map< std::string, bool > &elements) const override
Definition Logic.cxx:66
std::string m_name
Definition Logic.h:82
std::map< std::string, unsigned int > elementsCount() const override
Definition Logic.cxx:92
virtual void print(std::ostream &, size_t indSize, size_t indLevel) const override
Definition Logic.cxx:98
std::map< std::string, bool > elements() const override
Definition Logic.cxx:86
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:116
LogicOPS(NodeType nodeType)
Definition Logic.cxx:105
std::vector< std::unique_ptr< Logic > > takeSubLogics()
Definition Logic.cxx:127
virtual void print(std::ostream &, size_t indSize, size_t indLevel) const override
Definition Logic.cxx:134
std::vector< std::unique_ptr< Logic > > m_subs
Definition Logic.h:97
const std::vector< std::unique_ptr< Logic > > & subLogics() const
Definition Logic.cxx:122
std::map< std::string, bool > elements() const override
Definition Logic.cxx:226
std::map< std::string, unsigned int > elementsCount() const override
Definition Logic.cxx:237
bool evaluate(const std::map< std::string, bool > &elementsState) const override
Definition Logic.cxx:206
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.