public void AssignRulesToEntities() { JarsResource resource = FakeDataHelper.FakeResources[0]; JarsJob testJob = FakeDataHelper.FakeJarsJobs[1]; //set up a rule for a target entity where target entity can not be changed IJarsRule singleEntityRule = JarsRulePopupForm.AddRuleOnEntity(resource, testJob.GetType()); Assert.IsTrue(!string.IsNullOrEmpty(singleEntityRule.TargetCriteriaString)); ////set up a rule for an entity type instead of a certain entity //IEntityRule singleTypeRule = EntityRuleForm.AddRuleOnEntity(resource); //Assert.IsTrue(singleTypeRule.TargetTypeName == typeof(Resource).Name && singleTypeRule.SourceTypeName == typeof(Resource).Name); ////set up a rule for an entity type that relies on values from another entity type //IEntityRule linkedTypeRule = EntityRuleForm.AddRuleOnEntity(resource, typeof(JarsJob)); //Assert.IsTrue(linkedTypeRule.TargetTypeName == typeof(Resource).Name // && linkedTypeRule.SourceTypeName == typeof(JarsJob).Name // && !string.IsNullOrEmpty(linkedTypeRule.SourceCriteriaString) // && !string.IsNullOrEmpty(linkedTypeRule.TargetCriteriaString)); singleEntityRule = JarsRulePopupForm.EditRuleOnEntity(resource, singleEntityRule); Assert.IsTrue(!string.IsNullOrEmpty(singleEntityRule.TargetCriteriaString)); }
private void btnEditCondition_Click(object sender, EventArgs e) { if (defaultBindingSource.Current is JarsResource) { IJarsRule ec = entityRuleBindingSource.Current as JarsRule; ec = JarsRulePopupForm.EditRuleOnEntity(defaultBindingSource.Current as JarsResource, ec); OnSaveEntityRule(ec as JarsRule); } }
/// <summary> /// This rule check if the source entity has any list properties that contain types that has rules set against the target entity. /// ie. a Job with a sub job line that has restrictions on the target. /// </summary> /// <param name="entityRules">This can be a list of all the rules or an already filtered list, the rules will be checked again.</param> /// <param name="sourceEntity">The entity that acts as the source for the rule</param> /// <param name="targetEntity">The entity that acts as the target for the rule.</param> /// <param name="ruleApplicator">The event or process the event is applied to.</param> /// <returns></returns> public IJarsRule EvaluateSourceChildListsAgainstTarget(List <IJarsRule> entityRules, IEntityBase sourceEntity, IEntityBase targetEntity, RuleRunsOn ruleApplicator) { var sourceDictionaryListTypes = sourceEntity.GetGenericListTypesDictionary(); //get the list of possible rules where the target type is the type of the key value. //https://dotnetable.wordpress.com/2015/06/20/find-all-items-in-list-which-exist-in-another-list-using-linq/ //this can be seen as retrieve all the values from entityRules which also exist in dictionary values. var filteredRules = (from er in entityRules join kVals in sourceDictionaryListTypes.Values on er.SourceTypeName equals kVals.Name select er).Where(r => r.RuleRunsOn.Contains(ruleApplicator.ToString())).ToList(); if (!filteredRules.Any()) { return(null); } /* * * var listType = typeof(List<>); * var constructedListType = listType.MakeGenericType(t); * var instance = Activator.CreateInstance(constructedListType); * */ IJarsRule failRule = null; //test each collection foreach (var dictionaryKey in sourceDictionaryListTypes) { //var itemsListType = typeof(IList<>); //var defaultList = itemsListType.MakeGenericType(dictionaryKey.Value); var sourceItems = (IList)sourceEntity.GetType() .GetProperty(dictionaryKey.Key) .GetValue(sourceEntity); //test the property type first foreach (var subItem in sourceItems) { //test broad failRule = EvaluateSourceAgainstTargetType(filteredRules, subItem as IEntityBase, targetEntity, ruleApplicator); if (failRule != null) { return(failRule); } //test narrow failRule = EvaluateSourceAgainstTarget(filteredRules, subItem as IEntityBase, targetEntity, ruleApplicator); if (failRule != null) { return(failRule); } } } return(null); }
private void btnAddCondition_Click(object sender, EventArgs e) { if (defaultBindingSource.Current is JarsResource) { IJarsRule ec = JarsRulePopupForm.AddRuleOnEntity(defaultBindingSource.Current as JarsResource); if (ec != null) { OnSaveEntityRule(ec as JarsRule); } } }
private void btnRemoveCondition_Click(object sender, EventArgs e) { if (defaultBindingSource.Current is JarsResource) { IJarsRule ec = entityRuleBindingSource.Current as JarsRule; IJarsRule findCond = ResourceEntityRulesList.FirstOrDefault(c => c.Id == ec.Id); if (findCond != null) { //remove ResourceEntityRulesList.Remove(findCond); } UpdateLinkedBindingSources(); } }
public override void OnMessageEvent(ServiceStack.ServerEventMessage msg) { if (msg.Channel != typeof(JarsResource).Name) { return; } switch (msg.Selector) { case SelectorTypes.delete: MessageBox.Show($"DELETE - Op:{msg.Op} Selector:{msg.Selector} Target:{msg.Target} Channel:{msg.Channel} EventId:{msg.EventId}"); break; case SelectorTypes.store: if (msg.Channel == nameof(JarsRule)) { JarsRule entityCon = msg.Json.FromJson <JarsRule>(); IJarsRule findCond = ResourceEntityRulesList.FirstOrDefault(c => c.Id == entityCon.Id); if (findCond != null) { //replace ResourceEntityRulesList.Remove(findCond); ResourceEntityRulesList.Add(entityCon); } else { //add ResourceEntityRulesList.Add(entityCon); } //update bindings UpdateLinkedBindingSources(); } //MessageBox.Show($"SAVE - Op:{msg.Op} Selector:{msg.Selector} Target:{msg.Target} Channel:{msg.Channel} EventId:{msg.EventId}"); break; } base.OnMessageEvent(msg); }
/// <summary> /// Evaluates the rules that are linked to the entities involved in the operation being carried out. /// If it doesn't return a rule it means all rules have passed the test. /// </summary> /// <param name="entityRules">The list of rules available for evaluation, this list can be the full list of rules, as it will be filtered according to the passed in target and source entities.</param> /// <param name="sourceEntity">The entity that acts as the source for the rule</param> /// <param name="targetEntity">The entity that acts as the target for the rule.</param> /// <param name="ruleApplicator">The event or process the event is applied to.</param> /// <returns>If a rule gets returned it will be the first rule the process failed on, if nothing gets returned all rules passed.</returns> public IJarsRule EvaluateEntityRules(IList <IJarsRule> entityRules, IEntityBase sourceEntity, IEntityBase targetEntity, RuleRunsOn ruleRunsOn) { IJarsRule failedRule = null; List <IJarsRule> filteredByRuleRunsOnList = entityRules.Where(r => r.RuleRunsOn.Contains(ruleRunsOn.ToString())).ToList(); //first check the rule for source only rules var fullSourceOnlyFilterList = filteredByRuleRunsOnList.FindAll(rules => rules.RuleEvaluation == RuleEvaluation.SourceOnly && rules.SourceTypeName == sourceEntity.GetType().Name); //does the source meet the its own conditions? if (fullSourceOnlyFilterList.Any()) { failedRule = EvaluateEntityOwnRules(fullSourceOnlyFilterList, sourceEntity, ruleRunsOn, RuleEvaluation.SourceOnly); } if (failedRule != null) { return(failedRule); } //first check the rule for source only rules var fullTargeteOnlyFilterList = filteredByRuleRunsOnList.FindAll(rules => rules.RuleEvaluation == RuleEvaluation.TargetOnly && rules.TargetTypeName == targetEntity.GetType().Name); //does the source meet the its own conditions? if (fullTargeteOnlyFilterList.Any()) { failedRule = EvaluateEntityOwnRules(fullTargeteOnlyFilterList, targetEntity, ruleRunsOn, RuleEvaluation.TargetOnly); } if (failedRule != null) { return(failedRule); } //---- from here we test both sides //need to test rules of target type on target type //and source on target type //get all rules linked to the target var fullSourceFilterList = filteredByRuleRunsOnList.FindAll(rules => rules.RuleEvaluation == RuleEvaluation.Both && rules.SourceTypeName == sourceEntity.GetType().Name); //does the source meet the its own conditions? if (fullSourceFilterList.Any()) { failedRule = EvaluateEntityOwnRules(fullSourceFilterList, sourceEntity, ruleRunsOn); if (failedRule != null) { return(failedRule); } //does the target meet the source conditions? failedRule = EvaluateSourceAgainstTarget(fullSourceFilterList, targetEntity, sourceEntity, ruleRunsOn); if (failedRule != null) { return(failedRule); } } var fullTargetFilterList = filteredByRuleRunsOnList.FindAll(rules => rules.TargetTypeName == targetEntity.GetType().Name); //does the target meet the its own conditions? if (fullTargetFilterList.Any()) { failedRule = EvaluateEntityOwnRules(fullTargetFilterList, targetEntity, ruleRunsOn); if (failedRule != null) { return(failedRule); } //does the source meet the target conditions? failedRule = EvaluateSourceAgainstTarget(fullTargetFilterList, sourceEntity, targetEntity, ruleRunsOn); if (failedRule != null) { return(failedRule); } failedRule = EvaluateSourceAgainstTargetType(fullTargetFilterList, sourceEntity, targetEntity, ruleRunsOn); if (failedRule != null) { return(failedRule); } failedRule = EvaluateSourceChildListsAgainstTarget(fullTargetFilterList, sourceEntity, targetEntity, ruleRunsOn); if (failedRule != null) { return(failedRule); } } return(null); }
/// <summary> /// Process the rule criteria on the data provided. /// If this method return true the rule was met. /// </summary> /// <param name="sourceDataToTest">The data that will be used when testing the criteria string</param> /// <param name="entityRule">the criteria that will be used to determine if the condition is true or false</param> /// <returns>If true then the rule was met and the condition passed, if false the rule failed and the condition was not met.</returns> public bool EvaluateRule(DataTable sourceDataToTest = null, DataTable targetDataToTest = null, IJarsRule entityRule = null) { bool result = true; int sourceCount = 0, targetCount = 0, lowValue = 0; if (entityRule == null) { return(result); } try { if ((sourceDataToTest != null && targetDataToTest != null) && entityRule.RuleEvaluation == RuleEvaluation.Both) { sourceDataToTest.DefaultView.RowFilter = CriteriaToWhereClauseHelper.GetDataSetWhere(CriteriaOperator.Parse(entityRule.SourceCriteriaString)); targetDataToTest.DefaultView.RowFilter = CriteriaToWhereClauseHelper.GetDataSetWhere(CriteriaOperator.Parse(entityRule.TargetCriteriaString)); if (sourceDataToTest.DefaultView.Count > 0) { sourceCount = 1; } if (targetDataToTest.DefaultView.Count > 0) { targetCount = 1; } lowValue = 1; //starts at 1 because both conditions needs to be met. } if (sourceDataToTest != null && targetDataToTest == null && entityRule.RuleEvaluation == RuleEvaluation.SourceOnly) { sourceDataToTest.DefaultView.RowFilter = CriteriaToWhereClauseHelper.GetDataSetWhere(CriteriaOperator.Parse(entityRule.SourceCriteriaString)); sourceCount = sourceDataToTest.DefaultView.Count; } if (targetDataToTest != null && sourceDataToTest == null && entityRule.RuleEvaluation == RuleEvaluation.TargetOnly) { targetDataToTest.DefaultView.RowFilter = CriteriaToWhereClauseHelper.GetDataSetWhere(CriteriaOperator.Parse(entityRule.TargetCriteriaString)); targetCount = targetDataToTest.DefaultView.Count; } if ((sourceCount + targetCount) > lowValue)//there was a match found, so the entity matches the rule (pass) { //unless the rule needs to fail to pass if (entityRule.RulePassesWhen == RulePassesWhen.IsFalse) { result = true; } } else//there are no matches, so the rule fails.. { result = false; //if the rule needs to be false to pass we swap the values. if (entityRule.RulePassesWhen == RulePassesWhen.IsFalse) { result = true; } } } catch (Exception ex) { Logger.Error(ex.Message); result = false; #if DEBUG throw ex; #endif } return(result); }