Ejemplo n.º 1
0
        private TriggerConditionSet LoadTriggerConditionSet(
            JToken csTok,
            bool composeOr,
            bool modifierNot)
        {
            var props = csTok is JObject csObj
                                ? csObj.Properties()
                                : ((JArray)csTok).SelectMany(x => ((JObject)x).Properties());

            var cs = new TriggerConditionSet
            {
                ComposeOr     = composeOr,
                ModifierNot   = modifierNot,
                Conditions    = new List <TriggerCondition>(),
                ConditionSets = new List <TriggerConditionSet>()
            };

            foreach (var prop in props)
            {
                var upperName = prop.Name.ToUpper();
                if (upperName == "OR" || upperName == "AND" || upperName == "NOT" || upperName == "HIDDEN_TRIGGER" || upperName == "CALC_TRUE_IF")
                {
                    cs.ConditionSets.Add(LoadTriggerConditionSet(
                                             prop.Value,
                                             prop.Name == "OR",
                                             prop.Name == "NOT"));
                }
                else
                {
                    if (prop.Value is JArray arrayVal)
                    {
                        foreach (var val in arrayVal)
                        {
                            cs.Conditions.Add(new TriggerCondition
                            {
                                Name  = prop.Name,
                                Value = val.Value <string>()
                            });
                        }
                    }
                    else
                    {
                        if (prop.Name == "capital_scope")
                        {
                            continue;
                        }
                        cs.Conditions.Add(new TriggerCondition
                        {
                            Name  = prop.Name,
                            Value = prop.Value.ToString(),
                        });
                    }
                }
            }
            return(cs);
        }
        private static bool ResolveConditionSet(TriggerConditionSet cs, CountryIdeaQuery query)
        {
            var returnValue = false;

            foreach (var c in cs.Conditions)
            {
                if (ResolveCondition(c, query))
                {
                    if (cs.ComposeOr)
                    {
                        // OR condition and one returned true;
                        // The condition set resolves to true
                        return(!cs.ModifierNot);
                    }
                }
                else
                {
                    if (!cs.ComposeOr)
                    {
                        // AND condition and one returned false;
                        return(cs.ModifierNot);
                    }
                }
            }
            foreach (var subCs in cs.ConditionSets)
            {
                if (cs.ComposeOr && ResolveConditionSet(subCs, query))
                {
                    // OR condition and one returned true;
                    return(!cs.ModifierNot);
                }
                else if (!cs.ComposeOr && !ResolveConditionSet(subCs, query))
                {
                    // AND condition and one returned false;
                    return(cs.ModifierNot);
                }
            }
            return(cs.ComposeOr ? cs.ModifierNot : !cs.ModifierNot);
        }