void IPolicy.Execute(params object[] facts) { using (var policy = new Microsoft.RuleEngine.Policy(_ruleSetInfo.Name, _ruleSetInfo.MajorRevision, _ruleSetInfo.MinorRevision)) { var interceptor = RuleSetTrackingLogger.Create(policy.RuleSetInfo); if (interceptor == null) { policy.Execute(facts); } else { policy.Execute(facts, interceptor); } } }
private void FireRule(XmlDocument xd, string fileName) { string destDirectory = outboundTxtBx.Text.Trim(); string traceDirectory = traceTxtBx.Text.Trim(); TypedXmlDocument doc1 = new TypedXmlDocument("TrackSource.BizTalk.Schemas.InboundToRules", xd); string [] policies = new string[14] { "Data Preparation", "Pre-Processing Group 1", "Pre-Processing Group 2", "Pre-Processing Group 3", "Pre-Processing Group 4", "Pre-Processing Group 5", "Pre-Processing Group 6", "Lender Specific", "Insurance Servicing", "Post Processing Group 1", "Post Processing Group 2", "Black Hole", "Fidelity Hold", "Procedure Set" }; ProcessingTxtBx.Text = ProcessingTxtBx.Text + "Processing Started " + DateTime.Now + "\r\n"; string traceFileName = fileName + "_RuleTrace"; traceFileName = traceDirectory + traceFileName + ".txt"; if (File.Exists(traceFileName)) { File.Delete(traceFileName); } DebugTrackingInterceptor dti = new DebugTrackingInterceptor(traceFileName); try { for (int i = 0; i < policies.Length; i++) { string PolicyName = policies[i].Trim(); lblProcessing.Text = PolicyName; ProcessingTxtBx.Text = ProcessingTxtBx.Text + "Processing ... " + policies[i] + " " + DateTime.Now + "\r\n"; Application.DoEvents(); Microsoft.RuleEngine.Policy tstPolicy = new Microsoft.RuleEngine.Policy(PolicyName); ArrayList shortTermFacts = new ArrayList(); shortTermFacts = GetFacts(PolicyName); shortTermFacts.Add(doc1); tstPolicy.Execute(shortTermFacts.ToArray(), dti); tstPolicy = null; } } catch (Exception ex) { ProcessingTxtBx.Text = ProcessingTxtBx.Text + "Exception Caught Check _Excepion Text File"; string exceptionFileName = fileName + "_Exception"; exceptionFileName = traceDirectory + exceptionFileName + ".txt"; if (File.Exists(exceptionFileName)) { File.Delete(exceptionFileName); } StreamWriter sw2 = new StreamWriter(exceptionFileName); ProcessingTxtBx.Text = ProcessingTxtBx.Text + ex.Message; sw2.Write(ex.Message.ToString()); sw2.Close(); } finally { ProcessingTxtBx.Text = ProcessingTxtBx.Text + "Processing Done " + DateTime.Now + "\r\n"; lblProcessing.Text = "Writing output File"; string processedDoc = fileName + "_Outbound"; processedDoc = destDirectory + processedDoc + ".xml"; if (File.Exists(processedDoc)) { File.Delete(processedDoc); } StreamWriter sw = new StreamWriter(processedDoc); dti.CloseTraceFile(); sw.Write(doc1.Document.InnerXml.ToString()); sw.Close(); xd = null; doc1 = null; lblProcessing.Text = "Processing Completed for " + fileName; } //return xd; }
/// <summary> /// Execute the policy in question, utilizing the DebutTrackingInspector if a TrackingFolder has been specified, and applying specific policy versions /// if they have been specified /// </summary> private void ExecutePolicy(string policyName, object[] facts, string policyVersion) { DebugTrackingInterceptor dti = null; Microsoft.RuleEngine.Policy policy = null; if (string.IsNullOrEmpty(policyVersion)) { policy = new Policy(policyName); } else { int majorVersion; int minorVersion; string[] versionArray = policyVersion.Split('.'); if (versionArray.Length != 2) { throw new Exception(string.Format("Version number {0} for policy {1} did not correctly resolve to a proper major and minor version number.", policyVersion, policyName)); } if (!int.TryParse(versionArray[0], out majorVersion)) { throw new Exception(string.Format("Major version number {0} for policy {1} is invalid.", versionArray[0], policyName)); } if (!int.TryParse(versionArray[1], out minorVersion)) { throw new Exception(string.Format("Minor version number {0} for policy {1} is invalid.", versionArray[1], policyName)); } policy = new Policy(policyName, majorVersion, minorVersion); } try { TraceManager.PipelineComponent.TraceInfo("{0} - Executing Policy {1} {2}.{3}", callToken, policy.PolicyName, policy.MajorRevision.ToString(), policy.MinorRevision.ToString()); if (string.IsNullOrEmpty(trackingFolder)) { policy.Execute(facts); } else { if (trackingFolder.Substring(trackingFolder.Length - 1) != @"\") { trackingFolder = trackingFolder + @"\"; } if (!Directory.Exists(trackingFolder)) { throw new Exception(String.Format("Tracking folder {0} does not resolve to a valid folder location", trackingFolder)); } dti = new DebugTrackingInterceptor(trackingFolder + String.Format("{0}_{1}.{2}_{3}.txt", policy.PolicyName, policy.MajorRevision.ToString(), policy.MinorRevision.ToString(), callToken)); policy.Execute(facts, dti); } } catch (PolicyExecutionException ex) { TraceManager.PipelineComponent.TraceError(ex, true, Guid.Parse(callToken)); string exceptionMessage = string.Format("Exception encountered while executing BRE policy {0}. Top level exception message - {1}", policyName, ex.InnerException.Message); if (ex.InnerException.InnerException != null) { exceptionMessage = exceptionMessage + Environment.NewLine; exceptionMessage = exceptionMessage + string.Format("Innermost exception was - {0}", ex.GetBaseException()); } throw new Exception(exceptionMessage); } catch (Exception e) { TraceManager.PipelineComponent.TraceError(e, true, Guid.Parse(callToken)); throw; } finally { if (dti != null) { dti.CloseTraceFile(); } if (policy != null) { policy.Dispose(); } } }