5. Set Context

This session demonstrates how to your own context to override the default context. In fact, the contextual cells are so important for our Virtual Tutor that, we will keep talking about context in not only this but also next session.

The Problem

It's boring if our Tutor keeps saying "Hello!" to students every time when they open the virtual classroom. Naturally we want to a bit of Random elements to our opening message. This can be done by improving our cell as below.

Example 1.Random

  • Cell 1
    • myApp sayHello

    •         <rand>
                <i>Hello!</i>
                <i>Hey!</i>
                <i>Hi!</i>
              </rand>
           

Check BrainXML Reference for more information on rand element. Basically it returns a random answer each time when the cell is executed, for our Virtual Tutor, each time when students open the virtual Classroom.

Good but we get a problem: The context keeps changing because the last sentence of message sent by bot. To ensure our contextual cell below works, shall we create a contextual cell for each random reply?

Cell 2. Contextual Hello.

    • Hello
    • Hello
    • How is the weather there?

The Solution

Luckily, we don't have to create a contextual cell for each reply, because of set-context element. This is our opening message cell improved with set-context.

Example 2. Setting context

  • Cell 1
    • myApp sayHello

    •         <rand>
                <i><set-context>Hello</set-context>!</i>
                <i>Hey!</i>
                <i>Hi!</i>
              </rand>
           

Or to improve readability, we place set-context within think element.

Example 2. Setting context within think.

  • Cell 1
    • myApp sayHello

    •         <rand>
                <i>Hello!</i>
                <i>Hey!</i>
                <i>Hi!</i>
              </rand>
              <think><set-context>Hello</set-context></think>
           

Either of the above cells set the context as "Hello" regardless what reply they returns in the run time. If you haven't learned the think element, it executes the enclosed content in the brain but suppresses the results from the final output. As its name indicates, it doesn't return visible text to end users.

The outcome is our contextual cell will always work even the bot gives different opening message to students.

Furthermore, set-context works the same if you use it with multiple cells.

What to Keep in Mind

  • Use set-context if you apply the same contextual cell to literately different messages by bot.
  • Use set-context along with think if you don't want to return the context string to end users.

In addition, you may use rand to return dynamic message for the same input.