private void OnCLEMInitialiseResource(object sender, EventArgs e) { Details = this.FindAllChildren <RuminantTypeCohort>().FirstOrDefault(); ruminantType = Resources.GetResourceItem(this.Parent as Model, RuminantTypeName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as RuminantType; // create example ruminant Details.Number = 1; ExampleIndividual = Details.CreateIndividuals(null, BreedParams).FirstOrDefault(); }
/// <summary> /// /// </summary> /// <param name="validationContext"></param> /// <returns></returns> public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var results = new List <ValidationResult>(); if (this.FindAllChildren <Transmutation>().Count() > 0) { string[] memberNames = new string[] { "Transmutations" }; results.Add(new ValidationResult("Transmutations are not available for the CommonLandFoodStoreType (" + this.Name + ")", memberNames)); } pasture = new object(); // check that either a AnimalFoodStoreType or a GrazeFoodStoreType can be found if link required. if (PastureLink != null && !PastureLink.StartsWith("Not specified")) { // check animalFoodStoreType pasture = Resources.GetResourceItem(this, PastureLink, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop); if (pasture == null) { string[] memberNames = new string[] { "Pasture link" }; results.Add(new ValidationResult("A link to an animal food store or graze food store type must be supplied to link to common land (" + this.Name + ")", memberNames)); } } if (PastureLink != null && PastureLink.StartsWith("Not specified")) { // no link so need to ensure values are all supplied. List <string> missing = new List <string>(); if (NToDMDCoefficient == 0) { missing.Add("NToDMDCoefficient"); } if (NToDMDIntercept == 0) { missing.Add("NToDMDIntercept"); } if (NToDMDCrudeProteinDenominator == 0) { missing.Add("NToDMDCrudeProteinDenominator"); } if (missing.Count() > 0) { foreach (var item in missing) { string[] memberNames = new string[] { item }; results.Add(new ValidationResult("The common land [r=" + this.Name + "] requires [o=" + item + "] as it is not linked to an on-farm pasture", memberNames)); } } } return(results); }
/// <summary> /// Performs the transmutation of resources into a required resource /// </summary> public void TransmutateShortfall(List <ResourceRequest> requests, bool queryOnly) { List <ResourceRequest> shortfallRequests = requests.Where(a => a.Required > a.Available).ToList(); // Search through all limited resources and determine if transmutation available foreach (ResourceRequest request in shortfallRequests) { // Check if transmutation would be successful if (request.AllowTransmutation && (queryOnly || request.TransmutationPossible)) { // get resource type IModel model = request.Resource as IModel; if (model is null) { model = this.GetResourceItem(request.ActivityModel, request.ResourceType, request.ResourceTypeName, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.Ignore) as IModel; } if (model != null) { // get the resource holder to use for this request // not it is either this class or the holder for the market place required. ResourcesHolder resHolder = model.Parent.Parent as ResourcesHolder; // check if transmutations provided foreach (Transmutation trans in model.FindAllChildren <Transmutation>()) { double unitsNeeded = 0; // check if resources available for activity and transmutation foreach (ITransmutationCost transcost in trans.FindAllChildren <IModel>().Where(a => a is ITransmutationCost).Cast <ITransmutationCost>()) { double unitsize = trans.AmountPerUnitPurchase; if (transcost is TransmutationCostUsePricing) { // use pricing details if needed unitsize = (transcost as TransmutationCostUsePricing).Pricing.PacketSize; } unitsNeeded = Math.Ceiling((request.Required - request.Available) / unitsize); double transmutationCost; if (transcost is TransmutationCostUsePricing) { // use pricing details if needed transmutationCost = unitsNeeded * (transcost as TransmutationCostUsePricing).Pricing.PricePerPacket; } else { transmutationCost = unitsNeeded * transcost.CostPerUnit; } // get transcost resource IResourceType transResource = null; if (transcost.ResourceType.Name != "Labour") { transResource = resHolder.GetResourceItem(request.ActivityModel, transcost.ResourceTypeName, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.Ignore) as IResourceType; } if (!queryOnly) { // remove cost // create new request for this transmutation cost ResourceRequest transRequest = new ResourceRequest { Reason = trans.Name + " " + trans.Parent.Name, Required = transmutationCost, ResourceType = transcost.ResourceType, ActivityModel = request.ActivityModel }; // used to pass request, but this is not the transmutation cost if (transcost.ResourceType.Name == "Labour") { transRequest.ResourceType = typeof(Labour); transRequest.FilterDetails = (transcost as IModel).FindAllChildren <LabourFilterGroup>().ToList <object>(); CLEMActivityBase.TakeLabour(transRequest, true, transRequest.ActivityModel, this, OnPartialResourcesAvailableActionTypes.UseResourcesAvailable); } else { transResource.Remove(transRequest); } } else { double activityCost = requests.Where(a => a.ResourceType == transcost.ResourceType && a.ResourceTypeName == transcost.ResourceTypeName).Sum(a => a.Required); if (transmutationCost + activityCost <= transResource.Amount) { request.TransmutationPossible = true; break; } } } if (!queryOnly) { // Add resource (model as IResourceType).Add(unitsNeeded * trans.AmountPerUnitPurchase, trans, "Transmutation"); } } } } } }
private void OnCLEMInitialiseResources(object sender, EventArgs e) { Details = this.FindAllChildren <RuminantTypeCohort>().FirstOrDefault(); ruminantType = Resources.GetResourceItem(this.Parent as Model, RuminantTypeName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as RuminantType; }