Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
ScriptEngine Class Reference

#include <ored/scripting/scriptengine.hpp>

+ Collaboration diagram for ScriptEngine:

Public Member Functions

 ScriptEngine (const ASTNodePtr root, const QuantLib::ext::shared_ptr< Context > context, const QuantLib::ext::shared_ptr< Model > model=nullptr)
 
void run (const std::string &script="", bool interactive=false, QuantLib::ext::shared_ptr< PayLog > paylog=nullptr, bool includePastCashflows=false)
 

Private Attributes

const ASTNodePtr root_
 
const QuantLib::ext::shared_ptr< Contextcontext_
 
const QuantLib::ext::shared_ptr< Modelmodel_
 

Detailed Description

Definition at line 36 of file scriptengine.hpp.

Constructor & Destructor Documentation

◆ ScriptEngine()

ScriptEngine ( const ASTNodePtr  root,
const QuantLib::ext::shared_ptr< Context context,
const QuantLib::ext::shared_ptr< Model model = nullptr 
)

Definition at line 38 of file scriptengine.hpp.

40 : root_(root), context_(context), model_(model) {}
const QuantLib::ext::shared_ptr< Context > context_
const ASTNodePtr root_
const QuantLib::ext::shared_ptr< Model > model_

Member Function Documentation

◆ run()

void run ( const std::string &  script = "",
bool  interactive = false,
QuantLib::ext::shared_ptr< PayLog paylog = nullptr,
bool  includePastCashflows = false 
)

Definition at line 1163 of file scriptengine.cpp.

1164 {
1165
1166 ASTNode* loc;
1167 ASTRunner runner(model_, script, interactive, *context_, loc, paylog, paylog != nullptr && includePastCashflows);
1168
1169 randomvariable_output_pattern pattern;
1170 if (model_ == nullptr || model_->type() == Model::Type::MC) {
1171 pattern = randomvariable_output_pattern(randomvariable_output_pattern::pattern::expectation);
1172 } else if (model_->type() == Model::Type::FD) {
1173 pattern = randomvariable_output_pattern(randomvariable_output_pattern::pattern::left_middle_right);
1174 } else {
1175 QL_FAIL("model type not handled when setting output pattern for random variables");
1176 }
1177
1178 DLOG("run script engine, context before run is:");
1179 DLOGGERSTREAM(pattern << *context_);
1180
1181 if (interactive) {
1182 std::cerr << pattern << "\nInitial Context: \n" << (*context_) << std::endl;
1183 }
1184
1185 boost::timer::cpu_timer timer;
1186 try {
1187 reset(root_);
1188 root_->accept(runner);
1189 timer.stop();
1190 QL_REQUIRE(runner.value.size() == 1,
1191 "ScriptEngine::run(): value stack has wrong size (" << runner.value.size() << "), should be 1");
1192 QL_REQUIRE(runner.filter.size() == 1,
1193 "ScriptEngine::run(): filter stack has wrong size (" << runner.filter.size() << "), should be 1");
1194 DLOG("script engine successfully finished, context after run is:");
1195
1196 if (interactive) {
1197 std::cerr << "\nScript engine finished without errors. Context after run:" << std::endl;
1198 }
1199
1200 } catch (const std::exception& e) {
1201 std::ostringstream errorMessage;
1202 errorMessage << "Error during script execution: " << e.what() << " at "
1203 << (loc ? to_string(loc->locationInfo) : "(last visited ast node not known)") << ": "
1204 << printCodeContext(script, loc, true);
1205
1206 std::ostringstream strippedErrorMsg;
1207 strippedErrorMsg << "Error during script execution: " << e.what() << " at "
1208 << (loc ? to_string(loc->locationInfo) : "(last visited ast node not known)");
1209
1210 DLOGGERSTREAM(strippedErrorMsg.str());
1212 DLOGGERSTREAM("Context when hitting the error:");
1213 DLOGGERSTREAM(pattern << *context_);
1214
1215 if (interactive) {
1216 std::cerr << strippedErrorMsg.str() << "\n";
1217 std::cerr << printCodeContext(script, loc);
1218 std::cerr << "Context when hitting the error:" << std::endl;
1219 std::cerr << (*context_) << std::endl;
1220 std::cin.get();
1221 }
1222
1223 QL_FAIL(errorMessage.str());
1224 }
1225
1226 DLOGGERSTREAM(pattern << *context_);
1227 DLOG("Script engine running time: " << boost::timer::format(timer.elapsed()));
1228
1229 if (interactive) {
1230 std::cerr << pattern << *context_ << std::endl;
1231 std::cin.get();
1232 }
1233}
std::string script
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define DLOGGERSTREAM(text)
Definition: log.hpp:632
std::string printCodeContext(std::string script, const ASTNode *loc, bool compact)
void reset(const ASTNodePtr root)
Definition: astresetter.cpp:44
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ root_

const ASTNodePtr root_
private

Definition at line 45 of file scriptengine.hpp.

◆ context_

const QuantLib::ext::shared_ptr<Context> context_
private

Definition at line 46 of file scriptengine.hpp.

◆ model_

const QuantLib::ext::shared_ptr<Model> model_
private

Definition at line 47 of file scriptengine.hpp.