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:

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: object

Generated 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:
dict

Return 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

Parameters:
dtadict

The input data

fieldstr

Name of field

Returns:
Union[str, List[str]]

Return value(s) of the field

get_function_parameters(id_model: int, role: str) tuple[List[str], str]#

Get parameters of a function

Parameters:
id_modelint

ID of model.

role: str

One of “CycleMethod, “ResetMethod”, “InitMethod”.

Returns:
tuple[List[str], str]

Return a tuple including list of inputs and context parameter name.

get_interface_file(id_model: int) dict#

Returns (as a dictionary) the interface file data corresponding to given model identifier.

Parameters:
id_modelint

ID of model

Returns:
dict

Returns the interface file data

get_method_data(id_model: int, method_name: str) dict#

Get data of a method with its name given

Parameters:
id_modelint

ID of model

method_namestr

Name of model

Returns:
dict

Return a dict of method data

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

Parameters:
model_idint

ID of model

resolve_defbool, optional

If True, resolve NamedType definition, by default False

Returns:
list

Return a list of model type and its parameters

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:
dict

Return 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.

get_operator_name(id_model: int) str#

Get an operator name

Parameters:
id_modelint

ID of model

Returns:
str

Name of operator if it’s found in the model

get_role_type(id_model: int, id_code: int) str#

Get the role of a type with its ID given

Parameters:
id_modelint

ID of model

id_codeint

ID of type given

Returns:
str

Returns a role name

get_type(c_type_elt: List, c_type_id: int) TypeBase | None#

Get type data with its ID given

Parameters:
c_type_eltList

Code type element

c_type_idint

Code type ID

Returns:
Optional[TypeBase]

An instance of TypeBase

property code: List[dict]#

Returns the ‘code’ data section.

property generated_code_dir: str#

Returns the generated code directory path name.

property is_code_generated: bool#

Returns True if the code is generated for the job.

property mapping: dict#

Returns the mapping file as a dictionary.

property root_operators: List[str]#

Returns the list of root operators names.

Model classes#

Model classes are used to represent the model elements:

class ansys.scadeone.core.svc.generated_code.ModelOperator(gen_code: GeneratedCode, oper_elem: dict)#

Bases: ModelOperatorBase

Class 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 is_expanded: bool#

Returns True if the operator is expanded (inlined)

property is_imported: bool#

Returns True if the operator is an imported operator

property is_root: bool#

Returns True if the operator is a root operator

property is_specialized: bool#

Returns True if the operator is specialized

property outputs: List[ModelVariable]#

Returns the list of outputs for the operator (as ModelVariable objects).

property path: str#

Returns the Scade One operator path

property reset_method: CFunction | None#

Returns the reset method C function for the operator (as a CFunction object), if any.

property watches: List[dict]#

Returns the list of watches for the operator (as raw data, to be completed).

class ansys.scadeone.core.svc.generated_code.ModelMonomorphicInstance(gen_code: GeneratedCode, oper_elem: dict)#

Bases: ModelOperatorBase

Class 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 path: str#

Returns the Scade One operator path

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.

property watches: List[dict]#

Returns the list of watches for the operator (as raw data, to be completed).

class ansys.scadeone.core.svc.generated_code.ModelVariable(gen_code: GeneratedCode, var_elem: dict, parent: ModelOperator)#

Bases: ModelVariableBase

Class 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_name: str#

Returns the name of the variable as defined in the generated C function.

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 name: str#

Returns the variable name

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: ModelVariableBase

Class 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_name: str#

Returns the name of the variable as defined in the generated C function.

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: ModelObject

Class 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.

load_code_data() None#

Forces the assignment of code name and type.

property code_name: str#

Returns the name of the variable as defined in the generated C function.

property code_type: dict#

Returns the generated C code data corresponding to the sensor type. (‘name’: C type name, ‘category’: type category and ‘elements’: sub elements of the type)

property code_type_name: str#

Returns the sensor C type name

property path: str#

Returns the Scade One sensor path

Code classes#

Generated code classes are used to represent the generated code elements:

class ansys.scadeone.core.svc.generated_code.CFunction(gen_code: GeneratedCode, function_elem: dict, code_container_index: int)#

Bases: CDeclaration

Class 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

get_implementation_file() str#

Returns the name of the enclosed implementation C file.

get_interface_file() str#

Returns the name of the enclosed interface C header file.

property name: str#

Returns the function name

property parameters: List[CParameter]#

Returns the function parameters as a list of CParameter objects.

property return_type: dict | None#

Returns the function return type as raw mapping file dictionary data, if any.

class ansys.scadeone.core.svc.generated_code.CParameter(gen_code: GeneratedCode, param_elem: dict)#

Bases: MappingObject

Class for a parameter of a generated C function.

  • gen_code: GeneratedCode object

  • param_elem: mapping file element

property is_const: bool#

Indicates if the parameter is a const type

property is_pointer: bool#

Indicates if the parameter is a pointer

property name: str#

Returns the parameter name

property param_type_id: int#

Returns the type id of the parameter

property signature: str#

Returns the parameter type signature (taking into account is_pointer and is_const).

property type_name: str#

Returns the parameter type name

Base classes#

class ansys.scadeone.core.svc.generated_code.ModelOperatorBase(gen_code: GeneratedCode, oper_elem: dict)#

Bases: ModelObject

Base 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).

property path: str#

Returns the Scade One operator path

property reset_method: CFunction | None#

Returns the reset method C function for the operator (as a CFunction object), if any.

property watches: List[dict]#

Returns the list of watches for the operator (as raw data, to be completed).

class ansys.scadeone.core.svc.generated_code.ModelVariableBase(gen_code: GeneratedCode, var_elem: dict)#

Bases: ModelObject

Base 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_name: str#

Returns the name of the variable as defined in the generated C function.

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: MappingObject

Base Class for mapping file model elements.

  • gen_code: GeneratedCode object

  • model_elem: mapping file element

get_code_elem(role: str = '', silent: bool = False) dict | None#

Returns the data associated to the model element, if any.

class ansys.scadeone.core.svc.generated_code.CDeclaration(gen_code: GeneratedCode, decl_elem: dict, code_container_index: int)#

Bases: MappingObject

Base 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

get_implementation_file() str#

Returns the name of the enclosed implementation C file.

get_interface_file() str#

Returns the name of the enclosed interface C header file.

class ansys.scadeone.core.svc.generated_code.MappingObject(gen_code: GeneratedCode, mapping_elem: dict)#

Bases: ABC

Base class for all mapping file elements.

  • gen_code: GeneratedCode object

  • mapping_elem: mapping file element