Modeler#

This section uses the QuadFlightControl example provided with Scade One. The example is located in the examples/QuadFlightControl/QuadFlightControl folder in the Scade One installation directory.

# Update according to your installation
s_one_install = Path(r"C:\Scade One")

quad_flight_project = (
    s_one_install / "examples/QuadFlightControl/QuadFlightControl" / "QuadFlightControl.sproj"
)

ScadeOne instance#

A ScadeOne instance is created with the following code:

app = ScadeOne(install_dir=s_one_install)

where install_dir is the location of Scade One installation and it could take a string or a pathlib.Path object as value. The app object is then used to access to projects.

Swan projects#

A Swan project is opened with ansys.scadeone.core.ScadeOne.load_project().

project = app.load_project(quad_flight_project)

The ansys.scadeone.core.ScadeOne.load_project() can take a string or a pathlib.Path object as parameter.

If the project exists, a ansys.scadeone.core.project.Project object is returned, else None.

Several projects can be loaded, with successive calls to the load_project() method. They can be accessed using the app.projects property.

Note

From the ansys.pyscadeone.core.project.Project.app, one has access to the Scade One app containing the project.

Dependencies#

A project may have several sub-projects as dependencies. The list of dependencies is returned with the ansys.scadeone.core.project.Project.dependencies() method.

# Direct project dependencies
my_dependency = project.dependencies()

# All dependencies recursively
all_dependencies = project.dependencies(all=True)

Swan module bodies and interfaces files#

Swan module bodies (.swan files) and Swan module interfaces (.swani files) can be listed with the ansys.scadeone.core.project.Project.swan_sources() method.

# Direct project Swan sources
sources = project.swan_sources()

# All sources, including those from dependencies
all_sources = project.swan_sources(all=True)

Swan model#

A ansys.scadeone.core.model.Model object represents a Swan model or program. A model is built from the sources of the project.

The project’s model can be accessed with the ansys.scadeone.core.project.Project.model property.

# Get the model
model = project.model

Note

From a model, one can access to the Scade One instance, with the model’s project property as in my_app = model.project.app

A model contains all modules (body or interface) from the Swan sources. For each module, one has access to the declarations it contains.

From a ansys.scadeone.core.model.Model object, one can therefore access to:

  • the modules,

  • all declarations,

  • specific declarations, like types or constants

  • or a particular declaration.

Here are some examples:

# All sensors in the model
sensors = model.sensors

# All operators in the model
operators = model.operators

Note

PyScadeOne tries to be lazy to handle large projects. For instance, looking for all sensors requires to load all sources.

Looking for a specific item, the sources are loaded until the required item is found.

In the following example, the ansys.scadeone.core.model.Model.find_declaration() is used to filter a specific operator. In that case, the search (and the load) stops when the requested operator is found. As we will use Swan constructs, we need to import their definitions:

# Module defining all Swan-related classes, see below
import ansys.scadeone.core.swan as swan

Here is an example to filter declarations to get specific operator:

# Filter function, looking for an operator of name 'EngineControl'
def op_filter(obj: swan.GlobalDeclaration):
    if isinstance(obj, swan.Operator):
        return str(obj.id) == "MotorControl"
    return False


# Get the operator
op_decl = model.find_declaration(op_filter)

Swan language#

The model content represents the structure of the Swan program, starting with the declarations: types, constants, groups, sensors, operators, and signatures.

For an operator or a signature, one can access to the input and output flows and to the body for operator. Then, from the body, one can access to the content of diagrams, equations, etc.

All Swan language constructs are represented by classes from the ansys.scadeone.core.swan module. The section Swan language describes the Swan classes, with respect to the structure of the language reference documentation in the product.

Resuming with the previous code example, here is a usage sample of the Swan language API:

# All Swan constructs have a path.
type_list = list(model.types)
print(type_list[1].get_full_path())  # => "QuadFlightControl::EngineHealth"

# Stating op_decl is indeed a Swan operator
operator = cast(swan.Operator, op_decl)
print(f"first input: {next(operator.inputs).id}")  # => 'attitudeCmd'