Example #1
0
        public void TryDropPossibility_WhenRejectedByParent_LeavesUnchanged()
        {
            var fakePossibilities = new FakePossibility[] {
                new FakePossibility(),
                new FakePossibility(),
            };
            var childOptional = OptionalObjective.CreateWithPossibilities(
                Possibilities.CreatePossibilities(new(), 2), 1);
            var possibilities = fakePossibilities.Cast <IPossibility>().Append(childOptional).ToArray();
            var optional      = OptionalObjective.CreateWithPossibilities(possibilities, 3);
            var parent        = new FakeObjective(isRequired: true);

            parent.CanDropPossibilities = false;
            IPossibility possibility = optional;
            IObjective   objective   = optional;

            Link.CreateConnectedLink(possibility, parent);
            var childToDrop = fakePossibilities[0];

            Assert.False(objective.TryDropPossibility(childToDrop.AttachedObjectives.First()));

            Assert.Equal(NodeState.UNKNOWN, optional.State);
            Assert.Empty(parent.DroppedPossibilities);
            Assert.Empty(fakePossibilities[0].DroppedFromObjectives);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);
            Assert.Equal(NodeState.UNKNOWN, childOptional.State);
        }
Example #2
0
        public void TryDropAndReturnPossibility_WhenOkWithParent_Succeeds()
        {
            var fakePossibilities = new FakePossibility[] {
                new FakePossibility(),
                new FakePossibility(),
            };
            var          optional     = OptionalObjective.CreateWithPossibilities(fakePossibilities, 1);
            var          parent       = new FakeObjective(isRequired: true);
            IPossibility possibility  = optional;
            IObjective   objective    = optional;
            var          linkToParent = Link.CreateConnectedLink(possibility, parent);
            var          childToDrop  = fakePossibilities[0];

            Assert.True(objective.TryDropPossibility(childToDrop.AttachedObjectives.First()));
            Assert.Equal(NodeState.UNKNOWN, optional.State);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);
            Assert.Empty(parent.DroppedPossibilities);

            Assert.True(objective.TryDropPossibility(fakePossibilities[1].AttachedObjectives.First()));
            Assert.Equal(NodeState.DROPPED, optional.State);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);
            Assert.Single(parent.DroppedPossibilities, linkToParent);

            objective.ReturnPossibility(fakePossibilities[1].AttachedObjectives.First());
            Assert.Equal(NodeState.UNKNOWN, optional.State);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);
            Assert.Empty(parent.DroppedPossibilities);

            objective.ReturnPossibility(childToDrop.AttachedObjectives.First());
            Assert.Equal(NodeState.UNKNOWN, optional.State);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);
            Assert.Empty(parent.DroppedPossibilities);
        }
Example #3
0
        public void TryDropAndReturnFromObjective_CascadesToOtherParents_DropsThenReturns()
        {
            var fakePossibilities = new FakePossibility[] {
                new FakePossibility(),
                new FakePossibility(),
            };
            var childOptional = OptionalObjective.CreateWithPossibilities(
                Possibilities.CreatePossibilities(new(), 2), 1);
            var optional = OptionalObjective.CreateWithPossibilities(
                fakePossibilities.Cast <IPossibility>().Prepend(childOptional).ToArray(), 1);
            var parentToDropFrom = OptionalObjective.CreateWithPossibilities(
                new IPossibility[] {
                new FakePossibility(),
            }, 1);
            var linkToParent        = Link.CreateConnectedLink(optional, parentToDropFrom);
            var parentToCascadeDrop = OptionalObjective.CreateWithPossibilities(
                new IPossibility[] {
                new FakePossibility(),
                optional,
            }, 2);
            var required = new FakeObjective(isRequired: true);

            Link.CreateConnectedLink(parentToDropFrom, required);
            Link.CreateConnectedLink(parentToCascadeDrop, required);
            IPossibility possibility = optional;

            Assert.True(possibility.TryDropFromObjective(linkToParent));
            Assert.Equal(NodeState.DROPPED, optional.State);
            Assert.Equal(NodeState.DROPPED, parentToCascadeDrop.State);

            possibility.ReturnFromObjective(linkToParent);
            Assert.Equal(NodeState.UNKNOWN, optional.State);
            Assert.Equal(NodeState.UNKNOWN, parentToCascadeDrop.State);
        }
