next up previous
Next: Appendix: Listing of classes Up: The Neosim interface [ Previous: NEURON style connections

Example

This minimal example defines two new entity classes, a producer and a consumer, connects them together, and runs a simulation for 1 second.

The first step is to define the new classes of entity, first the producer which outputs a sine wave on its single output port:

 
class ProducerClass extends EntityClass {
  public double tstep;
  ProducerClass( String name, double tstep ) { super(name); this.tstep = tstep; }
  Entity makeEntity( Population p, int index ) {
    ProducerEntity e = new ProducerEntity(p, this);
    e.init( p, index );
  }
}

class ProducerEntity extends Entity {
  ProducerClass pc;
  PortID outp;
  ProducerEntity( Population p, ProducerClass pc ) { 
    super(p, pc); this.pc = pc;
    outp = addOutPort( DoubleEventID );
  }
  Time handleEvents( EventList el, Time t ) {
    for (int i=0; i < el.size(); i++) {       
      defaultEventHandler( el.elementAt(i) ); 
    }
    for (double tt = localTime(); tt < t; tt += pc.tstep) {
      sendEvent( outp, new DoubleEvent( tt, sine(tt) ) );
    }
    return t;
  }
}

The consumer can be defined similarly:

class ConsumerClass extends EntityClass {
  ConsumerClass( String name ) { super(name); }
  Entity makeEntity( Population p, int index ) {
    ProducerEntity e = new ProducerEntity(p, this);
  }
}

class ConsumerEntity extends Entity {
  PortID inp;
  ConsumerEntity( Population p, ConsumerClass cc ) { 
    super(p, cc); 
    inp = addInPort( DoubleEventID );
  }
  Time handleEvents( EventList el, Time t ) {
    for (int i=0; i < el.size(); i++) {  
      Event ev = el.elementAt(i);
      if (ev.getClassID() == DoubleEventID) {
        DoubleEvent dev = (DoubleEvent)ev;
        System.out.println("Received "+dev.getDouble()+" at "+dev.getTime());
      } else {
        defaultEventHandler( ev ); 
      }
    }
    return t;
  }
}

This simulation should be set up from a script reader:

class SimpleSimulation extends ScriptReader {
  void bootstrap(Object args[]) {
    Time tstep = ms(10);
    ProducerClass pc = new ProducerClass( "prod10ms", tstep );
    Population p = new PopulationBuilder( "producers", pc, 1, null );
    construct( p );

    ConsumerClass cc = new ConsumerClass( "cons" );
    Population c = new PopulationBuilder( "consumers", cc, 1, null );
    construct( c );

    Time delay = ms(10);
    EntityID prod = lookupEntity("producers0");
    EntityID cons = lookupEntity("consumers0");
    connect( new Projection( new Connection( prod, 1, cons, 1, delay ) ) );

    Time runtime = seconds(1);
    runSim( runtime );
  }
}


Fred Howell
8/15/1999