MPAgent.borg

MPAgent.borg


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

{

load("DataObjects.borg");
load("MeetingAgentObject.borg");
load("PalmWidgets.borg");


mymeetingtable_:            makeMeetingTable(16, true);
guestmeetingtable_:        makeMeetingTable(16, false);        
waitingmeetingagentstable_:    makeMeetingTable(16, false);

guiagent_:void;

password_:void;


` setting up/off a GUI for the MPAgent 
setupGUIAgent(guiagent, password):: {
        if(password=password_,
            {
            guiagent_:=guiagent;
            guiagent_->initMPAgentSuccess();    
            guiagent_->initMainWindow();    
            now:date();
            dateSchedule(now)
            },
            guiagent->showError("Wrong password")
        )                    
};

resetGUIAgent()::
    guiagent_:=void;


` return error or notification
returnError(msg):: 
     if(!is_void(guiagent_), guiagent_->showError(msg), display(msg, eoln));

returnNotification(msg)::
     if(!is_void(guiagent_), guiagent_->showNotification(msg), display(msg, eoln));


` setting the password
setPassword(password):: 
    password_:=password;

` changing the password
newPassword(currentpassword, newpassword):: {
    if(currentpassword=password_,
        {
        password_:=newpassword;
        if(!is_void(guiagent_), guiagent_->newPasswordSuccess())
        },
        returnError("Wrong current password")
    )
};
            

` create a unique name for a meetingagentobject
` example:
` caller of the meeting: kebab/Alice
` datetable of the meeting: [20,11,2001,[8,45],[10,30]]
` => name for the meetingagentobject: Alice201120018451030    
makeMeetingAgentName(datetable):: {
    day:        text(datetable[1]);
    month:        text(datetable[2]);
    year:        text(datetable[3]);
    begintimetable:    datetable[4];
    begintime:        text(begintimetable[1]) + text(begintimetable[2]);
    endtimetable:    datetable[5];
    endtime:        text(endtimetable[1]) + text(endtimetable[2]);    

    myname:    text(agentname);
    index:        strstr(myname, "/");
    strname:    substr(myname,index+1,length(myname));    

    meetingagentname: strname+day+month+year+begintime+endtime
};

` setup a meeting
` - datetable: [day,month,year,[beginhour,beginminutes],[endhour,endminutes]]
` - guesttable: table with reference(s) to agent(s)
` - 0 >= percentage <= 100, represents the present percentage of the meeting
` - subject: string 
` - place: reference to agent   
setupMeeting(datetable, guesttable, percentage, subject, place):: {
    if(checkDate(datetable),
        if(((guestmeetingtable_.checkForFreeTime(datetable)) 
            & (mymeetingtable_.checkForFreeTime(datetable))
            & (waitingmeetingagentstable_.checkForFreeTime(datetable))),
            {
            meetingagentname:makeMeetingAgentName(datetable);
            maref:makeMeetingAgentObject(meetingagentname, datetable, agentname, guesttable, percentage, subject, place);
            mymeetingtable_.addReservation(maref, datetable);
            if(!is_void(guiagent_), guiagent_->setupMeetingFinished())    
            },
            returnError("No free time found") 
        ),    
        returnError("You entered an invalid date and/or time")
    )
};

` if the the setup of a meeting is successfull, reservation becomes participation
setupMeetingSuccess(meetingagent, datetable):: {
    mymeetingtable_.setParticipation(meetingagent);
    returnNotification("Setup of your meeting on " + dateToString(datetable) + " is successfull")
};

` if the setup of a meeting failed, that meeting is deleted from the meetingtable
setupMeetingFail(meetingagent, datetable):: {
    mymeetingtable_.deleteMeetingAgent(meetingagent);
    returnNotification("Setup of your meeting on " + dateToString(datetable) + " failed")
};    

` if one of your called meetings is canceled, that meeting is deleted from the meetingtable
myMeetingCancel(meetingagent, datetable):: {
    mymeetingtable_.deleteMeetingAgent(meetingagent);
    returnNotification("Your meeting on " + dateToString(datetable) + " is canceled")
};

` if a meeting is successfull, that meeting is added to the meetingtable 
guestMeetingSuccess(meetingagent, datetable):: {
    guestmeetingtable_.setParticipation(meetingagent);
    returnNotification("New guest meeting on " + dateToString(datetable))    
};

` if a guest meeting is canceled, that meeting is deleted from the meetingtable 
guestMeetingCancel(meetingagent, datetable):: { 
    guestmeetingtable_.deleteMeetingAgent(meetingagent);
    returnNotification("Guest meeting on " + dateToString(datetable) + " is canceled")    
};
    
` try to reservate time for a meeting
tryReservate(meetingagent, datetable):: {
    if(waitingmeetingagentstable_.checkForFreeTime(datetable),
        {
        waitingmeetingagentstable_.addWaitingMeetingAgent(meetingagent, datetable);
        if(((mymeetingtable_.checkForFreeTime(datetable)) & (guestmeetingtable_.checkForFreeTime(datetable))),
            reservationSuccess(meetingagent, datetable),
            reservationFail(meetingagent)
        )
        },
        meetingagent->reservationFail(agentname)
    )
};

reservationSuccess(meetingagent, datetable):: {
    waitingmeetingagentstable_.deleteMeetingAgent(meetingagent);
    guestmeetingtable_.addReservation(meetingagent, datetable);
    meetingagent->reservationSuccess(agentname)
};

reservationFail(meetingagent):: {
    waitingmeetingagentstable_.deleteMeetingAgent(meetingagent);
    meetingagent->reservationFail(agentname)
};

` 2 possible place errors:
` - the reservation of the place failed
` - the place canceled  
reservationPlaceFail(datetable, place):: {
    msg: "Could not reservate place " + text(place) + " for your meeting on " + dateToString(datetable) 
        + eoln     + "You have to provide a new place before the setup of this meeting will continue"; 
    returnNotification(msg)
};

placeCancel(datetable, place):: {
    msg:"Place " + text(place) + " has canceled your meeting on " + dateToString(datetable) 
        + eoln     + "Provide a new place for this meeting a.s.a.p.";  
    returnNotification(msg)
};

` provide a new place for your meeting
newPlace(datetable, newplace):: {
    if(checkDate(datetable),
        {
        meetingagent: (mymeetingtable_.getMeetingAgent(datetable));
        if(is_false(meetingagent),
            returnError("You do not setup a meeting on " + dateToString(datetable)),
            {
            meetingagent->tryReservateNewPlace(newplace);
            if(!is_void(guiagent_), guiagent_->newPlaceFinished())
            }
        )
        },
        returnError("You entered an invalid date and/or time")
    )        
};

` cancel one of your meetings 
cancelMyMeeting(datetable):: {
    meetingagent: (mymeetingtable_.getMeetingAgent(datetable));
    if(is_false(meetingagent),
        returnError("You did not setup a meeting on " + dateToString(datetable)),
        {
        meetingagent->cancelMeeting(agentname);
        mymeetingtable_.deleteMeetingAgent(meetingagent);
        if(!is_void(guiagent_), guiagent_->cancelFinished())
        }
    )
};

` cancel one of your guestmeetings 
cancelGuestMeeting(datetable):: {
    meetingagent: (guestmeetingtable_.getMeetingAgent(datetable));
    if(is_false(meetingagent),
        returnError("You do not have a meeting on " + dateToString(datetable)),
        {
        meetingagent->cancelMeeting(agentname);
        guestmeetingtable_.deleteMeetingAgent(meetingagent);
        if(!is_void(guiagent_), guiagent_->cancelFinished())
        }
    )
};

` get the schedule of a day (particpating meetings)
meetingdata_: void;
feedbacks_: 0;
    
dateSchedule(table):: {
    if(is_void(meetingdata_),
        {
        day:table[1];    
        month:table[2];
        year:table[3];        
        mymeetingagents_:        mymeetingtable_.getPMeetingAgents(day, month, year);  
        guestmeetingagents_:    guestmeetingtable_.getPMeetingAgents(day, month, year);
        nbrmymeetingagents:    mymeetingagents_.getNumberElements();
        nbrguestmeetingagents:    guestmeetingagents_.getNumberElements();
               
        totalnbr:(nbrmymeetingagents + nbrguestmeetingagents);
        if(totalnbr=0,
            if(!is_void(guiagent_), guiagent_->retrieveDateSchedule("No meetings found for " 
                + text(day) + "/" + text(month) + "/" + text(year) 
                + eoln)),
            {
            meetingdata_:=makeVoidTable(totalnbr);
            feedbacks_:=totalnbr;

            for(i:1, i<=nbrmymeetingagents, i:=i+1,
                {
                cmeetingagent:mymeetingagents_.getElement(i);
                cmeetingagent->getMeetingData(agentname)         
                }
            ); 

            for(j:1, j<=nbrguestmeetingagents, j:=i+1,
                {
                cmeetingagent:guestmeetingagents_.getElement(j);
                cmeetingagent->getMeetingData(agentname)         
                }
            )
            }
        )
        }
    )
};

processMeetingData(meetingdata):: {
    meetingdata_.addElement(meetingdata);
    feedbacks_:=feedbacks_-1;
    if(feedbacks_=0, generateMeetingData())    
};

tableToString(agenttable):: {
    tablestring:"";
    for(i:1, ((i<=size(agenttable)) & (!is_void(agenttable[i]))), i:=i+1,
        {
        tablestring:=tablestring + (text(agenttable[i]));
        if(((i=size(agenttable))|(is_void(agenttable[i+1]))), 
            (tablestring:=tablestring + eoln), 
            (tablestring:=tablestring + ", ")
        )
        }
    );
    tablestring
};

bubbleSortMeetingData():: {
    lim:meetingdata_.getNumberElements();
    swap:true;
    for(i:1, (swap & (i1,
        {
        swap:=false;
        for(k:1, k<=(lim-i), k:=k+1,
            {
            cmeetingdata:meetingdata_.getElement(k);
            nmeetingdata:meetingdata_.getElement(k+1);
            cdatetable:cmeetingdata[1];
            cbegintime:cdatetable[4];
            ndatetable:nmeetingdata[1];
            nbegintime:ndatetable[4];
            if(laterTime(cbegintime, nbegintime),
                {
                temp:cmeetingdata;
                meetingdata_.setElement(k, nmeetingdata);
                meetingdata_.setElement(k+1, temp);
                swap:=true    
                }
            )
            }
        )
        } 
    )
};

generateMeetingData():: {
    bubbleSortMeetingData();
    meetingdatastring: "";        
    for(i:1, i<=(meetingdata_.getNumberElements()), i:=i+1,
        {
        cmeetingdata:    meetingdata_.getElement(i);
    
        datetable:        cmeetingdata[1];
        caller:        cmeetingdata[2]; 
        subject:        cmeetingdata[3];
        place:            cmeetingdata[4];
        percentage:        cmeetingdata[5];
        guesttable:        cmeetingdata[6];
        participanttable:    cmeetingdata[7];

        meetingdatastring:=meetingdatastring + (eoln + "----------------------------------------------------------" + eoln);

        if(caller~agentname,
            {
            meetingdatastring:=meetingdatastring + ("My meeting: " + dateToString(datetable));
            meetingdatastring:=meetingdatastring + (eoln + "----------------------------------------------------------" + eoln)
            },
            {
            meetingdatastring:=meetingdatastring + "Guest meeting: " + dateToString(datetable);
            meetingdatastring:=meetingdatastring + (eoln + "----------------------------------------------------------" + eoln);
            meetingdatastring:=meetingdatastring + ("Caller: " + text(caller) + eoln)
            }
        );

        meetingdatastring:=meetingdatastring + ("Subject: " + text(subject) + eoln);
        if(is_void(place), meetingdatastring:=meetingdatastring + ("Place: unknown" + eoln), meetingdatastring:=meetingdatastring + ("Place: " + text(place) + eoln));

        if(caller~agentname, 
            { 
            meetingdatastring:=meetingdatastring + ("Present percentage: " + text(percentage) + eoln);
            meetingdatastring:=meetingdatastring + ("Guests: " + tableToString(guesttable))
            }
        );

        meetingdatastring:=meetingdatastring + ("Particpants: " + tableToString(participanttable));

        meetingdatastring:=meetingdatastring + ("----------------------------------------------------------" + eoln)
        
        }
    );
    if(!is_void(guiagent_), guiagent_->retrieveDateSchedule(meetingdatastring), display(meetingdatastring));
    meetingdata_:=void    
}

}