How To Hydrotrend Component

This page needs to be updated: assigned to Eric Hutton
=== Creating a Hydrotrend project (in Python) with CCA ===
I found myself creating a project, screwing up, removing the project and starting again so often, I wrote a script that creates a new hydrotrend project. I called it make_my_hydrotrend_project.sh since that's what it does. I've listed it's contents here:
<geshi lang=bash>
#! /bin/ksh
bocca create project hydrotrend --language=python
cd hydrotrend
bocca create port FilenamePort
bocca create port StartPort
bocca create component Driver --uses=StartPort:model \
 --provides=gov.cca.ports.GoPort:run
bocca create component Hydrotrend --uses=FilenamePort:inputFile \
 --uses=FilenamePort:outputFile \
 --provides=StartPort:model
bocca create component GetFilename --provides=FilenamePort:filename
bocca create component PutFilename --provides=FilenamePort:filename
</geshi>
==== Attach member functions to the new ports ====
Now I edit the SIDL files to declare the member functions of the two ports I've defined (StartPort and FilenamePort). These files are located in ./ports/sidl (relative to the root project directory). To hydrotrend.StartPort.sidl add the line,
<geshi lang=c>
 int start_model();
</geshi>
after the 'Insert-code-here' comment. The FilenamePort has two member functions: one to get the name of a file, and one to provide the name of a file. Again in the sidl diretory, add the following lines to hydrotrendFilenamePort.sidl,
<geshi lang=c>
 int get_filename( out string file );
 int put_filename( in string file );
</geshi>
Now configure and make the project from the root project directory (you may not have to actually make it at this point),
<geshi lang=bash>
./configure ; make
</geshi>
==== Create implementations of port functions ====
We now work in the components directory.
In hydrotrend.Driver/hydrotrend/Driver_Impl.py, add Python code to the 'go' member function:
<geshi lang=python>
 port = self.d_services.getPort( "model" )
 if port==None:
 print "hydrotrend.PythonDriver not connected"
 self.d_services.releasePort( "model" )
 return -1
 hydrotrend.StartPort.StartPort( port ).start_model()
 self.d_services.releasePort( "model" )
 return 0
</geshi>
In hydrotrend.Hydrotrend/hydrotrend/Hydrotrend_Impl.py, add code to start_model:
<geshi lang=python>
 in_port = self.d_services.getPort( "inputFile" )
 out_port = self.d_services.getPort( "outputFile" )
 in_file = hydrotrend.FilenamePort.FilenamePort( in_port ).get_filename( )
 print "Input file is:" , in_file[1]
 hydrotrend.FilenamePort.FilenamePort( out_port ).put_filename( "hydrotrend output file" )
 self.d_services.releasePort( "inputFile" )
 self.d_services.releasePort( "outputFile" )
 return 0
</geshi>
In hydrotrend.GetFilename/hydrotrend/GetFilename_Impl.py, add code to get_filename:
<geshi lang=python>
 return ( 0 , "hydrotrend input file" )
</geshi>
In hydrotrend.PutFilename/hydrotrend/PutFilename_Impl.py, add code to put_filename:
<geshi lang=python>
 print "Output file: hydrotrend output file"
 return 0
</geshi>
Now configure and make the project once more. I find that I need to clean things up first since my changes aren't always noticed by the Makefile (I imaging that this is a bug). From the top project directory,
<geshi lang=bash>
./configure ; make clean ; make
</geshi>
==== Loading the project into ccafeine ====
You may not have to edit these files by hand but I had to. In the utils directory, edit run-gui.sh so that BOCCA_HOME and PROJECT_DRIECTORY are correct. For me, the bocca home directory is located in /usr/local/cca so I set BOCCA_HOME to,
<geshi lang=bash>
BOCCA_HOME=/usr/local/cca
</geshi>
Set PROJECT_DIR to the top level of you project directory
<geshi lang=bash>
PROJECT_DIR=/Users/huttone/bocca/csdms/hydrotrend
</geshi>
That should be it. If you want, you can edit components/test/test_gui_rc so that when you start ccafeine the components are already connected. My test_gui_rc file looks like,
<geshi lang=bash>
#!ccaffeine bootstrap file.
# ------- don't change anything ABOVE this line.-------------
path set /Users/huttone/bocca/csdms/hydrotrend/components/lib
repository get-global hydrotrend.Driver
repository get-global hydrotrend.GetFilename
repository get-global hydrotrend.Hydrotrend
repository get-global hydrotrend.PutFilename
instantiate hydrotrend.Driver Driver
instantiate hydrotrend.GetFilename GetFilename
instantiate hydrotrend.Hydrotrend Hydrotrend
instantiate hydrotrend.PutFilename PutFilename
connect Driver model Hydrotrend model
connect Hydrotrend inputFile GetFilename filename
connect Hydrotrend outputFile PutFilename filename
</geshi>
Now run utils/run-gui.sh.
Note:
run-gui.sh and test_gui_rc are recreated when you configure/make so you'll probably want to create copies so your changes aren't lost.