ATLAS Offline Software
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Attributes | List of all members
TrigConf::LogicExpression Class Reference

#include <LogicExpression.h>

Collaboration diagram for TrigConf::LogicExpression:

Public Member Functions

 LogicExpression (std::ostream &o=std::cout)
 constructor More...
 
virtual ~LogicExpression ()=default
 destructor More...
 
 LogicExpression (const std::string &name, std::ostream &o=std::cout)
 
virtual int parse (const std::string &expr, bool enclosed=false)
 
virtual std::string logicRep () const
 
void setState (char s)
 
void setElement (const std::string &e)
 
void addSubLogic (const LogicExpression &sub)
 
char state () const
 
const std::string & element () const
 
const LogicV_tsubLogics () const
 
const std::shared_ptr< LogicExpressionsubLogic (int i) const
 
bool isPlaceHolder () const
 
int totalNumberOfElements () const
 
void markPlaceHolder ()
 
void normalize ()
 
void printError (const std::string &message, int i)
 
void printCurrentState ()
 
void print (const std::string &indent="")
 
void clear ()
 

Static Public Member Functions

static bool isValidElementChar (char c)
 
static std::string extractElementName (const std::string &expr)
 

Static Public Attributes

static const char kAND = '&'
 AND of sub-logics. More...
 
static const char kOR = '|'
 OR of sub-logics. More...
 
static const char kNOT = '!'
 NOT of a sub-logic. (only one sub-logic) More...
 
static const char kELEMENT = '#'
 simple element. More...
 
static const char kOPEN = '('
 empty logic but may have sub-logics. More...
 
static const char kCLOSE = ')'
 ')' is a valid symbol, but not allowed as a state. More...
 

Protected Attributes

std::string m_LogicRep
 
char m_State
 
LogicV_t m_SubLogics
 
std::string m_Element
 
std::ostream & m_ostream
 output stream for all messages More...
 

Detailed Description

Definition at line 27 of file LogicExpression.h.

Constructor & Destructor Documentation

◆ LogicExpression() [1/2]

LogicExpression::LogicExpression ( std::ostream &  o = std::cout)

constructor

Definition at line 14 of file LogicExpression.cxx.

14  :
16  m_Element(""),
17  m_ostream(o)
18 {}

◆ ~LogicExpression()

virtual TrigConf::LogicExpression::~LogicExpression ( )
virtualdefault

destructor

◆ LogicExpression() [2/2]

LogicExpression::LogicExpression ( const std::string &  name,
std::ostream &  o = std::cout 
)

Definition at line 21 of file LogicExpression.cxx.

22  : m_State(kELEMENT),
23  m_Element(name),
24  m_ostream(o)
25 {}

Member Function Documentation

◆ addSubLogic()

void TrigConf::LogicExpression::addSubLogic ( const LogicExpression sub)
inline

Definition at line 56 of file LogicExpression.h.

56  {
57  m_SubLogics.emplace_back(std::make_shared<LogicExpression>(sub));
58  }

◆ clear()

void LogicExpression::clear ( )

Definition at line 293 of file LogicExpression.cxx.

293  {
294  m_State = kELEMENT;
295  m_SubLogics.clear();
296  m_Element = "";
297 }

◆ element()

const std::string& TrigConf::LogicExpression::element ( ) const
inline

Definition at line 61 of file LogicExpression.h.

61 { return m_Element; }

◆ extractElementName()

std::string LogicExpression::extractElementName ( const std::string &  expr)
static

Definition at line 40 of file LogicExpression.cxx.

40  {
41  std::string name="";
42  for (unsigned int i=0; i<expr.size(); ++i) {
43  if (isValidElementChar(expr[i])) name += expr[i];
44  else break;
45  }
46  return name;
47 }

◆ isPlaceHolder()

bool TrigConf::LogicExpression::isPlaceHolder ( ) const
inline

Definition at line 64 of file LogicExpression.h.

64 { return (m_State==kOPEN && m_SubLogics.size()==1); }

◆ isValidElementChar()

bool LogicExpression::isValidElementChar ( char  c)
static

Definition at line 29 of file LogicExpression.cxx.

29  {
30  std::string valid_sym = "_";
31  if (isalpha(c) || isdigit(c) || valid_sym.find(c)!=std::string::npos) {
32  return true;
33  } else {
34  return false;
35  }
36 }

◆ logicRep()

std::string LogicExpression::logicRep ( ) const
virtual

Definition at line 176 of file LogicExpression.cxx.

