diff --git a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java index 52d83df2c1bcf3f449b182f70319b193a0b0a5e4..3127dc0d83404980fdcaf9a7d50120cd50b510fe 100644 --- a/examples/src/OpenDis7Examples/ExampleSimulationProgram.java +++ b/examples/src/OpenDis7Examples/ExampleSimulationProgram.java @@ -20,6 +20,100 @@ import java.util.logging.Logger; * that includes DIS-capable entities doing tasks and reporting them. */ public class ExampleSimulationProgram { + /** + * runSimulation is for you! This block is programmer-modifiable method for defining and running a new simulation of interest. + * Support include DIS EntityStatePdu, FirePdu and CommentPdu all available for + * modification and sending in a simulation loop. + */ + @SuppressWarnings("SleepWhileInLoop") + public void runSimulation () + { + try + { + + + final double LOOP_DURATION_SECONDS = 1.0; // seconds + final int MAX_LOOP_COUNT = 10; + int loopCount = 0; + VariableRecordType narrativeType = VariableRecordType.OTHER; // of potential use + boolean simulationComplete = false; // sentinel variable as termination condition + + // TODO reset clock to zero each time for consistent outputs. + + // your model setup: who's who in this zoo? + // create PDU objects and set their values + + EntityID entityID_1 = new EntityID(); + entityID_1.setSiteID(1).setApplicationID(2).setEntityID(3); // made-up example ID + + EntityStatePdu entityStatePdu = pduFactory.makeEntityStatePdu(); + entityStatePdu.setEntityID(entityID_1); + + FirePdu firePdu = pduFactory.makeFirePdu(); + // should we customize this munition? what is it for your simulation? + + // loop the simulation while allowed, programmer can set additional conditions to break out and finish + while (loopCount < MAX_LOOP_COUNT) + { + String narrativeMessage1, narrativeMessage2, narrativeMessage3; + // initialize loop variables + loopCount++; + + // ============================================================================================= + // your own simulation code starts here! + + // compute a track, update an ESPDU, whatever it is that your model is doing... + + // Where is my entity? + entityStatePdu.getEntityLocation().setX(entityStatePdu.getEntityLocation().getX() + 1.0); // 1m per timestep + + // decide whether to fire, and then update the firePdu. Hmmm, you might want a target to shoort at! + + // etc. etc. your code goes here + + // something happens between my simulation entities, la de da de da... + System.out.println ("... My simulation just did something, no really..."); + + + // make your reports: narrative code for CommentPdu here (set all to empty strings to avoid sending) + narrativeMessage1 = "MV3500 ExampleSimulationProgram"; + narrativeMessage2 = "runSimulation() loop " + loopCount; + narrativeMessage3 = ""; // intentionally blank for testing + + // your loop termination condition goes here + if (loopCount > 4) // for example + { + simulationComplete = true; + } + // your own simulation code is finished here! + // ============================================================================================= + + // keep track of timestep: wait duration for elapsed time in this loop + // Thread.sleep needs a (long) parameter for milliseconds, which are clumsy to use sometimes + Thread.sleep((long)(LOOP_DURATION_SECONDS * 1000)); // seconds * (1000 msec/sec) = milliseconds + System.out.println ("... Pausing for " + LOOP_DURATION_SECONDS + " seconds"); + + // send the status PDUs for this loop and continue + System.out.println ("sending PDUs for simulation step " + loopCount + ", monitor loopback to confirm sent"); + sendAllPdus(entityStatePdu, firePdu, null, narrativeMessage1, narrativeMessage2, narrativeMessage3); + System.out.println ("... PDUs successfully sent"); + + // =============================== + // loop now finished, thus terminate if simulation complete, otherwise send latest PDUs and continue + if (simulationComplete || (loopCount > 10000)) // for example; including fail-safe condition is good + { + System.out.println ("... Termination condition met, simulationComplete=" + simulationComplete); + break; + } + } // end of while loop + } + catch (InterruptedException iex) // handle any exception that your code might choose to provoke! + { + Logger.getLogger(ExampleSimulationProgram.class.getName()).log(Level.SEVERE, null, iex); + } + } + /* **************************** infrastructure code, modification is seldom needed ************************* */ + /** * Output prefix to identify this class, helps with logging */ @@ -206,97 +300,4 @@ public class ExampleSimulationProgram System.out.println(TRACE_PREFIX + "complete."); // report successful completion } - - /** - * Programmer-modifiable method for defining and running a new simulation of interest. - * Support include DIS EntityStatePdu, FirePdu and CommentPdu all available for - * modification and sending in a simulation loop. - */ - @SuppressWarnings("SleepWhileInLoop") - public void runSimulation () - { - try - { - - - final double LOOP_DURATION_SECONDS = 1.0; // seconds - final int MAX_LOOP_COUNT = 10; - int loopCount = 0; - VariableRecordType narrativeType = VariableRecordType.OTHER; // of potential use - boolean simulationComplete = false; // sentinel variable as termination condition - - // TODO reset clock to zero each time for consistent outputs. - - // your model setup: who's who in this zoo? - // create PDU objects and set their values - - EntityID entityID_1 = new EntityID(); - entityID_1.setSiteID(1).setApplicationID(2).setEntityID(3); // made-up example ID - - EntityStatePdu entityStatePdu = pduFactory.makeEntityStatePdu(); - entityStatePdu.setEntityID(entityID_1); - - FirePdu firePdu = pduFactory.makeFirePdu(); - // should we customize this munition? what is it for your simulation? - - // loop the simulation while allowed, programmer can set additional conditions to break out and finish - while (loopCount < MAX_LOOP_COUNT) - { - String narrativeMessage1, narrativeMessage2, narrativeMessage3; - // initialize loop variables - loopCount++; - - // ============================================================================================= - // your own simulation code starts here! - - // compute a track, update an ESPDU, whatever it is that your model is doing... - - // Where is my entity? - entityStatePdu.getEntityLocation().setX(entityStatePdu.getEntityLocation().getX() + 1.0); // 1m per timestep - - // decide whether to fire, and then update the firePdu. Hmmm, you might want a target to shoort at! - - // etc. etc. your code goes here - - // something happens between my simulation entities, la de da de da... - System.out.println ("... My simulation just did something, no really..."); - - - // make your reports: narrative code for CommentPdu here (set all to empty strings to avoid sending) - narrativeMessage1 = "MV3500 ExampleSimulationProgram"; - narrativeMessage2 = "runSimulation() loop " + loopCount; - narrativeMessage3 = ""; // intentionally blank for testing - - // your loop termination condition goes here - if (loopCount > 4) // for example - { - simulationComplete = true; - } - // your own simulation code is finished here! - // ============================================================================================= - - // keep track of timestep: wait duration for elapsed time in this loop - // Thread.sleep needs a (long) parameter for milliseconds, which are clumsy to use sometimes - Thread.sleep((long)(LOOP_DURATION_SECONDS * 1000)); // seconds * (1000 msec/sec) = milliseconds - System.out.println ("... Pausing for " + LOOP_DURATION_SECONDS + " seconds"); - - // send the status PDUs for this loop and continue - System.out.println ("sending PDUs for simulation step " + loopCount + ", monitor loopback to confirm sent"); - sendAllPdus(entityStatePdu, firePdu, null, narrativeMessage1, narrativeMessage2, narrativeMessage3); - System.out.println ("... PDUs successfully sent"); - - // =============================== - // loop now finished, thus terminate if simulation complete, otherwise send latest PDUs and continue - if (simulationComplete || (loopCount > 10000)) // for example; including fail-safe condition is good - { - System.out.println ("... Termination condition met, simulationComplete=" + simulationComplete); - break; - } - } // end of while loop - } - catch (InterruptedException iex) // handle any exception that your code might choose to provoke! - { - Logger.getLogger(ExampleSimulationProgram.class.getName()).log(Level.SEVERE, null, iex); - } - } }