Quick Links
AIML Tutorials
MakeAiml Tutorials
C Programming
Download
Advertisements
Template Creation With Macros and Looping Tutorial
Introduction
In this tutorial you will learn how to create combine macros and loops in MakeAiml.
Combining Loops and Macros
MakeAiml Pattern-For loops and macros work very well together and enable a programmer to create reusable, easily fixable, and easily debuggable code. One good method for combining the two can be seen below:
$ like_terms like love
p i
pf $$like_terms
p fish
t You like fish? I HATE fish!
p i
pf $$like_terms
p dogs
t I like dogs, too!
As you can see, a single macro was made to define general emotions and that one macro was reused for multiple Pattern-For loops. This makes the code easily expandable...if I want to add a new "like term" for the bot to watch for, all I need to do is place it at the end of the like_terms macro and it is automatically expanded by MakeAiml everywhere else in the file! If this new term were added into an AIML file directly, a programmer would most likely spend anywhere from a few minutes to a few hours copying and pasting old AIML tags and editing them to provide the new "like term". In addition to this expandability, typos are easily fixed by consolidating all the "like terms" into one macro. If I make a typo in the list of "like terms" I reduce the probability of copying and pasting that typo elsewhere in the code and only need to fix the typo in one location.
Loop Tricks
In the release of MakeAiml 2.05 I added a feature that allows a user to directly reference elements in a pattern-for loop. A reference looks like a macro and is handled the same way. References look like the following: $$[A][B], where A and B are integers used to index into the list of elements (starting from 0). Here's an example:
p i
pf " " really
p love
pf dogs cats fish animals
t You love $$[1][*]? I love all kinds of $$[1][3]!
There are two references in the template line, the second template is $$[1][3] and has values of A=1 and B=3. This reference will expand to become the fourth element of the second pattern-for loop. The numbering scheme starts from zero because arrays in most programming languages follow this numbering scheme, it may be confusing to begin with, but learning this indexing scheme will only help prepare you to use other programming languages. You can think of the indexes for the example above in terms of the following table:
$$[A][B] is:
B 0 1 2 3
A
0 " " really
1 dogs cats fish animals
There is a special value that can be used for element B and it is the asterisk (*) character. As each pattern-for element is looped through, the special index * will choose the current loop element that is being processed. The best way I can suggest to learn how pattern-for elements are processed is to compare the following MakeAiml and AIML output.
p 1 pf a b c p 2 pf x y z t 3
<pattern>1 A 2 X</pattern><template>3</template> <pattern>1 B 2 X</pattern><template>3</template> <pattern>1 C 2 X</pattern><template>3</template> <pattern>1 A 2 Y</pattern><template>3</template> <pattern>1 B 2 Y</pattern><template>3</template> <pattern>1 C 2 Y</pattern><template>3</template> <pattern>1 A 2 Z</pattern><template>3</template> <pattern>1 B 2 Z</pattern><template>3</template> <pattern>1 C 2 Z</pattern><template>3</template>
By generating the above in MakeAiml and paying attention to what values appear in which area of the pattern it will become clear the order in which each element was processed. This will provide insight into how to index elements in the pattern-for list.
About the Author
Grant Dryden works as a computer engineer. He writes software in C and C++ for embedded systems as well as firmware in VHDL. He has a Bachelor of Science in Computer Engineering.

