Ejemplo n.º 1
0
        /// <summary>
        /// Create a copy of the current instance
        /// </summary>
        /// <returns></returns>
        public RuminantFilter Clone()
        {
            RuminantFilter clone = new RuminantFilter()
            {
                Parameter = this.Parameter,
                Operator  = this.Operator,
                Value     = this.Value
            };

            return(clone);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Filter extensions for herd list
        /// </summary>
        public static List <Ruminant> Filter(this IEnumerable <Ruminant> individuals, IModel filterGroup)
        {
            double proportionToUse = (filterGroup as IFilterGroup).Proportion;

            if (proportionToUse <= 0)
            {
                proportionToUse = 1;
            }

            bool femaleProperties = false;
            bool maleProperties   = false;
            var  rules            = new List <Rule>();

            foreach (RuminantFilter filter in filterGroup.FindAllChildren <RuminantFilter>())
            {
                ExpressionType op = (ExpressionType)Enum.Parse(typeof(ExpressionType), filter.Operator.ToString());
                // create rule list
                string gender = "";
                switch (filter.Parameter)
                {
                case RuminantFilterParameters.IsDraught:
                case RuminantFilterParameters.IsSire:
                case RuminantFilterParameters.IsCastrate:
                    maleProperties = true;
                    gender         = "Male";
                    break;

                case RuminantFilterParameters.IsBreeder:
                case RuminantFilterParameters.IsPregnant:
                case RuminantFilterParameters.IsLactating:
                case RuminantFilterParameters.IsPreBreeder:
                case RuminantFilterParameters.MonthsSinceLastBirth:
                    femaleProperties = true;
                    gender           = "Female";
                    break;

                default:
                    break;
                }
                if (gender != "")
                {
                    RuminantFilter fltr = new RuminantFilter()
                    {
                        Parameter = RuminantFilterParameters.Gender,
                        Operator  = FilterOperators.Equal,
                        Value     = gender
                    };
                    rules.Add(new Rule(fltr.Parameter.ToString(), (ExpressionType)Enum.Parse(typeof(ExpressionType), fltr.Operator.ToString()), fltr.Value));
                }
                rules.Add(new Rule(filter.Parameter.ToString(), op, filter.Value));
            }

            if (femaleProperties && maleProperties)
            {
                return(new List <Ruminant>());
            }
            else
            {
                if (femaleProperties)
                {
                    if ((filterGroup as IFilterGroup).CombinedRules is null)
                    {
                        (filterGroup as IFilterGroup).CombinedRules = CompileRule(new List <RuminantFemale>(), rules);
                    }
                    var result = GetItemsThatMatchAll <RuminantFemale>(individuals.Where(a => a.Gender == Sex.Female).Cast <RuminantFemale>(), (filterGroup as IFilterGroup).CombinedRules as List <Func <RuminantFemale, bool> >);
                    if (proportionToUse < 1)
                    {
                        int numberToUse = Convert.ToInt32(Math.Ceiling(result.Count() / proportionToUse));
                        return(result.OrderBy(x => RandomNumberGenerator.Generator.Next()).Take(numberToUse).ToList <Ruminant>());
                    }
                    else
                    {
                        return(result.ToList <Ruminant>());
                    }
                }
                else if (maleProperties)
                {
                    if ((filterGroup as IFilterGroup).CombinedRules is null)
                    {
                        (filterGroup as IFilterGroup).CombinedRules = CompileRule(new List <RuminantMale>(), rules);
                    }
                    var result = GetItemsThatMatchAll <RuminantMale>(individuals.Where(a => a.Gender == Sex.Male).Cast <RuminantMale>(), (filterGroup as IFilterGroup).CombinedRules as List <Func <RuminantMale, bool> >);
                    if (proportionToUse < 1)
                    {
                        int numberToUse = Convert.ToInt32(Math.Ceiling(result.Count() / proportionToUse));
                        return(result.OrderBy(x => RandomNumberGenerator.Generator.Next()).Take(numberToUse).ToList <Ruminant>());
                    }
                    else
                    {
                        return(result.ToList <Ruminant>());
                    }
                }
                else
                {
                    if ((filterGroup as IFilterGroup).CombinedRules is null)
                    {
                        (filterGroup as IFilterGroup).CombinedRules = CompileRule(new List <Ruminant>(), rules);
                    }
                    var result = GetItemsThatMatchAll <Ruminant>(individuals, (filterGroup as IFilterGroup).CombinedRules as List <Func <Ruminant, bool> >);
                    if (proportionToUse < 1)
                    {
                        int numberToUse = Convert.ToInt32(Math.Ceiling(result.Count() / proportionToUse));
                        return(result.OrderBy(x => RandomNumberGenerator.Generator.Next()).Take(numberToUse).ToList <Ruminant>());
                    }
                    else
                    {
                        return(result.ToList <Ruminant>());
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Filter extensions for herd list
        /// </summary>
        public static List <Ruminant> Filter(this IEnumerable <Ruminant> individuals, Model filterGroup)
        {
            bool femaleProperties = false;
            bool maleProperties   = false;
            var  rules            = new List <Rule>();

            foreach (RuminantFilter filter in Apsim.Children(filterGroup, typeof(RuminantFilter)))
            {
                ExpressionType op = (ExpressionType)Enum.Parse(typeof(ExpressionType), filter.Operator.ToString());
                // create rule list
                string gender = "";
                switch (filter.Parameter)
                {
                case RuminantFilterParameters.Draught:
                case RuminantFilterParameters.BreedingSire:
                    maleProperties = true;
                    gender         = "Male";
                    break;

                case RuminantFilterParameters.IsBreeder:
                case RuminantFilterParameters.IsPregnant:
                case RuminantFilterParameters.IsLactating:
                case RuminantFilterParameters.IsHeifer:
                case RuminantFilterParameters.MonthsSinceLastBirth:
                    femaleProperties = true;
                    gender           = "Female";
                    break;

                default:
                    break;
                }
                if (gender != "")
                {
                    RuminantFilter fltr = new RuminantFilter()
                    {
                        Parameter = RuminantFilterParameters.Gender,
                        Operator  = FilterOperators.Equal,
                        Value     = gender
                    };
                    rules.Add(new Rule(fltr.Parameter.ToString(), (ExpressionType)Enum.Parse(typeof(ExpressionType), fltr.Operator.ToString()), fltr.Value));
                }
                rules.Add(new Rule(filter.Parameter.ToString(), op, filter.Value));
            }

            if (femaleProperties && maleProperties)
            {
                return(new List <Ruminant>());
            }
            else
            {
                if (femaleProperties)
                {
                    if ((filterGroup as IFilterGroup).CombinedRules is null)
                    {
                        (filterGroup as IFilterGroup).CombinedRules = CompileRule(new List <RuminantFemale>(), rules);
                    }
                    return(GetItemsThatMatchAll <RuminantFemale>(individuals.Where(a => a.Gender == Sex.Female).Cast <RuminantFemale>(), (filterGroup as IFilterGroup).CombinedRules as List <Func <RuminantFemale, bool> >).ToList <Ruminant>());
                }
                else if (maleProperties)
                {
                    if ((filterGroup as IFilterGroup).CombinedRules is null)
                    {
                        (filterGroup as IFilterGroup).CombinedRules = CompileRule(new List <RuminantMale>(), rules);
                    }
                    return(GetItemsThatMatchAll <RuminantMale>(individuals.Where(a => a.Gender == Sex.Male).Cast <RuminantMale>(), (filterGroup as IFilterGroup).CombinedRules as List <Func <RuminantMale, bool> >).ToList <Ruminant>());
                }
                else
                {
                    if ((filterGroup as IFilterGroup).CombinedRules is null)
                    {
                        (filterGroup as IFilterGroup).CombinedRules = CompileRule(new List <Ruminant>(), rules);
                    }
                    return(GetItemsThatMatchAll <Ruminant>(individuals, (filterGroup as IFilterGroup).CombinedRules as List <Func <Ruminant, bool> >).ToList <Ruminant>());
                }
            }
        }