176  {
177  std::string s="";
178  if (m_State == kELEMENT) {
179  s = m_Element;
180  } else if (m_State == kAND || m_State == kOR) {
181  LogicV_t::const_iterator p;
182  s = kOPEN;
183  for (p=subLogics().begin(); p!=subLogics().end(); ++p) {
184  s += (*p)->logicRep();
185  s += m_State;
186  }
187  if (s.size() > 1) s[s.size()-1] = kCLOSE;
188  else s += kCLOSE;
189  } else if (m_State == kNOT) {
190  s = kNOT;
191  s += kOPEN;
192  if (subLogics().size() == 1) s += subLogics()[0]->logicRep();
193  s += kCLOSE;
194  } else if (m_State == kOPEN) {
195  s += kOPEN;
196  if (subLogics().size() == 1) s += subLogics()[0]->logicRep();
197  s += kCLOSE;
198  }
199  return s;
200 }

◆ markPlaceHolder()

void LogicExpression::markPlaceHolder ( )

Definition at line 169 of file LogicExpression.cxx.

169  {
170  if (m_State == kELEMENT && subLogics().size() == 1 && m_Element=="") {
171  m_State = kOPEN;
172  }
173 }

◆ normalize()

void LogicExpression::normalize ( )

Definition at line 204 of file LogicExpression.cxx.

204  {
205  LogicV_t::const_iterator p;
206  for (p=subLogics().begin(); p!=subLogics().end(); ++p) {
207  (*p)->normalize();
208  }
209  if (isPlaceHolder()) {
210  char s = subLogics()[0]->state();
211  std::string e = subLogics()[0]->element();
212  LogicV_t sublogics = subLogics()[0]->subLogics();
213  clear();
214  setState(s);
215  setElement(e);
216  for (p=sublogics.begin(); p!=sublogics.end(); ++p) {
217  addSubLogic(**p);
218  }
219  }
220 }

◆ parse()

int LogicExpression::parse ( const std::string &  expr,
bool  enclosed = false 
)
virtual

Definition at line 51 of file LogicExpression.cxx.

51  {
52  unsigned int i=0;
53  char c;
54  bool no_problem=true;
55  char prev = ' ';
56  int n;
57 
58  m_LogicRep = expr;
59  while (no_problem && i < expr.size()) {
60  c = expr[i];
61 
62  switch (c) {
63  case kOPEN:
64  {
66  if ( (n = sub.parse(expr.substr(i+1), true)) > 0) {
67  addSubLogic(sub);
68  prev = kELEMENT;
69  i += n;
70  } else {
71  no_problem = false;
72  }
73  }
74  break;
75  case kCLOSE:
76  if ( ( (m_State == kAND || m_State == kOR) && subLogics().size()>=2) ||
77  (m_State == kNOT && subLogics().size()==1) ||
78  (m_State == kELEMENT && subLogics().size()<=1 && m_Element=="")) {
79  prev = kELEMENT;
81  return i + 1;
82  } else {
83  std::ostringstream formatedoutput;
84  formatedoutput << "Invalid logic: State=" << m_State << " Nsublogics=" << subLogics().size() << " Element=" << m_Element;
85  printError(formatedoutput.str(), i);
86  no_problem = false;
87  }
88  break;
89  case kAND: case kOR:
90  if (prev == kELEMENT &&
91  ( (subLogics().size() >= 2 && m_State==c) ||
92  (subLogics().size()==1 && m_State==kELEMENT))
93  ) {
94  m_State = c;
95  prev = c;
96  } else {
97  std::ostringstream formatedoutput;
98  formatedoutput << kAND << " or " << kOR << " must come after a logic element";
99  printError(formatedoutput.str(), i);
100  no_problem = false;
101  }
102  break;
103  case kNOT:
104  if (kELEMENT != prev) {
106  sub.setState(kNOT);
107  if ( (i+1)<expr.size() && isValidElementChar(expr[i+1])) {
108  std::string name = extractElementName(expr.substr(i+1));
109  sub.addSubLogic(LogicExpression(name,m_ostream));
110  i += name.size();
111  addSubLogic(sub);
112  } else if ( (i+1)<expr.size() && expr[i+1]==kOPEN) {
114  if ( (n = sub2.parse(expr.substr(i+2), true)) > 0) {
115  sub.addSubLogic(sub2);
116  i += (n+1);
117  addSubLogic(sub);
118  } else {
119  no_problem = false;
120  }
121  } else {
122  printError("Was expecting a sub-logic after !", i);
123  no_problem = false;
124  }
125  } else {
126  printError("! must not come directly after a logic element", i);
127  no_problem = false;
128  }
129  prev = kELEMENT;
130  break;
131  default:
132  // Start of a candidate of an element string.
133  if (subLogics().size()==0 ||
134  (subLogics().size()>0 && m_State != kELEMENT)) {
135  std::string name = extractElementName(expr.substr(i));
136  if (name.size() > 0) {
138  i += name.size()-1;
139  } else {
140  printError("Unrecognized string", i);
141  no_problem = false;
142  }
143  prev = kELEMENT;
144  } else {
145  printError("A logic element may not come immediately after another logic element", i);
146  no_problem = false;
147  }
148  break;
149  }
150  i ++;
151 
152  }
153  if (!no_problem) {
154  return -1;
155  }
156 
157  if (enclosed) {
158  // The logic expression started with a '(' but didn't find ')' and
159  // reached the end of the string.
160  printError("The expression started with a '(' but ')' not found.", i);
161  return 0;
162  }
163  markPlaceHolder();
164 
165  return i;
166 }

