Context repository

Definition

The context repository is a database that allows storing user data that could be useful in the event rules definition and not present in the data messages. The context may include, for instance, thresholds definition, geographical zones, a list of device identifiers, a user preference, a group of contexts. The context has a key-value format. The key is a string and the value can be a primitive (string, numeric…​), a JSON object or an array. Optional tags are available to ease the search among the tenant contexts.

for geographical zones, a dedicated geozone database is provided. Once the user has provisioned his geozones, they are automatically available in the user context.

Context provisioning

The Live Objects API to manage context provisioning are described in the swagger documentation (Event processing - Context section) : https://liveobjects.orange-business.com/swagger-ui/index.html.

Context groups

A context value may reference other context keys. Instead of referencing each context individually, the rule can then reference the context group.

Example: See a context groups example.

extract context key

A context key is not necessarily hard coded in your rule. For instance, it can be extracted from your data message (using tags or device identifier).

Here, the context key is generated with the concatenation of the value.streamId field and a string.
 {"ctx" : {"cat":[{"var" : "value.streamId"},"alertingzone"]}}
Here, the context key is extracted from the value.tags field.
"ctx": { "get": [{"filter": [{"var": "value.tags"},"zone"]},0]}

Geozone repository

Definition

The Geozone repository is a database that allows the user to save his geographical sites/zones of interest. The geozones are stored as polygons (array of geopoints coordinates in decimal degrees). Meta information like a description and tags can be stored with the geozone.

Format:
  • coordinate order for polygon definition : use longitude as the first coordinate and latitude as the second coordinate.

  • the polygons are closed linestrings. Closed LineStrings have at least four coordinate pairs and specify the same position as the first and last coordinates.

Example of polygon :

[[[1.780892, 48.091452], [2.301382, 48.000565], [2.281961, 47.509630], [1.252634, 47.729556], [1.780892, 48.091452]]]

Provisioning

The Live Objects API to manage geozone provisioning are described in the swagger documentation (Event processing - Geozone section) : https://liveobjects.orange-business.com/swagger-ui/index.html.

Example:

PUT liveobjects.orange-business.com/api/v0/eventprocessing/geozones/grand-orleans
{
  "description": "my geozone grand Orleans",
  "geometry": {
    "coordinates": [[
        [1.780892, 48.091452],
        [2.301382, 48.000565],
        [2.281961, 47.509630],
        [1.252634, 47.729556],
        [1.780892, 48.091452]
        ]],
    "type": "Polygon"
  },
  "tags": ["zone-nord"]
}
  • Once a geozone is provisioned, it is available in the user context. Hence, it can be referenced in event processing rules or in groups of context.

  • When a geozone is updated, the modifications are immediately taken into account by the contexts or rules referencing the geozone.

Rules and JsonLogic syntax

A rule is a function applied on a data message in order to detect any significant change in the data (exceeding threshold, state modification, change of location). The rules in Simple Event Processsing and State Processing are defined within Live Objects plateform with the JsonLogic syntax.

the JsonLogic log operator has been deactivated.

Additional operators

In addition to the existing JsonLogic operators (logic and boolean operators, numeric operators, string operators, array operators), Live Objects provides geographic operators (distance, inside, insideindex, closeto, closetoindex), context operator (ctx) and miscellaneous operators (get, currentstate).

Table 1. distance
Name

distance

Description

Geographical operator. Returns the distance in meters between two points, given their latitude and longitude in decimal degrees.

Parameters

lon1, lat1, lon2, lat2 in decimal degrees

Logic

{
  ">" :
    {
      "distance" : [
        { "var" : "location.lon"},
        {"var" : "location.lat"},
        2.296565,
        48.800206
      ]
    },
    6000
  ]
}

Data

Eiffel Tower

{
  "location":{
     "lon" : 2.2945,
     "lat" : 48.8584
  }
}

inside

Result

true

Table 2. ctx
Name

ctx

Description

Retrieve, from the context repository, one or several values using a key or an array of keys. Several ctx operators can be nested (group of contexts).

Parameters

key or array of keys

Context

In the following example, "freezingThreshold" and "liquidThreshold" must have been provisioned in the tenant context before being used.

PUT liveobjects.orange-business.com/api/v0/eventprocessing/context/freezingThreshold
{
  "contextData": 0
}

PUT liveobjects.orange-business.com/api/v0/eventprocessing/context/liquidThreshold
{
  "contextData": 100
}

Logic

{
  "if": [
    {"<": [
      {"var":"value.temp"},
      {"ctx": "freezingThreshold"}
    ]},
    "ice",
    {"<": [
      {"ctx": "freezingThreshold"},
      {"var":"value.temp"},
      {"ctx": "liquidThreshold"}
    ]},
    "liquid",
    "gas"
  ]
}

Pre-requisite

Data

{
  "value":{"temp":55}
}

Result

"liquid"

Table 3. currentstate
Name

