ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
PhysicsAnalysis
CommonTools
ExpressionEvaluation
Root
ExpressionParser.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
// ExpressionParser.cxx, (c) ATLAS Detector software
8
// Author: Thomas Gillam (thomas.gillam@cern.ch)
9
// ExpressionParsing library
11
12
#include "
ExpressionEvaluation/ExpressionParser.h
"
13
#include "
ParsingInternals.h
"
14
#include "
ProxyLoaderSingleton.h
"
15
#include "
UnitInterpreterSingleton.h
"
16
#include "
AthContainers/CurrentContext.h
"
17
#include <boost/algorithm/string.hpp>
18
namespace
ExpressionParsing
{
19
20
ExpressionParser::ExpressionParser
()
21
{
22
m_proxyLoader
=
ProxyLoaderSingleton::getInstance
();
23
m_unitInterpreter
=
UnitInterpreterSingleton::getInstance
();
24
setup
();
25
}
26
27
ExpressionParser::ExpressionParser
(
IProxyLoader
*proxyLoader)
28
{
29
m_proxyLoader
= proxyLoader;
30
m_unitInterpreter
=
UnitInterpreterSingleton::getInstance
();
31
setup
();
32
}
33
34
ExpressionParser::ExpressionParser
(
IUnitInterpreter
*unitInterpreter)
35
{
36
m_proxyLoader
=
ProxyLoaderSingleton::getInstance
();
37
m_unitInterpreter
= unitInterpreter;
38
setup
();
39
}
40
41
ExpressionParser::ExpressionParser
(
IProxyLoader
*proxyLoader,
IUnitInterpreter
*unitInterpreter)
42
{
43
m_proxyLoader
= proxyLoader;
44
m_unitInterpreter
= unitInterpreter;
45
setup
();
46
}
47
48
ExpressionParser::~ExpressionParser
()
49
{
50
}
51
52
void
ExpressionParser::setup
()
53
{
54
55
}
56
57
bool
ExpressionParser::loadExpression
(
const
std::string &expression)
58
{
59
std::string trimmedExpression = boost::algorithm::trim_copy_if(expression, boost::is_any_of(
"\""
));
60
61
ProxyLoaderSingleton::getInstance
()->
reset
();
62
m_code
.clear();
63
64
Grammar<std::string::const_iterator> grammar;
65
ast::expression
expr;
66
67
std::string::const_iterator expressionIter = trimmedExpression.begin();
68
std::string::const_iterator expressionEnd = trimmedExpression.end();
69
using
boost::spirit::ascii::space_type;
70
bool
r
= phrase_parse(expressionIter, expressionEnd, grammar, space_type(), expr);
71
std::string remainingExpression(expressionIter, expressionEnd);
72
boost::trim(remainingExpression);
73
if
(remainingExpression.length() > 0) {
74
throw
std::runtime_error(
"Did you forget an operator? This was not parsed: '"
+ remainingExpression +
"'"
);
75
}
76
77
if
(!
r
)
return
false
;
78
79
Compiler compiler(
m_code
,
m_proxyLoader
,
m_unitInterpreter
);
80
compiler(expr);
81
m_code
.shrink_to_fit();
82
return
true
;
83
}
84
85
std::vector<std::string>
ExpressionParser::getVariables
()
const
{
86
std::vector<std::string> vars;
87
for
(
const
StackElement
&element :
m_code
) {
88
if
(element.isProxy() && std::find(vars.begin(),vars.end(),element.proxyVarName()) == vars.end() ) {
89
vars.push_back( element.proxyVarName() );
90
}
91
}
92
return
vars;
93
}
94
95
StackElement
ExpressionParser::evaluate
(
const
EventContext& ctx)
const
96
{
97
VirtualMachine vm{};
98
return
vm.execute(ctx,
m_code
);
99
}
100
101
StackElement
ExpressionParser::evaluate
()
const
102
{
103
const
EventContext& ctx = Gaudi::Hive::currentContext();
104
VirtualMachine vm{};
105
return
vm.execute(ctx,
m_code
);
106
}
107
108
bool
ExpressionParser::evaluateAsBool
()
const
109
{
110
StackElement
result =
evaluate
();
111
if
(result.isScalar())
return
result.asBool();
112
else
throw
std::runtime_error(
"Unable to evaluate vector quantity as a boolean"
);
113
}
114
115
double
ExpressionParser::evaluateAsDouble
()
const
116
{
117
StackElement
result =
evaluate
();
118
if
(result.isScalar())
return
result.scalarValue<
double
>();
119
else
throw
std::runtime_error(
"Unable to evaluate vector quantity as a double"
);
120
}
121
122
std::vector<int>
ExpressionParser::evaluateAsVector
()
const
123
{
124
StackElement
result =
evaluate
();
125
if
(result.isVector())
return
result.vectorValue<
int
>();
126
else
throw
std::runtime_error(
"Unable to evaluate scalar quantity as a vector"
);
127
}
128
}
CurrentContext.h
ExpressionParser.h
ParsingInternals.h
ProxyLoaderSingleton.h
UnitInterpreterSingleton.h
ExpressionParsing::EncapsulatingSingleton< IProxyLoader >::getInstance
static IProxyLoader * getInstance()
Definition
EncapsulatingSingleton.h:24
ExpressionParsing::ExpressionParser::~ExpressionParser
~ExpressionParser()
Definition
ExpressionParser.cxx:48
ExpressionParsing::ExpressionParser::m_code
std::vector< StackElement > m_code
Definition
ExpressionParser.h:54
ExpressionParsing::ExpressionParser::evaluateAsDouble
double evaluateAsDouble() const
Definition
ExpressionParser.cxx:115
ExpressionParsing::ExpressionParser::m_proxyLoader
IProxyLoader * m_proxyLoader
Definition
ExpressionParser.h:52
ExpressionParsing::ExpressionParser::ExpressionParser
ExpressionParser()
Definition
ExpressionParser.cxx:20
ExpressionParsing::ExpressionParser::setup
void setup()
Definition
ExpressionParser.cxx:52
ExpressionParsing::ExpressionParser::evaluateAsBool
bool evaluateAsBool() const
Definition
ExpressionParser.cxx:108
ExpressionParsing::ExpressionParser::evaluateAsVector
std::vector< int > evaluateAsVector() const
Definition
ExpressionParser.cxx:122
ExpressionParsing::ExpressionParser::loadExpression
bool loadExpression(const std::string &expression)
Definition
ExpressionParser.cxx:57
ExpressionParsing::ExpressionParser::evaluate
StackElement evaluate() const
Definition
ExpressionParser.cxx:101
ExpressionParsing::ExpressionParser::m_unitInterpreter
IUnitInterpreter * m_unitInterpreter
Definition
ExpressionParser.h:53
ExpressionParsing::ExpressionParser::getVariables
std::vector< std::string > getVariables() const
Definition
ExpressionParser.cxx:85
ExpressionParsing::IProxyLoader
Definition
IProxyLoader.h:21
ExpressionParsing::IProxyLoader::reset
virtual void reset()=0
ExpressionParsing::IUnitInterpreter
Definition
IUnitInterpreter.h:19
ExpressionParsing::StackElement
Class describing a single element in a text expression.
Definition
StackElement.h:55
r
int r
Definition
globals.cxx:22
ExpressionParsing
Namespace holding all the expression evaluation code.
Definition
ExpressionParser.h:26
ExpressionParsing::ast::expression
Definition
ParsingInternals.h:71
Generated on
for ATLAS Offline Software by
1.17.0