Starting with breve 2.6, breve simulations can be written using the Python language. This section describes how to use Python with the breve simulation environment. Because Python is a well-established language with excellent documentation and tutorials, this section does not go into much detail on the language itself, and instead focuses on the overall concepts required to write simulations for breve in Python. If you're completely new to Python, you may wish to consult the official Python documentation
The first step in writing breve simulations with Python is to familiarize yourself with thethe section called “Overview to breve's Simulation Structure: For All Frontend Languages”, which describes general information about how simulations are structured, for all frontend languages.
Next, you can take a look at a the section called “Python + breve Example: a basic simulation”, in order to see how the breve simulation structure is used with Python. You can also begin looking at the included demos for real-world examples of how Python is used to write simulations.
Finally, you'll want to explore Chapter 17 to learn how to use the features of the breve engine in your simulations.
There are a couple of requirements when creating Python objects which can be added to the breve engine.
All Python files to be loaded into breve should import the breve module
to gain access to important breve engine functionality. In order for a
Python object to be compatible with the breve engine, it must be a
subclass of the breve Python module class breve.object. If an object is
not a subclass of breve.object, it will not be visible to the breve
engine. The __init__
method of the class must execute the
breve.object.__init__
method before any breve bridge
functionality is used.
The following example illustrates a basic Python file with a simple breve-compatible class defined:
@import breve class PythonBridgeObject( breve.Object ): def __init__( self ): breve.Object.__init__( self ) print "Inited Python bridge object" def iterate( self ): print "Iterating Python bridge object"
This simple Python object will be visible to the breve engine and will have its iterate method called at each iteration of the breve engine.
Here's a simple skeleton of how to write a simulation in breve using Python.
This simulation defines two objects: the controller, and an agent. The controller is a required object, and it must be a subclass of Control. It is what is responsible for initializing the simulation. The agent we create here does nothing, but in a normal simulation, we would create multiple agents and specify their behaviors in the "iterate" method.
import breve class myController( breve.Control ): def __init__( self ): breve.Control.__init__( self ) self.agent = myAgent() print '''simulation started''' def iterate( self ): breve.Control.iterate( self ) class myAgent( breve.Mobile ): def __init__( self ): breve.Mobile.__init__( self ) print '''created agent''' def iterate( self ): # make the agent do something interesting her breve.Control.iterate( self ) # instantiate the controller to initialize the simulation myController()