ATLAS Offline Software
Logic.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef TRIGCONFDATA_LOGIC_H
6 #define TRIGCONFDATA_LOGIC_H
7 
8 #include <string>
9 #include <vector>
10 #include <map>
11 #include <iostream>
12 #include <memory>
13 
14 namespace TrigConf {
15 
17  public:
19  LogicParsingException(const std::string & error) : m_error(error)
20  {}
21  const std::string & msg() const { return m_error; }
22  private:
23  std::string m_error{};
24  };
25 
31  class Logic {
32  public:
33  enum NodeType { LEAF, AND, OR };
34  Logic() = delete;
35  virtual ~Logic() = default;
36 
37  NodeType nodeType() const { return m_nodeType; }
38 
39  void setNegate(bool negate = true);
40  bool negate() const;
41 
42  void setExpression(const std::string &);
43  const std::string & expression() const;
44 
45  virtual bool evaluate(const std::map<std::string,bool> & elementsState) const = 0;
46  virtual bool evaluate(const std::map<std::string, unsigned int> & elementsCount) const = 0;
47 
48  virtual std::map<std::string, bool> elements() const = 0;
49  virtual std::map<std::string, unsigned int> elementsCount() const = 0;
50 
51  void print(std::ostream & = std::cout) const;
52 
53  virtual void print(std::ostream &, size_t indSize, size_t indLevel) const = 0;
54 
55  protected:
57 
58  private:
60  bool m_negate { false };
61  std::string m_expression{ "" };
62  };
63 
64 
65  class LogicLeaf : public Logic {
66  public:
67  LogicLeaf() = delete;
68  LogicLeaf(const std::string & content);
69  virtual ~LogicLeaf() override = default;
70 
71  void setContent(const std::string & content);
72 
73  bool evaluate(const std::map<std::string,bool> & elements) const override;
74  bool evaluate(const std::map<std::string, unsigned int> & elementsCount) const override;
75 
76  std::map<std::string, bool> elements() const override;
77  std::map<std::string, unsigned int> elementsCount() const override;
78 
79  private:
80  virtual void print(std::ostream &, size_t indSize, size_t indLevel) const override;
81  std::string m_content { "" };
82  std::string m_name { "" };
83  unsigned int m_count { 1 };
84  };
85 
86 
87  class LogicOPS : public Logic {
88  public:
90  LogicOPS(NodeType nodeType, std::unique_ptr<Logic>&& left);
91  virtual ~LogicOPS() override = default;
92  void addSubLogic(std::unique_ptr<Logic>&& right);
93  const std::vector<std::unique_ptr<Logic>> & subLogics() const;
94  std::vector<std::unique_ptr<Logic>> takeSubLogics();
95  private:
96  virtual void print(std::ostream &, size_t indSize, size_t indLevel) const override;
97  std::vector<std::unique_ptr<Logic>> m_subs;
98  };
99 
100 
101  class LogicAND : public LogicOPS {
102  public:
103  LogicAND();
104  LogicAND(std::unique_ptr<Logic>&& left);
105  virtual ~LogicAND() override = default;
106  bool evaluate(const std::map<std::string, bool> & elementsState) const override;
107  bool evaluate(const std::map<std::string, unsigned int> & elementsCount) const override;
108  std::map<std::string, bool> elements() const override;
109  std::map<std::string, unsigned int> elementsCount() const override;
110  };
111 
112 
113  class LogicOR : public LogicOPS {
114  public:
115  LogicOR();
116  LogicOR(std::unique_ptr<Logic>&& left);
117  virtual ~LogicOR() override = default;
118  bool evaluate(const std::map<std::string,bool> & elementsState) const override;
119  bool evaluate(const std::map<std::string,unsigned int> & elementsCount) const override;
120  std::map<std::string, bool> elements() const override;
121  std::map<std::string, unsigned int> elementsCount() const override;
122  };
123 
124 }
125 
126 #endif
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::Logic::print
virtual void print(std::ostream &, size_t indSize, size_t indLevel) const =0
TrigConf::LogicAND::~LogicAND
virtual ~LogicAND() override=default
TrigConf::Logic::m_expression
std::string m_expression
Definition: Logic.h:61
TrigConf::Logic::LEAF
@ LEAF
Definition: Logic.h:33
TrigConf::LogicAND::elementsCount
std::map< std::string, unsigned int > elementsCount() const override
Definition: Logic.cxx:187
TrigConf::Logic::~Logic
virtual ~Logic()=default
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
TrigConf::LogicLeaf
Definition: Logic.h:65
TrigConf::Logic::Logic
Logic()=delete
TrigConf::Logic::evaluate
virtual bool evaluate(const std::map< std::string, bool > &elementsState) const =0
TrigConf::Logic::setNegate
void setNegate(bool negate=true)
Definition: Logic.cxx:19
TrigConf::LogicParsingException::m_error
std::string m_error
Definition: Logic.h:23
TrigConf::Logic::negate
bool negate() const
Definition: Logic.cxx:24
TrigConf::LogicAND::LogicAND
LogicAND()
Definition: Logic.cxx:147
TrigConf
Forward iterator to traverse the main components of the trigger configuration.
Definition: Config.h:22
TrigConf::LogicOR::LogicOR
LogicOR()
Definition: Logic.cxx:197
grepfile.content
string content
Definition: grepfile.py:56
TrigConf::Logic::AND
@ AND
Definition: Logic.h:33
TrigConf::LogicOR::~LogicOR
virtual ~LogicOR() override=default
TrigConf::LogicOPS
Definition: Logic.h:87
TrigConf::Logic::evaluate
virtual bool evaluate(const std::map< std::string, unsigned int > &elementsCount) const =0
TrigConf::LogicParsingException::LogicParsingException
LogicParsingException(const std::string &error)
Definition: Logic.h:19
TrigConf::LogicParsingException::msg
const std::string & msg() const
Definition: Logic.h:21
TrigConf::Logic::expression
const std::string & expression() const
Definition: Logic.cxx:34
TrigConf::Logic::elements
virtual std::map< std::string, bool > elements() const =0
TrigConf::LogicLeaf::m_name
std::string m_name
Definition: Logic.h:82
TrigConf::LogicLeaf::elementsCount
std::map< std::string, unsigned int > elementsCount() const override
Definition: Logic.cxx:92
TrigConf::Logic::m_nodeType
NodeType m_nodeType
Definition: Logic.h:59
TrigConf::LogicOPS::~LogicOPS
virtual ~LogicOPS() override=default
TrigConf::Logic::elementsCount
virtual std::map< std::string, unsigned int > elementsCount() const =0
TrigConf::Logic
Class to hold the logic structure representing a logical expression and evaluate it for a given state...
Definition: Logic.h:31
TrigConf::LogicLeaf::~LogicLeaf
virtual ~LogicLeaf() override=default
TrigConf::LogicLeaf::m_count
unsigned int m_count
Definition: Logic.h:83
TrigConf::LogicLeaf::LogicLeaf
LogicLeaf()=delete
TrigConf::LogicOR
Definition: Logic.h:113
TrigConf::LogicOR::elementsCount
std::map< std::string, unsigned int > elementsCount() const override
Definition: Logic.cxx:237
TrigConf::Logic::OR
@ OR
Definition: Logic.h:33
TrigConf::Logic::print
void print(std::ostream &=std::cout) const
Definition: Logic.cxx:39
TrigConf::LogicLeaf::m_content
std::string m_content
Definition: Logic.h:81
TrigConf::LogicOPS::LogicOPS
LogicOPS(NodeType nodeType)
Definition: Logic.cxx:105
TrigConf::Logic::m_negate
bool m_negate
Definition: Logic.h:60
TrigConf::Logic::NodeType
NodeType
Definition: Logic.h:33
TrigConf::Logic::nodeType
NodeType nodeType() const
Definition: Logic.h:37
TrigConf::LogicOPS::takeSubLogics
std::vector< std::unique_ptr< Logic > > takeSubLogics()
Definition: Logic.cxx:127
error
Definition: IImpactPoint3dEstimator.h:70
TrigConf::LogicOPS::addSubLogic
void addSubLogic(std::unique_ptr< Logic > &&right)
Definition: Logic.cxx:116
TrigConf::LogicParsingException
Definition: Logic.h:16
TrigConf::LogicAND
Definition: Logic.h:101
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
TrigConf::LogicParsingException::LogicParsingException
LogicParsingException()=delete