◆ print()

void LogicExpression::print ( const std::string &  indent = "")

Definition at line 268 of file LogicExpression.cxx.

268  {
269  std::string s=indent;
270  if (m_State == kELEMENT) {
271  m_ostream << indent << m_Element << std::endl;
272  } else if (m_State == kAND || m_State == kOR) {
273  if (m_State==kAND) m_ostream << indent << "AND" << std::endl;
274  if (m_State==kOR) m_ostream << indent << "OR" << std::endl;
275  LogicV_t::const_iterator p;
276  for (p=subLogics().begin(); p!=subLogics().end(); ++p) {
277  (*p)->print(indent + " ");
278  }
279  } else if (m_State == kNOT) {
280  m_ostream << indent << "NOT" << std::endl;
281  if (subLogics().size() == 1) {
282  subLogic(0)->print(indent + " ");
283  }
284  } else if (m_State == kOPEN) {
285  if (subLogics().size() == 1) subLogic(0)->print(indent);
286  } else {
287  m_ostream << indent << "ERROR" << std::endl;
288  }
289 }

◆ printCurrentState()

void LogicExpression::printCurrentState ( )

Definition at line 259 of file LogicExpression.cxx.

259  {
260  char aaa[100];
261  sprintf(aaa, "Current state: State=%c Nsublogics=%d Element=%s",
262  m_State, (int)subLogics().size(), m_Element.c_str());
263  m_ostream << aaa << std::endl;
264 }

◆ printError()

void LogicExpression::printError ( const std::string &  message,
int  i 
)

Definition at line 246 of file LogicExpression.cxx.

246  {
247  std::string m="Error while parsing : ";
248  std::string s="";
249  int n = m.size();
250  for (int j=0; j<(n+i); ++j) s += " ";
251  s += "^";
252  m_ostream << m << m_LogicRep << " at " << i << "-th character" << std::endl;
253  m_ostream << s << std::endl;
254  m_ostream << message << std::endl;
255 }

◆ setElement()

void TrigConf::LogicExpression::setElement ( const std::string &  e)
inline

Definition at line 55 of file LogicExpression.h.

55 { m_Element = e; }

◆ setState()

void TrigConf::LogicExpression::setState ( char  s)
inline

Definition at line 54 of file LogicExpression.h.

54 { m_State = s; }

◆ state()

char TrigConf::LogicExpression::state ( ) const
inline

Definition at line 60 of file LogicExpression.h.

60 { return m_State; }

◆ subLogic()

const std::shared_ptr<LogicExpression> TrigConf::LogicExpression::subLogic ( int  i) const
inline

Definition at line 63 of file LogicExpression.h.

63 { return subLogics()[i]; }

◆ subLogics()

const LogicV_t& TrigConf::LogicExpression::subLogics ( ) const
inline

Definition at line 62 of file LogicExpression.h.

62 { return m_SubLogics; }

◆ totalNumberOfElements()

int LogicExpression::totalNumberOfElements ( ) const

Definition at line 224 of file LogicExpression.cxx.

224  {
225  int n = 0;
226  LogicV_t::const_iterator p;
227 
228  switch (m_State) {
229  case kELEMENT:
230  n = 1;
231  break;
232  case kAND: case kOR: case kNOT: case kOPEN:
233  for (p=subLogics().begin(); p!=subLogics().end(); ++p) {
234  n += (*p)->totalNumberOfElements();
235  }
236  break;
237  default:
238  n = 0;
239  break;
240  }
241  return n;
242 }

Member Data Documentation

◆ kAND

const char TrigConf::LogicExpression::kAND = '&'
static

AND of sub-logics.

Definition at line 32 of file LogicExpression.h.

◆ kCLOSE

const char TrigConf::LogicExpression::kCLOSE = ')'
static

')' is a valid symbol, but not allowed as a state.

Definition at line 37 of file LogicExpression.h.

◆ kELEMENT

const char TrigConf::LogicExpression::kELEMENT = '#'
static

simple element.

Definition at line 35 of file LogicExpression.h.

◆ kNOT

const char TrigConf::LogicExpression::kNOT = '!'
static

