Generated code#
This section describes how to access generated code data from model elements using a Code Generation job (see Generated code).
The same QuadFlightControl
example is used. To setup the example see
QuadFlightControl example setup code.
The generated code related package is required:
from ansys.scadeone.core.svc.generated_code import GeneratedCode
The example uses the code generation job named CodeGen for which
a GeneratedCode
object is created:
app = ScadeOne(install_dir=s_one_install)
project = app.load_project(quad_flight_project)
# get generated code from CodeGen job
code_gen = GeneratedCode(project, "CodeGen")
Before being able to manipulate the generated code data, it is necessary to check that the CodeGen job executed (this is done from the Scade One IDE).
To check that the job has been executed, use the GeneratedCode.is_code_generated
property.
if not code_gen.is_code_generated:
print("The Generated Code API cannot be used if the job is not executed first.")
exit()
Model operators#
The list of operators as they are defined in the model can be retrieved using the GeneratedCode.get_model_operators()
method:
# get the list of all operators
for op in code_gen.get_model_operators():
print(f"operator {op.path} {'** root operator **' if op.root else ''}")
A given operator can also be retrieved by its model path using
the GeneratedCode.get_model_operator()
method.
The monomorphic instances of polymorphic operators can be retrieved using
the GeneratedCode.get_model_monomorphic_instance()
and GeneratedCode.get_model_monomorphic_instances()
methods:
# get the list of all monomorphic instances of polymorphic operators
for op in code_gen.get_model_monomorphic_instances():
print(f"monomorphic instance {op.path}, source operator: {op.source.path}")
Get generated code from model operators#
The ModelOperator object gives access to the different associated generated functions (for example the cycle function or the init function)
For example, to get the cycle function of the root operator:
# get model of root operator
root_op = code_gen.get_model_operator(code_gen.root_operators[0])
# get the corresponding cycle function
cfunc = root_op.cycle_method
print(f"\nOperator {root_op.path} => cycle function {cfunc.name}")
To directly get the parameters of the cycle function:
# print list of cycle function parameters
print(f"\nCycle function {cfunc.name}:")
for idx, p in enumerate(cfunc.parameters):
print(f"- param {idx + 1}: {p.name} of type {p.type_name}")
To get the list of inputs and outputs with the name of the associated parameters in the cycle function:
# print list of inputs
for idx, elem in enumerate(root_op.inputs):
print(
f"- input {idx + 1}: {elem.full_name('.')} => "
+ f"param {elem.code_name} of type {elem.code_type['name']}"
)
# print list of outputs
for idx, elem in enumerate(root_op.outputs):
print(
f"- output {idx + 1}: {elem.full_name('.')} => "
+ f"param {elem.code_name} of type {elem.code_type['name']}"
)