/* * Title: TestArguments * Version: 0.1 * Copyright: Copyright (c) 2001 * Author: Sven Thommesen * Description: Sample app to show parsing of command line arguments. * Example 2 of 5. */ /* * This example demonstrates a second way to extract command line data from * the Java Properties database, 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 1 in that: * a) probes are not used to set local data values * b) data are extracted from the system Properties database and put * into the local database first, then local variables have their * values set. * * Only data values passed to the program on the command line are entered * into the parameter database. * * 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 -DrandomSeed=456 -Dii=77 TestArgsmain -b * * You can capture program output by redirecting it to a disk file for * later inspection: * * $ javaswarm -DrandomSeed=456 -Dii=77 TestArgsMain -b > 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 2 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; HashMap arguments = null; ProbeMap aProbeMap = null; String[] commandLineArgs = null; public TestArgsBatchSwarm(Zone aZone) { super(aZone); aProbeMap = Globals.env.probeLibrary.getCompleteVarMapFor (this.getClass()); arguments = new HashMap(); // Set default values for instance variables: experimentDuration = 10; randomSeed = 123456789; ii = -1; dd = 987.6; ss = "DefaultValue"; } public void setArgs (String[] args) { commandLineArgs = args; System.out.println("BatchSwarm: command line args are"); for (int i=0; i * Description: Sample app to show parsing of command line arguments. */ /* * This version of the ModelSwarm expects a HashMap object * 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.HashMap; import java.util.Iterator; public class TestArgsModelSwarm extends SwarmImpl { // data public double evaporationRate; // objects ActionGroup modelActions; Schedule modelSchedule; HashMap arguments; public TestArgsModelSwarm (Zone aZone, HashMap args) { super (aZone); // This is a pointer to the arguments data base (HashMap): arguments = args; // Set default data values: evaporationRate = 0.225; } 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"); Iterator index = arguments.keySet().iterator(); while ( index.hasNext() ) { String key = (String) index.next(); if (key.equals("evaporationRate")) { evaporationRate = ((Double) arguments.get(key)).doubleValue(); System.out.println(" Setting variable \"" + key + "\" to value \'" + evaporationRate + "\'"); } } } /* * 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. } }