User avatar
Leslie
Lead Software Architect
Posts: 352
Joined: Fri Sep 14, 2012 12:20 pm
Contact: Website

Single-Threaded Questioning

Thu Oct 02, 2014 6:03 pm

Apart from Generating responses its also important that the program should be able to question the user. Questioning helps control the flow of program execution and requires some effort on the developers end.

We will take a look at an example where you wish to allow the user to Exit a program only after he confirms program termination.

Sample code for an Exit function

CSharp Code

//Function with Signature "Exit". When the input starts with "Exit" this function is called.
public void ExitApplication(ExitTerm cmd)
{
SynResponse resp = new SynResponse();
resp.Message = "Are you sure ? ( Yes / No )";
//Check if the Caller has been asked to confirm. If not then ask the user to Confirm
//and put the Caller under a Question Session.
if (cmd.Data.Caller.InQuestion == false)
{
cmd.Data.Caller.Question(cmd, resp);
}
else
{
//Once the confirmation message is sent to the Caller. The Caller is in a question session.
//Check if the Caller has responded with a known BooleanData. Yes = True , No = False.
if (cmd.Data.Caller.Context.Contains<BooleanData>())
{
BooleanData userAnswer = cmd.Data.Caller.Context.GetData<BooleanData>();
if (userAnswer.Value == true)
{
Application.Exit();
}
else
{
cmd.Data.Caller.Respond("You did not confirm.");
}
}
//If the Caller didnt respond with Yes/No then return the following message.
else
{
cmd.Data.Caller.Respond("Not a proper answer. Termination cancelled.");
}
}
}
Single threaded question mechanism requires slightly more effort when compared to Multi-Threaded questioning. We recommend using Single-Threaded questioning technique where ever possible as it has been known to be more thread-safe when compared to multi-threaded techniques.

In the sample code above we create a response which is "Are you sure ( Yes / No )?" and then we check if the Caller is in a question session. Question session is internal term given to a situation where the Caller has been asked a Question and the Engine has not received a reply yet. So if the Caller is not in a Question Session well we put him in one. We question the Caller "Are you sure ( Yes / No )". The response is immediately sent to the Caller and the Caller is then put inside a Question Session. Now when the Caller replies we then check if the Context ( current ) contains any BooleanData that is if the Context contains any word like Yes / Yea / Ok. If so we check that BooleanData and see if its value is set to True. If not we reply to the Caller "You did not confirm".

Once a reply is sent to the Caller the Caller is no more in a Question Session and the Engine will no longer interact with our Exit Function. The next part of our sample code is executed if the BooleanData has a False value i.e. No / Nope / Not All etc. If so then we reply to the Caller that "You did not confirm".

Finally what if the Caller didn't say anything that could be parsed into a BooleanData ? That is exactly when our function will respond to the Caller that "Not a proper answer. Termination Cancelled."

Developers should note that if your do not use the Question function while trying to control program execution the function will be ignored entirely and a new session will be considered if a reply is received from the user.

Return to “Archived (Old Posts)”

Who is online

Users browsing this forum: No registered users and 1 guest