Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Event attributes are one of the most important sources of data for computation in a rule because they convey information about the context of the event. In Chapter 3, you saw how eventexes can use expressions on event attributes as filters for which events match a given primitive eventex. Event attributes can also be used in KRL expressions. To avoid conflict with user-declared variables names, event attributes are namespaced using the keyword event. (Prepending the namespace name and a colon to a variable forms namespaces in KRL.)

 


Assume that a rule has been selected with an event attribute url. The following KRL expression could be used to extract the domain name from the url attribute:
 

...

Event attributes can be sent to an event in different ways. One way is by using GET or POST requests with the HTTP library. The other way is to include attributes when raising an event. The following KRL would raise an event with event attributes: 


raise explicit event "testEventAttributes" for a1x100
   attributes {"foo":"bar"};

...


This attribute could be used in the following rule: 


rule testEventAttributes {

...

         foo = event:attr("foo"); // Equals bar at this point
      }
      noop()

}

If an event attribute does not exist, it will evaluate to null. Idiomatic KRL is to use the defaultsTo() operator to give it a default value if one is desired, or the || (OR) operator to reassign the empty string as well.

Code Block
languagejs
titleExample
name = event:attr("name").defaultsTo("John Doe") // name will be bound to "John Doe" if the event attribute is null
name = event:attr("name") || ent:name // name will be bound to the value of ent:name if the event attribute is the empty string or null