- Use Kepler directory tree and create a directory for yourself under
kepler/src/edu/tutorial
- Copy HelloWorld.java under your directory and open it with your java editor.
- Add this file to your package:
package edu.tutorial.%YOUR_DIRECTORY%;
- Import the necessary classes to add and maintain an output port
import ptolemy.actor.IOPort;
import ptolemy.actor.TypedIOPort;
import ptolemy.data.Token;
import ptolemy.data.StringToken;
import ptolemy.data.type.BaseType;
import ptolemy.data.type.Type;
- Define an output port to send your greeting from. Add the following code segment to the place in the code shown by "// Exercise#1: ADD THE OUTPUT PORT DEFINITION HERE."
/** The output port. The type of this port will be set to String.
*/
public TypedIOPort output = null;
- Create the output and set type. Add the follwing 3 lines to the class constructor.
output = new TypedIOPort(this, "output", false, true);
// Set the type constraint.
output.setTypeEquals(BaseType.STRING);
- Send the greeting message from your output port. Add to fire method:
output.send(0, new StringToken("HelloWorld!"));
- Comment your code...
- Add the class description at the place shown in the file and write your name as the author. As class desription, add:
"This is the implementation of a HelloWorld actor using Ptolemy II. This actor outputs "Hello World!" as string from its output port."
- Add the javadoc description for the class constructor at the place shown in the file.
/** Construct a HelloWorld source with the given container and name.
* @param name The name of this actor.
* @exception IllegalActionException If the entity cannot be contained
* by the proposed container.
* @exception NameDuplicationException If the container already has an
* actor with this name.
*/
- Add the javadoc description for the fire method at the place shown in the file
/** Send the token in the value parameter to the output.
* @exception IllegalActionException If it is thrown by the
* send() method sending out the token.
*/
- Compile your code, using "ant compile" comment under $KEPLER
- If the compilation has finished successfully, you will be able to run it under Vergil after just one more step.
- Add your actor as an entity under the kepler/tutorial library. Open the file "$KEPLER/lib/tutorial.xml"
Update the file in the shown place by:
Don't forget to change %YOUR_DIRECTORY% with the directory name you have created under the tutorial directory. :-)
- And now, you can run your actor! :) Run Kepler using "ant run" comment.
- Under Vergil, open a new graph editor using the file menu.
- You will see your actor and a 'Display' actor under actor "library/kepler/tutorial".
- Create a mini-workflow using these two actors. Link HelloWorld to Display.
- Drag and drop an SDF director from the director library and configure it to iterate just once. (Set iterations==1)
- Save your workflow with the name "HelloWorldTest.xml" under "$KEPLER/lib". Run your workflow.
- Now we will add one more functionality to the actor. We'll add a parameter and use it to greet the user with his/her name.
We need a parameter to accept user's name.
- Import the classes to add and maintain a parameter.
import ptolemy.data.expr.Parameter;
- Define the parameter. Add the parameter to the place shown in HelloWorld.
/** The name of the user.
*/
public Parameter userName;
- Create the parameter in the class constructor. Add the following line to the place shown in the class constructor:
userName = new Parameter(this, "userName", new StringToken(""));
- Send the personalized greeting :) through the output port. Replace the "output.send(...)" line in the fire method by:
String userNameStr = userName.getToken().toString();
userNameStr = userNameStr.substring(1, userNameStr.length() - 1);
output.send(0, new StringToken("Hello " + userNameStr + "!"));
You can also use the following two lines instead of these three lines:
String userNameStr = ((StringToken)userName.getToken()).stringValue();
output.send(0, new StringToken("Hello " + userNameStr + "!"));
Notice that you did not need to remove the double-quotes in this one since you directly got the string value of the string token.
- We are finished with adding the parameter. Compile your code and run it.
- Open the "HelloWorldTest.xml" workflow that you have created using your HelloWorld actor again.
- Double-click on the HelloWorld actor and put your name between double-quotes. Save it and run the workflow. You'll see the message. :)
--- End of exercise-1 ---