Main > Reference Manual > Modeling > Python Interpreter
FrePPle embeds an interpreter for the Python language.
The full capabilities of this scripting language are accessible from frePPLe, and Python also has access to the frePPLe objects in memory.
Python is thus a very powerful way to interact with frePPLe.
Python code can be executed in two ways:
- A XML processing instruction in XML data files.
<?xml version="1.0" encoding="UTF-8" ?> <plan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> ... <?python Your python code goes here. ?> ... </plan>
- Python code in a file init.py, located in one of the frePPLe directories, is executed automatically when frePPLe starts. This provides a clean mechanism to define global Python functions and classes you application needs.
The interpreter is multi-threaded. Multiple python scripts can run in parallel. However, Python internally executes only one thread at a time and the interpreter switches between the active threads.
A single, global interpreter instance is used. A global Python variable or function is thus visible across multiple invocations of the Python interpreter.
A number of Python functions are defined:
- loadmodule dynamically loads an extension module.
- readXMLfile processes a XML-file from the local file system.
- readXMLdata processes a XML-formatted string.
- erase removes part of the model or plan from memory.
- saveXMLfile saves the model to an XML-formatted file.
- saveplan saves the most important plan information to a file.
- printsize prints information about the memory size of the model and other system parameters.
loadmodule
This command dynamically loads an extension module.
Field | Type | Description |
filename | normalizedString |
Name of the shared library file to be loaded.
|
parameter | parameter |
Initialization and configuration values that are passed to the module's initialization routine. |
Example code:
<?python frepple.loadmodule("your_module.so", parameter1="string value", parameter2=100, parameter3=True) ?>
readXMLfile
This command reads and processes a XML-file from the local file system.
Field | Type | Description |
filename | normalizedString | Name of the data file to be loaded. |
validate | boolean |
When set to true, the XML data are validated against the XML-schema. |
Example code:
<?python frepple.readXMLfile("input.xml",True,True) ?>
readXMLdata
This command processes a XML-formatted data string.
Field | Type | Description |
data | string | XML-formatted data to be processed. |
validate | boolean |
When set to true, the XML data are validated against the XML-schema. |
Example code:
<?python frepple.readXMLdata(''' <plan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <locations> <location name="Location 1" action="R"/> </locations> </plan>''',True,True) ?>
erase
Use this command to erase the plan or the entire model from memory.
Field | Type | Description |
mode | boolean |
When set to true the complete model is erased. You will again have a completely empty model. |
Example code:
<?python frepple.erase(False) ?>
saveXMLfile
This commands saves the model into an XML-formatted file.
Field | Type | Description |
filename | normalizedString |
Name of the output file. |
content |
STANDARD |
Controls the level of detail in the output:
|
headerstart | string |
The first line of the XML output. <?xml version="1.0" encoding="UTF-8"?> |
headeratts | string | Predefined attributes of the XML root-element. The default value is: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
Example code:
<?python frepple.saveXMLfile("output.xml") frepple.saveXMLfile("detailedoutput.xml","PLANDETAIL") ?>
saveplan
This command saves the most important plan information to a file.
It is used for the unit tests, but its' usefullness in a real-life implementation is probably limited.
Field | Type | Description |
filename | normalizedString |
Name of the output file. |
Example code:
<?python frepple.saveplan("output.xml") ?>
printsize
This command prints information about the memory size of the model and other sytem parameters.
Example code:
<?python frepple.printsize() ?>