private static RuleScope getRuleScope(SqlString objScope) { Debug.Assert(!objScope.IsNull); RuleScope fs = RuleScope.Unknown; if (objScope.IsNull) { fs = RuleScope.Unknown; } else { if (string.Compare(objScope.Value, Sql.Constants.RuleScopeAll, true) == 0) { fs = RuleScope.All; } else if (string.Compare(objScope.Value, Sql.Constants.RuleScopeSystem, true) == 0) { fs = RuleScope.System; } else if (string.Compare(objScope.Value, Sql.Constants.RuleScopeUser, true) == 0) { fs = RuleScope.User; } else { fs = RuleScope.Unknown; } } return(fs); }
public ActionResult AddRule(int ruleSetId, RuleScope scope, string ruleType) { var provider = _ruleProvider(scope); var descriptor = provider.RuleDescriptors.FindDescriptor(ruleType); var op = descriptor.Operators.First(); var rule = new RuleEntity { RuleSetId = ruleSetId, RuleType = ruleType, Operator = op.Operator }; if (op == RuleOperator.In || op == RuleOperator.NotIn) { // Avoid ArgumentException "The 'In' operator only supports non-null instances from types that implement 'ICollection<T>'." rule.Value = string.Empty; } _ruleStorage.InsertRule(rule); var expression = provider.VisitRule(rule); PrepareTemplateViewBag(ruleSetId); return(PartialView("_Rule", expression)); }
public ActionResult AddGroup(int ruleSetId, RuleScope scope) { var provider = _ruleProvider(scope); var group = new RuleSetEntity { IsActive = true, IsSubGroup = true, Scope = scope }; _ruleStorage.InsertRuleSet(group); var groupRefRule = new RuleEntity { RuleSetId = ruleSetId, RuleType = "Group", Operator = RuleOperator.IsEqualTo, Value = group.Id.ToString() }; _ruleStorage.InsertRule(groupRefRule); var expression = provider.VisitRuleSet(group); expression.RefRuleId = groupRefRule.Id; return(PartialView("_RuleSet", expression)); }
public static Type GetScopeType(this RuleScope scope) { if (scope.IsMultiple()) { throw new InvalidOperationException("The provided RuleScope enum value has more than one flag set."); } switch (scope) { case RuleScope.Model: return(typeof(Model)); case RuleScope.Table: return(typeof(Table)); case RuleScope.Measure: return(typeof(Measure)); case RuleScope.Hierarchy: return(typeof(Hierarchy)); case RuleScope.Level: return(typeof(Level)); case RuleScope.Relationship: return(typeof(SingleColumnRelationship)); case RuleScope.Perspective: return(typeof(Perspective)); case RuleScope.Culture: return(typeof(Culture)); case RuleScope.Partition: return(typeof(Partition)); case RuleScope.ProviderDataSource: return(typeof(ProviderDataSource)); case RuleScope.StructuredDataSource: return(typeof(StructuredDataSource)); case RuleScope.DataColumn: return(typeof(DataColumn)); case RuleScope.CalculatedColumn: return(typeof(CalculatedColumn)); case RuleScope.CalculatedTable: return(typeof(CalculatedTable)); case RuleScope.CalculatedTableColumn: return(typeof(CalculatedTableColumn)); case RuleScope.KPI: return(typeof(KPI)); case RuleScope.Variation: return(typeof(Variation)); case RuleScope.NamedExpression: return(typeof(NamedExpression)); case RuleScope.ModelRole: return(typeof(ModelRole)); case RuleScope.CalculationGroup: return(typeof(CalculationGroupTable)); //case RuleScope.CalculationGroupAttribute: return typeof(CalculationGroupAttribute); case RuleScope.CalculationItem: return(typeof(CalculationItem)); case RuleScope.TablePermission: return(typeof(TablePermission)); case RuleScope.ModelRoleMember: return(typeof(ModelRoleMember)); default: throw new InvalidOperationException("Unknown scope type"); } }
public FilterObject(RuleObjectType type, RuleScope scope, string matchString) { m_objectScope = scope; m_objectType = type; m_objectMatchString = matchString; m_objectMatchStringList = new List <string>(); m_objectMatchStringList.Add(matchString); switch (type) { case RuleObjectType.Database: case RuleObjectType.Table: case RuleObjectType.StoredProcedure: case RuleObjectType.View: case RuleObjectType.Function: case RuleObjectType.ExtendedStoredProcedure: m_isChecked = true; break; default: m_isChecked = true; break; } InitializeAndUpdate(); }
private void CompileQueries(Model model) { _queries = new List <IQueryable>(); ErrorMessage = null; if (CompatibilityLevel > model.Database.CompatibilityLevel) { invalidCompatibilityLevel = true; IsValid = false; return; } try { foreach (var scope in Scope.Enumerate()) { errorScope = scope; var collection = Analyzer.GetCollection(model, scope); _queries.Add(CompileQuery(collection)); } IsValid = true; } catch (Exception ex) { IsValid = false; ErrorMessage = ex.Message; } }
public ActionResult AddRule(int ruleSetId, RuleScope scope, string ruleType) { var provider = _ruleProvider(scope); var descriptor = provider.RuleDescriptors.FindDescriptor(ruleType); var op = descriptor.Operators.First(); var rule = new RuleEntity { RuleSetId = ruleSetId, RuleType = ruleType, Operator = op.Operator }; if (descriptor.RuleType == RuleType.Boolean) { // Do not store NULL. Irritating because UI indicates 'yes'. var val = op == RuleOperator.IsEqualTo; rule.Value = val.ToString(CultureInfo.InvariantCulture).ToLower(); } else if (op == RuleOperator.In || op == RuleOperator.NotIn) { // Avoid ArgumentException "The 'In' operator only supports non-null instances from types that implement 'ICollection<T>'." rule.Value = string.Empty; } _ruleStorage.InsertRule(rule); var expression = provider.VisitRule(rule); PrepareTemplateViewBag(ruleSetId); return(PartialView("_Rule", expression)); }
private static string getRuleScope(RuleScope objScope) { string rs = string.Empty; switch (objScope) { case RuleScope.All: rs = Constants.RuleScopeAll; break; case RuleScope.System: rs = Constants.RuleScopeSystem; break; case RuleScope.User: rs = Constants.RuleScopeUser; break; case RuleScope.Unknown: default: Debug.Assert(false); rs = string.Empty; break; } return(rs); }
private void SaveRuleData(RuleEditItem[] ruleData, RuleScope ruleScope) { var rules = _ruleStorage.GetRulesByIds(ruleData?.Select(x => x.RuleId)?.ToArray(), true); if (!rules.Any()) { return; } using (var scope = new DbContextScope(ctx: Services.DbContext, autoCommit: false)) { var rulesDic = rules.ToDictionarySafe(x => x.Id); var provider = _ruleProvider(ruleScope); foreach (var data in ruleData) { if (rulesDic.TryGetValue(data.RuleId, out var entity)) { // TODO? Ugly. There should be a better way. Do not store culture variant values. if (data.Value.HasValue()) { var descriptor = provider.RuleDescriptors.FindDescriptor(entity.RuleType); if (descriptor.RuleType == RuleType.Money) { data.Value = data.Value.Convert <decimal>(CultureInfo.CurrentCulture).ToString(CultureInfo.InvariantCulture); } else if (descriptor.RuleType == RuleType.Float || descriptor.RuleType == RuleType.NullableFloat) { data.Value = data.Value.Convert <float>(CultureInfo.CurrentCulture).ToString(CultureInfo.InvariantCulture); } else if (descriptor.RuleType == RuleType.DateTime || descriptor.RuleType == RuleType.NullableDateTime) { data.Value = data.Value.Convert <DateTime>(CultureInfo.CurrentCulture).ToString(CultureInfo.InvariantCulture); } } //if (data.Value?.Contains(',') ?? false) //{ // var provider = _ruleProvider(ruleScope); // var descriptor = provider.RuleDescriptors.FindDescriptor(entity.RuleType); // var floatingPointTypes = new Type[] { typeof(decimal), typeof(decimal?), typeof(float), typeof(float?), typeof(double), typeof(double?) }; // if (floatingPointTypes.Contains(descriptor.RuleType.ClrType)) // { // data.Value = data.Value.Replace(",", "."); // } //} entity.Operator = data.Op; entity.Value = data.Value; _ruleStorage.UpdateRule(entity); } } scope.Commit(); } }
public void RuleScopeMatchResult(RuleScope scope, ScopeMatchResult result) { this.logger.Log( LogLevel.Diagnostic, "Rule scope {0} {1} {2}", scope.DisplayName, result.Success ? "matches" : "does not match", result.Arguments); }
/// <summary> /// Enumerates the currently set flags on this Enum as individual Enum values. /// </summary> /// <param name="input"></param> /// <returns></returns> public static IEnumerable <RuleScope> Enumerate(this RuleScope input) { foreach (RuleScope value in Enum.GetValues(input.GetType())) { if (input.HasFlag(value)) { yield return(value); } } }
public Rule( RuleObjectType objectType, RuleScope objectScope, string matchString ) { m_ObjectType = objectType; m_ObjectScope = objectScope; m_MatchString = matchString; }
protected virtual IEnumerable <Rule> GetRulesByScope(RuleScope scope, bool includeGlobalScope, Func <Rule, RuleScope, bool> scopeEvaluator) { if (scopeEvaluator == null) { return(_rules.Where(x => (x.Scope == scope || (includeGlobalScope && x.Scope == RuleScope.Global))).OrderBy(x => x.Order).ToList()); } else { return(_rules.Where(x => scopeEvaluator(x, scope) || (includeGlobalScope && x.Scope == RuleScope.Global)).OrderBy(x => x.Order).ToList()); } }
public ActionResult UpdateRules(RuleEditItem[] ruleData, RuleScope ruleScope) { try { SaveRuleData(ruleData, ruleScope); } catch (Exception ex) { return(Json(new { Success = false, ex.Message })); } return(Json(new { Success = true })); }
public void InvalidRegexPatternMustFailOnObjectInitialization( RuleScope scope) { var invalidPattern = "["; var ex = Assert.Throws <ArgumentException>( () => new RegexRule(invalidPattern, scope)); Assert.Equal("Given pattern \"[\" was invalid regular expression.", ex.Message); Assert.NotNull(ex.InnerException); Assert.IsType <ArgumentException>(ex.InnerException); }
/** Return the scope containing name */ public virtual AttributeScope GetAttributeScope(string name) { AttributeScope scope = GetLocalAttributeScope(name); if (scope != null) { return(scope); } if (RuleScope != null && RuleScope.GetAttribute(name) != null) { scope = RuleScope; } return(scope); }
public Rule( SqlInt32 ruleId, SqlInt32 objType, SqlString objScope, SqlString matchString ) { Debug.Assert(!ruleId.IsNull); Debug.Assert(!objType.IsNull); m_RuleId = ruleId.IsNull ? Constants.InvalidId : ruleId.Value; m_ObjectType = getRuleObjectType(objType); m_ObjectScope = getRuleScope(objScope); m_MatchString = matchString.IsNull ? string.Empty : matchString.Value; }
/// <summary> /// The abstract rule constructor /// </summary> /// <param name="name">The name of this rule. Must be globally unique among all rules.</param> /// <param name="number">The number of this rule in format [D]D.DD[.DD]. Must be globally unique among all rules.</param> /// <param name="scope">The scope that this rule applies. Direct matches are searched for when applying rule logic</param> /// <param name="order">The order in which a rule should run relative to it's scope. Rules are ordered ascending by this value.</param> public Rule(string name, string number, RuleScope scope, int order = 100) { if (String.IsNullOrEmpty(name) || name.Trim().Length == 0) { throw new RuleException("Rules must be provided with a rule name."); } if (String.IsNullOrEmpty(number)) { throw new RuleException("Rules must be provided with a rule number."); } if (!Regex.IsMatch(number, @"^[1-9]{1}(?:\d{1})?((\.\d{1,2}){0,2})?$")) { throw new RuleException("Rule numbers must be in the form ##.##[.##]. Number can not start with a 0 (zero)."); } _name = name.Trim(); _scope = scope; _number = number; _order = order; }
public override RuleOutcome EvaluateRuleSet(RuleScope scope, bool includeGlobalScope = true, Func <Rule, RuleScope, bool> scopeEvaluator = null) { Debug.Print("~~~~~ RULE SET EVAL ~~~~~~~"); Debug.Print("Scope: {0}", scope.ToString()); Debug.Print("PRE EVAL CONTEXT ----------"); Debug.Print(Context.ToString()); Debug.Print("-"); var outcome = base.EvaluateRuleSet(scope, includeGlobalScope, scopeEvaluator); Debug.Print("POST EVAL CONTEXT --------"); Debug.Print(Context.ToString()); Debug.Print("Outcome: {0}", outcome.Result.ToString()); Debug.Print("~~~~~~~~~~~~~~~~~~~~~~~~~~~"); Debug.Print(""); return(outcome); }
public virtual RuleOutcome EvaluateRuleSet(RuleScope scope, bool includeGlobalScope = true, Func <Rule, RuleScope, bool> scopeEvaluator = null) { List <Rule> rules = GetRulesByScope(scope, includeGlobalScope, scopeEvaluator); if (rules != null && rules.Count > 0) { foreach (var rule in rules) { RuleOutcome outcome = rule.Evaluate(); if (!outcome.IsAllowed) { //rule pipeline failed :( return(outcome); } } } return(RuleOutcome.Allowed); }
public static string GetTypeName(this RuleScope scope) { if (scope.IsMultiple()) { throw new InvalidOperationException("The provided RuleScope enum value has more than one flag set."); } var x = scope.ToString().SplitCamelCase(); if (scope == RuleScope.Hierarchy) { x = "Hierarchies"; } else if (scope != RuleScope.Model) { x += "s"; } return(x); }
internal SpamRuleBlobPackage FindSpamRulesBlob(RuleScope scopeId, RuleStatusType publishingState, ref string pageCookie) { List <SpamRuleBlob> list = this.FindPagedSpamRules(scopeId, publishingState, ref pageCookie).ToList <SpamRuleBlob>(); if (list.Any <SpamRuleBlob>()) { HashSet <string> hashSet = new HashSet <string>(); foreach (SpamRuleBlob spamRuleBlob in list) { hashSet.AddRange(SpamRuleBlobUtils.GetProcessorIds(spamRuleBlob.ProcessorData)); } List <SpamRuleProcessorBlob> spamRuleProcessorBlobs = this.ReadSpamRuleProcessorsByProcessorIds <SpamRuleProcessorBlob>(hashSet).ToList <SpamRuleProcessorBlob>(); return(new SpamRuleBlobPackage { SpamRuleBlobs = list, SpamRuleProcessorBlobs = spamRuleProcessorBlobs }); } return(null); }
internal IEnumerable <SpamRuleBlob> FindPagedSpamRules(RuleScope scopeId, RuleStatusType publishingState, ref string pageCookie) { List <SpamRuleBlob> list = new List <SpamRuleBlob>(); QueryFilter baseQueryFilter = QueryFilter.AndTogether(new QueryFilter[] { new ComparisonFilter(ComparisonOperator.Equal, SpamRuleBlobSchema.ScopeIdProperty, (byte)scopeId), new ComparisonFilter(ComparisonOperator.Equal, SpamRuleBlobSchema.PublishingStateProperty, (byte)publishingState), base.BuildVersionParam }); bool flag = false; while (!flag) { QueryFilter pagingQueryFilter = PagingHelper.GetPagingQueryFilter(baseQueryFilter, pageCookie); IEnumerable <SpamRuleBlob> collection = base.DataProvider.FindPaged <SpamRuleBlob>(pagingQueryFilter, null, true, null, 1000); list.AddRange(collection); pageCookie = PagingHelper.GetProcessedCookie(pagingQueryFilter, out flag); } return(list); }
/// <summary> /// Creates a new rule instance requiring naming to match given /// pattern on certain scope. /// </summary> /// <param name="regexPattern"> /// Regular expression pattern which must be met on scope given by /// parameter <paramref name="scope"/>. /// </param> /// <param name="scope"> /// Scope of the rule. /// </param> /// <param name="excludedSchemaNames"> /// List of schema names which are exluded from the rule. /// </param> public RegexRule(string regexPattern, RuleScope scope, ICollection <string> excludedSchemaNames) { if (excludedSchemaNames == null) { throw new ArgumentNullException(nameof(excludedSchemaNames)); } try { Pattern = new Regex(regexPattern); } catch (ArgumentException ex) { throw new ArgumentException( $"Given pattern \"{regexPattern}\" was invalid regular expression.", ex); } Scope = scope; _excludedSchemaNames = excludedSchemaNames; }
public static bool IsMultiple(this RuleScope scope) { return((scope & (scope - 1)) != 0); }
public BaseSubverseMinCCPRule(string name, string number, RuleScope scope) : base(name, number, scope) { }
public SubmissionBannedDomainRule(string name, string number, RuleScope scope) : base(name, number, scope) { }
private IQueryable GetCollection(RuleScope scope) { switch (scope) { case RuleScope.KPI: return(Model.AllMeasures.Where(m => m.KPI != null).Select(m => m.KPI).AsQueryable()); case RuleScope.CalculatedColumn: return(Model.AllColumns.OfType <CalculatedColumn>().AsQueryable()); case RuleScope.CalculatedTable: return(Model.Tables.OfType <CalculatedTable>().AsQueryable()); case RuleScope.CalculatedTableColumn: return(Model.Tables.OfType <CalculatedTable>().SelectMany(t => t.Columns).OfType <CalculatedTableColumn>().AsQueryable()); case RuleScope.Culture: return(Model.Cultures.AsQueryable()); case RuleScope.DataColumn: return(Model.AllColumns.OfType <DataColumn>().AsQueryable()); case RuleScope.ProviderDataSource: return(Model.DataSources.OfType <ProviderDataSource>().AsQueryable()); case RuleScope.StructuredDataSource: return(Model.DataSources.OfType <StructuredDataSource>().AsQueryable()); case RuleScope.Hierarchy: return(Model.AllHierarchies.AsQueryable()); case RuleScope.Level: return(Model.AllLevels.AsQueryable()); case RuleScope.Measure: return(Model.AllMeasures.AsQueryable()); case RuleScope.Model: return(Enumerable.Repeat(Model, 1).AsQueryable()); case RuleScope.Partition: return(Model.AllPartitions.AsQueryable()); case RuleScope.Perspective: return(Model.Perspectives.AsQueryable()); case RuleScope.Relationship: return(Model.Relationships.OfType <SingleColumnRelationship>().AsQueryable()); case RuleScope.Table: return(Model.Tables.Where(t => !(t is CalculatedTable)).AsQueryable()); case RuleScope.ModelRole: return(Model.Roles.AsQueryable()); case RuleScope.NamedExpression: return(Model.Expressions.AsQueryable()); case RuleScope.Variation: return(Model.AllColumns.SelectMany(c => c.Variations).AsQueryable()); default: return(Enumerable.Empty <TabularNamedObject>().AsQueryable()); } }
public MinCCPRule(string name, string number, int minCCP, RuleScope scope) : base(name, number, scope) { MinCCP = minCCP; }
private IQueryable GetCollection(RuleScope scope) { return(GetCollection(Model, scope)); }
public TestRule(string name, string number, RuleScope scope) : base(name, number, scope) { }
public BaseVoatRule(string name, string number, RuleScope scope, int order = 100) : base(name, number, scope, order) { /*no-op*/ }
public BaseCCPVote(string name, string number, int minCCP, RuleScope scope) : base(name, number, minCCP, scope) { }
/// <summary> /// 判断当前规则是否与指定的范围有重叠。 /// </summary> /// <param name="scope"></param> /// <returns></returns> public bool HasScope(RuleScope scope) { return (this.Scope & scope) != 0; }