/// <summary>
        /// Compares the conditions and actions of both rules.
        /// </summary>
        private static bool AreEqual(DTRule a, DTRule b)
        {
            if (a.ConditionRuleElements.Count != b.ConditionRuleElements.Count ||
                a.ActionRuleElements.Count != b.ActionRuleElements.Count)
                return false;

            for (int i = 0; i < a.ConditionRuleElements.Count; i++)
            {
                if (a.ConditionRuleElements[i].ElementId != b.ConditionRuleElements[i].ElementId ||
                    a.ConditionRuleElements[i].State != b.ConditionRuleElements[i].State)
                    return false;
            }

            for (int i = 0; i < a.ActionRuleElements.Count; i++)
            {
                if (a.ActionRuleElements[i].ElementId != b.ActionRuleElements[i].ElementId ||
                    a.ActionRuleElements[i].State != b.ActionRuleElements[i].State)
                    return false;
            }

            return true;
        }
 public SelectableRuleContainer(DTRule rule)
 {
     Rule = rule;
 }
        public void CompareElementsTest()
        {
            List<DTRuleElement> testConditions = new List<DTRuleElement>
                (new DTRuleElement[] {
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.Yes },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.No },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.No },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.Yes },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.No },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.Yes }});
            List<DTRuleElement> testActions = new List<DTRuleElement>
                (new DTRuleElement[] {
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.No },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.No },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.Yes },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.Yes }});

            List<DTRuleElement> changedConditions = new List<DTRuleElement>
                (new DTRuleElement[] {
                    new DTRuleElement { ElementId = testConditions[0].ElementId, State = testConditions[0].State },
                    new DTRuleElement { ElementId = testConditions[1].ElementId, State = DTState.Yes }, //Changed
                    new DTRuleElement { ElementId = testConditions[2].ElementId, State = DTState.Yes }, //Changed
                    new DTRuleElement { ElementId = testConditions[3].ElementId, State = testConditions[3].State },
                    new DTRuleElement { ElementId = testConditions[4].ElementId, State = DTState.Yes }, //Changed
                    new DTRuleElement { ElementId = testConditions[5].ElementId, State = DTState.No }}); //Changed

            List<DTRuleElement> changedActions = new List<DTRuleElement>
                (new DTRuleElement[] {
                    new DTRuleElement { ElementId = testActions[0].ElementId, State = testActions[0].State },
                    new DTRuleElement { ElementId = testActions[1].ElementId, State = DTState.Yes }, //Changed
                    new DTRuleElement { ElementId = testActions[2].ElementId, State = DTState.No }, //Changed
                    new DTRuleElement { ElementId = testActions[3].ElementId, State = testActions[3].State }});

            DTRule_Accessor target = new DTRule_Accessor(1, changedConditions, changedActions);
            DTRule rule = new DTRule(0, testConditions, testActions);

            List<DTRuleElement> allElements = new List<DTRuleElement>(rule.ConditionRuleElements);
            allElements.AddRange(rule.ActionRuleElements);
            IEnumerable<DTRuleElement> elements = allElements;

            RuleComparison ruleComparison = RuleComparison.DifferentStates;
            List<string> expected = new List<string>
                (new string[] {
                    testConditions[1].ElementId,
                    testConditions[2].ElementId,
                    testConditions[4].ElementId,
                    testConditions[5].ElementId,
                    testActions[1].ElementId,
                    testActions[2].ElementId});

            IEnumerable<string> actual = target.CompareElements(rule, elements, ruleComparison);
            CollectionAssert.AreEqual(expected, actual.ToList());

            ruleComparison = RuleComparison.MatchingStates;
            actual = target.CompareElements(rule, elements, ruleComparison);
            expected = expected = new List<string>
                (new string[] {
                    testConditions[0].ElementId,
                    testConditions[3].ElementId,
                    testActions[0].ElementId,
                    testActions[3].ElementId});
            CollectionAssert.AreEqual(expected, actual.ToList());
        }
        public void MergeTest()
        {
            List<DTRuleElement> testConditions = new List<DTRuleElement>
                (new DTRuleElement[] {
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.Yes },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.No },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.No },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.Yes },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.No },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.Yes }});
            List<DTRuleElement> testActions = new List<DTRuleElement>
                (new DTRuleElement[] {
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.No },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.No },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.Yes },
                    new DTRuleElement { ElementId = Guid.NewGuid().ToString(), State = DTState.Yes }});

            List<DTRuleElement> changedConditions = new List<DTRuleElement>
                (new DTRuleElement[] {
                    new DTRuleElement { ElementId = testConditions[0].ElementId, State = testConditions[0].State },
                    new DTRuleElement { ElementId = testConditions[1].ElementId, State = DTState.Yes }, //Changed
                    new DTRuleElement { ElementId = testConditions[2].ElementId, State = DTState.Yes }, //Changed
                    new DTRuleElement { ElementId = testConditions[3].ElementId, State = testConditions[3].State },
                    new DTRuleElement { ElementId = testConditions[4].ElementId, State = DTState.Yes }, //Changed
                    new DTRuleElement { ElementId = testConditions[5].ElementId, State = DTState.No }}); //Changed

            List<DTRuleElement> changedActions = new List<DTRuleElement>
                (new DTRuleElement[] {
                    new DTRuleElement { ElementId = testActions[0].ElementId, State = testActions[0].State },
                    new DTRuleElement { ElementId = testActions[1].ElementId, State = DTState.Yes }, //Changed
                    new DTRuleElement { ElementId = testActions[2].ElementId, State = DTState.No }, //Changed
                    new DTRuleElement { ElementId = testActions[3].ElementId, State = testActions[3].State }});

            int ruleIndex = 0;
            DTRule target = new DTRule(ruleIndex, testConditions, testActions);
            DTRule mergeRule = new DTRule(1, changedConditions, changedActions);
            target.Merge(mergeRule);

            //Conditions should be merged
            Assert.AreEqual(target.GetState(testConditions[0].ElementId), testConditions[0].State);
            Assert.AreEqual(target.GetState(testConditions[1].ElementId), DTState.Empty);
            Assert.AreEqual(target.GetState(testConditions[2].ElementId), DTState.Empty);
            Assert.AreEqual(target.GetState(testConditions[3].ElementId), testConditions[3].State);
            Assert.AreEqual(target.GetState(testConditions[4].ElementId), DTState.Empty);
            Assert.AreEqual(target.GetState(testConditions[5].ElementId), DTState.Empty);

            //Actions should not be merged
            Assert.AreEqual(target.GetState(changedActions[0].ElementId), testActions[0].State);
            Assert.AreEqual(target.GetState(changedActions[1].ElementId), testActions[1].State);
            Assert.AreEqual(target.GetState(changedActions[2].ElementId), testActions[2].State);
            Assert.AreEqual(target.GetState(changedActions[3].ElementId), testActions[3].State);
        }
        public void GetStateTest()
        {
            int ruleIndex = 0;
            IEnumerable<DTRuleElement> conditions = ConditionRuleElements;
            IEnumerable<DTRuleElement> actions = ActionRuleElements;
            DTRule target = new DTRule(ruleIndex, conditions, actions);

            foreach (var condition in ConditionRuleElements)
                Assert.AreEqual(target.GetState(condition.ElementId), condition.State);
            foreach (var action in ActionRuleElements)
                Assert.AreEqual(target.GetState(action.ElementId), action.State);
        }
        public void DTRuleConstructorTest()
        {
            int ruleIndex = 5;
            IEnumerable<DTRuleElement> conditions = ConditionRuleElements;
            IEnumerable<DTRuleElement> actions = ActionRuleElements;
            DTRule rule = new DTRule(ruleIndex, conditions, actions);

            Assert.AreEqual(rule.Index, ruleIndex);
            Assert.AreEqual(conditions.Count(), rule.ConditionRuleElements.Count);
            Assert.AreEqual(actions.Count(), rule.ActionRuleElements.Count);
        }