Example #4
0
        public void TrySelectPossibility_WhenRejectedByParent_LeavesUnchanged()
        {
            var fakePossibilities = new FakePossibility[] {
                new FakePossibility(),
                new FakePossibility(),
            };
            var childOptional = OptionalObjective.CreateWithPossibilities(
                Possibilities.CreatePossibilities(new(), 2), 1);
            var optional            = OptionalObjective.CreateWithPossibilities(fakePossibilities, 1);
            var linkToChildOptional = Link.CreateConnectedLink(childOptional, optional);
            var parent = new FakeObjective(isRequired: true);

            parent.CanSelectPossibilities = false;
            var          linkToParent = Link.CreateConnectedLink(optional, parent);
            IPossibility possibility  = optional;
            IObjective   objective    = optional;

            Assert.False(objective.TrySelectPossibility(fakePossibilities[0].AttachedObjectives.First()));
            Assert.Equal(NodeState.UNKNOWN, optional.State);
            Assert.Equal(NodeState.UNKNOWN, childOptional.State);
            Assert.All(((IObjective)childOptional).GetUnknownDirectPossibilities().Cast <Possibility>(),
                       p => Assert.Equal(NodeState.UNKNOWN, p.State));
            Assert.Empty(fakePossibilities[0].DroppedFromObjectives);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);
            Assert.Empty(parent.SelectedPossibilities);
        }
Example #5
0
        private List <Link> CreateThreeLinksWithSameObjective()
        {
            var objective = new FakeObjective();

            return(new List <Link> {
                Link.CreateConnectedLink(new FakePossibility(), objective),
                Link.CreateConnectedLink(new FakePossibility(), objective),
                Link.CreateConnectedLink(new FakePossibility(), objective),
            });
        }
Example #6
0
        public void TryDrop_WithObjective_Succeeds()
        {
            var possibility = new Possibility(new(), 1);
            var objective   = new FakeObjective();
            var link        = Link.CreateConnectedLink(possibility, objective);

            Assert.True(possibility.TryDrop());

            Assert.Single(objective.DroppedPossibilities, link);
            Assert.Equal(NodeState.DROPPED, possibility.State);
        }
Example #7
0
        public void TrySelectAndDeselectPossibility_Succeeds()
        {
            var fakePossibilities = new FakePossibility[] {
                new FakePossibility(),
                new FakePossibility(),
            };
            var childOptional = OptionalObjective.CreateWithPossibilities(
                Possibilities.CreatePossibilities(new(), 2), 1);
            var          optional            = OptionalObjective.CreateWithPossibilities(fakePossibilities, 2);
            var          linkToChildOptional = Link.CreateConnectedLink(childOptional, optional);
            var          parent       = new FakeObjective(isRequired: true);
            var          linkToParent = Link.CreateConnectedLink(optional, parent);
            IPossibility possibility  = optional;
            IObjective   objective    = optional;

            Assert.True(objective.TrySelectPossibility(fakePossibilities[0].AttachedObjectives.First()));
            Assert.Equal(NodeState.UNKNOWN, optional.State);
            Assert.Equal(NodeState.UNKNOWN, childOptional.State);
            Assert.All(((IObjective)childOptional).GetUnknownDirectPossibilities().Cast <Possibility>(),
                       p => Assert.Equal(NodeState.UNKNOWN, p.State));
            Assert.Empty(fakePossibilities[0].DroppedFromObjectives);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);
            Assert.Empty(parent.SelectedPossibilities);

            Assert.True(objective.TrySelectPossibility(fakePossibilities[1].AttachedObjectives.First()));
            Assert.Equal(NodeState.SELECTED, optional.State);
            Assert.Equal(NodeState.UNKNOWN, childOptional.State);
            Assert.All(((IObjective)childOptional).GetUnknownDirectPossibilities().Cast <Possibility>(),
                       p => Assert.Equal(NodeState.UNKNOWN, p.State));
            Assert.Single(parent.SelectedPossibilities, linkToParent);
            Assert.Empty(fakePossibilities[0].DroppedFromObjectives);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);

            objective.DeselectPossibility(fakePossibilities[1].AttachedObjectives.First());
            Assert.Equal(NodeState.UNKNOWN, optional.State);
            Assert.Equal(NodeState.UNKNOWN, childOptional.State);
            Assert.All(((IObjective)childOptional).GetUnknownDirectPossibilities().Cast <Possibility>(),
                       p => Assert.Equal(NodeState.UNKNOWN, p.State));
            Assert.Empty(fakePossibilities[0].DroppedFromObjectives);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);
            Assert.Empty(parent.SelectedPossibilities);

            objective.DeselectPossibility(fakePossibilities[0].AttachedObjectives.First());
            Assert.Equal(NodeState.UNKNOWN, optional.State);
            Assert.Equal(NodeState.UNKNOWN, childOptional.State);
            Assert.All(((IObjective)childOptional).GetUnknownDirectPossibilities().Cast <Possibility>(),
                       p => Assert.Equal(NodeState.UNKNOWN, p.State));
            Assert.Empty(fakePossibilities[0].DroppedFromObjectives);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);
            Assert.Empty(parent.SelectedPossibilities);
        }
