Getting Started with AuQL

AuQL can seem daunting, but getting started need not be difficult. This article presents a number of simple but practical scripts that illustrate AuQL syntax and also demonstrate practical solutions to common types of scripting tasks.

Basic Integer Operations

Among the simplest possible scripts, this example asks whether one is less than two.


    # is 1 less than 2?
    function main(){
      result = Logic.LessThan(1, 2)
      return(value = result)

Passing Values To and From Functions

The following script demonstrates how to pass two parameters (called arguments in some langauges) as inputs for a function. AuQL Parameters are named and typed (left and right are both specified as integers). A default value can also provided as they are here, but this is not required. The two parameters are referenced by name as inputs of the addition operation.

In addition passing paremeters in, AuQL allows functions to pass a value back to the calling context. In this case, the sum of the Math.Add function called in this function's first statement is returned to the user as the named value result. As with input parameters, AuQL functions can return an arbitrary number of parameters by separating each parameter to return with commas (e.g. return(x = 5, y = 3, z = 4)).

function main(left = int(1), right = int(2)){
  sum = Math.Add(left, right)
  return(result = sum)

Formatting Strings

The following script demonstrates string manipulation, in this case for the purpose of creating user-friendly messages out of dynamic information.

function main(org = str("Aunalytics"), rate = int(89)){

  # Define a template string
  template = "Job completion rate for %s is %s% as of '%s'"

  # Pack values into an array using Util.ArrayOf()
  args = Util.ArrayOf(org, rate, Date.Now())

  # Format output string with String.Format()
  output = String.Format(template, args)

  return(value = output)

Invoking Subscripts

This example multiplies each element of the array [1, 2, 3, 4, 5] by two and returns the mapped array. Here we are using the Util.MapArray operator, which requires a function to be passed as an argument. We define the mapper function below the main function and "import" it to the main function using the Util.Function operator.

function main(){
  arr = [1,2,3,4,5]
  mapper = Util.Function("mapper")
  output = Util.MapArray(arr, mapper)
  return(value = output)

function mapper(value = int()){
  product = Math.Multiply(value, 2)
  return(value = product)

Is My Dataset Growing?

This script fetches information from a Memento Series that has the stats of a record written to it periodically. It then asserts that the size of the record is growing by testing whether the newest filesize is greater than the second newest.

function main(series_id = str("a27c46b6-6634-4f41-951a-5ca753c8793f"), atlas_id = str())
  # fetch the latest two mementos
  mementos = AU.Memento.LatestN(series_id, 2)

  # fetch and parse from the newest memento
  new_memento_str = Util.Get(mementos, 0)
  new_memento = Util.Object(new_memento_str)
  new_record = Util.Get(new_memento, atlas_id)
  new_filesize = Util.Get(new_record, "file_size")

  # fetch and parse from the 2nd newest memento
  old_memento_str = Util.Get(mementos, 1)
  old_memento = Util.Object(old_memento_str)
  old_record = Util.Get(old_memento, atlas_id)
  old_filesize = Util.Get(old_record, "file_size")

  # is the 1st greater than the 2nd?
  comparator = Logic.GreaterThan(new_filesize, old_filesize)

  return(value = comparator)

AI-Designed Date Checking Algorithm

This script is an interesting one insofar as the logic chain was developed by AI and put into a Rule by a human. The script asks whether today is a day that the algorithm determined to be a "good" day.

function main(max_date = String()) {
  convert_to_date = Util.MakeDate(date = max_date)

  days_diff = Date.Difference(Date.Now(), convert_to_date, unit = String("days"))

  md_dow = Date.GetDay(convert_to_date)
  cd_dow = Date.GetDay(convert_to_date)

  gt1 = Logic.GreaterThan(days_diff, 2)

  gt2 = Logic.GreaterThan(cd_dow, 2)

  gt3 = Logic.GreaterThan(md_dow, 4)

  gt4 = Logic.GreaterThan(days_diff, 3)

  lte1 = Logic.LessThanEqual(cd_dow, 2)

  and1_1 = Logic.And(gt1, gt2)

  and2_1 = Logic.And(gt1, lte1)

  and2_2 = Logic.And(and2_1, gt3)

  and2_3 = Logic.And(and2_2, gt4)

  or1_1 = Logic.Or(and1_1, and2_3)

  return(value = or1_1)

Format Parameters for RunWorkflowMulti

This script takes an input object and maps an array in it so that it can be passed to a RunWorkflowMulti Workflow component.

function main(input = Object()){
  get = Util.Get(input, "NewRecords")
  parseJSON = Util.Object(get)

  mapper = Util.Function(str("embedded"))
  map = Util.MapArray(parseJSON, mapper, opts = input)

  return(value = map)

function embedded(value = str(), index = int(), opts = Object()){
  unset = Util.Unset(opts, "NewRecords")
  setNewFile = Util.Set(unset, "NewRecords", value)
  return(value = setNewFile)