Giving Unomi Intelligence

Otávio Machado
Nov 12, 2018 · 6 min read
You, looking at all the nothing Unomi has just done
Rock Lee, from Naruto, giving my nice guy attitude a thumbs up

Rules

{
"metadata" : {
"id": "_ed2tvbd9p_evaluateProfileAge.json",
"name": "Evaluate profile age",
"description" : "Evaluate age when a profile is modified",
"readOnly":true
},

"condition" : {
"type": "profileUpdatedEventCondition",
"parameterValues": {
}
},

"actions" : [
{
"type": "evaluateProfileAgeAction",
"parameterValues": {
}
}
]

}

Conditions

{
"metadata": {
"id": "profileUpdatedEventCondition",
"name": "profileUpdatedEventCondition",
"description": "",
"systemTags": [
"profileTags",
"event",
"condition",
"eventCondition"
],
"readOnly": true
},
"parentCondition": {
"type": "eventTypeCondition",
"parameterValues": {
"eventTypeId": "profileUpdated"
}
},

"parameters": [
]
}
{
"metadata": {
"id": "eventTypeCondition",
"name": "eventTypeCondition",
"description": "",
"systemTags": [
"profileTags",
"event",
"condition",
"eventCondition"
],
"readOnly": true
},
"parentCondition": {
"type": "eventPropertyCondition",
"parameterValues": {
"propertyName": "eventType",
"propertyValue": "parameter::eventTypeId",
"comparisonOperator": "equals"
}
},
"parameters": [
{
"id": "eventTypeId",
"type": "String",
"multivalued": false
}
]
}
{
"metadata": {
"id": "booleanCondition",
"name": "booleanCondition",
"description": "",
"systemTags": [
"profileTags",
"logical",
"condition",
"profileCondition",
"eventCondition",
"sessionCondition",
"sourceEventCondition"
],
"readOnly": true
},
"conditionEvaluator": "booleanConditionEvaluator",
"queryBuilder": "booleanConditionESQueryBuilder",
"parameters": [
{
"id": "operator",
"type": "String",
"multivalued": false,
"defaultValue": "and"
},
{
"id": "subConditions",
"type": "Condition",
"multivalued": true
}
]
}
public class BooleanConditionEvaluator implements ConditionEvaluator {

@Override
public boolean eval(Condition condition, Item item, Map<String, Object> context,
ConditionEvaluatorDispatcher dispatcher) {
boolean isAnd = "and".equalsIgnoreCase((String) condition.getParameter("operator"));
@SuppressWarnings("unchecked")
List<Condition> conditions = (List<Condition>) condition.getParameter("subConditions");
for (Condition sub : conditions) {
boolean eval = dispatcher.eval(sub, item, context);
if (!eval && isAnd) {
// And
return false;
} else if (eval && !isAnd) {
// Or
return true;
}
}
return isAnd;
}
}

Actions

{
"metadata": {
"id": "evaluateProfileAgeAction",
"name": "evaluateProfileAgeAction",
"description": "",
"systemTags": [
"profileTags",
"event"
],
"readOnly": true
},
"actionExecutor": "evaluateProfileAge",
"parameters": [
]
}
public class EvaluateProfileAgeAction implements ActionExecutor {

@Override
public int execute(Action action, Event event) {
boolean updated = false;
if (event.getProfile().getProperty("birthDate") != null) {
Integer y = Years.yearsBetween(new DateTime(event.getProfile().getProperty("birthDate")), new DateTime()).getYears();
if (event.getProfile().getProperty("age") == null || event.getProfile().getProperty("age") != y) {
updated = true;
event.getProfile().setProperty("age", y);
}
}
return updated ? EventService.PROFILE_UPDATED : EventService.NO_CHANGE;
}
}

Featuring: Segments

{
"metadata": {
"id": "heavyUser",
"name": "heavyUser",
"scope": "systemscope",
"description": "The user has this segment if it has acessed our products Website.",
"readOnly":true
},
"condition": {
"parameterValues": {
"subConditions": [
{
"parameterValues": {
"propertyName": "properties.visitedSiteNb",
"comparisonOperator": "greaterThan",
"propertyValueInteger": "5"
},
"type": "profilePropertyCondition"
}
],
"operator" : "and"
},
"type": "booleanCondition"
}
}

Patching things up

<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

<reference id="profileService" interface="org.apache.unomi.api.services.ProfileService"/>

<!-- Action executor -->
<service id="incrementTweetNumberAction" interface="org.apache.unomi.api.actions.ActionExecutor">
<service-properties>
<entry key="actionExecutorId" value="incrementTweetNumber"/>
</service-properties>
<bean class="org.apache.unomi.samples.tweet_button_plugin.actions.IncrementTweetNumberAction">
<property name="profileService" ref="profileService"/>
</bean>
</service>
</blueprint>

Otávio Machado

Written by

Eternal learner, Software Developer, Team Enabler and occasional Pokémon Master. I play with words and with code — and I want to make the world a better place.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade