Beispiel #1
0
        /// <summary>
        /// Invokes a Business Rules policy specified in the supplied <see cref="PolicyExecutionInfo"/> object using the list of zero or more facts.
        /// </summary>
        /// <param name="policyExecInfo">The <see cref="PolicyExecutionInfo"/> object containing policy name, version number and parameters to be used when invoking the policy.</param>
        /// <param name="facts">The list of facts to be used when invoking the policy.</param>
        /// <returns>The <see cref="PolicyExecutionResult"/> object containing the results of policy execution.</returns>
        public static PolicyExecutionResult Execute(PolicyExecutionInfo policyExecInfo, params object[] facts)
        {
            Guard.ArgumentNotNull(policyExecInfo, "policyExecInfo");
            Guard.ArgumentNotNullOrEmptyString(policyExecInfo.PolicyName, "policyExecInfo.PolicyName");

            var callToken = TraceManager.RulesComponent.TraceIn(policyExecInfo.PolicyName, policyExecInfo.PolicyVersion);

            Version policyVersion            = policyExecInfo.PolicyVersion;
            PolicyExecutionResult execResult = new PolicyExecutionResult(policyExecInfo.PolicyName, false);

            PolicyFetchErrorHandler errorHandler = delegate(Exception ex)
            {
                TraceManager.RulesComponent.TraceError(ex);
                execResult.Errors.Add(ex);
            };

            try
            {
                using (Policy policy = policyVersion != null ? new Policy(policyExecInfo.PolicyName, policyVersion.Major, policyVersion.Minor, errorHandler) : new Policy(policyExecInfo.PolicyName, errorHandler))
                {
                    List <object> agendaFacts = new List <object>(facts);
                    agendaFacts.Add(policyExecInfo);

                    using (TracingRuleTrackingInterceptor trackingInterceptor = new TracingRuleTrackingInterceptor())
                    {
                        // Write the Start event to measure how long it takes to execute the BRE policy.
                        var scopeStarted = TraceManager.RulesComponent.TraceStartScope(String.Format("{0} (\"{1}\", {2}.{3})", InstrumentedScopes.ExecutePolicy, policy.PolicyName, policy.MajorRevision, policy.MinorRevision));

                        policy.Execute(agendaFacts.ToArray(), trackingInterceptor);

                        // Once finished, write the End event along with calculated duration.
                        TraceManager.RulesComponent.TraceEndScope(String.Format("{0} (\"{1}\", {2}.{3})", InstrumentedScopes.ExecutePolicy, policy.PolicyName, policy.MajorRevision, policy.MinorRevision), scopeStarted);
                    }

                    execResult = new PolicyExecutionResult(policyExecInfo.PolicyName, policy.MajorRevision, policy.MinorRevision);
                }
            }
            catch (Exception ex)
            {
                TraceManager.RulesComponent.TraceError(ex);
                execResult.Errors.Add(ex);
            }

            TraceManager.RulesComponent.TraceOut(callToken, execResult.PolicyName, execResult.PolicyVersion, execResult.Success);
            return(execResult);
        }
Beispiel #2
0
 /// <summary>
 /// Invokes a Business Rules policy specified in the supplied <see cref="PolicyExecutionInfo"/> object using the collection of specified facts.
 /// </summary>
 /// <param name="policyExecInfo">The <see cref="PolicyExecutionInfo"/> object containing policy name, version number and parameters to be used when invoking the policy.</param>
 /// <param name="facts">The collection of facts to be used when invoking the policy.</param>
 /// <returns>The <see cref="PolicyExecutionResult"/> object containing the results of policy execution.</returns>
 public static PolicyExecutionResult Execute(PolicyExecutionInfo policyExecInfo, IEnumerable facts)
 {
     Guard.ArgumentNotNull(facts, "facts");
     return(Execute(policyExecInfo, facts.Cast <object>().ToArray <object>()));
 }