Template-Axiom-Query (TAQ)

Regular Expressions

Regular expressions were introduced in Operations. Each regular expression is declared with a unique name using the pattern keyword. Here is the declaration from query_in_words.taq

pattern in_words "^in[^ ]+"

A hash '#' character is used as a regular expression operator and is followed by the name of a pattern. Here is the match operation which applies the "in_words" pattern to a template term named "word"

word # in_words

Before the match operation takes place, "word" is loaded with an "i" word dictionary definition. If there is no match, then execution short circuits instead of continuing on to produce a solution.

TAQ regular expressions are implemented using the Java Pattern class. Regular expression features are:

  • Can be applied as either a term or criterion expression
  • Announced with'distinctive #' symbol
  • Options are available , like case-insensitive match
  • A cursor can provide input
  • Group variables are configurable (type, default)

Criterion

When placed in a criterion, a regular expression acts like a boolean expression and supports grouping. group_in_words.taq demonstrates a criterion using a regular expression with grouping. The pattern to match on is named "defPattern" and uses grouping to extract the meaning of a word from a dictionary entry

definition #defPattern ( def )

Here is the program showing it has another regular expression to select only words beginning with "in"

include "i-words.taq"

pattern in_word "^in[^ ]+"
pattern defPattern "^[nvaj.]+ (.*+)"

flow in_words
{
string word,
string definition
}
(
term word,
term definition,
word # in_word,
? definition #defPattern ( def )
{ definition = def }
)

Group Type and Default

service-items.taq is An example of a group item having both type and default value applied prior to the regular expression match. A number of services are input to the "scan_service_items" query and each service is identified by a code and attracts a charge specified in dollar and cents. However, the charge is omitted if the service is for free. A regular expression extracts as group items, a service code and amount. Each amount needs to be converted from text to a currency type, and if the amount is absent, a default value of $0.00 is to be applied. A final consideration is that the input has a line specifying an account number which the regular expression will fail to match, so a conditional branch is needed to allow the input to be skipped in this case. Here is an extract of the program "scan_items" flow. Note the declaration and setting of the "amount" variable which collects the second group item of the "itemPattern" pattern

flow scan_items
{
export list<axiom> charges,
cursor<string> item,
currency.USD amount
}
(
{
? fact item,
amount = 0.0,
? item++ # itemPattern ( service, amount )
{
charges += axiom {
Service = service,
Amount = amount.format()
}
}
}
}