lingua-franca 0.10.1
Lingua Franca code generator
Loading...
Searching...
No Matches
org.lflang.generator.python.PythonGenerator Class Reference

Generator for Python target. More...

Inherits CGenerator, and org.lflang.generator.c.CCmakeGenerator.SetUpMainTarget.

Public Member Functions

void doGenerate (Resource resource, LFGeneratorContext context)
 Generate C code from the Lingua Franca model contained by the specified resource.
void generateAuxiliaryStructs (CodeBuilder builder, TypeParameterizedReactor tpr, boolean userFacing)
 Generate the aliases for inputs, outputs, and struct type definitions for actions of the specified reactor in the specified federate.
String generateDirectives ()
 Generate code that needs to appear at the top of the generated C file, such as #define and #include statements.
String generatePythonCode (String pyModuleName)
 Generate the Python code constructed from reactor classes and user-written classes.
Map< Path, CodeMapgeneratePythonFiles (String lfModuleName, String pyModuleName, String pyFileName) throws IOException
 Generate the necessary Python files.
String generatePythonReactorClasses ()
 Generate all Python classes if they have a reaction.
String getCmakeCode (boolean hasMain, String executableName, Stream< String > cSources)
Target getTarget ()
 Returns the Target enum for this generator.
TargetTypes getTargetTypes ()
boolean isOSCompatible ()
 Return true if the host operating system is compatible and otherwise report an error and return false.
void processProtoFile (String filename)
 Process a given .proto file.
 PythonGenerator (LFGeneratorContext context)

Protected Member Functions

void additionalPostProcessingForModes ()
void copyTargetFiles () throws IOException
 Copy Python specific target code to the src-gen directory.
void generateMethods (CodeBuilder src, TypeParameterizedReactor reactor)
 Do nothing.
void generateParameterInitialization (ReactorInstance instance)
 Generate runtime initialization code in C for parameters of a given reactor instance.
void generateReaction (CodeBuilder src, Reaction reaction, TypeParameterizedReactor tpr, int reactionIndex)
 Generate a reaction function definition for a reactor.
void generateReactorClassHeaders (TypeParameterizedReactor tpr, String headerName, CodeBuilder header, CodeBuilder src)
void generateReactorInstanceExtension (ReactorInstance instance)
 Generate code that is executed while the reactor instance is being initialized.
void generateSelfStructExtension (CodeBuilder selfStructBody, Reactor reactor, CodeBuilder constructorCode)
 This function is provided to allow extensions of the CGenerator to append the structure of the self struct.
void generateStateVariableInitializations (ReactorInstance instance)
 Generate code that initializes the state variables for a given instance.
String generateTopLevelPreambles (Reactor ignored)
 Override generate top-level preambles, but put the user preambles in the .py file rather than the C file.
void generateUserPreamblesForReactor (Reactor reactor, CodeBuilder src)
 Generate C preambles defined by user for a given reactor Since the Python generator expects preambles written in C, this function is overridden and does nothing.
String getConflictingConnectionsInModalReactorsBody (VarRef sourceRef, VarRef destRef)
PythonDockerGenerator getDockerGenerator (LFGeneratorContext context)
void handleProtoFiles ()
boolean setUpGeneralParameters ()

Package Attributes

String genericActionType = "generic_action_instance_struct"
 Generic struct for actions.
String genericPortType = "generic_port_instance_struct"
 Generic struct for ports with primitive types and statically allocated arrays in Lingua Franca.

Detailed Description

Generator for Python target.

This class generates Python code defining each reactor class given in the input .lf file and imported .lf files.

Each class will contain all the reaction functions defined by the user in order, with the necessary ports/actions given as parameters. Moreover, each class will contain all state variables in native Python format.

A backend is also generated using the CGenerator that interacts with the C code library (see CGenerator.xtend). The backend is responsible for passing arguments to the Python reactor functions.

Author
Soroush Bateni

Constructor & Destructor Documentation

