Click here to Skip to main content
15,886,026 members
Articles / Desktop Programming / Windows Forms

Trigger Based Rule Engine

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
17 Jun 2009CPOL3 min read 46.3K   838   38  
Simple Rule Engine with Triggers to apply the rule
using System;
using System.Collections.Generic;

namespace SimpleRulesEngine.Trigger
{
	public class Or : ConditionBase
	{
		#region ctor

		/// <summary>
		/// Initializes a new instance of the <see cref="OrCondition"/> class.
		/// </summary>
		/// <param name="conditions">The conditions.</param>
		public Or(List<ConditionBase> conditions)
		{
			this.conditions = conditions;
		}

		#endregion

		#region private fields

		/// <summary>
		/// Conditions
		/// </summary>
		private List<ConditionBase> conditions;

		/// <summary>
		/// Trigger call back
		/// </summary>
		private Trigger trigger;

		#endregion

		#region methods

		/// <summary>
		/// Determines whether [is condition satisfied].
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if [is condition satisfied]; otherwise, <c>false</c>.
		/// </returns>
		public override bool IsConditionSatisfied()
		{
			bool blnIsConditionSatisfied = false;
			foreach (ConditionBase condition in conditions)
			{
				blnIsConditionSatisfied = blnIsConditionSatisfied ||
					condition.IsConditionSatisfied();
			}
			return blnIsConditionSatisfied;
		}

		#endregion

		#region prop

		/// <summary>
		/// Gets or sets the trigger.
		/// </summary>
		/// <value>The trigger.</value>
		public override Trigger Trigger
		{
			get
			{
				return trigger;
			}
			set
			{
				trigger = value;
				foreach (ConditionBase condition in conditions)
				{
					condition.Trigger = value;
				}
			}
		}

		#endregion

		#region dtor

		/// <summary>
		/// Releases unmanaged resources and performs other cleanup operations before the
		/// <see cref="Trigger&lt;K, T&gt;"/> is reclaimed by garbage collection.
		/// </summary>
		~Or()
		{
			Dispose(false);
		}

		#endregion

		#region overrides

		/// <summary>
		/// Releases unmanaged and - optionally - managed resources
		/// </summary>
		/// <param name="blnDisposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
		protected override void Dispose(bool blnDispose)
		{
			if (!isDisposed && blnDispose)
			{
				if (null != conditions)
				{
					foreach (ConditionBase condition in conditions)
					{
						condition.Dispose();
					}
				}

				isDisposed = true;

				GC.SuppressFinalize(this);
			}
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions