//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2010, 2026 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available
// under the terms of the MIT License which is available at
// https://opensource.org/licenses/MIT
//
// SPDX-License-Identifier: MIT
//////////////////////////////////////////////////////////////////////////////

include::../_part_attributes.asciidoc[]

:leveloffset: -2

[[tools-cif2cif-chapter-elim-aut-casts]]
== Eliminate automata to string casts

indexterm:[CIF to CIF transformer,eliminate automata to string casts]
indexterm:[eliminate automata to string casts]
indexterm:[automata to string casts,eliminate]
This CIF to CIF transformation eliminates casts of automata to strings.

indexterm:[eliminate automata to string casts,supported specifications]

=== Supported specifications

This transformation supports a subset of CIF specifications.
The following restrictions apply:

* Component definitions and component instantiations are not supported.

indexterm:[eliminate automata to string casts,preprocessing]

=== Preprocessing

No preprocessing is currently performed by this CIF to CIF transformation.
To increase the subset of specifications that can be transformed, apply the following <<tools-cif2cif-chapter-index,CIF to CIF transformations>> (in the given order):

* <<tools-cif2cif-chapter-elim-comp-def-inst>>

indexterm:[eliminate automata to string casts,implementation]

=== Implementation details

All casts of automata to strings, using either implicit automaton `self` references or explicit automata references, are eliminated.
If the referred automaton has only a single location, the cast is replaced by a string literal with the name of that location.
If the automaton has multiple locations, the cast is replaced by an `if` expression, using the locations of the automaton as guards, and string literals containing the location names as values.
For nameless locations the string literal `"*"` is used.

For instance, consider the following CIF specification:

[source, cif]
----
automaton a:
  alg string x = <string>self;

  location:
    initial;
end

automaton b:
  alg string x = <string>self;

  location l1:
    initial;
  location l2:
end
----

This is transformed to the following CIF specification:

[source, cif]
----
automaton a:
  alg string x = "*";

  location:
    initial;
end

automaton b:
  alg string x = if   l1: "l1"
                 elif l2: "l2"
                 else     "l3"
                 end;

  location l1:
    initial;
  location l2;
  location l3;
end
----

=== Renaming

n/a

indexterm:[eliminate automata to string casts,size considerations]

=== Size considerations

The created `if` expressions are usually larger than the original cast expressions.
The increase is linear in terms of the number of locations times the number of casts.

indexterm:[eliminate automata to string casts,optimality]

=== Optimality

This transformation introduces 'if' expressions for automaton to string casts, for automata with at least two locations.
The guards of such 'if' expressions refer to the locations of the automata.
To eliminate such location references, apply the <<tools-cif2cif-chapter-elim-locs-in-exprs>> CIF to CIF transformation.

=== Annotations

This transformation does not process, add, or remove any annotations.
