Predicates

Current version: 9.2

A predicate is a class that defines a condition and segmentation query relating to a specific aspect of a contact - such as preferred language or whether or not they have ever triggered a particular campaign. There is no predicate interface or base class; the combination of a condition and segmentation query makes up a predicate.

Predicates implement the following interfaces:

  • Sitecore.Framework.Rules.ICondition defines a condition

  • Sitecore.XConnect.Segmentation.Predicates.IContactSearchQueryFactory defines a segmentation query

Context determines whether the condition or segmentation query is used. For example:

  • The List Manager passes the predicate to the segmentation engine, which in turn uses the IContactSearchQueryFactory to get a list of matching contacts.

  • The Marketing Automation Engine evaluates the ICondition and uses the result to determine where a contact should move within a plan.

Note

For some predicates it only makes sense to implement ICondition - for example, the logic for “when contact’s e-mail address matches ___________” (Sitecore.XConnect.Segmentation.Predicates.Contacts.EmailAddressMatches) does not lend itself well to a segmentation query, as only one contact will ever be returned.

Sitecore.Rules vs Sitecore.Framework.Rules

Sitecore.Rules and Sitecore.Framework.Rules are two entirely separate implementations of the rules engine and do not overlap. However, both types of conditions are available in the Rules Editor UI. In the following example, the highlighted condition implements ICondition:

Note

Component personalization does not support ICondition. If you want to use the same logic for component personalization and Marketing Automation, you must create an ICondition implementation for Marketing Automation and a RuleCondition<T>whereT:RuleContext implementation for component personalization.

Example: AddressCityMatches

In the following example, the AddressCityMatches implements both IContactSearchQueryFactory and ICondition.

  • In a Marketing Automation context, Evaluate() returns true or false depending on whether the context contact’s city matches the supplied city.

  • In a segmentation context, the CreateContactSearchQuery() builds an expression that forms part of a segmentation query.

RequestResponse
using System;
using System.Linq.Expressions;
using Sitecore.Framework.Rules;
using Sitecore.XConnect.Collection.Model;

namespace Sitecore.XConnect.Segmentation.Predicates.Contacts
{
    public class AddressCityMatches : ICondition, IContactSearchQueryFactory
    {
        public string City { get; set; }

        public StringOperationType Comparison { get; set; }

        // Used for marketing automation
        public bool Evaluate(IRuleExecutionContext context)
        {
            var contact = context.Fact<Contact>();

            return Comparison.Evaluate(contact.Addresses()?.PreferredAddress?.City, City);
        }

        // Used for segmentation
        public Expression<Func<Contact, bool>> CreateContactSearchQuery(IContactSearchQueryContext context)
        {
            return contact => Comparison.Evaluate(contact.Addresses().PreferredAddress.City, City);
        }
    }
}

Do you have some feedback for us?

If you have suggestions for improving this article,