currentstate

Description

Retrieve the current state of a device when applying a stateProcessing function. For state processing rules only. In the following example, current state can be "cold", "normal" or "hot".

The example following logic function is an hysteresis :

if current state is not hot, transition to hot if value.temp > 100

if current state is hot, transition to normal if value.temp < 80

if value.temp < 0 transition to cold

Logic

{"if" : [
  {"and": [
    { "!==": [
      { "currentstate": [] },
      "hot"
    ]},
    {"<": [
      80,
      {"var": "value.temp"},100
    ]}
  ]},
  "normal",
  {"<": [
    {"var":"value.temp"},
    0
  ]},
  "cold",
  {"<": [
    {"var":"value.temp"},
    80
  ]},
  "normal",
  "hot"
]}

Data

{
  "value":{"temp":20.0}
}

Result

"normal"

Table 4. get
Name

get

Description

Returns the element at the specified position in an array.

Parameters

array, index in the array

Context

In the following example, an array containing latitude and longitude values must have been provisioned in the tenant context :

PUT liveobjects.orange-business.com/api/v0/eventprocessing/context/2geopoints
{
  "contextData": [48.800206, 2.296565, 48.800474, 2.295562]
}

Logic

{
  "distance": [
    {
      "get": [
        {"ctx": "2geopoints"}, <- lat1 in contextData value index 0
        0
      ]
    },
    {
      "get": [
        {"ctx": "2geopoints"}, <- lon1 in contextData value index 1
        1
      ]
    },
    {
      "get": [
        {"ctx": "2geopoints"}, <- lat2 in contextData value index 2
        2
      ]
    },
    {
      "get": [
        {"ctx": "2geopoints"}, <- lon2 in contextData value index 3
        3
      ]
    }
  ]
}

Data

 {}

Result

79 (distance between coordinates lat1,lon1 and lat2,lon2)

Table 5. inside
Name

inside

Description

Checks if a point defined by its latitude and longitude is inside a polygon (or at least one polygon if an array of polygons is provided as input parameter).

Parameters

longitude, latitude in decimal degrees for the point to be tested, polygon(s) defined by the coordinates of their vertices (lon, lat in decimal degrees).

Logic

{
  "inside": [
    {"var": "location.lon"},
    {"var": "location.lat"},
    [[
      [2.381121,48.627973],
      [2.129376,48.629499],
      [2.099351,48.768217],
      [2.116302,48.955198],
      [2.317994,48.927845],
      [2.455176,48.913357],
      [2.489472,48.841933],
      [2.392301,48.762871],
      [2.381121,48.627973]
    ]]
  ]
}

Data

{
  "location":{
    "lon":2.350350,
    "lat":48.854064
  }
}

inside

Result

true

Table 6. insideindex
Name

insideindex

Description

Checks if a point is inside an array of polygons. Returns the index of the first matching polygon. Returns -1 if no matching was found. This operator is usually in conjunction with the "get" operator which will return the matching polygon.

Parameters

longitude, latitude in decimal degrees for the point to be tested, array of polygons defined by the coordinates of their vertices (lon, lat in decimal degrees).

Context

In the example, an array containing latitude and longitude values must have been provisioned in the tenant context :

PUT liveobjects.orange-business.com/api/v0/eventprocessing/context/zone-nord
{
  "contextData": ["zone-grandparis", "zone-grandorleans"]
}

PUT liveobjects.orange-business.com/api/v0/eventprocessing/context/zone-grandparis
{
  "contextData": [[
    [2.381121, 48.627973],
    [2.129376, 48.629499],
    [2.099351, 48.768217],
    [2.116302, 48.955198],
    [2.317994, 48.927845],
    [2.455176, 48.913357],
    [2.489472, 48.841933],
    [2.392301, 48.762871],
    [2.381121, 48.627973]
  ]]
}

PUT liveobjects.orange-business.com/api/v0/eventprocessing/context/zone-grandorleans
{
  "contextData": [[
    [1.780892, 48.091452],
    [2.301382, 48.000565],
    [2.281961, 47.509630],
    [1.252634, 47.729556],
    [1.780892, 48.091452]
  ]]
}

Logic

{
  "get": [
    {
      "ctx": {
        "get": [
          {
            "filter": [
              {"var": "tags"},
              "zone"
            ]
          },
          0
        ]
      }
    },
    {
      "insideindex": [
        {"var": "location.lon"},
        {"var": "location.lat"},
        {
          "ctx": {
            "ctx": {
              "get": [
                {
                  "filter": [
                    {"var": "tags"},
                    "zone"
                  ]
                },
                0
              ]
            }
          }
        }
      ]
    }
  ]
}

}

Data

{
  "location":{
    "lat" : 48.854064,
    "lon" : 2.350350
  },
  "tags" : [
    "otherTag2",
    "zone-nord",
    "otherTag1"
  ]
}

Result

"zone-grandparis"

Table 7. closeto
Name

closeto

Description

Checks if a circle is close to a polygon or at least one of the polygons (polygon array).

Parameters

longitude, latitude (in decimal degrees for the circle center), circle radius, polygon or array of polygons

Logic

{
  "closeto": [
    { "var": "location.lon" },
    { "var": "location.lat" },
    { "var": "location.accuracy" },
    [
      [[
        [1.780892,48.091452],
        [2.301382,48.000565],
        [2.281961,47.509630],
        [1.252634,47.729556],
        [1.780892,48.091452]
      ]],
      [[
        [2.281961,47.509630],
        [1.252634,47.729556]
      ]],
      [[
        [2.22412,48.85863],
        [2.25219,48.88143],
        [2.28404,48.8785],
        [2.26816,48.86721],
        [2.2588,48.84913],
        [2.22859,48.85004],
        [2.22412,48.85863]
      ]]
    ]
  ]
}

Data1 : circle center outside polygons, the circle does not intersect any polygon.

{
  "location":{
    "lon": 2.263849,
    "lat": 48.855983,
    "accuracy" : 100
  }
}

closeTo1

Result1

false

Data2 : circle center outside polygons, the circle intersects one polygon.

{
  "lon" : 2.263849,
  "lat" : 48.855983,
  "accuracy" : 200
}

closeTo2

Result2

true

Data3 : a point inside one of the polygons.

{
  "lon" : 2.260265350341797,
  "lat" : 48.85693640789798,
  "accuracy" : 0
}

closeTo3

Result3

true

Table 8. closetoindex
Name

closetoindex

Description

Checks if a circle is close to an array of polygons. Returns the index of the first matching polygon (first index in the array is 0). Returns -1 if no matching was found.

Parameters

longitude, latitude (in decimal degrees for the circle center), circle radius, array of polygons

Logic

{ "
  closetoindex" : [
    { "var" : "location.lon"},
    { "var" : "location.lat"},
    { "var" : "location.accuracy"} ,
    [
      [[
        [1.780892, 48.091452],
        [2.301382, 48.000565],
        [2.281961, 47.509630],
        [1.252634, 47.729556],
        [1.780892, 48.091452]
      ]],
      [[
        [2.224120, 48.858630],
        [2.252190, 48.881430],
        [2.284040, 48.878500],
        [2.268160, 48.867210],
        [2.258800, 48.849130],
        [2.228590, 48.850040],
        [2.224120, 48.858630]
      ]]
    ]
  ]
}

N.B.: first polygon in the array is the Orleans area; 2nd polygon is the Paris area.

Data

{
  "location":{
    "lon" : 2.263849,
    "lat" : 48.855983,
    "accuracy" : 500
  }
}

closeToIndex1

Result

1

Table 9. now_utc
Name

now_utc

Description

Returns the processing time as ISO 8601 string.

Similar to javaScript new Date().toISOString()

Parameters

none

Logic

{
   "<=":[
      0,
      {
         "get_utc_hours":[
            {
               "now_utc":[

               ]
            }
         ]
      },
      12
   ]
}

Data

 {}

Result

true if processing hour is between 0 and 12

Table 10. get_utc_hours
Name

get_utc_hours

Description

Returns the hour of the ISO 8601 provided parameter .

Similar to javaScript new Date(param).getUTCHours()

Parameters

ISO 8601 String

Logic

{\"get_utc_hours\" : [\"2018-02-15T13:01:37.290Z\"]}

Data

 {}

Result

13

Table 11. • get_utc_minutes
Name
  • get_utc_minutes

Description

Returns the minutes of the ISO 8601 provided parameter .

Similar to javaScript new Date(param).getUTCMinutes()

Parameters

ISO 8601 String

Logic

{\"get_utc_minutes\" : [\"2018-02-15T13:01:37.290Z\"]}

Data

 {}

Result

1

Table 12. • get_utc_day
Name
  • get_utc_day

Description

Returns the day of the week of the ISO 8601 provided parameter .

Similar to javaScript new Date(param).getUTCDay()

Parameters

ISO 8601 String

Logic

{\"get_utc_day\" : [\"2018-02-15T13:01:37.290Z\"]}

Data

 {}

Result

4

Table 13. • get_utc_date
Name
  • get_utc_date

Description

Returns the day of the month of the ISO 8601 provided parameter .

Similar to javaScript new Date(a).getUTCDate()

Parameters

ISO 8601 String

Logic

{\"get_utc_day\" : [\"2018-02-15T13:01:37.290Z\"]}

Data

 {}

Result

15

Table 14. • get_time
Name
  • get_time

Description

Returns epoch (milliseconds) of the ISO 8601 provided parameter .

Similar to javaScript new Date(a).getTime()

Parameters

ISO 8601 String

Logic

{\"get_time\" : [\"2018-01-15T13:00:37.290Z\"]}

Data

 {}

Result

1516021237290.0