public async Task <bool> SatisfiesPredicate(object entity, ScriptPredicate predicate) { Check.NotNull(entity, nameof(entity)); Check.NotNull(predicate, nameof(predicate)); var attributedEntity = entity as IAttributedEntity; if (attributedEntity == null) { throw new InvalidOperationException( $"The entity being evaluated must implement: {typeof(IAttributedEntity)}"); } await ExecuteAsync(entity, predicate.ScriptName); if (!attributedEntity.Attributes.Contains(predicate.AttributeName)) { throw new InvalidOperationException( $"After the predicate script named: {predicate.ScriptName} was executed, " + $"the expected predicate attribute named: {predicate.AttributeName} was not" + $"calculated."); } object value = attributedEntity.Attributes.GetValue(predicate.AttributeName); if (value.GetType() != typeof(bool)) { throw new InvalidOperationException( $"After the predicate script named: {predicate.ScriptName} was executed, " + $"the expected predicate attribute named: {predicate.AttributeName} was not" + $"a Boolean value type. The type of the value was: {value.GetType()}."); } return((bool)value); }
// Determines if the message should be delivered to the queue. If the exchange is marked // with a predicate attribute, the corresponding externally named script is executed to // determine if the message has passing criteria. If no external script is specified, // the exchange's Satisfies method is called. private async Task <bool> SatisfiesExchangeCriteria(MessageExchangeDefinition exchangeDef, IMessage message) { ScriptPredicate predicate = exchangeDef.Exchange.Settings.Predicate; if (predicate != null) { return(await _scriptingSrv.SatisfiesPredicate(message, predicate)); } return(exchangeDef.Exchange.Satisfies(message)); }
private async Task <bool> PassesDispatchCriteria(MessageDispatchInfo dispatchInfo, IMessage message) { ScriptPredicate predicate = dispatchInfo.Predicate; if (predicate != null) { return(await _scriptingSrv.SatisfiesPredicate(message, predicate)); } return(dispatchInfo.IsMatch(message)); }
// Determines message dispatchers that apply to the message being published. This is optional and // specified by decorating the message handler method with attributes. private async Task <bool> PassesDispatchCriteria(MessageDispatchInfo dispatchInfo, IMessage message) { ScriptPredicate predicate = dispatchInfo.Predicate; // Run a dynamic script against the message and check the result of the specified predicate property. // This allow storing rule predicates external from the code. if (predicate != null) { return(await _scriptingSrv.SatisfiesPredicateAsync(message, predicate)); } // Check static rules to determine if message meets criteria. return(dispatchInfo.IsMatch(message)); }
/// <summary> /// Specifies a named script that should be evaluated for the message to determine if /// it applies to the exchange and should be published. /// </summary> /// <param name="scriptName">The name of the script to execute against the message.</param> /// <param name="attributeName">The script predicate attribute to check value of after /// the script's expression has been evaluated.</param> public void SetPredicate(string scriptName, string attributeName) { if (string.IsNullOrWhiteSpace(scriptName)) { throw new ArgumentException("Script name not specified.", nameof(scriptName)); } if (string.IsNullOrWhiteSpace(attributeName)) { throw new ArgumentException("Attribute name not specified.", nameof(attributeName)); } ScriptPredicate = new ScriptPredicate { ScriptName = scriptName, AttributeName = attributeName }; }