Model execution¶
execute_model¶
Executes the current model in the session. Depending on the types of objects specified in the model, it will adjust the execution of the LTM core applications accordingly.
The return value is the last return code and a list of a dictionary of the programs, including the input file and output file contents.
Example:
def open_and_write_model(filename: str):
session = LtmSession("ikernel", ltm_module_path)
with session:
try:
session.load(filename=filename)
session.write_model()
last_rc, results = session.execute_model()
except Exception as e:
print(e)
Note
The execution will break if any return code is not 0.
Override LTM core location¶
It is possible to manually override the location of the LTM core
applications with the optional argument ltm_core_path to LtmSession.
If ltm_core_path is set to a directory and it exists, it will prepend
that path onto PATH.
ltm_core_path = "/opt/ltm/release/bin"
filename = "/path/to/model.json"
with LtmSession("override", ltm_core_path=ltm_core_path) as session:
session.load(filename=filename)
session.write_model()
last_rc, results = session.execute_model()
LtmSession will raise an exception if ltm_core_path is not a folder.
set_execute_callback¶
set_execute_callback registers a user-callback that will be invoked
after every LTM core program when the model is executing. The callback takes
2 parameters, a dict and any.
The dictionary is program_results and any is any
userdata the user supplies.
# Callback is invoked after every LTM core application
def usercallback(program_info: dict, userdata: any):
print(userdata)
print(program_info)
return True
Register the callback with session.
# Set the callback
session.set_execute_callback(usercallback, {"user": "data", "num": 42})
program_info structure:
{
'return_code': 0,
'name': 'arcltm < IMPORT_HISTORICAL_SERIES.FLEX > ./historical.out 2>&1',
'input_file_contents': 'input file contents',
'log_file_contents': 'output log file contents'
}
set_pre_execute_callback¶
set_pre_execute_callback registers a user-callback that will be invoked
before each LTM core program when the model is executing. The callback
takes two parameters: a ProgramInfo and an arbitrary userdata.
The callback should return True to allow the program to run. Returning
False will stop the execution of the current program and stop executing the model.
Example:
def pre_usercallback(program_info: ProgramInfo, userdata: any):
print("About to run:", program_info.name)
return True
session.set_pre_execute_callback(pre_usercallback, {"user": "data", "num": 42})
set_post_execute_callback¶
set_post_execute_callback registers a user-callback that will be invoked
after each LTM core program when the model is executing. The callback
takes two parameters: a ProgramResult and an arbitrary userdata.
This callback is functionally identical to set_execute_callback - the
difference is purely semantic, intended to make the intent of the callback
clearer when it is placed after a program.
Example:
def post_usercallback(program_result: ProgramResult, userdata: any):
print("Finished:", program_result.program_name)
return True
session.set_post_execute_callback(post_usercallback, {"user": "data", "num": 42})
set_pre_model_execute_callback¶
set_pre_model_execute_callback registers a user-callback that will be
invoked before the entire model is executed. The callback takes two
parameters:
program_infos- a list ofProgramInfo, one for each program that will be executed.userdata- arbitrary data supplied by the caller.
The callback should return True to allow the model execution to proceed.
Returning False aborts the entire model execution.
The program_infos list provides a preview of all programs that will be
run. The order of the list matches the order in which the core programs
will be invoked.
Example:
def pre_model_callback(program_results: list[ProgramInfo], userdata: any):
print("Model about to start - will run", len(program_results), "programs")
return True
session.set_pre_model_execute_callback(pre_model_callback, {"user": "data"})
set_post_model_execute_callback¶
set_post_model_execute_callback registers a user-callback that will be
invoked after the entire model has finished executing. The callback
takes two parameters:
program_results- a list ofProgramResultcontaining the results for every program that was run.userdata- arbitrary data supplied by the caller.
The callback may inspect the results, perform logging, or trigger subsequent actions.
Example:
def post_model_callback(program_results: list[ProgramResult], userdata: any):
print("Model finished - checked", len(program_results), "programs")
return True
session.set_post_model_execute_callback(post_model_callback, {"user": "data"})