|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# |
| 4 | +# Copyright 2019, Optimizely and contributors |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +module Optimizely |
| 19 | + module ConditionTreeEvaluator |
| 20 | + # Operator types |
| 21 | + AND_CONDITION = 'and' |
| 22 | + OR_CONDITION = 'or' |
| 23 | + NOT_CONDITION = 'not' |
| 24 | + |
| 25 | + EVALUATORS_BY_OPERATOR_TYPE = { |
| 26 | + AND_CONDITION => :and_evaluator, |
| 27 | + OR_CONDITION => :or_evaluator, |
| 28 | + NOT_CONDITION => :not_evaluator |
| 29 | + }.freeze |
| 30 | + |
| 31 | + module_function |
| 32 | + |
| 33 | + def evaluate(conditions, leaf_evaluator) |
| 34 | + # Top level method to evaluate audience conditions. |
| 35 | + # |
| 36 | + # conditions - Nested array of and/or conditions. |
| 37 | + # Example: ['and', operand_1, ['or', operand_2, operand_3]] |
| 38 | + # |
| 39 | + # leaf_evaluator - Function which will be called to evaluate leaf condition values. |
| 40 | + # |
| 41 | + # Returns boolean if the given user attributes match/don't match the given conditions, |
| 42 | + # nil if the given conditions are invalid or can't be evaluated. |
| 43 | + |
| 44 | + if conditions.is_a? Array |
| 45 | + first_operator = conditions[0] |
| 46 | + rest_of_conditions = conditions[1..-1] |
| 47 | + |
| 48 | + # Operator to apply is not explicit - assume 'or' |
| 49 | + unless EVALUATORS_BY_OPERATOR_TYPE.include?(conditions[0]) |
| 50 | + first_operator = OR_CONDITION |
| 51 | + rest_of_conditions = conditions |
| 52 | + end |
| 53 | + |
| 54 | + return send(EVALUATORS_BY_OPERATOR_TYPE[first_operator], rest_of_conditions, leaf_evaluator) |
| 55 | + end |
| 56 | + |
| 57 | + leaf_evaluator.call(conditions) |
| 58 | + end |
| 59 | + |
| 60 | + def and_evaluator(conditions, leaf_evaluator) |
| 61 | + # Evaluates an array of conditions as if the evaluator had been applied |
| 62 | + # to each entry and the results AND-ed together. |
| 63 | + # |
| 64 | + # conditions - Array of conditions ex: [operand_1, operand_2] |
| 65 | + # |
| 66 | + # leaf_evaluator - Function which will be called to evaluate leaf condition values. |
| 67 | + # |
| 68 | + # Returns boolean if the user attributes match/don't match the given conditions, |
| 69 | + # nil if the user attributes and conditions can't be evaluated. |
| 70 | + |
| 71 | + found_nil = false |
| 72 | + conditions.each do |condition| |
| 73 | + result = evaluate(condition, leaf_evaluator) |
| 74 | + return result if result == false |
| 75 | + |
| 76 | + found_nil = true if result.nil? |
| 77 | + end |
| 78 | + |
| 79 | + found_nil ? nil : true |
| 80 | + end |
| 81 | + |
| 82 | + def not_evaluator(single_condition, leaf_evaluator) |
| 83 | + # Evaluates an array of conditions as if the evaluator had been applied |
| 84 | + # to a single entry and NOT was applied to the result. |
| 85 | + # |
| 86 | + # single_condition - Array of a single condition ex: [operand_1] |
| 87 | + # |
| 88 | + # leaf_evaluator - Function which will be called to evaluate leaf condition values. |
| 89 | + # |
| 90 | + # Returns boolean if the user attributes match/don't match the given conditions, |
| 91 | + # nil if the user attributes and conditions can't be evaluated. |
| 92 | + |
| 93 | + return nil if single_condition.empty? |
| 94 | + |
| 95 | + result = evaluate(single_condition[0], leaf_evaluator) |
| 96 | + result.nil? ? nil : !result |
| 97 | + end |
| 98 | + |
| 99 | + def or_evaluator(conditions, leaf_evaluator) |
| 100 | + # Evaluates an array of conditions as if the evaluator had been applied |
| 101 | + # to each entry and the results OR-ed together. |
| 102 | + # |
| 103 | + # conditions - Array of conditions ex: [operand_1, operand_2] |
| 104 | + # |
| 105 | + # leaf_evaluator - Function which will be called to evaluate leaf condition values. |
| 106 | + # |
| 107 | + # Returns boolean if the user attributes match/don't match the given conditions, |
| 108 | + # nil if the user attributes and conditions can't be evaluated. |
| 109 | + |
| 110 | + found_nil = false |
| 111 | + conditions.each do |condition| |
| 112 | + result = evaluate(condition, leaf_evaluator) |
| 113 | + return result if result == true |
| 114 | + |
| 115 | + found_nil = true if result.nil? |
| 116 | + end |
| 117 | + |
| 118 | + found_nil ? nil : false |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments