[kepler-users] IterateOverArray - Type conversion problem

Christopher Brooks cxh at eecs.berkeley.edu
Thu Jan 12 10:28:58 PST 2012


Hi Muhannad,
I'm slightly confused, but attached is some overly complex code that 
implements the model along with the steps I used to create that code.

You included a model called IterateOverArray.xml and you are trying to 
write Java code (HelloWorld.java) that replicates the functionality of 
that model, right?

However, you did not include the model that runs your java code.
The stack trace below is from a test run of a model that uses your 
HelloWorld.java,

I would need to see your test driver to get much further, but attached 
is some code that could help you.

Ptolemy II does have a code generator that generates Java code that 
constructs a TypedComposite.
This code generator is called "copernicus".  See the Ptolemy II faq for 
details about the code generators.
http://ptolemy.berkeley.edu/ptolemyII/ptIIfaq.htm#CodeGen

To follow the steps below, you would need to work from a Ptolemy II tree.
However, you don't have to run copernicus, you could just look at the 
.java file that is attached and
not actually *do* the steps below.

Under Mac OS X, to create the $PTII/bin/copernicus shell script, these 
steps might work:
   cd kepler/ptII/src
   export PTII=`pwd`
   ./configure
   ant
   cd bin; make

I was able to generate Java code that calls the Ptolemy methods for your 
model, though it was not easy.

1) To simplify the output, I stripped out the graphical xml attributes 
of the model.  I did this by running:
java -classpath $PTII ptolemy.moml.filter.RemoveGraphicalClasses 
IterateOverArray.xml > ISimple.xml

2) I then edited the model and removed a bunch of Kepler specific 
attributes.
There is a significant bug in Kepler where the documentation is 
replicated for each actor, see
http://bugzilla.ecoinformatics.org/show_bug.cgi?id=4168

3) Eventually, I ended up with ISimple.xml, which is attached.

4) I ran the copernicus code generator with
   $PTII/bin/copernicus -shallow ISimple.xml
This created $PTII/ptolemy/copernicus/shallow/cg/ISimple, which contains 
a .class file for the model.

5) I changed to that directory and ran:
make jodeDecompile

6) That created a CGISimple.java file, which I then edited.
The issue is that the copernicus code generator seems to change Display 
actors to Discard actors.
Probably the RemoveGraphicalClassesFilter is running.

My solution was to edit CGISimple.java and change
   import ptolemy.actor.lib.Discard;
to
   import ptolemy.actor.lib.gui.Display;

and
  Discard discard = new Discard(this, "Display2");
to
  Display discard = new Display(this, "Display2");

See the attached CGISimple.java file for the code.
Unfortunately, it is difficult to see the forest for the trees because I 
believe that the .xml file had many of the properties for the actors 
set, and/or the copernicus code generator just blindly creates code to 
instantiate everything.

Anyway, it is difficult to see what to do, but it should give you an idea.

7) I then recompiled the code.  Unfortunately, the makefile has a bug
  javac -classpath $PTII:. ISimple/CGISimple.java

8) I then ran the code:
  java -classpath .:${PTvII} ptolemy.actor.gui.CompositeActorApplication 
-class ISimple.CGISimple

_Christopher

