Beispiel #1
0
        private static ConditionalExpression ProcessLine(ParameterExpression eventParameter, string line, ParameterExpression inputParameter)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(line), "Empty line");
            Contract.Requires(eventParameter != null, "eventParameter is null");

            int slot  = 0;
            var words = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            Contract.Assert(words.Length >= 2);

            // When
            if (words[0].Equals("when", StringComparison.OrdinalIgnoreCase))
            {
                slot++;
            }

            var condition = new  TestExpressionInfo();

            Expression criteria = Expression.Constant(true);

            // loop through the conditions until you find an operation (Add/subtract)
            bool firstCondition = true;

            while (slot < words.Length && !IsOperation(words[slot]))
            {
                Contract.Assert(words.Length > slot + 3);
                if (!firstCondition)
                {
                    slot++;
                }
                firstCondition = false;
                condition      = TestExpressionInfo.Parse(words[slot++], EnumParsing.Parse <Condition>(words[slot++]), words[slot++]);
                var conditionalExpression = GetConditionExpression(condition, eventParameter, inputParameter);
                criteria = Expression.And(criteria, conditionalExpression);
            }

            // operation
            var operation = EnumParsing.Parse <Operation>(words[slot++]);

            Contract.Assert(words.Length > slot);
            var rewardApplied = words[slot++];

            int count;

            if (int.TryParse(rewardApplied, out count))
            {
                Contract.Assert(words.Length >= slot + 1, "if specifying the number of rewards, you must have 7 tokens");
                Contract.Assert(count != 0, "you must not specify 0 count");
                rewardApplied = words[slot++];
            }

            log.Info("when {0} {1} {2}", condition.Event, condition.Condition, condition.Value);
            rewardApplied = rewardApplied.EndsWith("s") ? rewardApplied : rewardApplied + "s";

            // Operation
            var updateCount = GetOperationToUpdateCount(operation, eventParameter, count, rewardApplied);

            return(Expression.IfThen(criteria, updateCount));
        }
        internal static TestExpressionInfo Parse(string critiera, Condition condition, string value)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(critiera));
            Contract.Requires(value != null);

            var g = EnumParsing.TryParse <InputCriteria>(critiera);
            var e = EnumParsing.TryParse <EventCriteria>(critiera);

            object val = null;

            if (e.HasValue)
            {
                val = UInt32.Parse(value);
            }
            if (g.HasValue)
            {
                switch (g.Value)
                {
                case InputCriteria.Mode:
                    val = EnumParsing.Parse <Mode>(value);
                    break;

                case InputCriteria.Variant:
                    val = EnumParsing.Parse <Variant>(value);
                    break;

                default: throw new ArgumentOutOfRangeException();
                }
            }

            Contract.Assert(val != null);

            return(new TestExpressionInfo(e.HasValue ? e.Value : EventCriteria.None,
                                          g.HasValue ? g.Value : InputCriteria.None,
                                          condition,
                                          val));
        }