// // ConditionGroup.cs - a group of IConditionalStatements joined by a ConditionCombination // // Author: // Michael Becker // // Copyright (c) 2011-2020 Mike Becker's Software // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . namespace MBS.Framework.Logic.Conditional { /// /// A group of s joined by a . /// public class ConditionGroup : IConditionalStatement { /// /// Creates a new with no conditional statements specified and a /// default of . /// public ConditionGroup() { // I know it's initialized to this but I'm doing it here for clarity's sake (and because // it's documented here... if you change it, make sure to update the documentation! don't // rely on the field initializer) mvarCombination = ConditionCombination.And; } /// /// Creates a new with the specified /// and s. /// /// The used to join s when testing this . /// The s and s that are part of this . public ConditionGroup(ConditionCombination combination, params IConditionalStatement[] statements) { mvarCombination = combination; for (int i = 0; i < statements.Length; i++) { mvarConditions.Add(statements[i]); } } private ConditionalStatementCollection mvarConditions = new ConditionalStatementCollection(); /// /// Gets all s in this . /// public ConditionalStatementCollection Conditions { get { return mvarConditions; } } private ConditionCombination mvarCombination = ConditionCombination.And; /// /// The type of combination used to join the s in this /// . /// public ConditionCombination Combination { get { return mvarCombination; } set { mvarCombination = value; } } /// /// Evaluates the conditional statement based on the given criteria. /// /// The set of values against which to evaluate the conditional statement. /// True if the conditions are satisfied; false otherwise. public bool Test(params System.Collections.Generic.KeyValuePair[] propertyValues) { bool retval = false; if (mvarCombination == ConditionCombination.And) { retval = true; } for (int i = 0; i < mvarConditions.Count; i++) { switch (mvarCombination) { case ConditionCombination.And: { retval &= mvarConditions[i].Test(propertyValues); break; } case ConditionCombination.Or: { retval |= mvarConditions[i].Test(propertyValues); break; } case ConditionCombination.Xor: { retval ^= mvarConditions[i].Test(propertyValues); break; } } } return retval; } /// /// Evaluates the conditional statement based on the given criteria. /// /// The set of values against which to evaluate the conditional statement. /// True if the conditions are satisfied; false otherwise. public bool Test(System.Collections.Generic.Dictionary propertyValues) { bool retval = false; if (mvarCombination == ConditionCombination.And) { retval = true; } for (int i = 0; i < mvarConditions.Count; i++) { switch (mvarCombination) { case ConditionCombination.And: { retval &= mvarConditions[i].Test(propertyValues); break; } case ConditionCombination.Or: { retval |= mvarConditions[i].Test(propertyValues); break; } case ConditionCombination.Xor: { retval ^= mvarConditions[i].Test(propertyValues); break; } } } return retval; } /// /// Evaluates the conditional statement based on the given criterion. /// /// The value against which to evaluate the conditional statement. /// True if the conditions are satisfied; false otherwise. public bool Test(object value) { bool retval = true; for (int i = 0; i < mvarConditions.Count; i++) { switch (mvarCombination) { case ConditionCombination.And: { retval &= mvarConditions[i].Test(value); break; } case ConditionCombination.Or: { retval |= mvarConditions[i].Test(value); break; } case ConditionCombination.Xor: { retval ^= mvarConditions[i].Test(value); break; } } } return retval; } } }