Generated code#
This section contains the classes related to Scade One Generated Code.
It relies on the JSON mapping file generated by Scade One CG via a code generation job defined in a Scade One project.
from ansys.scadeone.core import ScadeOne
from ansys.scadeone.core.svc.generated_code import GeneratedCode
with ScadeOne() as app:
project = app.load_project('project.sproj')
code_gen = GeneratedCode(project, "CodeGen")
From the GeneratedCode object, methods are provided to navigate in the model as it is defined in the mapping file
in order to get the corresponding generated code artifacts.
The next sections describe:
the main class for generated code
GeneratedCode,the classes related to model elements,
the classes related to generated code.
Main class#
This section describes the main class for generated code.
- class ansys.scadeone.core.svc.generated_code.GeneratedCode(project: Project, job_name: str)#
Bases:
objectGenerated code main class.
- Parameters:
- project: Project
Current project.
- job_name: str
Code generation name.
- decompose_code_type(ctype: dict) dict#
Returns as a dictionary with the C type name, category, and sub elements of a given type element.
The dictionary content is:
ret_type ::= { "name": "<str>", # C name "category": "array|struct|enum|union|typedef|predefined_type", "elements": <sub_elements> }
where sub_elements depends on the category:
- array
dictionary of the form:
{ "base_type": <ret_type>, # base type of the array "size": int } # size of the array
- struct
list of dictionaries, such as:
{ "name": str, # field name "type": <ret_type>, # field type data "pointer": bool, # True if field is a pointer type "size": list } # optional list of integer or constant names
- enum
dictionary of the form:
{ "tag_name": str, # internal tag name for the enum "values": [<str, ...] } # list of names corresponding to the enum values
- union
list of dictionaries, such as:
{ "name": str, # variant field name "type": <ret_type>, # variant field type data "enum_value": int } # optional integer value
- predefined_type
the value is None.
- get_code_elem(code_id: int) dict#
Returns (as a dictionary) the code data corresponding to given code identifier.
code_index: position of the element in the code section list
parent_category: category (declarations, function, struct, enum, union) of the parent code element
parent_name: name of the parent code element
parent_id: code identifier of the parent code element
category: category (declarations, function, struct, enum, union) of the code element
content: data content (the element itself)
index: position of the element in the declaration
- get_code_id(model_id: int, role: str = '', silent: bool = False) int#
Returns the code identifier associated to a given model identifier for a given role.
If silent is set to True, the functions returns -1 in case no code is found. Otherwise, exception is raised.
- get_code_ids(model_id: int) Generator[Tuple[int, str], None, None]#
Returns the list of code identifiers associated to a given model identifier (as a Generator of code, role tuples).
- get_code_type_id_mapping() dict#
Get type mapping
- Returns:
dictReturn a dict where key is ID and values are a tuple of data type and parameters
- get_elaboration_function() CFunction | None#
Returns the elaboration C function for the operator, if any or None.
- get_field_value(dta: dict, field: str) str | List[str]#
Get value(s) of the field from a dict with its given name
- get_function_parameters(id_model: int, role: str) tuple[List[str], str]#
Get parameters of a function
- get_interface_file(id_model: int) dict#
Returns (as a dictionary) the interface file data corresponding to given model identifier.
- get_model_element(model_id: int, resolve_def: bool = False) list#
Returns the model element with its given ID. If resolve_def is set to True, and the element is a NamedType, the definition of the NamedType is resolved and returned
- get_model_elements(filter: str = '') Generator[List[str | dict], None, None]#
Returns the list of model elements defined in the mapping file (as a Generator of MapElem objects).
If filter is set, returns only the elements of the given category (elaboration, predefined_type, array, struct, enum, variant, named_type, sensor, operator, mono).
- get_model_id_mapping() dict#
Get model mapping
- Returns:
dictReturn a dict with key is ID and values are a tuple of data type and parameters
- get_model_monomorphic_instance(op_path: str) ModelMonomorphicInstance#
Returns the ModelMonomorphicInstance object corresponding to given operator path name.
- get_model_monomorphic_instances() List[ModelMonomorphicInstance]#
Returns the list of ModelMonomorphicInstance objects for the mapping file.
- get_model_operator(op_path: str) ModelOperator#
Returns the ModelOperator object corresponding to given operator path name.
- get_model_operators() List[ModelOperator]#
Returns the list of ModelOperator objects for the mapping file.
- get_model_sensor(sensor_path: str) ModelSensor#
Returns the ModelSensor object corresponding to given sensor path name.
- get_model_sensors() List[ModelSensor]#
Returns the list of ModelSensor objects for the mapping file.
Model classes#
Model classes are used to represent the model elements:
ModelOperatorrepresents a model operatorModelMonomorphicInstancerepresents a monomorphic instance (after generic type resolution)ModelVariablerepresents a model variable (input or output)ModelVariableMonomorphicrepresents a monomorphic variable (after generic type resolution)ModelSensorrepresents a model sensor
- class ansys.scadeone.core.svc.generated_code.ModelOperator(gen_code: GeneratedCode, oper_elem: dict)#
Bases:
ModelOperatorBaseClass for model operator.
gen_code: GeneratedCode object
oper_elem: mapping file element
- get_code_elem(role: str = '', silent: bool = False) dict | None#
Returns the data associated to the model element, if any.
- property cycle_method: CFunction | None#
Returns the cycle method C function for the operator (as a CFunction object), if any.
- property init_method: CFunction | None#
Returns the init method C function for the operator (as a CFunction object), if any.
- property inputs: List[ModelVariable]#
Returns the list of inputs for the operator (as ModelVariable objects).
- property instances: List[dict]#
Returns the list of instances for the operator (as raw data, to be completed).
- property outputs: List[ModelVariable]#
Returns the list of outputs for the operator (as ModelVariable objects).
- class ansys.scadeone.core.svc.generated_code.ModelMonomorphicInstance(gen_code: GeneratedCode, oper_elem: dict)#
Bases:
ModelOperatorBaseClass for monomorphic instance of polymorphic model operator.
gen_code: GeneratedCode object
oper_elem: mapping file element
- get_code_elem(role: str = '', silent: bool = False) dict | None#
Returns the data associated to the model element, if any.
- property cycle_method: CFunction | None#
Returns the cycle method C function for the operator (as a CFunction object), if any.
- property init_method: CFunction | None#
Returns the init method C function for the operator (as a CFunction object), if any.
- property inputs: List[ModelVariableMonomorphic]#
Returns the list of inputs for the operator (as ModelVariableMonomorphic objects).
- property instances: List[dict]#
Returns the list of instances for the operator (as raw data, to be completed).
- property outputs: List[ModelVariableMonomorphic]#
Returns the list of outputs for the operator (as ModelVariableMonomorphic objects).
- property reset_method: CFunction | None#
Returns the reset method C function for the operator (as a CFunction object), if any.
- property source: ModelOperator#
Returns ModelOperator object source of the monomorphic instance.
- class ansys.scadeone.core.svc.generated_code.ModelVariable(gen_code: GeneratedCode, var_elem: dict, parent: ModelOperator)#
Bases:
ModelVariableBaseClass for model variables (inputs or outputs) of operator.
gen_code: GeneratedCode object
var_elem: mapping file element
parent: ModelOperator object of the parent operator
- full_name(separator: str = '_') str#
Returns variable name taking into account group item(s) to suffix the name when applicable (main separator between the operator variable name and group item(s) name can be changed, default is ‘_’)
- get_code_elem(role: str = '', silent: bool = False) dict | None#
Returns the data associated to the model element, if any.
- property code_type: dict#
Returns the generated C code data corresponding to the variable type. (‘name’: C type name, ‘category’: type category and ‘elements’: sub elements of the type)
- property group_items: List[str | int]#
Returns the group item(s) associated with variable (only applicable for group type).
- property parent: ModelOperator#
Returns the model operator object owning the variable
- class ansys.scadeone.core.svc.generated_code.ModelVariableMonomorphic(gen_code: GeneratedCode, var_elem: dict, parent: ModelMonomorphicInstance)#
Bases:
ModelVariableBaseClass for model variables (inputs or outputs) of monomorphic instance of operator.
gen_code: GeneratedCode object
var_elem: mapping file element
parent: ModelMonomorphicInstance object of the parent monomorphic instance
- get_code_elem(role: str = '', silent: bool = False) dict | None#
Returns the data associated to the model element, if any.
- property code_type: dict#
Returns the generated C code data corresponding to the variable type. (‘name’: C type name, ‘category’: type category and ‘elements’: sub elements of the type)
- property parent: ModelMonomorphicInstance#
Returns the model operator object owning the variable
- property source: ModelVariable#
Returns ModelVariable object source of the monomorphic instance variable.
- class ansys.scadeone.core.svc.generated_code.ModelSensor(gen_code: GeneratedCode, sensor_elem: dict)#
Bases:
ModelObjectClass for model sensors.
gen_code: GeneratedCode object
var_elem: mapping file element
- get_code_elem(role: str = '', silent: bool = True) dict#
Returns the data associated to the model element, if any.
Code classes#
Generated code classes are used to represent the generated code elements:
CFunctionrepresents a C functionCParameterrepresents a C parameter
- class ansys.scadeone.core.svc.generated_code.CFunction(gen_code: GeneratedCode, function_elem: dict, code_container_index: int)#
Bases:
CDeclarationClass for a generated C function.
gen_code: GeneratedCode object
function_elem: mapping file element
code_container_index: position in the list of code container elements
- property parameters: List[CParameter]#
Returns the function parameters as a list of CParameter objects.
- class ansys.scadeone.core.svc.generated_code.CParameter(gen_code: GeneratedCode, param_elem: dict)#
Bases:
MappingObjectClass for a parameter of a generated C function.
gen_code: GeneratedCode object
param_elem: mapping file element
Base classes#
- class ansys.scadeone.core.svc.generated_code.ModelOperatorBase(gen_code: GeneratedCode, oper_elem: dict)#
Bases:
ModelObjectBase class for model operator or monomorphic instance of an operator.
gen_code: GeneratedCode object
oper_elem: mapping file element
- get_code_elem(role: str = '', silent: bool = False) dict | None#
Returns the data associated to the model element, if any.
- property cycle_method: CFunction | None#
Returns the cycle method C function for the operator (as a CFunction object), if any.
- property init_method: CFunction | None#
Returns the init method C function for the operator (as a CFunction object), if any.
- property instances: List[dict]#
Returns the list of instances for the operator (as raw data, to be completed).
- class ansys.scadeone.core.svc.generated_code.ModelVariableBase(gen_code: GeneratedCode, var_elem: dict)#
Bases:
ModelObjectBase class for model variables (operators inputs or outputs).
gen_code: GeneratedCode object
var_elem: mapping file element
- get_code_elem(role: str = '', silent: bool = False) dict | None#
Returns the data associated to the model element, if any.
- property code_type: dict#
Returns the generated C code data corresponding to the variable type. (‘name’: C type name, ‘category’: type category and ‘elements’: sub elements of the type)
- abstract property parent: ModelOperatorBase#
Returns the model operator object owning the variable
- class ansys.scadeone.core.svc.generated_code.ModelObject(gen_code: GeneratedCode, model_elem: dict)#
Bases:
MappingObjectBase Class for mapping file model elements.
gen_code: GeneratedCode object
model_elem: mapping file element
- class ansys.scadeone.core.svc.generated_code.CDeclaration(gen_code: GeneratedCode, decl_elem: dict, code_container_index: int)#
Bases:
MappingObjectBase class for C declaration (function, global, macro, predefined_type, struct, array, enum, union, typedef, imported_type).
gen_code: GeneratedCode object
decl_elem: mapping file element
code_container_index: position in the list of code container elements
- class ansys.scadeone.core.svc.generated_code.MappingObject(gen_code: GeneratedCode, mapping_elem: dict)#
Bases:
ABCBase class for all mapping file elements.
gen_code: GeneratedCode object
mapping_elem: mapping file element