Michael Doorley writes: > Hi Christopher > > Thanks for adding me to the list. > > > > 1. Can I use Ptolemy II by calling methods in the API that would > > > create a model, populate it, run a simulation and extract the results? > > > As a bonus for the advanced user I would like to save the model as an > > > XML file that could be opened in Ptolemy II GUI tools (e.g. Vergil), > > > but that is not a priority. > > > > Yes. See ptolemy/actor/gui/MoMLSimpleApplication.java and > > ptolemy/actor/gui/PtExecuteApplication.java, which both instantiate and > > run > > a model. > > > > I have looked at MoMLSimpleApplication.java and my problem with it is that > it seems to require a file as input. I am looking for a solution where I > can create and populate model in memory directly from my program. The > closest thing I have found is the Dining Philosophers demo of the CSP > domain, which does not seem to have a file dependency, unless I am missing > it. > > I am currently trying build an equivalent of that program for DE. Do you > know if any similar sample already exists? > > Thanks for your help, > Michael
Sorry, I thought you wanted to _read_ a MoML file. You could do something similar to how the ptII/ptolemy/domains/ct/demo/Helicopter/Helicopter.java works. You could create a similar model with domain polymorphic or DE actors and using the DE Director Note that if you call System.out.println(exportMoML()); from within a TypedCompositeActor, then you will get the MoML output. See also: ct/demo/Lorenz/Lorenz.java ct/demo/SigmaDelta/SigmaDelta.java ct/demo/SquareWave/SquareWave.java ct/demo/StickyMasses/StickyMasses.java ct/demo/Thermostat/Thermostat.java sdf/demo/Butterfly/Butterfly.java For example, to run the last demo, you would do java -classpath ptolemy.actor.gui.CompositeActorApplication -class ptolemy.domains.sdf.demo.Butterfly.Butterfly The shallow code generator will read in certain models and generate java files. (See http://ptolemy.eecs.berkeley.edu/ptolemyII/ptIIlatest/ptII/doc/codegen.htm) Below I ran the shallow code generator on ptII/ptolemy/domains/de/lib/test/auto/Queue.xml and added "throws" lines so that it would compile The file is in the ptolemy/copernicus/shallow/cg/Queue directory, so it is in the ptolemy.copernicus.shallow.cg.Queue package. [EMAIL PROTECTED] 66% pwd /vol/maury/maury2/cxh/ptII/ptolemy/copernicus/shallow/cg/Queue [EMAIL PROTECTED] 67% cat CGQueue.java /* CGQueue - Decompiled by JODE * Visit http://jode.sourceforge.net/ */ package ptolemy.copernicus.shallow.cg.Queue; import ptolemy.actor.IOPort; import ptolemy.actor.TypedCompositeActor; import ptolemy.actor.TypedIOPort; import ptolemy.actor.TypedIORelation; import ptolemy.actor.gui.LocationAttribute; import ptolemy.actor.gui.SizeAttribute; import ptolemy.actor.lib.Clock; import ptolemy.actor.lib.Test; import ptolemy.data.expr.Parameter; import ptolemy.domains.de.kernel.DEDirector; import ptolemy.domains.de.kernel.DEIOPort; import ptolemy.domains.de.lib.Queue; import ptolemy.kernel.ComponentPort; import ptolemy.kernel.CompositeEntity; import ptolemy.kernel.attributes.VersionAttribute; import ptolemy.kernel.util.Settable; import ptolemy.kernel.util.Workspace; import ptolemy.kernel.util.IllegalActionException; import ptolemy.kernel.util.NameDuplicationException; import ptolemy.moml.Documentation; import ptolemy.moml.Vertex; public class CGQueue extends TypedCompositeActor { public VersionAttribute A_createdBy; public SizeAttribute A_vergilSize; public LocationAttribute A_vergilLocation; public DEDirector ADE; public Parameter ADE_startTime; public Parameter ADE_stopTime; public Parameter ADE_stopWhenQueueIsEmpty; public Parameter ADE_synchronizeToRealTime; public Parameter ADE_isCQAdaptive; public Parameter ADE_minBinCount; public Parameter ADE_binCountFactor; public ComponentPort PQueue_input; public ComponentPort PQueue_output; public ComponentPort PQueue_trigger; public ComponentPort PClock_output; public ComponentPort PClock_trigger; public Parameter AClock_stopTime; public Parameter AClock_period; public Parameter AClock_offsets; public Parameter AClock_values; public Parameter AClock_numberOfCycles; public Documentation AClock__doc; public ComponentPort PClock2_output; public ComponentPort PClock2_trigger; public Parameter AClock2_stopTime; public Parameter AClock2_period; public Parameter AClock2_offsets; public Parameter AClock2_values; public Parameter AClock2_numberOfCycles; public Documentation AClock2__doc; public ComponentPort PTest_input; public ComponentPort PTest_output; public Parameter ATest_correctValues; public Parameter ATest_tolerance; public Parameter ATest_trainingMode; public Vertex Arelation5_vertex1; public CGQueue(Workspace workspace) throws IllegalActionException, NameDuplicationException { super(workspace); __CGInit(); } public CGQueue(CompositeEntity compositeentity, String string) throws IllegalActionException, NameDuplicationException { super(compositeentity, string); __CGInit(); } public CGQueue() throws IllegalActionException, NameDuplicationException { __CGInit(); } public void __CGInit() throws IllegalActionException, NameDuplicationException { VersionAttribute versionattribute = new VersionAttribute(this, "_createdBy"); A_createdBy = versionattribute; Settable settable = versionattribute; settable.setExpression("2.1-devel-2"); settable.validate(); SizeAttribute sizeattribute = new SizeAttribute(this, "_vergilSize"); A_vergilSize = sizeattribute; Settable settable_0_ = sizeattribute; settable_0_.setExpression("[604, 454]"); settable_0_.validate(); LocationAttribute locationattribute = new LocationAttribute(this, "_vergilLocation"); A_vergilLocation = locationattribute; settable_0_ = locationattribute; settable_0_.setExpression("[102, 100]"); settable_0_.validate(); DEDirector dedirector = new DEDirector(this, "DE"); ADE = dedirector; ptolemy.kernel.util.Attribute attribute = this.getAttribute("DE.startTime"); Parameter parameter = (Parameter) attribute; ADE_startTime = parameter; settable = (Settable) attribute; settable.setExpression("0.0"); settable.validate(); ptolemy.kernel.util.Attribute attribute_1_ = this.getAttribute("DE.stopTime"); parameter = (Parameter) attribute_1_; ADE_stopTime = parameter; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("10.0"); settable_0_.validate(); attribute_1_ = this.getAttribute("DE.stopWhenQueueIsEmpty"); parameter = (Parameter) attribute_1_; ADE_stopWhenQueueIsEmpty = parameter; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("true"); settable_0_.validate(); attribute_1_ = this.getAttribute("DE.synchronizeToRealTime"); parameter = (Parameter) attribute_1_; ADE_synchronizeToRealTime = parameter; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("false"); settable_0_.validate(); attribute_1_ = this.getAttribute("DE.isCQAdaptive"); parameter = (Parameter) attribute_1_; ADE_isCQAdaptive = parameter; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("true"); settable_0_.validate(); attribute_1_ = this.getAttribute("DE.minBinCount"); parameter = (Parameter) attribute_1_; ADE_minBinCount = parameter; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("2"); settable_0_.validate(); attribute_1_ = this.getAttribute("DE.binCountFactor"); parameter = (Parameter) attribute_1_; ADE_binCountFactor = parameter; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("2"); settable_0_.validate(); Queue queue = new Queue(this, "Queue"); ptolemy.kernel.Port port = queue.getPort("input"); IOPort ioport = (IOPort) port; ioport.setInput(true); DEIOPort deioport = (DEIOPort) port; PQueue_input = deioport; ptolemy.kernel.Port port_2_ = queue.getPort("output"); ioport = (IOPort) port_2_; ioport.setOutput(true); DEIOPort deioport_3_ = (DEIOPort) port_2_; PQueue_output = deioport_3_; port_2_ = queue.getPort("trigger"); ioport = (IOPort) port_2_; ioport.setInput(true); TypedIOPort typedioport = (TypedIOPort) port_2_; PQueue_trigger = typedioport; Clock clock = new Clock(this, "Clock"); ptolemy.kernel.Port port_4_ = clock.getPort("output"); ioport = (IOPort) port_4_; ioport.setOutput(true); TypedIOPort typedioport_5_ = (TypedIOPort) port_4_; PClock_output = typedioport_5_; port_2_ = clock.getPort("trigger"); ioport = (IOPort) port_2_; ioport.setInput(true); ioport.setMultiport(true); TypedIOPort typedioport_6_ = (TypedIOPort) port_2_; PClock_trigger = typedioport_6_; ptolemy.kernel.util.Attribute attribute_7_ = this.getAttribute("Clock.stopTime"); Parameter parameter_8_ = (Parameter) attribute_7_; AClock_stopTime = parameter_8_; Settable settable_9_ = (Settable) attribute_7_; settable_9_.setExpression("0.0"); settable_9_.validate(); attribute_1_ = this.getAttribute("Clock.period"); parameter_8_ = (Parameter) attribute_1_; AClock_period = parameter_8_; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("1.0"); settable_0_.validate(); attribute_1_ = this.getAttribute("Clock.offsets"); parameter_8_ = (Parameter) attribute_1_; AClock_offsets = parameter_8_; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("{0.0, 0.25, 0.75}"); settable_0_.validate(); attribute_1_ = this.getAttribute("Clock.values"); parameter_8_ = (Parameter) attribute_1_; AClock_values = parameter_8_; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("{1.0, 0.5, 0.0}"); settable_0_.validate(); attribute_1_ = this.getAttribute("Clock.numberOfCycles"); parameter_8_ = (Parameter) attribute_1_; AClock_numberOfCycles = parameter_8_; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("-1"); settable_0_.validate(); Documentation documentation = new Documentation(clock, "_doc"); AClock__doc = documentation; settable_0_ = documentation; settable_0_.setExpression("Create a generalized square wave"); settable_0_.validate(); Clock clock_10_ = new Clock(this, "Clock2"); ptolemy.kernel.Port port_11_ = clock_10_.getPort("output"); IOPort ioport_12_ = (IOPort) port_11_; ioport_12_.setOutput(true); TypedIOPort typedioport_13_ = (TypedIOPort) port_11_; PClock2_output = typedioport_13_; port_2_ = clock_10_.getPort("trigger"); IOPort ioport_14_ = (IOPort) port_2_; ioport_14_.setInput(true); ioport_14_.setMultiport(true); typedioport_6_ = (TypedIOPort) port_2_; PClock2_trigger = typedioport_6_; attribute_7_ = this.getAttribute("Clock2.stopTime"); Parameter parameter_15_ = (Parameter) attribute_7_; AClock2_stopTime = parameter_15_; settable_9_ = (Settable) attribute_7_; settable_9_.setExpression("0.0"); settable_9_.validate(); attribute_1_ = this.getAttribute("Clock2.period"); parameter_15_ = (Parameter) attribute_1_; AClock2_period = parameter_15_; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("2.0"); settable_0_.validate(); attribute_1_ = this.getAttribute("Clock2.offsets"); parameter_15_ = (Parameter) attribute_1_; AClock2_offsets = parameter_15_; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("{0.0, 1.0}"); settable_0_.validate(); attribute_1_ = this.getAttribute("Clock2.values"); parameter_15_ = (Parameter) attribute_1_; AClock2_values = parameter_15_; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("{1, 0}"); settable_0_.validate(); attribute_1_ = this.getAttribute("Clock2.numberOfCycles"); parameter_15_ = (Parameter) attribute_1_; AClock2_numberOfCycles = parameter_15_; settable_0_ = (Settable) attribute_1_; settable_0_.setExpression("-1"); settable_0_.validate(); Documentation documentation_16_ = new Documentation(clock_10_, "_doc"); AClock2__doc = documentation_16_; settable_0_ = documentation_16_; settable_0_.setExpression("Create a generalized square wave"); settable_0_.validate(); Test test = new Test(this, "Test"); ptolemy.kernel.Port port_17_ = test.getPort("input"); ioport_12_ = (IOPort) port_17_; ioport_12_.setInput(true); ioport_12_.setMultiport(true); TypedIOPort typedioport_18_ = (TypedIOPort) port_17_; PTest_input = typedioport_18_; port_2_ = test.getPort("output"); ioport = (IOPort) port_2_; ioport.setOutput(true); typedioport_6_ = (TypedIOPort) port_2_; PTest_output = typedioport_6_; attribute_7_ = this.getAttribute("Test.correctValues"); Parameter parameter_19_ = (Parameter) attribute_7_; ATest_correctValues = parameter_19_; settable_9_ = (Settable) attribute_7_; settable_9_.setExpression("{1.0,0.5, 0.0, 1.0, 0.5, 0.0}"); settable_9_.validate(); attribute_7_ = this.getAttribute("Test.tolerance"); parameter_19_ = (Parameter) attribute_7_; ATest_tolerance = parameter_19_; settable_0_ = (Settable) attribute_7_; settable_0_.setExpression("1.0E-9"); settable_0_.validate(); attribute_7_ = this.getAttribute("Test.trainingMode"); parameter_19_ = (Parameter) attribute_7_; ATest_trainingMode = parameter_19_; settable_0_ = (Settable) attribute_7_; settable_0_.setExpression("false"); settable_0_.validate(); TypedIORelation typediorelation = new TypedIORelation(this, "relation3"); TypedIORelation typediorelation_20_ = new TypedIORelation(this, "relation5"); Vertex vertex = new Vertex(typediorelation_20_, "vertex1"); Arelation5_vertex1 = vertex; settable_0_ = vertex; settable_0_.setExpression("162.0, 213.0"); settable_0_.validate(); TypedIORelation typediorelation_21_ = new TypedIORelation(this, "relation"); deioport.insertLink(0, typediorelation_20_); deioport_3_.insertLink(0, typediorelation_21_); typedioport.insertLink(0, typediorelation); typedioport_5_.insertLink(0, typediorelation_20_); typedioport_13_.insertLink(0, typediorelation); typedioport_18_.insertLink(0, typediorelation_21_); } } [EMAIL PROTECTED] 68% javac -classpath $PTII CGQueue.java [EMAIL PROTECTED] 69% java -classpath ${PTII}:. ptolemy.actor.gui.CompositeActorApplication -class ptolemy.copernicus.shallow.cg.Queue.CGQueue 197 ms. Memory: 3520K Free: 1623K (46%) [EMAIL PROTECTED] 70% All the demo does is run and compare the output against some known good values. Obviously, the code is ugly because it is auto generated, but you can see that we create a DE director and some actors and connect them. -Christopher ---------------------------------------------------------------------------- Posted to the ptolemy-hackers mailing list. Please send administrative mail for this list to: [EMAIL PROTECTED]