Model#

A Model object gives access to the model items defined in the Swan sources of a project. The unique Model instance of a project is the object returned by the ansys.scadeone.core.project.Project.model property of a project instance.

from ansys.scadeone.core import ScadeOne
with ScadeOne() as app:
    project = app.load_project('project.sproj')
    model = project.model

A model exposes only the modules of its own project. Cross-project resolution (for example through use directives or path_id references) is performed transparently by walking the project’s dependencies. To iterate over the modules of a project together with the modules of its dependencies, use ansys.scadeone.core.project.Project.all_modules().

Model documentation#

class ansys.scadeone.core.model.Model(project: IProject)#

Bases: IModel

Per-project model.

A Model describes the Swan content of a single Project (modules in its own assets/*.swan|.swani|.swant). Cross-project lookups (use directives, Lib::M::id paths) are resolved by walking into the project’s dependencies’ models, with cycle protection.

Loading of Swan sources is lazy: when a module body or interface is needed, it is loaded from the Swan source file.

static get_path_in_module(declaration: Declaration, module: Module) → PathIdentifier#

Get the path of declaration in module. If the declaration is in module, return the identifier, else return the path of the declaration in the module based on the use directives.

Parameters:
item: SwanItem

Declaration to get the path for.

module: Module

Module where the declaration is used.

Returns:
PathIdentifier

Path of the item in the module.

Raises:
ScadeOneException
  • If the declaration does not have a module.

  • If the module does not exist.

  • If the module does not have a use directive for the declaration module.

filter_declarations(filter_fn: Callable[[GlobalDeclaration], bool], all: bool = True) → List[GlobalDeclaration]#

Returns declarations matched by a filter.

Parameters:
filter_fnCallable[[GlobalDeclaration], bool]

A function of one argument of type GlobalDeclaration, returning True or False.

allbool, optional

If True, include declarations from dependencies, by default True, else only declarations from the modules of the model project are included.

Returns:
List[GlobalDeclaration]

List of matching declarations.

find_declaration(predicate_fn: Callable[[GlobalDeclaration], bool], all: bool = True) → GlobalDeclaration | None#
Finds a declaration for which predicate_fn returns True, else only declarations

from the modules of the model project are included.

Parameters:
predicate_fnCallable[[GlobalDeclaration], bool]

Function taking one GlobalDeclaration as argument and returning True when some property holds, else False.

allbool, optional

If True, include declarations from dependencies, by default True.

Returns:
Union[GlobalDeclaration, None]

Found declaration or None.

get_declarations(all: bool = True) → List[GlobalDeclaration]#

Returns a list of declarations in the model, including those from model project dependencies if all is True

Parameters:
allbool, optional

If True, include declarations from dependencies, by default True.

Returns:
List[swan.GlobalDeclaration]

List of all declarations.

get_module_body(name: str) → ModuleBody | None#

Returns module body of name ‘name’, searching dependencies on miss.

get_module_from_pathid(pathid: str, module: Module) → Module | None#

Return the Module instance for a given pathid A pathId is of the form [ID ::]+ ID, where the last ID is the object name, and the previous IDs with “::” form the the module full name.

If module’s name is reduced to a single ID, search for the module from the use directive in module.

If the pathid is reduced to a single ID, return module.

Parameters:
pathidstr

object full path

moduleModule

Context module where the search occurs.

Returns:
Union[Module, None]

Module of the object, or None if not module found

get_module_interface(name: str) → ModuleInterface | None#

Returns module interface of name ‘name’, searching dependencies on miss.

load_all_modules(*, bodies: bool = True, interfaces: bool = True, test_modules: bool = True, dependencies: bool = True) → None#

Loads systematically all modules.

Parameters:
bodiesbool, optional

Includes module bodies

interfacesbool, optional

Includes module interfaces

test_modulesbool, optional

Includes test modules

dependenciesbool, optional

Includes modules from dependencies

load_project(project_instance: IProject) → Model#

Register the model’s bound project Swan sources as lazy _PreModule entries.

Parameters:
project_instanceIProject

Must be the project this model is bound to.

Returns:
Model

Itself, for chaining.

Raises:
ScadeOneException

If project_instance is not the project bound to this model.

module_exists(module: Module | SwanFile) → bool#

Check if a module exists by its path in the model.

Parameters:
moduleswan.Module | SwanFile

Module source or module object to check. In case of a source, it is not loaded.

Returns:
bool

True if the module exists, False otherwise.

property all_modules: List[Module]#

Returns all modules (loaded Module objects, or not-yet-loaded module files) as a list.

property app: IScadeOne#

Scade One application instance.

property constants: List[ConstDecl]#

Returns a list of constant declarations.

Note that this includes all constants, including those from model project dependencies. Consider using Model.get_declarations() or Model.filter_declarations() for more general queries.

property declarations: List[GlobalDeclaration]#

Declarations in the model.

Note that this includes all declarations, including those from dependencies. Consider using Model.get_declarations() or Model.filter_declarations() for more general queries.

property groups: List[GroupDecl]#

Returns a list of group declarations.

Note that this includes all groups, including those from model project dependencies. Consider using Model.get_declarations() or Model.filter_declarations() for more general queries.

property is_all_modules_loaded: bool#

Returns True when all Swan modules have been loaded.

property modules: List[Module]#

Returns Module objects of the current Model (module body, module interface, test module) as a list.

property operator_declarations: List[OperatorDeclaration]#

Returns a list of operator declarations.

Note that this includes all operator declarations, including those from model project dependencies. Consider using Model.get_declarations() or Model.filter_declarations() for more general queries.

property operator_definitions: List[OperatorDefinition]#

Returns a list of operator definitions.

Note that this includes all operator definitions, including those from model project dependencies. Consider using Model.get_declarations() or Model.filter_declarations() for more general queries.

property parser: Parser#

Swan parser (shared at app level).

property project: IProject#

Project this model belongs to.

property sensors: List[SensorDecl]#

Returns a list of sensor declarations.

Note that this includes all sensors, including those from model project dependencies. Consider using Model.get_declarations() or Model.filter_declarations() for more general queries.

property types: List[TypeDecl]#

Returns a list of type declarations.

Note that this includes all types, including those from model project dependencies. Consider using Model.get_declarations() or Model.filter_declarations() for more general queries.

A model also handles test modules, see Swan Test constructs section.

Model.add_test_module(swan_elt: SwanFile | TestModule) → None#

Add a test module to the model.

Parameters:
swan_eltUnion[swan.SwanFile, swan.TestModule]

Content of the test module

Model.get_test_module(name: str) → TestModule | None#

Returns test module of name ‘name’, searching dependencies on miss.