Example #8
0
        public void PopFromObjective_OnlyUpdatesLinkReferences()
        {
            var poppedPossibility = new FakePossibility();
            var secondPossibility = new FakePossibility();
            var objective         = new FakeObjective();
            var poppedLink        = Link.CreateConnectedLink(poppedPossibility, objective);
            var secondLink        = Link.CreateConnectedLink(secondPossibility, objective);

            poppedLink.PopFromObjective();

            Assert.Same(secondLink, poppedLink.NextOnObjective);
            Assert.Same(secondLink, poppedLink.PreviousOnObjective);
            Assert.Same(secondLink, secondLink.NextOnObjective);
            Assert.Same(secondLink, secondLink.PreviousOnObjective);
            Assert.Same(poppedLink, objective.AttachedPossibilities.First());
        }
Example #9
0
        public void TrySelect_IfRejectedByObjective_LeavesUnchanged()
        {
            var possibility       = new Possibility(new(), 1);
            var objective         = new FakeObjective();
            var objectiveToReject = new FakeObjective();

            objectiveToReject.CanSelectPossibilities = false;
            Link.CreateConnectedLink(possibility, objective);
            Link.CreateConnectedLink(possibility, objectiveToReject);

            Assert.False(possibility.TrySelect());

            Assert.Equal(NodeState.UNKNOWN, possibility.State);
            Assert.Empty(objective.SelectedPossibilities);
            Assert.Empty(objectiveToReject.SelectedPossibilities);
        }
Example #10
0
        public void TrySelectAndDeselect_IfAcceptedByObjective_Succeeds()
        {
            var possibility = new Possibility(new(), 1);
            var objective   = new FakeObjective();
            var link        = Link.CreateConnectedLink(possibility, objective);

            Assert.True(possibility.TrySelect());

            Assert.Equal(NodeState.SELECTED, possibility.State);
            Assert.Single(objective.SelectedPossibilities, link);

            possibility.Deselect();

            Assert.Equal(NodeState.UNKNOWN, possibility.State);
            Assert.Empty(objective.SelectedPossibilities);
        }
Example #11
0
        public void ReinsertToObjective_UndoesPop()
        {
            var poppedPossibility = new FakePossibility();
            var secondPossibility = new FakePossibility();
            var objective         = new FakeObjective();
            var poppedLink        = Link.CreateConnectedLink(poppedPossibility, objective);
            var secondLink        = Link.CreateConnectedLink(secondPossibility, objective);

            poppedLink.PopFromObjective();
            poppedLink.ReinsertToObjective();

            Assert.Same(secondLink, poppedLink.NextOnObjective);
            Assert.Same(secondLink, poppedLink.PreviousOnObjective);
            Assert.Same(poppedLink, secondLink.NextOnObjective);
            Assert.Same(poppedLink, secondLink.PreviousOnObjective);
            Assert.Same(poppedLink, objective.AttachedPossibilities.First());
        }
Example #12
0
        public void CreateConnectedLink_ConnectsCorrectly()
        {
            var possibility = new FakePossibility();
            var objective   = new FakeObjective();

            var link = Link.CreateConnectedLink(possibility, objective);

            Assert.Single(possibility.AttachedObjectives, link);
            Assert.Single(objective.AttachedPossibilities, link);
            Assert.Same(link, link.NextOnObjective);
            Assert.Same(link, link.PreviousOnObjective);
            Assert.Same(link, link.NextOnPossibility);
            Assert.Same(link, link.PreviousOnPossibility);
            Assert.Same(possibility, link.Possibility);
            Assert.Same(objective, link.Objective);
            Assert.Single(link.GetLinksOnObjective(), link);
            Assert.Single(link.GetLinksOnPossibility(), link);
        }
