pet-names.taq

$ java -jar taq.jar regex-pet-names

Running query pets in global scope 
Lassie
Cuddles
Bruiser
Rex
Pixie
Axel
Amiele
Fido

Running query reverse_pets in global scope 
Fido
Amiele
Axel
Pixie
Rex
Bruiser
Cuddles
Lassie

Description

pet-names.taq shows a cursor used in combination with a regular expression. It is critical that the cursor either increments or decrements when accessed by the regular expression or else an infinite loop is created when the first non-match occurs. Hence for query “pets” progressing in the forward direction, the “pet” cursor increments

? pet++ #petName ( name )

Reaching the end of the “pets_info” list is easy to detect

? fact pet

The keyword reverse is used to nagivate the “pet_info” list in reverse and the the “pet” cursor decrements

reverse cursor pet(pets_info)
… ? pet– #petName ( name )


string namePattern = "<name>([a-zA-z']*)[^a-zA-z']"
string petPattern = "^.*" + namePattern +".*"
pattern petName petPattern

flow pets
{ export list<string> pet_names }
(
cursor<string> pet(pets_xml),
{
? fact pet,
? pet++ #petName ( name ),
pet_names += name
}
)

flow reverse_pets
{ export list<string> pet_names }
(
reverse cursor<string> pet(pets_xml),
{
? fact pet,
? pet-- #petName ( name ),
pet_names += name
}
)

list<string> pets_xml =
{
"<pet><species>dog</species><name>Lassie</name><color>blonde</color></pet>",
"<pet><species>cat</species><name>Cuddles</name><color>tortoise</color></pet>",
"<pet><species>Dog</species><name>Bruiser</name><color>brindle</color></pet>",
"<pet><species>Dog</species><name>Rex</name><color>black and tan</color></pet>",
"<pet><species>Cat</species><name>Pixie</name><color>black</color></pet>",
"<pet><species>dog</species><name>Axel</name><color>white</color></pet>",
"<pet><species>Cat</species><name>Amiele</name><color>ginger</color></pet>",
"<pet><species>dog</species><name>Fido</name><color>brown</color></pet>"
}

query pets(list pets_xml : pets)

query reverse_pets(list pets_xml : reverse_pets)