Multi-Threaded Responses
Developers may at times wish to call slow async operations when certain SIML Models are activated. For such scenarios we recommend working with SIML Events instead of making your app wait for responses to be generated.
The following Wiki Adapter demonstrates how an Adapter can trigger SIML Events on task completion and pass relevant information in temporary var variables.
Example Adapter
class WikiAdapter: IAdapter
{
public XName TagName => SimlSpecification.Namespace.X + "Wiki";
public bool IsRecursive => true;
public string Evaluate(Context context)
{
Task.Factory.StartNew(() =>
{
var result = WikipediaApi.Search(context.Element.Value);
context.User.Vars["Wiki-Query"] = context.Element.Value;
var dispatcher = //Some Dispatcher instance.
if (dispatcher != null && !string.IsNullOrEmpty(extract))
{
dispatcher.Invoke(() =>
{
context.User.Vars["Wiki-Extract"].Value = result;
context.Bot.Raise("Wiki-Result", context.User);
});
}
else
{
context.Bot.Raise("Wiki-No-Result");
}
});
return string.Empty;
}
}
In the above code
WikipediaApi
is some static API class that retrieves first extract from an article that matches the query.
Example SIML Code
<Model>
<Pattern>Search Wikipedia for *</Pattern>
<Response>
Searching Wikipedia for "<Match/>".
<x:Wiki>
<Match />
</x:Wiki>
</Response>
</Model>
<Event>
<Pattern>Wiki-Result</Pattern>
<Response>
<Var Get="Wiki-Extract" />
</Response>
</Event>
<Event>
<Pattern>WIKI-NO-RESULT</Pattern>
<Response>Sorry, I couldn't find any Wikipedia article on "<Var Get="Wiki-Query" />"</Response>
</Event>
User Input
Search wikipedia for Random Access Memory.
Bot Output (Immediate)
Searching Wikipedia for "Random Access Memory".
Event Output (Later)
Random-access memory is a form of computer data storage. A random-access memory device allows data items to be read or written in almost the same amount ...
Explanation
Once the Wiki adapter it called it returns an empty response immediately. However, after an Extract from Wikipedia is obtained then Adapter triggers the event Wiki-Result
. The value of the variable Wiki-Extract
represents the extract received from Wikipedia API.
If no results are obtained the Adapter triggers the event Wiki-No-Result
.