Home -> Support -> VoiceXML Examples

Mapping of Semantics to Slots, Filled and Log Example

example11.vxml
<?xml version="1.0" encoding="UTF-8"?>
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">

<meta name="description" content="mapping of semantics to slots, filled and log example"/>
<meta name="author" content="OptimSys, s.r.o., Czech Republic (http://www.optimsys.cz)"/>
<meta name="copyright" content="free for any purpose"/>

<!-- very useful script for debugging - dumps value of a variable -->
<script> <![CDATA[
  function dump (v, name) {
    var out = "";
    try {
      if (typeof v == "undefined") {
        out = name + ": undefined\n";
      }
      else if (typeof v == "number") {
        out = name + ": " + v + "\n";
      }
      else if (typeof v == "boolean") {
        out = name + ": " + v + "\n";
      }
      else if (typeof v == "string") {
        out = name + ": \"" + v + "\"\n";
      }
      else if (typeof v == "object") {
        if (v == null) { // null
          out = name + ": null\n";
        }
        else { // object or array
          for (var prop in v) {
            if (v.length!==undefined) {
              // it is an array
              if (prop != "") { // fix for multidimensional arrays
                out = out + dump (v[prop], name+"["+prop+"]");
              }
            }
            else {
              // it is an "normal" object
              out = out + dump (v[prop], name+"."+prop);
            }
          }
        }
      }
    }
    catch (e) {
    }
    return out;
  }
]]> </script>

<form id="start">
  <nomatch>
    <prompt> I didn't understand you </prompt>
    <log expr="(new Date()).toLocaleString()"> nomatch event occured </log>
  </nomatch>

  <grammar src="order.grxml"/>

  <initial name="mixed">
    I want the following information from you: drink type, number of pizzas,
    size of pizzas and topping of pizzas. Say only isolated words like 'sprite
    big cheese ham three'
  </initial>

  <field name="drink" cond="false"/> <!-- will be never visited -->
  <filled namelist="drink">
    <prompt> You specified drink. </prompt>
  </filled>

  <field name="pizza" cond="false"/> <!-- will be never visited -->

  <field name="pizzanumber" slot="pizza.number" cond="false"/> <!-- will be never visited -->
  <filled namelist="pizzanumber">
    <prompt> You specified number of your pizzas. </prompt>
  </filled>

  <field name="pizzasize" slot="pizza.size" cond="false"/> <!-- will be never visited -->
  <filled namelist="pizzasize">
    <prompt> You specified size of your pizzas. </prompt>
  </filled>

  <field name="pizzatopping" slot="pizza.topping" cond="false"/> <!-- will be never visited -->
  <filled namelist="pizzatopping">
    <prompt> You specified topping of your pizzas. </prompt>
  </filled>

  <block>
    <prompt> Dump of variable pizza: </prompt>
    <prompt> <value expr="dump(pizza, 'pizza')"/> </prompt>
    <prompt> Dump of variable drink: </prompt>
    <prompt> <value expr="dump(drink, 'drink')"/> </prompt>
  </block>
</form>

</vxml>
drink.grxml
<?xml version="1.0" encoding="UTF-8"?>
<grammar version="1.0" xml:lang="en">

  <meta name="description" content="grammar with drinks"/>
  <meta name="author" content="OptimSys, s.r.o., Czech Republic (http://www.optimsys.cz)"/>
  <meta name="copyright" content="free for any purpose"/>

  <rule id="drink" scope="public">
    <one-of>
      <item>coca cola<tag>out="coke"</tag></item>
      <item>coke</item>
      <item>sprite</item>
      <item>fanta</item>
    </one-of>
  </rule>

</grammar>
order.grxml
<?xml version="1.0" encoding="UTF-8"?>
<grammar version="1.0" root="order" xml:lang="en">

  <meta name="description" content="grammar for ordering a pizza and a drink"/>
  <meta name="author" content="OptimSys, s.r.o., Czech Republic (http://www.optimsys.cz)"/>
  <meta name="copyright" content="free for any purpose"/>

  <rule id="order" scope="public">
    <tag>out.pizza = new Object;</tag>
    <item repeat="1-">
      <one-of>
        <item>
          <ruleref uri="drink.grxml#drink"/>
          <tag>out.drink = rules.drink</tag>
        </item>

        <item>
          <ruleref uri="pizza.grxml#number"/>
          <tag>out.pizza.number = rules.number</tag>
        </item>

        <item>
          <ruleref uri="pizza.grxml#size"/>
          <tag>out.pizza.size = rules.size</tag>
        </item>

        <item>
          <ruleref uri="pizza.grxml#topping"/>
          <tag>
            if (typeof out.pizza.topping != "object") out.pizza.topping = new Array;
            out.pizza.topping.push (rules.topping);
          </tag>
        </item>
      </one-of>
    </item>
  </rule>

</grammar>
pizza.grxml
<?xml version="1.0" encoding="UTF-8"?>
<grammar version="1.0" xml:lang="en">

  <meta name="description" content="grammar for description of a pizza"/>
  <meta name="author" content="OptimSys, s.r.o., Czech Republic (http://www.optimsys.cz)"/>
  <meta name="copyright" content="free for any purpose"/>

  <rule id="number" scope="public">
    <one-of>
      <item> a <tag>out=1</tag></item>
      <item> one <tag>out=1</tag></item>
      <item> 1 </item>
      <item> two <tag>out=2</tag></item>
      <item> 2 </item>
      <item> three <tag>out=3</tag></item>
      <item> 3 </item>
      <item> four <tag>out=4</tag></item>
      <item> 4 </item>
      <item> five <tag>out=5</tag></item>
      <item> 5 </item>
    </one-of>
  </rule>

  <rule id="size" scope="public">
    <one-of>
      <item><ruleref uri="#size_small"/><tag>out="small"</tag></item>
      <item><ruleref uri="#size_medium"/><tag>out="medium"</tag></item>
      <item><ruleref uri="#size_big"/><tag>out="big"</tag></item>
    </one-of>
    <item repeat="0-1"> size </item>
  </rule>

  <rule id="size_small">
    small
  </rule>

  <rule id="size_medium">
    <one-of>
      <item> average </item>
      <item> medium </item>
      <item> normal </item>
    </one-of>
  </rule>

  <rule id="size_big">
    <one-of>
      <item> big </item>
      <item> large </item>
    </one-of>
  </rule>

  <rule id="toppings" scope="public">
    <tag> out = new Array; </tag>
    <item repeat="1-">
      <ruleref uri="#topping"/>
      <tag> out.push(rules.topping); </tag>
    </item>
  </rule>

  <rule id="topping" scope="public">
    <one-of>
      <item> cheese </item>
      <item> ham </item>
      <item> pepperoni </item>
      <item> eggs </item>
      <item> anchovies </item>
      <item> mushrooms </item>
    </one-of>
  </rule>

</grammar>