◆ PythonGenerator()

org.lflang.generator.python.PythonGenerator.PythonGenerator ( LFGeneratorContext context)

Member Function Documentation

◆ additionalPostProcessingForModes()

void org.lflang.generator.python.PythonGenerator.additionalPostProcessingForModes ( )
protected

◆ copyTargetFiles()

void org.lflang.generator.python.PythonGenerator.copyTargetFiles ( ) throws IOException
protected

Copy Python specific target code to the src-gen directory.

◆ doGenerate()

void org.lflang.generator.python.PythonGenerator.doGenerate ( Resource resource,
LFGeneratorContext context )

Generate C code from the Lingua Franca model contained by the specified resource.

This is the main entry point for code generation.

Parameters
resourceThe resource containing the source code.
contextContext relating to invocation of the code generator.

◆ generateAuxiliaryStructs()

void org.lflang.generator.python.PythonGenerator.generateAuxiliaryStructs ( CodeBuilder builder,
TypeParameterizedReactor tpr,
boolean userFacing )

Generate the aliases for inputs, outputs, and struct type definitions for actions of the specified reactor in the specified federate.

Parameters
builderThe code builder.
tprThe concrete reactor class.
userFacingWhether this is user-facing code.

◆ generateDirectives()

String org.lflang.generator.python.PythonGenerator.generateDirectives ( )

Generate code that needs to appear at the top of the generated C file, such as #define and #include statements.

◆ generateMethods()

void org.lflang.generator.python.PythonGenerator.generateMethods ( CodeBuilder src,
TypeParameterizedReactor reactor )
protected

Do nothing.

Methods are generated in Python not C.

See also
PythonMethodGenerator

◆ generateParameterInitialization()

void org.lflang.generator.python.PythonGenerator.generateParameterInitialization ( ReactorInstance instance)
protected

Generate runtime initialization code in C for parameters of a given reactor instance.

Parameters
instanceThe reactor instance.

◆ generatePythonCode()

String org.lflang.generator.python.PythonGenerator.generatePythonCode ( String pyModuleName)

Generate the Python code constructed from reactor classes and user-written classes.

Returns
the code body

◆ generatePythonFiles()

Map< Path, CodeMap > org.lflang.generator.python.PythonGenerator.generatePythonFiles ( String lfModuleName,
String pyModuleName,
String pyFileName ) throws IOException

Generate the necessary Python files.

◆ generatePythonReactorClasses()

String org.lflang.generator.python.PythonGenerator.generatePythonReactorClasses ( )

Generate all Python classes if they have a reaction.

◆ generateReaction()

void org.lflang.generator.python.PythonGenerator.generateReaction ( CodeBuilder src,
Reaction reaction,
TypeParameterizedReactor tpr,
int reactionIndex )
protected

Generate a reaction function definition for a reactor.

This function has a single argument that is a void* pointing to a struct that contains parameters, state variables, inputs (triggering or not), actions (triggering or produced), and outputs.

Parameters
srcThe code builder.
reactionThe reaction.
tprThe reactor.
reactionIndexThe position of the reaction within the reactor.

◆ generateReactorClassHeaders()

void org.lflang.generator.python.PythonGenerator.generateReactorClassHeaders ( TypeParameterizedReactor tpr,
String headerName,
CodeBuilder header,
CodeBuilder src )
protected

◆ generateReactorInstanceExtension()

void org.lflang.generator.python.PythonGenerator.generateReactorInstanceExtension ( ReactorInstance instance)
protected

Generate code that is executed while the reactor instance is being initialized.

This wraps the reaction functions in a Python function.

Parameters
instanceThe reactor instance.

◆ generateSelfStructExtension()

void org.lflang.generator.python.PythonGenerator.generateSelfStructExtension ( CodeBuilder selfStructBody,
Reactor reactor,
CodeBuilder constructorCode )
protected

This function is provided to allow extensions of the CGenerator to append the structure of the self struct.