NOT of a sub-logic. (only one sub-logic)

Definition at line 34 of file LogicExpression.h.

◆ kOPEN

const char TrigConf::LogicExpression::kOPEN = '('
static

empty logic but may have sub-logics.

Definition at line 36 of file LogicExpression.h.

◆ kOR

const char TrigConf::LogicExpression::kOR = '|'
static

OR of sub-logics.

Definition at line 33 of file LogicExpression.h.

◆ m_Element

std::string TrigConf::LogicExpression::m_Element
protected

Definition at line 85 of file LogicExpression.h.

◆ m_LogicRep

std::string TrigConf::LogicExpression::m_LogicRep
protected

Definition at line 80 of file LogicExpression.h.

◆ m_ostream

std::ostream& TrigConf::LogicExpression::m_ostream
protected

output stream for all messages

Definition at line 87 of file LogicExpression.h.

◆ m_State

char TrigConf::LogicExpression::m_State
protected

Definition at line 82 of file LogicExpression.h.

◆ m_SubLogics

LogicV_t TrigConf::LogicExpression::m_SubLogics
protected

Definition at line 84 of file LogicExpression.h.


The documentation for this class was generated from the following files:
TrigConf::LogicExpression::markPlaceHolder
void markPlaceHolder()
Definition: LogicExpression.cxx:169
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
TrigConf::LogicExpression::setState
void setState(char s)
Definition: LogicExpression.h:54
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
TrigConf::LogicExpression::m_SubLogics
LogicV_t m_SubLogics
Definition: LogicExpression.h:84
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
TrigConf::LogicExpression::subLogics
const LogicV_t & subLogics() const
Definition: LogicExpression.h:62
TrigConf::LogicV_t
std::vector< std::shared_ptr< LogicExpression > > LogicV_t
Definition: LogicExpression.h:24
TrigConf::LogicExpression::setElement
void setElement(const std::string &e)
Definition: LogicExpression.h:55
ReweightUtils.message
message
Definition: ReweightUtils.py:15
TrigConf::LogicExpression
Definition: LogicExpression.h:27
TrigConf::LogicExpression::m_State
char m_State
Definition: LogicExpression.h:82
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
geometry_dat_to_json.indent
indent
Definition: geometry_dat_to_json.py:36
lumiFormat.i
int i
Definition: lumiFormat.py:85
TrigConf::LogicExpression::isPlaceHolder
bool isPlaceHolder() const
Definition: LogicExpression.h:64
TrigConf::LogicExpression::addSubLogic
void addSubLogic(const LogicExpression &sub)
Definition: LogicExpression.h:56
beamspotman.n
n
Definition: beamspotman.py:731
TrigConf::LogicExpression::clear
void clear()
Definition: LogicExpression.cxx:293
TrigConf::LogicExpression::m_Element
std::string m_Element
Definition: LogicExpression.h:85
TrigConf::LogicExpression::LogicExpression
LogicExpression(std::ostream &o=std::cout)
constructor
Definition: LogicExpression.cxx:14
TrigConf::LogicExpression::kNOT
static const char kNOT
NOT of a sub-logic. (only one sub-logic)
Definition: LogicExpression.h:34
TrigConf::LogicExpression::kELEMENT
static const char kELEMENT
simple element.
Definition: LogicExpression.h:35
TrigConf::LogicExpression::extractElementName
static std::string extractElementName(const std::string &expr)
Definition: LogicExpression.cxx:40
TrigConf::LogicExpression::kOR
static const char kOR
OR of sub-logics.
Definition: LogicExpression.h:33
TrigConf::name
Definition: HLTChainList.h:35
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
TrigConf::LogicExpression::kCLOSE
static const char kCLOSE
')' is a valid symbol, but not allowed as a state.
Definition: LogicExpression.h:37
TrigConf::LogicExpression::subLogic
const std::shared_ptr< LogicExpression > subLogic(int i) const
Definition: LogicExpression.h:63
TrigConf::LogicExpression::kAND
static const char kAND
AND of sub-logics.
Definition: LogicExpression.h:32
TrigConf::LogicExpression::printError
void printError(const std::string &message, int i)
Definition: LogicExpression.cxx:246
python.compressB64.c
def c
Definition: compressB64.py:93
TrigConf::LogicExpression::m_ostream
std::ostream & m_ostream
output stream for all messages
Definition: LogicExpression.h:87
TrigConf::LogicExpression::isValidElementChar
static bool isValidElementChar(char c)
Definition: LogicExpression.cxx:29
TrigConf::LogicExpression::kOPEN
static const char kOPEN
empty logic but may have sub-logics.
Definition: LogicExpression.h:36
TrigConf::LogicExpression::m_LogicRep
std::string m_LogicRep
Definition: LogicExpression.h:80