示例#1
0
        //protected Type _type;
        //public CustomParameterBase(Type type)
        //{
        //    _type = type;
        //}

        public void Apply(IRuleElement element)
        {
            if (element is T t)
            {
                InnerApply(t);
            }
        }
示例#2
0
        public bool Process(IRuleElement variable, Operator @operator)
        {
            switch (@operator.Value)
            {
            case Operators.NotEqual:
                return(NotEqual(Value, variable.Value));

            case Operators.Equal:
                return(Equal(Value, variable.Value));

            case Operators.GreaterThan:
                return(GreaterThan(Value, variable.Value));

            case Operators.GreaterThanOrEqual:
                return(GreaterThanOrEqual(Value, variable.Value));

            case Operators.LessThan:
                return(LessThan(Value, variable.Value));

            case Operators.LessThanOrEqual:
                return(LessThanOrEqual(Value, variable.Value));

            case Operators.Contains:
                return(Contains(Value, variable.Value));

            default:
                throw new ArgumentOutOfRangeException(nameof(@operator));
            }
        }
示例#3
0
        public static IEnumerable <string> AllGraphicTags(this IRuleElement t)
        {
            var tags = new List <string>();

            if (t.GraphicTag != null)
            {
                tags.Add(t.GraphicTag);
            }

            tags.AddRange(t.AlternativeGraphicTags);
            return(tags);
        }
示例#4
0
        /// <summary>
        /// Данный функционал вынесен отдельно для того, чтобы можно было повторно использовать в ModRuleEntity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rule"></param>
        /// <param name="serializerRoot"></param>
        /// <param name="definition"></param>
        /// <returns></returns>
        protected virtual T InnerGetRule <T>(ref IRuleElement rule, IRuleSerializerRoot serializerRoot, string definition) where T : IRuleElement
        {
            if (serializerRoot == null)
            {
                throw new ArgumentNullException(nameof(serializerRoot));
            }

            if (rule == null)
            {
                rule = serializerRoot.Container <T>().Deserialize(definition);
            }

            return((T)rule);
        }
示例#5
0
        public static bool FindFirstTile <TRenderTile>(this ITileRegistry <TRenderTile> tileSet,
                                                       IRuleElement t,
                                                       out TRenderTile result)
        {
            foreach (var tg in t.AllGraphicTags())
            {
                if (tileSet.TryFind(tg, out TRenderTile g))
                {
                    result = g;
                    return(true);
                }
            }

            result = default(TRenderTile);
            return(false);
        }
示例#6
0
        private Proposition ProcessOperator(Guid contextId, Operator @operator)
        {
            IRuleElement rhs = _stack.Pop();
            IRuleElement lhs = _stack.Pop();

            string propName = string.Empty;

            if ((Operators)@operator.Value == Operators.Not)
            {
                propName = string.Join(" ", "(", @operator.Name, lhs.Name, ":", lhs.Value, ")");
            }
            else
            {
                propName = string.Join(" ", "(", string.Join("", lhs.Name, ":", lhs.Value), @operator.Name, string.Join("", rhs.Name, ":", rhs.Value), ")");
            }

            return(new Proposition(propName, lhs.Process(rhs, @operator)));
        }
示例#7
0
        public bool Process(IRuleElement inProposition, Operator @operator)
        {
            switch (@operator.Value)
            {
            case Operators.And:
                return(And((bool)Value, (bool)inProposition.Value));

            case Operators.Not:
                return(Not((bool)Value));

            case Operators.Or:
                return(Or((bool)Value, (bool)inProposition.Value));

            case Operators.Xor:
                return(Xor((bool)Value, (bool)inProposition.Value));

            default:
                throw new ArgumentOutOfRangeException(nameof(@operator));
            }
        }
 public override string Serialize(IRuleElement element)
 {
     throw new NotImplementedException();
 }
示例#9
0
        public Proposition Evaluate(RuleContext context, Queue <IRuleElement> elements = default(Queue <IRuleElement>))
        {
            if (elements == null)
            {
                elements = new Queue <IRuleElement>(Elements.ToArray());
            }

            int count      = elements.Count;
            int groupCount = 0;

            for (int i = 0; i < count; i++)
            {
                IRuleElement e       = elements.Dequeue();
                IRuleElement element = context.Find(e.Name);
                if (e.GetType() != typeof(Operator))
                {
                    _stack.Push(element ?? e);
                }
                else if ((Operators)e.Value == Operators.StartGrouping)
                {
                    groupCount++;

                    var rule        = new Rule($"Group_{groupCount}");
                    var proposition = rule.Evaluate(context, elements);
                    _stack.Push(proposition);
                    count = elements.Count + 1;
                }
                else if ((Operators)e.Value == Operators.EndGrouping)
                {
                    if (_stack.Count > 1)
                    {
                        throw new InvalidOperationException("Invalid group expression.");
                    }
                    return((Proposition)_stack.Pop());
                }
                else
                {
                    string cacheName = string.Join(" ", Name, context.Name);
                    if (!cache.TryGetValue(cacheName, out Proposition proposition))
                    {
                        proposition = ProcessOperator(context.Id, (Operator)e);

                        if (i == (count - 1))
                        {
                            cache.TryAdd(cacheName, proposition);
                            return(proposition);
                        }
                    }

                    if (i == (count - 1))
                    {
                        return(proposition);
                    }
                    else
                    {
                        _stack.Push(proposition);
                    }
                }
            }

            return((Proposition)_stack.Pop());
        }
 public bool Check(IRuleElement element)
 {
     return((element is T t) ? InnerCheck(t) : true);
 }
 public bool CheckCustomParameters(IRuleElement element)
 {
     return(CheckParameters?.TrueForAll(c => c.Check(element)) ?? true);
 }
 /// <summary>
 /// Применяет кастомные праметры к элементу, если таковые найдутся - соответствующие его типу
 /// </summary>
 public void ApplyCustomParameters(IRuleElement element)
 {
     ApplyParameters?.ForEach(c => c.Apply(element));
 }
示例#13
0
 public bool Process(IRuleElement element, Operator @operator)
 {
     throw new InvalidOperationException("This operation is not available on this rule element.");
 }
示例#14
0
 public void AddElement(IRuleElement element)
 {
     _collection.Add(element);
 }