Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

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 (of the book The Live Web: Building Event-based Connections in the Cloud (download PDF of 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. This is used in three ways: a namespace as a built-in library (such as event), a namespace for a KRL module, the namespace ent for entity variables.)

Receiving an event attribute

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

event:attr("url").extract(re#http://([^/:]+)#)

Note that the syntax above has been deprecated in favor of

event:attrs{"url"}.extract(re#http://([^/:]+)#)

Sending an event 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 {
   select when explicit testEventAttributes
      pre {
         foo = event:attr("foo"); // Equals bar at this point
      }
      noop()
}


If an event attribute does not exist, the expression 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.

Example
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 or any other false value




  • No labels