Table of Contents
Let’s illustrate those concepts by defining another simple object: adder. This object has one variable v, and a method ’add’ that returns the sum of this variable and its argument.
First you need to import the class of the type used by our UObject:
import liburbi.main.UObjectJava; import liburbi.main.UVar; import liburbi.main.UValue;
Then implement the Adder class:
public class Adder extends UObjectJava // must extends UObjectJava
{
/// Declare a variable v that will be accessible in Urbi
private UVar v = new UVar ();
/// the class must have a single constructor taking a string
public Adder (String s) {
super (s);
try
{
/// Bind the variable v to Urbi
UBindVar (v, "v");
/// Initialise our UVar v to some value
/// (we choose 42 :)
v.set(42);
/// Bind the function add to Urbi
UBindFunction (this, "add");
}
catch (Exception e)
{
System.out.println (e);
}
}
/// The binded function can take no other type than UValue as
/// parameters.
public double add (UValue rhs) {
/// Convert the UValue to a double
double drhs = rhs.getDouble ();
/// Return the value of our UVar v (converted to double)
/// plus the value of the argument of the function.
return v.getDouble () + drhs;
}
}
To bind the variables to Urbi, we use the function:
void UBindVar (UVar v, String name)
This function take as parameter the UVar variables, and the name of the UVar (because Urbi need to know what is the name of your variable). Once your variable is binded with UBindVar it will be accessible in Urbi.
To bind the functions to Urbi, you can use:
void UBindFunction (Object obj, String method_name, String[] parameters_name)
or the convenient version:
void UBindFunction (Object obj, String method_name)
The first function takes as parameter the object containing the function (for now it is only possible to bind instance method, we do not handle static methods). The second parameter is the name of the function you want to bind. The third parameter is a list of the names if the types of the arguments. For example for the function add, in the Adder example, we could have used:
String[] params = { "liburbi.main.UValue" };
UBindFunction (this, "add", params);
If in your UObject you only have one method with the name 'method_name', then you can use the second function, which is a convenient version of the first one.
NB: For now, the functions you can bind must follow these rules:
And finally start your UObject by adding UObjectJava.UStart (Adder.class) in your main:
import liburbi.main.*;
public class Main {
/// load urbijava library (see explanations in next section)
static {
System.loadLibrary("urbijava");
}
public static void main(String argv[]) {
try
{
UObjectJava.UStart (Adder.class);
/// Call our main ('main' function in UObjectJava class)
UObjectJava.main (argv);
}
catch (Exception e)
{
System.out.println (e);
}
}
}
Now you can run your UObject and test it in Urbi:
[07670270:start] *** ********************************************************** [07670270:start] *** URBI Language specif 1.0 - Copyright (C) 2005-2008 Gostai SAS [07670270:start] *** URBI Kernel version 1.5 rev. 785a811 [07670270:start] *** [07670270:start] *** URBI Engine version 1.5 rev. 1cf8ce9 [07670270:start] *** (C) 2006-2007 Gostai SAS [07670270:start] *** [07670270:start] *** URBI comes with ABSOLUTELY NO WARRANTY; [07670270:start] *** This software can be used under certain conditions; [07670270:start] *** see LICENSE file for details. [07670270:start] *** [07670270:start] *** See http://www.urbiforge.com for news and updates. [07670270:start] *** ********************************************************** [07670270:ident] *** ID: U135753752 Adder, [07845030] OBJ [v:42,load:1] Adder.v, [07852341] 42 Adder.add (-12), [07866436] 30 Adder.add (-1.2), [07876488] 40.8
To summarize:
Declare your object class as extending UObjectJava.
Declare a single constructor taking a String, and pass this string to the constructor of UObjectJava.
Declare the variables you want to share with Urbi with the type liburbi.main.UVar.
In the constructor, call UBindVar for each UVar you want as an instance variable, and UBindFunction for each function you want to bind.
Don’t forget to call the function UObjectJava.UStart for each UObject in your main class.