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)); }
private static BinaryExpression GetConditionExpression(TestExpressionInfo c, Expression eventParameter, Expression inputParameter) { Contract.Requires(eventParameter != null); Contract.Requires(inputParameter != null); Func <Expression, Expression, BinaryExpression> comparer; switch (c.Condition) { case Condition.Is: case Condition.Equal: case Condition.Eq: log.Debug("Equal"); comparer = Expression.Equal; break; case Condition.NotEqual: case Condition.Neq: log.Debug("NotEqual"); comparer = Expression.NotEqual; break; case Condition.Greater: case Condition.GreaterThan: case Condition.Gt: log.Debug("GreaterThan"); comparer = Expression.GreaterThan; break; case Condition.Less: case Condition.LessThan: case Condition.Lt: log.Debug("LessThan"); comparer = Expression.LessThan; break; case Condition.GreaterThanOrEqual: case Condition.Gte: log.Debug("GreaterThan or Equal"); comparer = Expression.GreaterThanOrEqual; break; case Condition.LessThanOrEqual: case Condition.Lte: log.Debug("LessThan or Equal"); comparer = Expression.LessThanOrEqual; break; default: throw new ArgumentOutOfRangeException("c", c, "Invalid condition"); } var comparisonSource = c.Source == TestExpressionInfo.ComparisonSource.Event ? eventParameter : inputParameter; var comparisonParameterName = c.Source == TestExpressionInfo.ComparisonSource.Event ? c.Event.ToString() : c.Input.ToString(); var comparisonSourceParameter = Expression.PropertyOrField(comparisonSource, comparisonParameterName); return(comparer(comparisonSourceParameter, c.Value)); }
private static BinaryExpression GetConditionExpression(TestExpressionInfo c, Expression eventParameter, Expression inputParameter) { Contract.Requires(eventParameter != null); Contract.Requires(inputParameter != null); Func<Expression, Expression, BinaryExpression> comparer; switch (c.Condition) { case Condition.Is: case Condition.Equal: case Condition.Eq: log.Debug("Equal"); comparer = Expression.Equal; break; case Condition.NotEqual: case Condition.Neq: log.Debug("NotEqual"); comparer = Expression.NotEqual; break; case Condition.Greater: case Condition.GreaterThan: case Condition.Gt: log.Debug("GreaterThan"); comparer = Expression.GreaterThan; break; case Condition.Less: case Condition.LessThan: case Condition.Lt: log.Debug("LessThan"); comparer = Expression.LessThan; break; case Condition.GreaterThanOrEqual: case Condition.Gte: log.Debug("GreaterThan or Equal"); comparer = Expression.GreaterThanOrEqual; break; case Condition.LessThanOrEqual: case Condition.Lte: log.Debug("LessThan or Equal"); comparer = Expression.LessThanOrEqual; break; default: throw new ArgumentOutOfRangeException("c", c, "Invalid condition"); } var comparisonSource = c.Source == TestExpressionInfo.ComparisonSource.Event ? eventParameter : inputParameter; var comparisonParameterName = c.Source == TestExpressionInfo.ComparisonSource.Event ? c.Event.ToString() : c.Input.ToString(); var comparisonSourceParameter = Expression.PropertyOrField(comparisonSource, comparisonParameterName); return comparer(comparisonSourceParameter, c.Value); }
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); }