Esempio n. 1
0
        private void OnCLEMInitialiseActivity(object sender, EventArgs e)
        {
            // get all ui tree herd filters that relate to this activity
            this.InitialiseHerd(true, true);

            // locate FeedType resource
            FeedType = Resources.FindResourceType <ResourceBaseWithTransactions, IResourceType>(this, FeedTypeName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as IFeedType;
        }
        private void OnCLEMInitialiseActivity(object sender, EventArgs e)
        {
            // locate FeedType resource
            FeedType   = Resources.GetResourceItem(this, typeof(AnimalFoodStore), FeedTypeName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as IFeedType;
            FoodSource = FeedType;

            // get labour specifications
            labour = Apsim.Children(this, typeof(LabourFilterGroupSpecified)).Cast <LabourFilterGroupSpecified>().ToList(); //  this.Children.Where(a => a.GetType() == typeof(LabourFilterGroupSpecified)).Cast<LabourFilterGroupSpecified>().ToList();
            if (labour == null)
            {
                labour = new List <LabourFilterGroupSpecified>();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Attempts to add a new feed type to the persisted store.
        /// </summary>
        /// <param name="item">The feed type to be added.</param>
        /// <param name="cancellationToken">A token that can be used to signal operation cancellation.</param>
        /// <returns>The added feed type.</returns>
        public virtual async Task <IFeedType> AddAsync(IFeedType item, CancellationToken cancellationToken)
        {
            Logger.LogInformation("Adding a feed type...");

            var entity  = Mapper.Map(item);
            var changes = await LivestockContext.FeedTypes
                          .AddAsync(entity, cancellationToken)
                          .ConfigureAwait(false);

            await LivestockContext.SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            return(Mapper.Map(changes.Entity));
        }
Esempio n. 4
0
        /// <summary>
        /// Updates the properties of an feed type in the persisted store.
        /// </summary>
        /// <param name="item">The feed type with its desired values.</param>
        /// <param name="cancellationToken">A token that can be used to signal operation cancellation.</param>
        /// <returns>The updated feed type.</returns>
        /// <exception cref="EntityNotFoundException{IFeedType}">When the feed type with the given key is not found.</exception>
        public virtual async Task <IFeedType> UpdateAsync(IFeedType item, CancellationToken cancellationToken)
        {
            Logger.LogInformation($"Updating the feed type with ID {item.Id}...");

            var entity = await LivestockContext.FeedTypes
                         .FindAsync(new object[] { item.Id }, cancellationToken)
                         .ConfigureAwait(false);

            if (entity == null)
            {
                throw new EntityNotFoundException <FeedTypeModel>(item.Id);
            }

            entity.Description = item.Description;

            var changes = LivestockContext.FeedTypes.Update(entity);
            await LivestockContext.SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            return(Mapper.Map(changes.Entity));
        }
 private void OnCLEMInitialiseActivity(object sender, EventArgs e)
 {
     // locate FeedType resource
     FeedType = Resources.GetResourceItem(this, FeedTypeName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as IFeedType;
 }
Esempio n. 6
0
 private void OnCLEMInitialiseActivity(object sender, EventArgs e)
 {
     // locate FeedType resource
     FeedType = Resources.FindResourceType <ResourceBaseWithTransactions, IResourceType>(this, FeedTypeName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as IFeedType;
 }
Esempio n. 7
0
        private void OnWFFeedAllocation(object sender, EventArgs e)
        {
            // if no requests end
            if (requests.Count() == 0)
            {
                return;
            }

            // month needed for montly pasture limits.
            int month = Clock.Today.Month;

            // get list of unique priorities provided in requests
            List <int> priorityList = requests.OrderBy(b => b.FeedActivity.FeedPriority).Select(a => a.FeedActivity.FeedPriority).Distinct().ToList();

            foreach (int priority in priorityList)
            {
                // group by feed types requested with each priority
                var feedGrouping = requests.Where(a => a.FeedActivity.FeedPriority == priority).GroupBy(a => a.FeedActivity.FeedType);
                foreach (var feedTypeGroup in feedGrouping)
                {
                    IFeedType feedType = feedTypeGroup.Key as IFeedType;

                    // determine the total requested for each feedtype
                    double amountRequested = feedTypeGroup.Sum(a => Math.Min(a.Amount, (a.Requestor.PotentialIntake - a.Requestor.Intake) * 30.4) * a.Requestor.Number);

                    // if something requested and something available
                    if (amountRequested > 0 & feedType.Amount > 0)
                    {
                        // determine if shortfall
                        double deficit = Math.Min(1.0, feedType.Amount / amountRequested);
                        if (deficit < 1.0)
                        {
                            // TODO: work out what do do.
                            // Buy fodder
                            // Use common land
                            // use Animal Food Stores.
                        }
                        // prepare to take available from resource store
                        amountRequested *= deficit;

                        // group requests by ruminant breed to allow calculations of limits
                        var breedGrouping = feedTypeGroup.GroupBy(a => a.Requestor.BreedParams.Name);
                        foreach (var breedGroup in breedGrouping)
                        {
                            // If feeding on pasture calculate limits based on proportion green
                            // This needs to be done for pasture/breed combinations each month
                            // So cannot be done for Ruminants or Pasture seperately at start of month
                            if (feedType.GetType().Name == "PastureType")
                            {
                                GrazeFoodStoreType pasture = feedType as GrazeFoodStoreType;

                                double total = 0;
                                foreach (var pool in pasture.Pools)
                                {
                                    pool.Limit = 1.0;
                                    total     += pool.Amount;
                                }

                                // if Jan-March then user first three months otherwise use 2
                                int greenage = 2;
                                if (month <= 3)
                                {
                                    greenage = 3;
                                }

                                double green      = pasture.Pools.Where(a => (a.Age <= greenage)).Sum(b => b.Amount);
                                double propgreen  = green / total;
                                double greenlimit = breedGroup.FirstOrDefault().Requestor.BreedParams.GreenDietMax *(1 - Math.Exp(-breedGroup.FirstOrDefault().Requestor.BreedParams.GreenDietCoefficient *((propgreen * 100.0) - breedGroup.FirstOrDefault().Requestor.BreedParams.GreenDietZero)));
                                greenlimit = Math.Max(0.0, greenlimit);
                                if (propgreen > 90)
                                {
                                    greenlimit = 100;
                                }

                                foreach (var pool in pasture.Pools.Where(a => a.Age <= greenage))
                                {
                                    pool.Limit = greenlimit / 100.0;
                                }
                            }

                            // update Ruminants Intake, ProteinConcentration and DietDryMatterDigestibility
                            foreach (var request in feedTypeGroup)
                            {
                                switch (request.FeedActivity.FeedType.GetType().Name)
                                {
                                case "GrazeFoodStoreType":
                                    // take from pools as specified for the individual
                                    GrazeFoodStoreType pasture        = request.FeedActivity.FeedType as GrazeFoodStoreType;
                                    double             amountRequired = (request.Requestor.PotentialIntake - request.Requestor.Intake) * 30.4;

                                    int  index = 0;
                                    bool secondTakeFromPools = request.Requestor.BreedParams.StrictFeedingLimits;
                                    while (amountRequired > 0)
                                    {
                                        // limiter obtained from filter group or unlimited if second take of pools
                                        double limiter = 1.0;
                                        if (!secondTakeFromPools)
                                        {
                                            limiter = pasture.Pools[index].Limit;
                                        }

                                        double amountToRemove = Math.Min(pasture.Pools[index].Amount, amountRequired * limiter);
                                        amountRequired -= amountToRemove;

                                        request.Amount = amountToRemove;
                                        pasture.Pools[index].Remove(request);

                                        index++;
                                        if (index >= pasture.Pools.Count)
                                        {
                                            // if we've already given second chance to get food so finish without full satisfying individual
                                            // or strict feeding limits are enforced
                                            if (secondTakeFromPools)
                                            {
                                                break;
                                            }
                                            // if not strict limits allow a second request for food from previously limited pools.
                                            secondTakeFromPools = true;
                                            index = 0;
                                        }
                                    }
                                    break;

                                case "AnimalFoodStoreType":
                                    // take directly from store if available
                                    request.Amount *= deficit;
                                    feedTypeGroup.FirstOrDefault().FeedActivity.FeedType.Remove(request);
                                    break;

                                default:
                                    string error = String.Format("Unrecognised feed type {0} in {1} of name {2}", request.GetType().ToString(), this.GetType().ToString(), this.Name);
                                    Summary.WriteWarning(this, error);
                                    throw new Exception("Unrecognised Feedtype found in feed request");
                                }
                            }
                        }
                    }
                }
            }
        }