Learning and Memorizing
Every unit of knowledge in SIML is created using Models. So learning and memorizing almost always revolves around saving and loading SIML Models. Learning involves loading of Models and Concepts to the main Graph of the Bot while memorizing is a similar process but is tightly integrated to the user involved in triggering the Memorizing process.
Learning and saving a Learned Model to disk
Whenever a Learn element is used in a Response and that particular response gets activated the Learning event is fired by the Bot. The LearningEventArgs
provides the SIML document (along with the User who triggered the event) that can be saved to disk and later be loaded into the Bot’s graph.
An event handler can be attached to the Learning event of the Bot using the code below.
var simlBot = new SimlBot();
simlBot.Learning += SimlBot_Learning;
In the following code whenever the Learning event is fired we save it to a disk file under the name Learned.siml
. The e.Document is the complete SIML code which includes the root SIML element along with the Concept and Models learned.
void SimlBot_Learning(object sender, LearningEventArgs e)
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Learned.siml");
e.Document.Save(filePath);
}
For example if your SIML code looks like the following:
<Siml>
<Concept Name="Learning new Models" Type="Public">
<Model>
<Pattern>A * IS A *</Pattern>
<Response>
<Learn>
<Model>
<Pattern>What is a <Process><Match /></Process></Pattern>
<Response>It's a <Process><Match Index="2" /></Process></Response>
</Model>
</Learn>
Alright I will keep that in mind.
</Response>
</Model>
</Concept>
</Siml>
And if the user says A car is a vehicle then the Document property would return an SIML document like the following:
<Siml>
<Concept Name="Learning new Models" Type="Public">
<Model>
<Pattern>WHAT IS A CAR</Pattern>
<Response>It's a vehicle</Response>
</Model>
</Concept>
</Siml>
Note
Because in our Learn element we didn’t explicitly specify a Concept the Concept part gets derived from the Concept to which the Learn element belonged to.
Now, Once you’ve saved the document to a disk file you can any time load the saved document back into the Bot’s graph (If you restart the Bot later on) by using the following code. All the code does is creates a filePath variable that points to the SIML file to be loaded and then creates an XDocument by using the XDocument.Load method and later adds the document to the Bot using the AddSiml method, the overload of which accepts an XElement. The XElement MUST be the root Siml element
var simlBot = new SimlBot();
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Learned.siml");
simlBot.AddSiml(XDocument.Load(filePath).Root);
Memorizing and saving Memorized Models to disk
If a Bot is asked by a user to remember a fact the Bot updates an isolated Graph associated with that particular user. This graph is a separate Graph kept in memory just for interacting and understanding that particular user.
A Memorizing event is fired whenever a user asks the bot to remember a fact and the graph associated with that user is updated. A MemorizingEventArgs
is passed as an argument to the event handler which provides the user information and the SIML document that may be saved to disk.
Saving a Memorized Model to disk is similar to saving a Learned Model to disk i.e. you’ll have to attach an event handler.
var simlBot = new SimlBot();
simlBot.Memorizing += simlBot_Memorizing;
void simlBot_Memorizing(object sender, MemorizingEventArgs e)
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), e.User.ID, "Memorized.siml");
e.Document.Save(filePath);
}
In the above code we save the Memorized document to a folder that has a name similar to the ID of the user. This helps us to load the right memorized Model to the right Graph.
To load the Memorized document we’ll have to tell the Bot the User for which the document is being loaded. This can be done using an overload of the AddSiml
method as shown below.
var simlBot = new SimlBot();
var onlineUser = new BotUser(simlBot, "X-15652C");
var filePath = Path.Combine(Directory.GetCurrentDirectory(), onlineUser.ID, "Memorized.siml");
var memorizedDocument = XDocument.Load(filePath);
simlBot.AddSiml(memorizedDocument,onlineUser);
As you can see in the above code we add the SIML to the Bot but we also pass the User as an argument to tell the Bot that the document belongs to the specified user.