protected virtual void ConvertRule(RuleEntity entity, RuleExpression expression) { Guard.NotNull(entity, nameof(entity)); Guard.NotNull(expression, nameof(expression)); var descriptor = RuleDescriptors.FindDescriptor(entity.RuleType); if (descriptor == null) { // A descriptor for this entity data does not exist. Allow deletion of it. descriptor = new InvalidRuleDescriptor(Scope) { Name = entity.RuleType, DisplayName = entity.RuleType }; } else if (descriptor.Scope != Scope) { throw new SmartException($"Differing rule scope {descriptor.Scope}. Expected {Scope}."); } expression.Id = entity.Id; expression.RuleSetId = entity.RuleSetId; expression.Descriptor = descriptor; expression.Operator = entity.Operator; expression.RawValue = entity.Value; expression.Value = entity.Value.Convert(descriptor.RuleType.ClrType); }
private async Task <IRuleExpression> CreateExpression(RuleEntity ruleEntity, IRuleVisitor visitor) { if (!ruleEntity.IsGroup) { return(await visitor.VisitRuleAsync(ruleEntity)); } // It's a group, do recursive call. var group = await CreateExpressionGroupAsync(ruleEntity.Value.Convert <int>(), visitor); if (group != null) { group.RefRuleId = ruleEntity.Id; } return(group); }
public abstract IRuleExpression VisitRule(RuleEntity rule);
public abstract Task <IRuleExpression> VisitRuleAsync(RuleEntity rule);
private async Task <IRuleExpressionGroup> CreateExpressionGroup(RuleSetEntity ruleSet, RuleEntity refRule, IRuleVisitor visitor) { if (ruleSet.Scope != visitor.Scope) { // TODO: ErrHandling (ruleSet is for a different scope) return(null); } var group = visitor.VisitRuleSet(ruleSet); if (refRule != null) { group.RefRuleId = refRule.Id; } var expressions = await ruleSet.Rules .SelectAsync(x => CreateExpression(x, visitor)) .Where(x => x != null) .AsyncToArray(); group.AddExpressions(expressions); return(group); }