IfThengine V1

The If-Then-Engine or IfThengine V1 is a powerful and complex tool. It's an engine that evaluates data points (with the data point providers) based on defined rules to trigger an outcome.

There are three parts to this flow:

IfThengine V1

If an event occurs in the system, such as:

  • Updating stats
  • Placing an order
  • Creating an AutoShip
  • Changing Associate information

The system collects data and sends it to the IfThengine V1.

The following is the code example that shows how the IfThengine V1 evaluates the data point provider.

private void ProcessAssociateDataPoint(DataPoint dataPoint, Dictionary<int, List<int>> associateToRuleIdMap)
{
    // get associate rules
    var associateRules = _ruleService.GetAssociateRules(dataPoint.AssociateId);

    // get "relevant" rules, i.e., rules that apply to the data point that changed
    var relevantRules = _ruleService.FilterRules(associateRules, new[] { dataPoint.Id });

    if (relevantRules.Length > 0)
    {
        // retrieve the data that is necessary to run all rules that apply to the DataPoint that changed
        var dataPointIds = _ruleService.ExtractDataPointIds(relevantRules);

        var dataPointsForRetrieval = dataPointIds.FillAssociateId(dataPoint.AssociateId, dataPoint.RelatedId);
        // note: in a future release, retrieving data via _dataPointService.GetDataPoints() will be done in batch. For now, it's done by datapoint-by-datapoint

        var retrieveDataPoints = _dataPointService.GetDataPointValues(dataPointsForRetrieval);

        foreach (var rule in relevantRules)
        {
            List<int> associateRuleIds = null;
            if (associateToRuleIdMap.TryGetValue(dataPoint.AssociateId, out associateRuleIds))
            {
                if (associateRuleIds.Contains(rule.Id))
                {
                    // this rule has already been run for this associate, so skip it.
                    continue;
                }
            }

            var evaluator = new Evaluator();
            // add the data points as variables
            evaluator.AddDataPoints(retrievedDataPoints);

            // evaluate the rule
            if (evaluator.Evaluate(rule.Condition))
            {
                RunOutcomes(dataPoint, rule, retrieveDataPoints);
            }

            // keep track of running this rule for the associate
            if (!associateToRuleIdMap.TryGetValue(dataPoint.AssociateId, out associateRuleIds))
            {
                associateRulesIds = new List<int>();
                associateToRuleIdMap.Add(dataPoint.AssociateId, associateRuleIds);
            }
            associateRuleIds.Add(rule.Id);
        }
    }
}