private static void AnalyzeRules(RuleChainingBehavior behavior, List<RuleState> ruleStates, RuleValidation validation, Tracer tracer) { int i; int numRules = ruleStates.Count; // if no chaining is required, then nothing to do if (behavior == RuleChainingBehavior.None) return; // Analyze all the rules and collect all the dependencies & side-effects RuleSymbolInfo[] ruleSymbols = new RuleSymbolInfo[numRules]; for (i = 0; i < numRules; ++i) ruleSymbols[i] = AnalyzeRule(behavior, ruleStates[i].Rule, validation, tracer); for (i = 0; i < numRules; ++i) { RuleState currentRuleState = ruleStates[i]; if (ruleSymbols[i].thenSideEffects != null) { currentRuleState.ThenActionsActiveRules = AnalyzeSideEffects(ruleSymbols[i].thenSideEffects, ruleSymbols); if ((currentRuleState.ThenActionsActiveRules != null) && (tracer != null)) tracer.TraceThenTriggers(currentRuleState.Rule.Name, currentRuleState.ThenActionsActiveRules, ruleStates); } if (ruleSymbols[i].elseSideEffects != null) { currentRuleState.ElseActionsActiveRules = AnalyzeSideEffects(ruleSymbols[i].elseSideEffects, ruleSymbols); if ((currentRuleState.ElseActionsActiveRules != null) && (tracer != null)) tracer.TraceElseTriggers(currentRuleState.Rule.Name, currentRuleState.ElseActionsActiveRules, ruleStates); } } }
private static RuleSymbolInfo AnalyzeRule(RuleChainingBehavior behavior, Rule rule, RuleValidation validator, Tracer tracer) { RuleSymbolInfo rsi = new RuleSymbolInfo(); if (rule.Condition != null) { rsi.conditionDependencies = rule.Condition.GetDependencies(validator); if ((rsi.conditionDependencies != null) && (tracer != null)) tracer.TraceConditionSymbols(rule.Name, rsi.conditionDependencies); } if (rule.thenActions != null) { rsi.thenSideEffects = GetActionSideEffects(behavior, rule.thenActions, validator); if ((rsi.thenSideEffects != null) && (tracer != null)) tracer.TraceThenSymbols(rule.Name, rsi.thenSideEffects); } if (rule.elseActions != null) { rsi.elseSideEffects = GetActionSideEffects(behavior, rule.elseActions, validator); if ((rsi.elseSideEffects != null) && (tracer != null)) tracer.TraceElseSymbols(rule.Name, rsi.elseSideEffects); } return rsi; }
internal static IList<RuleState> Preprocess(RuleChainingBehavior behavior, ICollection<Rule> rules, RuleValidation validation, Tracer tracer) { // start by taking the active rules and make them into a list sorted by priority List<RuleState> orderedRules = new List<RuleState>(rules.Count); foreach (Rule r in rules) { if (r.Active) orderedRules.Add(new RuleState(r)); } orderedRules.Sort(); // Analyze the rules to match side-effects with dependencies. // Note that the RuleSet needs to have been validated prior to this. AnalyzeRules(behavior, orderedRules, validation, tracer); // return the sorted list of rules return orderedRules; }
internal static void ExecuteRuleSet(IList<RuleState> orderedRules, RuleExecution ruleExecution, Tracer tracer, string trackingKey) { // keep track of rule execution long[] executionCount = new long[orderedRules.Count]; bool[] satisfied = new bool[orderedRules.Count]; // clear the halted flag ruleExecution.Halted = false; ActivityExecutionContext activityExecutionContext = ruleExecution.ActivityExecutionContext; // loop until we hit the end of the list int current = 0; while (current < orderedRules.Count) { RuleState currentRuleState = orderedRules[current]; // does this rule need to be evaluated? if (!satisfied[current]) { // yes, so evaluate it and determine the list of actions needed if (tracer != null) tracer.StartRule(currentRuleState.Rule.Name); satisfied[current] = true; bool result = currentRuleState.Rule.Condition.Evaluate(ruleExecution); if (tracer != null) tracer.RuleResult(currentRuleState.Rule.Name, result); if (activityExecutionContext != null && currentRuleState.Rule.Name != null) activityExecutionContext.TrackData(trackingKey, new RuleActionTrackingEvent(currentRuleState.Rule.Name, result)); ICollection<RuleAction> actions = (result) ? currentRuleState.Rule.thenActions : currentRuleState.Rule.elseActions; ICollection<int> activeRules = result ? currentRuleState.ThenActionsActiveRules : currentRuleState.ElseActionsActiveRules; // are there any actions to be performed? if ((actions != null) && (actions.Count > 0)) { ++executionCount[current]; string ruleName = currentRuleState.Rule.Name; if (tracer != null) tracer.StartActions(ruleName, result); // evaluate the actions foreach (RuleAction action in actions) { action.Execute(ruleExecution); // was Halt executed? if (ruleExecution.Halted) break; } // was Halt executed? if (ruleExecution.Halted) break; // any fields updated? if (activeRules != null) { foreach (int updatedRuleIndex in activeRules) { RuleState rs = orderedRules[updatedRuleIndex]; if (satisfied[updatedRuleIndex]) { // evaluate at least once, or repeatedly if appropriate if ((executionCount[updatedRuleIndex] == 0) || (rs.Rule.ReevaluationBehavior == RuleReevaluationBehavior.Always)) { if (tracer != null) tracer.TraceUpdate(ruleName, rs.Rule.Name); satisfied[updatedRuleIndex] = false; if (updatedRuleIndex < current) current = updatedRuleIndex; } } } } continue; } } ++current; } // no more rules to execute, so we are done }
internal static void ExecuteRuleSet(IList <RuleState> orderedRules, RuleExecution ruleExecution, Tracer tracer, string trackingKey) { long[] numArray = new long[orderedRules.Count]; bool[] flagArray = new bool[orderedRules.Count]; ruleExecution.Halted = false; ActivityExecutionContext activityExecutionContext = ruleExecution.ActivityExecutionContext; int index = 0; while (index < orderedRules.Count) { RuleState state = orderedRules[index]; if (!flagArray[index]) { if (tracer != null) { tracer.StartRule(state.Rule.Name); } flagArray[index] = true; bool result = state.Rule.Condition.Evaluate(ruleExecution); if (tracer != null) { tracer.RuleResult(state.Rule.Name, result); } if ((activityExecutionContext != null) && (state.Rule.Name != null)) { activityExecutionContext.TrackData(trackingKey, new RuleActionTrackingEvent(state.Rule.Name, result)); } ICollection <RuleAction> is2 = result ? state.Rule.thenActions : state.Rule.elseActions; ICollection <int> is3 = result ? state.ThenActionsActiveRules : state.ElseActionsActiveRules; if ((is2 != null) && (is2.Count > 0)) { numArray[index] += 1L; string name = state.Rule.Name; if (tracer != null) { tracer.StartActions(name, result); } foreach (RuleAction action in is2) { action.Execute(ruleExecution); if (ruleExecution.Halted) { break; } } if (ruleExecution.Halted) { return; } if (is3 != null) { foreach (int num2 in is3) { RuleState state2 = orderedRules[num2]; if (flagArray[num2] && ((numArray[num2] == 0L) || (state2.Rule.ReevaluationBehavior == RuleReevaluationBehavior.Always))) { if (tracer != null) { tracer.TraceUpdate(name, state2.Rule.Name); } flagArray[num2] = false; if (num2 < index) { index = num2; } } } } continue; } } index++; } }
internal static IList <RuleState> Preprocess(RuleChainingBehavior behavior, ICollection <Rule> rules, RuleValidation validation, Tracer tracer) { List <RuleState> ruleStates = new List <RuleState>(rules.Count); foreach (Rule rule in rules) { if (rule.Active) { ruleStates.Add(new RuleState(rule)); } } ruleStates.Sort(); AnalyzeRules(behavior, ruleStates, validation, tracer); return(ruleStates); }
private static void AnalyzeRules(RuleChainingBehavior behavior, List <RuleState> ruleStates, RuleValidation validation, Tracer tracer) { int count = ruleStates.Count; if (behavior != RuleChainingBehavior.None) { RuleSymbolInfo[] ruleSymbols = new RuleSymbolInfo[count]; int index = 0; while (index < count) { ruleSymbols[index] = AnalyzeRule(behavior, ruleStates[index].Rule, validation, tracer); index++; } for (index = 0; index < count; index++) { RuleState state = ruleStates[index]; if (ruleSymbols[index].thenSideEffects != null) { state.ThenActionsActiveRules = AnalyzeSideEffects(ruleSymbols[index].thenSideEffects, ruleSymbols); if ((state.ThenActionsActiveRules != null) && (tracer != null)) { tracer.TraceThenTriggers(state.Rule.Name, state.ThenActionsActiveRules, ruleStates); } } if (ruleSymbols[index].elseSideEffects != null) { state.ElseActionsActiveRules = AnalyzeSideEffects(ruleSymbols[index].elseSideEffects, ruleSymbols); if ((state.ElseActionsActiveRules != null) && (tracer != null)) { tracer.TraceElseTriggers(state.Rule.Name, state.ElseActionsActiveRules, ruleStates); } } } } }
private static RuleSymbolInfo AnalyzeRule(RuleChainingBehavior behavior, Rule rule, RuleValidation validator, Tracer tracer) { RuleSymbolInfo info = new RuleSymbolInfo(); if (rule.Condition != null) { info.conditionDependencies = rule.Condition.GetDependencies(validator); if ((info.conditionDependencies != null) && (tracer != null)) { tracer.TraceConditionSymbols(rule.Name, info.conditionDependencies); } } if (rule.thenActions != null) { info.thenSideEffects = GetActionSideEffects(behavior, rule.thenActions, validator); if ((info.thenSideEffects != null) && (tracer != null)) { tracer.TraceThenSymbols(rule.Name, info.thenSideEffects); } } if (rule.elseActions != null) { info.elseSideEffects = GetActionSideEffects(behavior, rule.elseActions, validator); if ((info.elseSideEffects != null) && (tracer != null)) { tracer.TraceElseSymbols(rule.Name, info.elseSideEffects); } } return(info); }
internal static IList<RuleState> Preprocess(RuleChainingBehavior behavior, ICollection<Rule> rules, RuleValidation validation, Tracer tracer) { List<RuleState> ruleStates = new List<RuleState>(rules.Count); foreach (Rule rule in rules) { if (rule.Active) { ruleStates.Add(new RuleState(rule)); } } ruleStates.Sort(); AnalyzeRules(behavior, ruleStates, validation, tracer); return ruleStates; }
internal static void ExecuteRuleSet(IList<RuleState> orderedRules, RuleExecution ruleExecution, Tracer tracer, string trackingKey) { long[] numArray = new long[orderedRules.Count]; bool[] flagArray = new bool[orderedRules.Count]; ruleExecution.Halted = false; ActivityExecutionContext activityExecutionContext = ruleExecution.ActivityExecutionContext; int index = 0; while (index < orderedRules.Count) { RuleState state = orderedRules[index]; if (!flagArray[index]) { if (tracer != null) { tracer.StartRule(state.Rule.Name); } flagArray[index] = true; bool result = state.Rule.Condition.Evaluate(ruleExecution); if (tracer != null) { tracer.RuleResult(state.Rule.Name, result); } if ((activityExecutionContext != null) && (state.Rule.Name != null)) { activityExecutionContext.TrackData(trackingKey, new RuleActionTrackingEvent(state.Rule.Name, result)); } ICollection<RuleAction> is2 = result ? state.Rule.thenActions : state.Rule.elseActions; ICollection<int> is3 = result ? state.ThenActionsActiveRules : state.ElseActionsActiveRules; if ((is2 != null) && (is2.Count > 0)) { numArray[index] += 1L; string name = state.Rule.Name; if (tracer != null) { tracer.StartActions(name, result); } foreach (RuleAction action in is2) { action.Execute(ruleExecution); if (ruleExecution.Halted) { break; } } if (ruleExecution.Halted) { return; } if (is3 != null) { foreach (int num2 in is3) { RuleState state2 = orderedRules[num2]; if (flagArray[num2] && ((numArray[num2] == 0L) || (state2.Rule.ReevaluationBehavior == RuleReevaluationBehavior.Always))) { if (tracer != null) { tracer.TraceUpdate(name, state2.Rule.Name); } flagArray[num2] = false; if (num2 < index) { index = num2; } } } } continue; } } index++; } }
private static void AnalyzeRules(RuleChainingBehavior behavior, List<RuleState> ruleStates, RuleValidation validation, Tracer tracer) { int count = ruleStates.Count; if (behavior != RuleChainingBehavior.None) { RuleSymbolInfo[] ruleSymbols = new RuleSymbolInfo[count]; int index = 0; while (index < count) { ruleSymbols[index] = AnalyzeRule(behavior, ruleStates[index].Rule, validation, tracer); index++; } for (index = 0; index < count; index++) { RuleState state = ruleStates[index]; if (ruleSymbols[index].thenSideEffects != null) { state.ThenActionsActiveRules = AnalyzeSideEffects(ruleSymbols[index].thenSideEffects, ruleSymbols); if ((state.ThenActionsActiveRules != null) && (tracer != null)) { tracer.TraceThenTriggers(state.Rule.Name, state.ThenActionsActiveRules, ruleStates); } } if (ruleSymbols[index].elseSideEffects != null) { state.ElseActionsActiveRules = AnalyzeSideEffects(ruleSymbols[index].elseSideEffects, ruleSymbols); if ((state.ElseActionsActiveRules != null) && (tracer != null)) { tracer.TraceElseTriggers(state.Rule.Name, state.ElseActionsActiveRules, ruleStates); } } } } }