On 1/12/12 9:14 AM, Ali, Muhannad wrote:
>
> Hello,
>
> I need to develop a composite actor (programmatically) using eclipse. 
> Before I code it, I designed it on Kepler canvas (see file 
> IterateOverArray.xml attached) in order to test the functionality and 
> then encapsulate it in my own composite actor. When I designed it, it 
> worked perfectly but when I coded it and tried to run the program .. I 
> always get the following error:
>
> "
> ptolemy.kernel.util.IllegalActionException: Conversion is not 
> supported from ptolemy.data.ArrayToken '{"1 - 1"}' to the type string 
> because the type of the token is higher or incomparable with the given 
> type.
> at ptolemy.data.StringToken.convert(StringToken.java:172)
> at ptolemy.data.type.BaseType$StringType.convert(BaseType.java:615)
> at ptolemy.data.ArrayToken.<init>(ArrayToken.java:151)
> at 
> ptolemy.actor.lib.hoc.IterateOverArray$IterateDirector.transferOutputs(IterateOverArray.java:706)
> at ptolemy.actor.CompositeActor.fire(CompositeActor.java:469)
> at ptolemy.actor.CompositeActor.iterate(CompositeActor.java:1069)
> at 
> ptolemy.actor.sched.StaticSchedulingDirector.fire(StaticSchedulingDirector.java:188)
> at ptolemy.actor.CompositeActor.fire(CompositeActor.java:458)
> at ptolemy.actor.Manager.iterate(Manager.java:742)
> at ptolemy.actor.Manager.execute(Manager.java:351)
> at ptolemy.actor.Manager.run(Manager.java:1111)
> at ptolemy.actor.Manager$PtolemyRunThread.run(Manager.java:1641
> "
>
> My code is:
>
> "
> package org.helloworld;
>
> import ptolemy.actor.TypedCompositeActor;
> import ptolemy.actor.TypedIOPort;
> import ptolemy.actor.TypedIORelation;
> import ptolemy.actor.lib.ElementsToArray;
> import ptolemy.actor.lib.Expression;
> import ptolemy.actor.lib.hoc.IterateOverArray;
> import ptolemy.actor.lib.hoc.MirrorPort;
> import ptolemy.kernel.CompositeEntity;
> import ptolemy.kernel.util.IllegalActionException;
> import ptolemy.kernel.util.NameDuplicationException;
>
> public class HelloWorld extends TypedCompositeActor {
>
> public HelloWorld(CompositeEntity container, String name)
> throws NameDuplicationException, IllegalActionException {
> super(container, name);
>
>
> params = new TypedIOPort(this, "params", true, false);
> params.setMultiport(true);
>
>
> output = new TypedIOPort(this, "output", false, true);
>
>
> elems2Arr = new ElementsToArray(this, "elems2Arr");
> relation1 = new TypedIORelation(this, "realtion1");
> params.link(relation1);
> elems2Arr.input.link(relation1);
>
>
> iter = new IterateOverArray(this, "iter");
> mpInput = new MirrorPort(iter, "input"); mpInput.setInput(true);
> mpOutput = new MirrorPort(iter, "output"); mpOutput.setOutput(true);
> exp = new Expression(iter, "exp");
> exp.expression.setExpression("input + ' - ' + iterationCount");
> relation2 = new TypedIORelation(this, "realtion2");
> elems2Arr.output.link(relation2);
> iter.getPort("input").link(relation2);
>
>
> relation3 = new TypedIORelation(this, "realtion3");
> iter.getPort("output").link(relation3);
> output.link(relation3);
> }
>
>
> public TypedIOPort params;
> public TypedIOPort output;
>
> private MirrorPort mpInput;
> private MirrorPort mpOutput;
>
>
> private ElementsToArray elems2Arr;
> private IterateOverArray iter;
> private Expression exp;
>
> private TypedIORelation relation1;
> private TypedIORelation relation2;
> private TypedIORelation relation3;
>
>
> @Override
> public void fire() throws IllegalActionException {
> super.fire();
> }
>
>
> }
> "
>
>
> I guess the problem is related to the director inside IterateOverArray 
> actor .. any help please?
>
>
> Thank you!
>
> Muhannad Ali
>
>
>
>
> _______________________________________________
> Kepler-users mailing list
> Kepler-users at kepler-project.org
> http://lists.nceas.ucsb.edu/kepler/mailman/listinfo/kepler-users

-- 
Christopher Brooks, PMP                       University of California
CHESS Executive Director                      US Mail: 337 Cory Hall
Programmer/Analyst CHESS/Ptolemy/Trust        Berkeley, CA 94720-1774
ph: 510.643.9841                                (Office: 545Q Cory)
home: (F-Tu) 707.665.0131 cell: 707.332.0670

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nceas.ucsb.edu/kepler/pipermail/kepler-users/attachments/20120112/40be38cc/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ISimple.xml
Type: text/xml
Size: 5926 bytes
Desc: not available
URL: <http://lists.nceas.ucsb.edu/kepler/pipermail/kepler-users/attachments/20120112/40be38cc/attachment-0001.xml>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CGISimple.java
URL: <http://lists.nceas.ucsb.edu/kepler/pipermail/kepler-users/attachments/20120112/40be38cc/attachment-0001.ksh>


More information about the Kepler-users mailing list