[kepler-dev] Re: ObjectToken <--> RecordToken ?

Tobin Fricke tobin at splorg.org
Wed Jun 30 15:22:13 PDT 2004


On Thu, 24 Jun 2004, Tobin Fricke wrote:

> I'm imagining that we could take the object embedded in an ObjectToken and
> produce a record that maps field names to new tokens with the
> corresponding field values.  It would be possible to attempt the inverse,
> too.

I have implemented this, but I am having trouble setting the type
constraint on the output port correctly.  Since the actor simply
transforms object fields to record fields, the fields in the generated
records can in general vary token-to-token.  One way of addressing this
would be to have Classname as a parameter to ObjectToRecord, and have the
ObjectToRecord actor refuse to transform objects that are not of this
type, but it seems to me more useful to have a general record type (?).

 public ObjectToRecord(CompositeEntity container, String name)
	throws NameDuplicationException, IllegalActionException {
	super(container, name);

	input.setTypeEquals(BaseType.OBJECT);
	//output.setTypeAtLeast(new RecordType(new String[0], new Type[0])); // FIXME
    }

 public void fire() throws IllegalActionException {
	super.fire();

        Object o = ((ObjectToken)(input.get(0))).getValue();
        Class c = o.getClass();
        Field[] fields = c.getFields();
        int nFields = Array.getLength(fields);

        String[] labels = new String[nFields];
        ObjectToken[] values = new ObjectToken[nFields];

        for (int i = 0; i < nFields; i++) {
          Field f = (Field)(Array.get(fields, i));
          labels[i] = f.getName();
          try {
            /* Here there is room to do something smart, like convert
               java.lang.String objects into StringTokens. */
	    values[i] = new ObjectToken(f.get(o));
          } catch (IllegalAccessException e) {
            values[i] = new ObjectToken(null);  // not the best approach
          }
        }

> Then I could use generic actors to extract fields and act upon them,
> instead of having specific actors for each class of object I want to
> transport with ObjectTokens.

Screenshot of a small application of this:

	http://mercali.ucsd.edu/~tobin/OrbObjectToRecordDemo.png

Tobin


----------------------------------------------------------------------------
Posted to the ptolemy-hackers mailing list.  Please send administrative
mail for this list to: ptolemy-hackers-request at ptolemy.eecs.berkeley.edu



More information about the Kepler-dev mailing list