swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Scheduling with floating point time values


From: Marcus G. Daniels
Subject: Scheduling with floating point time values
Date: 18 Feb 2000 17:03:04 -0800
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4

D3> It gets broken down into two timeval_t's.
D3> The first of which is the integer part of the real number and the
D3> other is the fractional part multiplied by a "large" number that
D3> defines the precsion of the the schedule. 

D3> However, other than the occasional bugs when Swarm has upgraded, it
D3> has had no major problems.

Great!  I've reviewed the Swarm 2.1 interfaces to make sure this will
work in Java too.  I made one change to insertGroup: so that it is
typing is more specific (you'll be able to get rid of the time value
casting to id).

As a impromptu tribute to The Almighty Roger, I offer a Java
implementation of his approach.  :-)

import swarm.Globals;
import swarm.defobj.Zone;
import swarm.objectbase.SwarmImpl;
import swarm.objectbase.Swarm;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.activity.ScheduleC;
import swarm.activity.ScheduleCImpl;
import swarm.activity.ConcurrentScheduleCImpl;
import swarm.activity.ConcurrentSchedule;
import swarm.activity.Activity;
import swarm.activity.ScheduleActivity;
import swarm.Selector;
import java.text.DecimalFormat;
import java.util.Arrays;

public class DoubleSchedule extends SwarmImpl {
  Schedule schedule;
  ScheduleActivity scheduleActivity;
  Schedule stopSchedule;
  DecimalFormat formatter;

  final int precision = 10000;
  final int digits =
    (int) Math.rint (Math.log ((double) precision) / Math.log (10));
  DoubleSchedule (Zone aZone) {
    super (aZone);
    ConcurrentScheduleCImpl typeProto =
      new ConcurrentScheduleCImpl ();
    Object concGroupType;
    ScheduleC scheduleProto;
    
    typeProto.customizeBegin (aZone);
    typeProto.setAutoDrop (true);
    concGroupType = typeProto.customizeEnd ();
    
    scheduleProto = new ScheduleCImpl (new ScheduleImpl ());
    scheduleProto.createBegin (aZone);
    scheduleProto.setAutoDrop (true);
    scheduleProto.setConcurrentGroupType (concGroupType);
    schedule = (Schedule) scheduleProto.createEnd ();

    formatter = (DecimalFormat) DecimalFormat.getInstance ();
    char ary[] = new char[digits];
    Arrays.fill (ary, '0');
    formatter.applyPattern (new String (ary));
  }

  public Object step () {
    int subtval = (Globals.env.getCurrentTime () + 5) / 10;
  
    System.out.println (formatter.format (scheduleActivity.getCurrentTime ())
                        + "." +
                        formatter.format (subtval));
    return this;
  }

  void at (double val, Selector sel) {
    ((ConcurrentSchedule) schedule.insertGroup ((int) val)).
       at$createActionTo$message
         ((int) ((val - Math.floor (val)) * precision * 10), this, sel);
  }

  public Activity activateIn (Swarm swarmContext) {
    super.activateIn (swarmContext);
    
    scheduleActivity = (ScheduleActivity) schedule.activateIn (this);
    return getActivity ();
  }

  void go () {
    activateIn (null).run ();
  }

  static void main (String args[]) {
    Globals.env.initSwarm ("DoubleSchedule", "0.0", "address@hidden",
                           args);
    DoubleSchedule modelSwarm =
      new DoubleSchedule (Globals.env.globalZone);

    try {
      Selector sel = new Selector (modelSwarm.getClass (), "step", false);

      modelSwarm.at (1.999, sel);
      modelSwarm.at (2500.0051, sel);
      modelSwarm.at (2000.11, sel);
    } catch (Exception e) {
      e.printStackTrace ();
    }
    modelSwarm.go ();
  }
}

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]