service-items.taq

$ java -jar taq.jar service-items

Running query scan_service_items in global scope 
charges(Service=83057, Amount=USD60.00)
charges(Service=93001, Amount=USD0.00)
charges(Service=10800, Amount=USD30.00)
charges(Service=10661, Amount=USD45.00)
charges(Service=00200, Amount=USD0.00)
charges(Service=78587, Amount=USD15.00)
charges(Service=99585, Amount=USD10.00)
charges(Service=99900, Amount=USD5.00)
accumulator(total=165.00)

Description

service-items.taq demonstrates a regular expression pattern used to filter incoming items as well as extract text. Each item contains a service number prefixed with a ‘#’ character, and in most cases, an amount. A missing amount indicates a free service. Using a regular expression combines data capture from eligible services with skipping over the ineligible services.


export axiom accumulator(total) { 0.0 }

string servicePattern = "#([0-9]+)"
string amountPattern = "(\\$[0-9]+\\.[0-9]+)"
string scanPattern =
"^Service " + servicePattern +
"\\s+" + amountPattern + "?$"

pattern itemPattern scanPattern

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

query scan_service_items(items : scan_items)

list<string> account_info =
{
"Invoice #00035",
"Service #83057 $60.00",
"Service #93001 ",
"Service #10800 $30.00",
"Service #10661 $45.00",
"Service #00200 ",
"Service #78587 $15.00",
"Service #99585 $10.00",
"Service #99900 $5.00"
}

axiom items(item) { list account_info }