MeetingAgentObject.borg

MeetingAgentObject.borg


`-------------------------------------------
` FILE:        MeetingAgentObject.borg
` AUTHOR:        Pieter Verheyden
` E-MAIL:        pverheyd@vub.ac.be
` LAST CHANGED:    18/12/2001
`-------------------------------------------

makeMeetingAgentObject(name, datetable, caller, guesttable, percentage, subject, place)::
{
    ` maximum number of participants (= number of guests)
    MAXNUMBER: size(guesttable);
    ` minimum number of participants
    MINNUMBER: ((MAXNUMBER*percentage)//100);

    ` void table to hold the particpants of the meeting
    participanttable_:    makeVoidTable(MAXNUMBER);
    ` integer counter
    feedbacks_: 0;
    
    
    ` get all (relevant) meeting data
    getMeetingData(agt):: {
        participanttable: participanttable_.returnVoidTable(); 
        meetingdata: [datetable, caller, subject, place, percentage, guesttable, participanttable];
        agt->processMeetingData(meetingdata)
    };

    ` try to reservate the place 
    tryReservatePlace()::    
        place->tryReservate(agentname, datetable);

    ` possibility to set an alternative place when the first place is occupied or has canceled
    tryReservateNewPlace(newplace):: { 
        place:=newplace;
        tryReservatePlace()
    };    

    ` try to reservate the guests
    tryReservateGuests():: {
        for(i:1, i<=MAXNUMBER, i:=i+1,
            {
            cguest:guesttable[i];
            cguest->tryReservate(agentname, datetable)
            }                        
        )
    };
    
    ` check if there are (still) enough particpants
    checkMeetingStatus():: {
        nbrparticipants:participanttable_.getNumberElements();
        if(nbrparticipants>=MINNUMBER, true, false)        
    };
    
    ` try to setup the meeting
    trySetupMeeting():: {
        if(checkMeetingStatus(),
            {
            caller->setupMeetingSuccess(agentname, datetable);
            place->guestMeetingSuccess(agentname, datetable);
            for(i:1, i<=(participanttable_.getNumberElements()), i:=i+1,
                {
                cparticipant:(participanttable_.getElement(i));
                cparticipant->guestMeetingSuccess(agentname, datetable)
                }
            )
            },
            {
            nbrparticipants:participanttable_.getNumberElements();
            caller->setupMeetingFail(agentname, datetable);
            place->guestMeetingCancel(agentname, datetable);
            agentdie()
            }
        )
    };        

    ` the meeting fails:
    ` - particpants and place should be warned
    ` - meetingagent must die
    meetingFail():: {
        nbrparticipants:participanttable_.getNumberElements();
        for(i:1, i<=nbrparticipants, i:=i+1,
            {
            cparticipant: participanttable_.getElement(i);
            cparticipant->guestMeetingCancel(agentname, datetable)
            }                        
        );
        place->guestMeetingCancel(agentname, datetable);
        agentdie()    
    };

    ` process a cancel request from an agent (caller, place or participant) 
    cancelMeeting(agt):: {
        if(agt~caller,
            meetingFail(),
            if(agt~place,
                {            
                caller->placeCancel(datetable, place);
                place:=void
                },
                {
                participanttable_.deleteElement(agt);
                if(is_false(checkMeetingStatus()),
                    {
                    caller->myMeetingCancel(agentname, datetable);
                    meetingFail()
                    }
                )
                }
            )
        )
    };
 
    reservationSuccess(agt):: {
        if(agt~place, 
            { 
            nbrparticipants:participanttable_.getNumberElements();
            if(nbrparticipants=0, tryReservateGuests())},
            {
            participanttable_.addElement(agt);
            feedbacks_:=feedbacks_+1;
            if(feedbacks_=MAXNUMBER, { feedbacks_:=0; trySetupMeeting() })
            }
        )
    };

    reservationFail(agt):: {
        if(agt~place,
            caller->reservationPlaceFail(datetable, place),
            {
            feedbacks_:=feedbacks_+1;
            if(feedbacks_=MAXNUMBER, { feedbacks_:=0; trySetupMeeting() })
            }
        )
    };
    

    ref: agentclone(clone(), name);

    ` first the place has to be reservated
    ref->tryReservatePlace();

    ref
}