Event Attributes

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#https?://([^/:]+)#)

Note that the syntax above has been deprecated in favor of

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

Other alternatives

Besides the alternatives shown above (short form that is deprecated, and the official form), there are other ways to obtain an event attribute’s value inside a rule. The first two above are expressions which can appear anywhere a KRL expression can appear (the where clause of a select when, the prelude of a rule, the condition of a rule, and a postlude of a rule, the expression in a foreach, etc.).

In addition, an expression like the following could be used anywhere a KRL expression can appear:

event:attrs.get("url").extract(re#https?://([^/:]+)#)

That works because event:attrs evaluates to a KRL map, so that any of Map Operators and expressions can be used with it. As an additional example, if we just wanted to see if the attributes included an attribute named “url” we could use:

Finally, as one of the matches in a select when we could use (see Event Expressions in the Grammar page):

This does more than simply extract the domain name from an event attribute named “url” but causes the rule to be selected only when there actually is an attributed named “url” associated with the event. Furthermore, it binds the domain portion of said URL to the name domain which is now in scope for the entire rule.

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:

This attribute could be used in the following rule:


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







Copyright Picolabs | Licensed under Creative Commons.