/* * Title: TestArguments * Version: 0.1 * Copyright: Copyright (c) 2001 * Author: Sven Thommesen * Description: Sample app to show parsing of command line arguments. * Example 5 of 5. */ /* * This example demonstrates another way to extract command line data * directly from the command line, using some of them to set local variables * in the BatchSwarm, and putting data into an application parameter database * which can be passed around for objects in the simulation to access. * * It differs from Example 3 in that: * a) the '-Dkey=value' arguments are specified on the command line * AFTER the name of the program and FOLLOWING ' -- '. * b) no '%' characters are needed after '-D' * * The example consists of 3 parts (objects), named TestArgsMain.java, * TestArgsBatchSwarm.java, and TestArgsModelSwarm.java. Cut the relevant * sections from the file below and create the 3 named files, then compile * the application: * * $ javacswarm TestArgsMain.java * * You then invoke the application in the usual way, except you place * command line arguments of the form '-Dkey=value' between the word * 'javaswarm' (or 'java') and the name of the application, for example: * * $ javaswarm TestArgsMain -b -- -DrandomSeed=456 -Dii=77 * * You can capture program output by redirecting it to a disk file for * later inspection: * * $ javaswarm TestArgsMain -b -- -DrandomSeed=456 -Dii=77 > runlog.txt * * As you will see from the program listing, you can also set the values of * parameters 'experimentDuration', 'dd', 'ss', and 'evaporationRate' on * the command line and the program will pick the new values up. * * The activities of the ModelSwarm depend on the values of the parameters * 'experimentDuration' and 'randomSeed'. * */ // ----- cut here --- // TestArgsMain.java /* * Title: TestArguments * Version: 0.1 * Copyright: Copyright (c) 2001 * Author: Sven Thommesen * Description: Sample app to show parsing of command line arguments. */ import swarm.Globals; public class TestArgsMain { public static void main (String[] args) { System.out.println("Main: initializing Swarm environment."); Globals.env.initSwarm ("TestArgs", "0.1", "sthomme@swarm.org", args); System.out.println("Main: command line arguments are:"); for (int i=0; i * Description: Sample app to show parsing of command line arguments. * Example 5 of 5. */ import swarm.Globals; import swarm.Selector; import swarm.SignatureNotFoundException; import swarm.NonUniqueMethodSignatureException; import swarm.defobj.Zone; import swarm.objectbase.Swarm; import swarm.objectbase.SwarmImpl; import swarm.objectbase.Probe; import swarm.objectbase.VarProbe; import swarm.objectbase.VarProbeImpl; import swarm.objectbase.ProbeMap; import swarm.activity.Activity; import swarm.activity.ActionGroup; import swarm.activity.ActionGroupImpl; import swarm.activity.Schedule; import swarm.activity.ScheduleImpl; import java.lang.String; import java.util.HashMap; import java.util.Properties; import java.util.Enumeration; import java.util.Iterator; public class TestArgsBatchSwarm extends SwarmImpl { // Program parameters: // (note that these must be 'public' to be settable by probes) public int experimentDuration = 0; public int randomSeed = 0; public int ii = 0; public double dd = 0.0; public String ss = null; // Objects: public TestArgsModelSwarm modelSwarm = null; Schedule stopSchedule = null; Schedule modelSchedule = null; ActionGroup modelActions = null; Properties defaultArgs = null; Properties arguments = null; ProbeMap aProbeMap = null; String[] commandLineArgs = null; public TestArgsBatchSwarm(Zone aZone) { super(aZone); aProbeMap = Globals.env.probeLibrary.getCompleteVarMapFor (this.getClass()); defaultArgs = new Properties(); arguments = new Properties (defaultArgs); // Set default values for local instance variables: experimentDuration = -1; randomSeed = -1; ii = -1; dd = -1.0; ss = "MinusOne"; } public void setArgs (String[] args) { commandLineArgs = args; System.out.println("BatchSwarm: command line args are"); for (int i=0; i Unable to poke value \'" + val // + "\' into non-existent variable \"" + key + "\""); } else { if (aProbe.setData$ToString((Object)this, val)) { System.out.println(" Setting variable \"" + key + "\" to value \'" + val + "\'"); } else { System.out.println(" > Unable to poke value \'" + val + "\' into variable \"" + key + "\""); } } } } public Object buildObjects() { super.buildObjects(); // add default parameter values to the default-Properties object: this.createDefaults(); // extract command line arguments from the command line // and add them to the Properties database: this.parseCommandLine(); // set values of instance variables for this object (batchSwarm): this.parseArguments(); // initialize the random generator with the provided seed: if (randomSeed != 0) { System.out.println("Setting random generator with seed " + randomSeed); Globals.env.randomGenerator.setStateFromSeed (randomSeed); } // Build the ModelSwarm and pass it a pointer to the 'arguments' object: System.out.println("Building ModelSwarm."); modelSwarm = new TestArgsModelSwarm(this.getZone(), arguments); // tell modelSwarm to build its own objects: modelSwarm.buildObjects(); return this; } /* * Here we show the use of a schedule to halt the simulation at a * predetermined simulation time, regulated by the 'experimentDuration' * parameter, which the user may set from the command line. * The modelSwarm builds its own actions (to run its 'step' method.) * */ public Object buildActions() { super.buildActions(); modelSwarm.buildActions(); // Create schedules for runtime actions: stopSchedule = new ScheduleImpl(this.getZone(), true); // autodrop try { stopSchedule.at$createActionTo$message (experimentDuration-1, this, new Selector (this.getClass(), "stopRunning", false) ); } catch (SignatureNotFoundException e) { System.err.println("BatchSwarm::build--Couldn't find method."); e.printStackTrace(); System.exit(-1); } catch (NonUniqueMethodSignatureException e) { System.err.println("BatchSwarm::build--NonUnique method."); e.printStackTrace(); System.exit(-1); } return this; } public Activity activateIn (Swarm swarmContext) { super.activateIn(swarmContext); modelSwarm.activateIn(this); stopSchedule.activateIn(this); return getActivity(); } public Object go() { System.out.println("You typed -b or --batch."); System.out.println("TestArgs is running in non-gui batch mode."); System.out.println ("Model will run for at most " + experimentDuration + " periods."); (getActivity().getSwarmActivity()).run(); System.out.println("BatchSwarm: stopped."); return getActivity().getStatus(); } public void stopRunning () { System.out.println ("BatchSwarm: Quitting at " + Globals.env.getCurrentTime() ); Globals.env.getCurrentSwarmActivity().terminate(); } public void drop() { System.out.println("BatchSwarm: drop."); super.drop(); System.out.println("BatchSwarm: halt."); System.exit(0); } } // ----- cut here --- // TestArgsModelSwarm.java /* * Title: TestArguments * Version: 0.1 * Copyright: Copyright (c) 2001 * Author: Sven Thommesen * Description: Sample app to show parsing of command line arguments. */ /* * This version of the ModelSwarm expects a Properties object * rather than a HashMap passed to it in the constructor. */ import swarm.Globals; import swarm.Selector; import swarm.defobj.Zone; import swarm.activity.Activity; import swarm.activity.ActionGroup; import swarm.activity.ActionGroupImpl; import swarm.activity.Schedule; import swarm.activity.ScheduleImpl; import swarm.objectbase.Swarm; import swarm.objectbase.SwarmImpl; import java.util.Properties; import java.util.Enumeration; public class TestArgsModelSwarm extends SwarmImpl { // data public double evaporationRate; // objects ActionGroup modelActions; Schedule modelSchedule; Properties arguments; public TestArgsModelSwarm (Zone aZone, Properties args) { super (aZone); // This is a pointer to the arguments data base (HashMap): arguments = args; // Set default data values: evaporationRate = 0.667; } public Object buildObjects () { super.buildObjects(); // Extract data values from the data base: this.parseArguments(); return this; } public Object buildActions () { super.buildActions(); modelActions = new ActionGroupImpl (getZone ()); try { modelActions.createActionTo$message (this, new Selector (this.getClass(), "step", true)); } catch (Exception e) { System.err.println ("Exception step: " + e.getMessage ()); } modelSchedule = new ScheduleImpl (getZone (), 1); modelSchedule.at$createAction (0, modelActions); return this; } public Activity activateIn (Swarm swarmContext) { super.activateIn (swarmContext); modelSchedule.activateIn (this); return getActivity (); } /* * This method shows how to extract data from the HashMap which was * passed in from the BatchSwarm. We cannot assume that any particular * key-value pair exists in the HashMap. * */ public void parseArguments() { System.out.println("ModelSwarm: setting local variables"); Enumeration index = arguments.propertyNames(); while ( index.hasMoreElements() ) { String key = (String) index.nextElement(); String val = (String) arguments.getProperty(key); if (key.equals("evaporationRate")) { evaporationRate = (new Double(val)).doubleValue(); System.out.println(" Setting variable \"" + key + "\" to value \'" + val + "\'"); } } } /* * The 'step' method will be executed 'experimentDuration' number * of times. The random samples drawn will depend on the 'randomSeed' * with which the generator was initialized. The value of 'evaporationRate' * is printed to show either the default value (set in the constructor * for the modelSwarm) or the value set on the command line. * */ public void step() { // Note: the number of steps taken should depend on 'experimentDuration' // Note: the random variates printed should depend on 'randomSeed' System.out.println(" -> Step: t = " + Globals.env.getCurrentTime() + " random int = " + Globals.env.randomGenerator.getUnsignedSample() + " evapRate = " + evaporationRate); // Caution: since Java does not support unsigned integers, the random // generator method getUnsignedSample() returns a signed integer // in java programs. } }