Reference Guide Appendix: A2

A2. Source code for GPT Test Programs

//
// gpt.C
//
#include <stdio.h>

int main(int argc, char ** argv) {
    genr * g = new genr(“g”,5);
    transd * t = new transd(“t”,9);
    proc * p = new proc(“p”,2);                     //try also 5,6 and INFINITY
    digraph * efsim = new digraph(“efsim”);
    efsim->add(g);
    efsim->add(t);
    efsim->add(p);
    g->add_coupling(g, “out”, t, “ariv”);
    g->add_coupling(g, “out”, p, “in”);
    t->add_coupling(t,”out”,g,”stop”);
    p->add_coupling(p, “out”, t, “solved”);
    efsim->get_inports()->add(“start”);
    efsim->get_outports()->add(“stop”);
    efsim->get_outports()->add(“result”);
    efsim->add_coupling(efsim, “start”, g, “start”);
    t->add_coupling(t, “out”, efsim, “result”);
    efsim->initialize();
    entity * ent = new entity(“a”);
    efsim->inject(“start”, ent);
    efsim->simulate(4);
    return 0;
}

//
// gpt.h
//

#ifndef _GENRH_
#define _GENRH_

#include <atomic.h>
#include <strio.h>

class genr:public atomic{
protected:
    timetype int_arr_time;
    int count;
    entity * ent;
public:
    genr(char * name,timetype int_arr_time) ;
    void deltext(timetype e,message * x) ;
    void deltint(timetype e);
    message * out(timetype e);
};

#endif

/*
* genr.C
*/

#include “genr.h”

genr::genr(char * name,timetype int_arr_time):atomic(name){
    inports->add(“stop”);
    inports->add(“start”);
    outports->add(“out”);
    phases->add(“busy”);
    this->int_arr_time = int_arr_time ;
    count = 0;
}

void genr::deltext(timetype e,message * x) {
    Continue();
    int i;
    for (i=0; i< x->get_length();i++)
        if (message_on_port(x,”start”,i)) {
            ent = x->get_val_on_port(“start”,i);
            hold_in(“busy”,int_arr_time);
        }
    for (i=0; i< x->get_length();i++)
        if (message_on_port(x,”stop”,i))
            passivate();
}

void genr::deltint(timetype e) {
    count = count +1;
    hold_in(“busy”,int_arr_time);
}

message * genr::out(timetype e) {
    message * m = new message();
    entity * p = new entity(name_gen(ent->get_name(),count));
    content * con = make_content(“out”, p);
    m->add(con);
    m->show_message();
    return m;
}

/*
* proc.h
*/

#ifndef _PROC_H
#define _PROC_H

#include <atomic.h>

class proc:public atomic {
protected:
    timetype processing_time;
    entity * job;
public:
    proc(char * name,timetype proc_time);
    void deltext(timetype e,message * x) ;
    void deltint(timetype e);
    message * out(timetype e);
};

#endif

/*
* proc.C
*/

#include “proc.h”

proc::proc(char * name,timetype processing_time):atomic(name) {
    inports->add(“in”);
    outports->add(“out”);
    phases->add(“busy”);
    this->processing_time = processing_time ;
}

void proc::deltext(timetype e,message * x) {
    Continue();
    int i;
    if (phase_is(“passive”))
        for (i=0; i< x->get_length();i++)
            if (message_on_port(x,”in”,i)) {
                job = x->get_val_on_port(“in”,i);
                hold_in(“busy”,processing_time);
            }
}

void proc::deltint(timetype e) {
    passivate();
}

message * proc::out(timetype e) {
    message * m = new message();
    entity *val = job;
    content * con = make_content(“out”,val);
    m->add(con);
    m->print_message();
    return m;
}

/*
* transd.h
*/

#ifndef _TRANSDH_
#define _TRANSDH_

#include <atomic.h>
#include <relation.h>
#include <num_ent.h>

class transd:public atomic {
protected:
    relation *arrived, *solved;
    timetype clock,total_ta,observation_time;
public:
    transd(char * name,timetype observ_time);
    void deltext(timetype e,message * x);
    void deltint(timetype e);
    message * out(timetype e);
};

#endif

/*
* transd.C
*/
 
#include “transd.h”

class number:public entity {
private:
    float NUMBER;
public:
    number(float i):NUMBER(i){}
    float get_number(){return NUMBER;}
    void print(){cout << “number: ” << NUMBER;}
};

transd::transd(char * name,timetype observation_time):atomic(name) {
    inports->add(“ariv”);
    inports->add(“solved”);
    outports->add(“out”);
    phases->add(“active”);
    arrived = new relation();
    solved = new relation();
    phase = “active”;
    this->observation_time = observation_time;
    sigma = observation_time;
    clock = 0.0;
    total_ta = 0.0;
}

void transd::deltext(timetype e,message * x) {
    clock = clock + e;
    Continue();
    entity * val;
    for (int i=0; i< x->get_length();i++) {
        if (message_on_port(x,”ariv”,i)) {
            val = x->get_val_on_port(“ariv”,i);
            arrived->add(val,new number(clock));
        }
        if (message_on_port(x,”solved”,i)) {
            val = x->get_val_on_port(“solved”,i);
            if (arrived->key_name_is_in(val->get_name())) {
                entity * ent = arrived->assoc(val->get_name());
                number * num = (number *)ent;
                timetype arrival_time = num->get_number();
                timetype turn_around_time = clock – arrival_time;
                total_ta = total_ta + turn_around_time;
                solved->add(val, new number(clock));
            }
            else {
                cerr << “arriving job ” ; val->print();cout << ” did not depart!”;
                exit(1);
            }
        }
    }
}

void transd::deltint(timetype e) {
    clock = clock + sigma;
    passivate();
}

message * transd::out(timetype e) {
    float throughput;
    timetype avg_ta_time = 0;
    if (!solved->empty()) avg_ta_time = total_ta/solved->get_length();
    if (clock > 0)
        throughput = solved->get_length()/clock;
    else throughput = 0;
    char a[10], b[10];
    strcpy(a, arrived->get_head()->get_ent()->get_name());
    strcpy(b, solved->get_head()->get_ent()->get_name());
    if ( strcmp(a,b)==0 ) {
        cout << endl;
        cout << “*** TEST SATISFIED ***” << endl;
        cout << endl;
    }
    else {
        cout << endl;
        cout << “test NOT satisfied” << endl;
        cout << endl;
    }
    cout << “jobs arrived :” ;
    arrived->print();
    cout << “jobs solved :” ;
    solved->print();
    cout << endl;
    cout << “AVERAGE TA = ” << avg_ta_time <<endl;
    cout << “THROUGHPUT = ” << throughput <<endl;
    cout << endl;
    message * m = new message();
    content * con = make_content(“out”,new float_ent(throughput));
    m->add(con);
    return m;
}