Example #13
0
        public void TryDetachAndReattachObjective_IfRequiredObjective_DropsThenReturns()
        {
            var          concretePossibility = new Possibility(new(), 1);
            IPossibility possibility         = concretePossibility;
            var          objective           = new FakeObjective();
            var          objectiveToDetach   = new FakeObjective(isRequired: true);
            var          linkToDrop          = Link.CreateConnectedLink(possibility, objective);
            var          linkToDetach        = Link.CreateConnectedLink(possibility, objectiveToDetach);

            Assert.True(possibility.TryDropFromObjective(linkToDetach));
            Assert.Equal(NodeState.DROPPED, concretePossibility.State);
            Assert.Single(objective.DroppedPossibilities, linkToDrop);
            Assert.Empty(objectiveToDetach.DroppedPossibilities);

            possibility.ReturnFromObjective(linkToDetach);
            Assert.Equal(NodeState.UNKNOWN, concretePossibility.State);
            Assert.Empty(objective.DroppedPossibilities);
            Assert.Empty(objectiveToDetach.DroppedPossibilities);
        }
Example #14
0
        public void TryDetach_CausingDrop_IfDropDisallowed_LeavesUnchanged()
        {
            var          concretePossibility = new Possibility(new(), 1);
            IPossibility possibility         = concretePossibility;
            var          objective           = new FakeObjective(isRequired: true);

            objective.CanDropPossibilities = false;
            var  objectiveToDetach = new FakeObjective(isRequired: true);
            Link linkToOther       = Link.CreateConnectedLink(possibility, objective);
            Link linkToDetach      = Link.CreateConnectedLink(possibility, objectiveToDetach);

            Assert.False(possibility.TryDropFromObjective(linkToDetach));
            Assert.Equal(NodeState.UNKNOWN, concretePossibility.State);
            Assert.Empty(objective.DroppedPossibilities);
            Assert.Empty(objectiveToDetach.DroppedPossibilities);

            // Verify the attempted-detached objective was still attached by dropping the
            // possibility from it.
            Assert.True(possibility.TryDropFromObjective(linkToOther));
            Assert.Equal(NodeState.DROPPED, concretePossibility.State);
            Assert.Single(objectiveToDetach.DroppedPossibilities, linkToDetach);
        }
Example #15
0
        public void CreateConnectedLink_WithExistingLinksOnPossibility_ConnectsCorrectly()
        {
            var possibility = new FakePossibility();
            var objective   = new FakeObjective();

            var firstLink = Link.CreateConnectedLink(possibility, new FakeObjective());
            var link      = Link.CreateConnectedLink(possibility, objective);

            Assert.Same(link, link.PreviousOnObjective);
            Assert.Same(link, link.NextOnObjective);
            Assert.Same(firstLink, link.NextOnPossibility);
            Assert.Same(firstLink, link.PreviousOnPossibility);
            Assert.Same(firstLink, firstLink.PreviousOnObjective);
            Assert.Same(firstLink, firstLink.NextOnObjective);
            Assert.Same(link, firstLink.NextOnPossibility);
            Assert.Same(link, firstLink.PreviousOnPossibility);
            Assert.Same(possibility, link.Possibility);
            Assert.Same(objective, link.Objective);
            Assert.Equal(2, link.GetLinksOnPossibility().Count());
            Assert.Contains(link, link.GetLinksOnPossibility());
            Assert.Contains(firstLink, link.GetLinksOnPossibility());
            Assert.Single(link.GetLinksOnObjective(), link);
            Assert.Single(firstLink.GetLinksOnObjective(), firstLink);
        }
Example #16
0
        public void TryDropFromObjective_LeavesPossibilitiesUnchanged()
        {
            var fakePossibilities = new FakePossibility[] {
                new FakePossibility(),
                new FakePossibility(),
            };
            var childOptional = OptionalObjective.CreateWithPossibilities(
                Possibilities.CreatePossibilities(new(), 2), 1);
            var optional = OptionalObjective.CreateWithPossibilities(
                fakePossibilities.Cast <IPossibility>().Prepend(childOptional).ToArray(), 1);
            var          parent       = new FakeObjective(isRequired: true);
            var          linkToParent = Link.CreateConnectedLink(optional, parent);
            IPossibility possibility  = optional;
            IObjective   objective    = optional;

            Assert.True(possibility.TryDropFromObjective(linkToParent));
            Assert.Equal(NodeState.DROPPED, optional.State);
            Assert.Equal(NodeState.UNKNOWN, childOptional.State);
            Assert.All(((IObjective)childOptional).GetUnknownDirectPossibilities().Cast <Possibility>(),
                       p => Assert.Equal(NodeState.UNKNOWN, p.State));
            Assert.Empty(parent.SelectedPossibilities);
            Assert.Empty(fakePossibilities[0].DroppedFromObjectives);
            Assert.Empty(fakePossibilities[1].DroppedFromObjectives);
        }