예제 #1
0
        private AnimalNode CreateNode(AnimalNode animalNode, List <AnimalAttribute> currAttributes, List <Animal> data)
        {
            if (data == null)
            {
                return(null);
            }

            if (animalNode == null)
            {
                animalNode = new AnimalNode();
            }

            var question = currAttributes.FirstOrDefault();

            if (question == null)
            {
                // no more question/attribute to check so this is the end of the branch node
                animalNode.MatchedAnimals = data;
                return(animalNode);
            }

            animalNode.Attribute = question;

            List <Animal> matched = null, unmatched = null;

            foreach (var item in data)
            {
                if (item.Attributes.Any(i => i.Text.Equals(question.Text, StringComparison.OrdinalIgnoreCase)))
                {
                    if (matched == null)
                    {
                        matched = new List <Animal>();
                    }
                    if (!matched.Contains(item))
                    {
                        matched.Add(item);
                    }
                }
                else
                {
                    if (unmatched == null)
                    {
                        unmatched = new List <Animal>();
                    }
                    if (!unmatched.Contains(item))
                    {
                        unmatched.Add(item);
                    }
                }
            }
            if (matched != null)
            {
                RemoveAttribute(matched, question);
                animalNode.MatchedAnimals = matched;
            }

            var yesQuestions = (from i in animalNode.MatchedAnimals from a in i.Attributes
                                where !a.Text.Equals(question.Text, StringComparison.OrdinalIgnoreCase)
                                select a).Distinct().ToList();

            if (unmatched != null)
            {
                RemoveAttribute(unmatched, question);
            }

            var noQuestions = unmatched == null ? null : (from i in unmatched from a in i.Attributes
                                                          where !a.Text.Equals(question.Text, StringComparison.OrdinalIgnoreCase)
                                                          select a).Distinct().ToList();

            animalNode.MatchedNode   = CreateNode(animalNode.MatchedNode, yesQuestions, matched);
            animalNode.UnmatchedNode = CreateNode(animalNode.UnmatchedNode, noQuestions, unmatched);

            return(animalNode);
        }
예제 #2
0
        public void BuildTree(Animals data)
        {
            var currAttributes = GetAnimalAttributes(data);

            RootNode = CreateNode(RootNode, currAttributes, data);
        }