Parameters
selfStructBodyThe body of the self struct
reactorThe reactor declaration for the self struct
constructorCodeCode that is executed when the reactor is instantiated

◆ generateStateVariableInitializations()

void org.lflang.generator.python.PythonGenerator.generateStateVariableInitializations ( ReactorInstance instance)
protected

Generate code that initializes the state variables for a given instance.

Unlike parameters, state variables are uniformly initialized for all instances of the same reactor. This task is left to Python code to allow for more liberal state variable assignments.

Parameters
instanceThe reactor class instance

◆ generateTopLevelPreambles()

String org.lflang.generator.python.PythonGenerator.generateTopLevelPreambles ( Reactor ignored)
protected

Override generate top-level preambles, but put the user preambles in the .py file rather than the C file.

Also handles including the federated execution setup preamble specified in the target config.

◆ generateUserPreamblesForReactor()

void org.lflang.generator.python.PythonGenerator.generateUserPreamblesForReactor ( Reactor reactor,
CodeBuilder src )
protected

Generate C preambles defined by user for a given reactor Since the Python generator expects preambles written in C, this function is overridden and does nothing.

Parameters
reactorThe given reactor
srcThe code builder.

◆ getCmakeCode()

String org.lflang.generator.python.PythonGenerator.getCmakeCode ( boolean hasMain,
String executableName,
Stream< String > cSources )

◆ getConflictingConnectionsInModalReactorsBody()

String org.lflang.generator.python.PythonGenerator.getConflictingConnectionsInModalReactorsBody ( VarRef sourceRef,
VarRef destRef )
protected

◆ getDockerGenerator()

PythonDockerGenerator org.lflang.generator.python.PythonGenerator.getDockerGenerator ( LFGeneratorContext context)
protected

◆ getTarget()

Target org.lflang.generator.python.PythonGenerator.getTarget ( )

Returns the Target enum for this generator.

◆ getTargetTypes()

TargetTypes org.lflang.generator.python.PythonGenerator.getTargetTypes ( )

◆ handleProtoFiles()

void org.lflang.generator.python.PythonGenerator.handleProtoFiles ( )
protected

◆ isOSCompatible()

boolean org.lflang.generator.python.PythonGenerator.isOSCompatible ( )

Return true if the host operating system is compatible and otherwise report an error and return false.

◆ processProtoFile()

void org.lflang.generator.python.PythonGenerator.processProtoFile ( String filename)

Process a given .proto file.

Run, if possible, the proto-c protocol buffer code generator to produce the required .h and .c files.

Parameters
filenameName of the file to process.

◆ setUpGeneralParameters()

boolean org.lflang.generator.python.PythonGenerator.setUpGeneralParameters ( )
protected

Member Data Documentation

◆ genericActionType

String org.lflang.generator.python.PythonGenerator.genericActionType = "generic_action_instance_struct"
package

Generic struct for actions.

This template is defined as typedef struct { trigger_t* trigger; PyObject* value; bool is_present; bool has_value; lf_token_t* token; FEDERATED_CAPSULE_EXTENSION } generic_action_instance_struct;

See reactor-c/python/lib/pythontarget.h for details.

◆ genericPortType

String org.lflang.generator.python.PythonGenerator.genericPortType = "generic_port_instance_struct"
package

Generic struct for ports with primitive types and statically allocated arrays in Lingua Franca.

This template is defined as typedef struct { bool is_present; lf_sparse_io_record_t* sparse_record; // NULL if there is no sparse record. int destination_channel; // -1 if there is no destination. PyObject* value; int num_destinations; lf_token_t* token; int length; void (destructor) (void value); void* (copy_constructor) (void value); FEDERATED_GENERIC_EXTENSION } generic_port_instance_struct;

See reactor-c/python/lib/pythontarget.h for details.


The documentation for this class was generated from the following file:
  • /Users/runner/work/lingua-franca/lingua-franca/core/src/main/java/org/lflang/generator/python/PythonGenerator.java