Skip to content

Commit

Permalink
Add methods to crop initialization service
Browse files Browse the repository at this point in the history
  • Loading branch information
holos-aafc committed Aug 2, 2024
1 parent 29e9248 commit 49106ce
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 35 deletions.
10 changes: 5 additions & 5 deletions H.Core.Test/Integration/FieldComponentIntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void CarbonModellingIntegrationTest()
// Assign default properties to
foreach (var cropViewItem in cropsInRotation)
{
_initializationService.InitializeCropDefaults(cropViewItem, farm, _globalSettings);
_initializationService.InitializeCrop(cropViewItem, farm, _globalSettings);
}

// Override default properties for some crops (optional)
Expand Down Expand Up @@ -177,7 +177,7 @@ public void FCC()

var cvi1 = new CropViewItem();
cvi1.CropType = CropType.AlfalfaMedicagoSativaL;
_initializationService.InitializeCropDefaults(cvi1, farm, _applicationData.GlobalSettings); // moisture content will be set here
_initializationService.InitializeCrop(cvi1, farm, _applicationData.GlobalSettings); // moisture content will be set here



Expand Down Expand Up @@ -241,7 +241,7 @@ public void FCC()


cvi2.CropType = CropType.Canola;
_initializationService.InitializeCropDefaults(cvi2, farm, _applicationData.GlobalSettings);
_initializationService.InitializeCrop(cvi2, farm, _applicationData.GlobalSettings);



Expand Down Expand Up @@ -293,7 +293,7 @@ public void FCC()


cvi3.CropType = CropType.FieldPeas;
_initializationService.InitializeCropDefaults(cvi3, farm, _applicationData.GlobalSettings);
_initializationService.InitializeCrop(cvi3, farm, _applicationData.GlobalSettings);



Expand Down Expand Up @@ -341,7 +341,7 @@ public void FCC()


cvi4.CropType = CropType.Wheat;
_initializationService.InitializeCropDefaults(cvi4, farm, _applicationData.GlobalSettings);
_initializationService.InitializeCrop(cvi4, farm, _applicationData.GlobalSettings);



Expand Down
139 changes: 136 additions & 3 deletions H.Core.Test/Services/Initialization/CropInitializationServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void TestInitialize()

_field.CropViewItems.Add(_crop);
_farm.Components.Add(_field);


_cropsInitializationService = new CropInitializationService();
}
Expand All @@ -66,10 +65,144 @@ public void InitializeCropDefaultTest()
_crop.Year = 1999;
_crop.CropType = CropType.Barley;

_cropsInitializationService.InitializeCropDefaults(_crop, _farm, _globalSettings);
_cropsInitializationService.InitializeCrop(_crop, _farm, _globalSettings);

Assert.AreEqual(2214, _crop.Yield);
}
}

[TestMethod]
public void InitializeAvailableSoilTypesDoesNotAddOrganicType()
{
var soilData = base.GetTestSoilData();
soilData.SoilFunctionalCategory = SoilFunctionalCategory.Organic;

_farm.GeographicData = base.GetTestGeographicData();
_farm.GeographicData.SoilDataForAllComponentsWithinPolygon.Clear();
_farm.GeographicData.SoilDataForAllComponentsWithinPolygon.Add(soilData);

var field = base.GetTestFieldComponent();

_cropsInitializationService.InitializeAvailableSoilTypes(_farm, field);

Assert.AreEqual(0, field.SoilDataAvailableForField.Count);
}

[TestMethod]
public void InitializeAvailableSoilTypesAddSoilToTypesAvailable()
{
var soilData = base.GetTestSoilData();
soilData.SoilFunctionalCategory = SoilFunctionalCategory.Black;

_farm.GeographicData = base.GetTestGeographicData();
_farm.GeographicData.SoilDataForAllComponentsWithinPolygon.Clear();
_farm.GeographicData.SoilDataForAllComponentsWithinPolygon.Add(soilData);

var field = base.GetTestFieldComponent();

_cropsInitializationService.InitializeAvailableSoilTypes(_farm, field);

Assert.AreEqual(1, field.SoilDataAvailableForField.Count);
}

[TestMethod]
public void InitializeAvailableSoilTypesAddsMultipleSoilsToTypesAvailable()
{
var soilData = base.GetTestSoilData();
soilData.SoilFunctionalCategory = SoilFunctionalCategory.Black;
soilData.SoilGreatGroup = SoilGreatGroupType.BrownChernozem;

var soilData2 = base.GetTestSoilData();
soilData2.SoilFunctionalCategory = SoilFunctionalCategory.Black;
soilData2.SoilGreatGroup = SoilGreatGroupType.BlackChernozem;

_farm.GeographicData = base.GetTestGeographicData();
_farm.GeographicData.SoilDataForAllComponentsWithinPolygon.Clear();
_farm.GeographicData.SoilDataForAllComponentsWithinPolygon.Add(soilData);
_farm.GeographicData.SoilDataForAllComponentsWithinPolygon.Add(soilData2);

var field = base.GetTestFieldComponent();

_cropsInitializationService.InitializeAvailableSoilTypes(_farm, field);

Assert.AreEqual(2, field.SoilDataAvailableForField.Count);
}

[TestMethod]
public void InitializeDefaultSoilForFieldSetsNonNullValueForSoilData()
{
var field = base.GetTestFieldComponent();
field.SoilData = null;

var soilData = base.GetTestSoilData();
soilData.SoilFunctionalCategory = SoilFunctionalCategory.Black;
soilData.SoilGreatGroup = SoilGreatGroupType.BrownChernozem;
_farm.GeographicData = base.GetTestGeographicData();
_farm.GeographicData.SoilDataForAllComponentsWithinPolygon.Clear();
_farm.GeographicData.SoilDataForAllComponentsWithinPolygon.Add(soilData);

_cropsInitializationService.InitializeDefaultSoilForField(_farm, field);

Assert.IsNotNull(field.SoilData);
}

[TestMethod]
public void InitializeManureApplicationMethodForBeefCattleTypes()
{
var viewItem = base.GetTestCropViewItem();
var manureApplication = base.GetTestDairyCattleManureApplicationViewItemUsingImportedManure();
manureApplication.AnimalType = AnimalType.BeefBackgrounder;

var validApplicationTypes = new List<ManureApplicationTypes>();

viewItem.ManureApplicationViewItems.Add(manureApplication);

_cropsInitializationService.InitializeManureApplicationMethod(viewItem, manureApplication, validApplicationTypes);

Assert.AreEqual(ManureApplicationTypes.TilledLandSolidSpread, manureApplication.ManureApplicationMethod);
Assert.AreEqual(2, manureApplication.AvailableManureApplicationTypes.Count);
Assert.AreEqual(ManureApplicationTypes.UntilledLandSolidSpread, manureApplication.AvailableManureApplicationTypes[0]);
Assert.AreEqual(ManureApplicationTypes.TilledLandSolidSpread, manureApplication.AvailableManureApplicationTypes[1]);
}

[TestMethod]
public void InitializeManureApplicationMethodForDairyCattleTypes()
{
var viewItem = base.GetTestCropViewItem();
var manureApplication = base.GetTestDairyCattleManureApplicationViewItemUsingImportedManure();

var validApplicationTypes = new List<ManureApplicationTypes>();

viewItem.ManureApplicationViewItems.Add(manureApplication);

_cropsInitializationService.InitializeManureApplicationMethod(viewItem, manureApplication, validApplicationTypes);

Assert.AreEqual(ManureApplicationTypes.TilledLandSolidSpread, manureApplication.ManureApplicationMethod);
Assert.AreEqual(6, manureApplication.AvailableManureApplicationTypes.Count);
}

[TestMethod]
public void InitializeFertilizerApplicationMethodForAnnuals()
{
var viewItem = base.GetTestCropViewItem();
var fertilizerApplication = base.GetTestFertilizerApplicationViewItem();

_cropsInitializationService.InitializeFertilizerApplicationMethod(viewItem, fertilizerApplication);

Assert.AreEqual(FertilizerApplicationMethodologies.IncorporatedOrPartiallyInjected, fertilizerApplication.FertilizerApplicationMethodology);
}

[TestMethod]
public void InitializeFertilizerApplicationMethodForPerennials()
{
var viewItem = base.GetTestCropViewItem();
viewItem.CropType = CropType.TameGrass;

var fertilizerApplication = base.GetTestFertilizerApplicationViewItem();

_cropsInitializationService.InitializeFertilizerApplicationMethod(viewItem, fertilizerApplication);

Assert.AreEqual(FertilizerApplicationMethodologies.Broadcast, fertilizerApplication.FertilizerApplicationMethodology);
}

#endregion
}
Expand Down
19 changes: 19 additions & 0 deletions H.Core.Test/UnitTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,16 @@ public AnimalComponentEmissionsResults GetNonEmptyTestDairyCattleAnimalComponent
return results;
}

public GeographicData GetTestGeographicData()
{
var geographicData = new GeographicData();

geographicData.SoilDataForAllComponentsWithinPolygon = new List<SoilData>();
geographicData.SoilDataForAllComponentsWithinPolygon.Add(this.GetTestSoilData());

return geographicData;
}

public FieldSystemComponent GetTestFieldComponent()
{
var component = new FieldSystemComponent();
Expand All @@ -363,6 +373,7 @@ public FieldSystemComponent GetTestFieldComponent()
public CropViewItem GetTestCropViewItem()
{
var cropViewItem = new CropViewItem();
cropViewItem.CropType = CropType.Wheat;
cropViewItem.Area = 1;
cropViewItem.Year = DateTime.Now.Year;
cropViewItem.Guid = Guid.NewGuid();
Expand All @@ -373,12 +384,20 @@ public CropViewItem GetTestCropViewItem()
return cropViewItem;
}

public FertilizerApplicationViewItem GetTestFertilizerApplicationViewItem()
{
var fertilizerApplicationViewItem = new FertilizerApplicationViewItem();

return fertilizerApplicationViewItem;
}

public SoilData GetTestSoilData( )
{
var soilData = new SoilData();
soilData.EcodistrictId = 679;
soilData.Province = Province.Alberta;
soilData.PolygonId = 679001;
soilData.SoilGreatGroup = SoilGreatGroupType.BlackChernozem;
soilData.SoilTexture = SoilTexture.Fine;

return soilData;
Expand Down
3 changes: 2 additions & 1 deletion H.Core/Models/LandManagement/Fields/FieldSystemComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public bool UseFieldLevelSoilData {
set
{
SetProperty(ref _useFieldLevelSoilData, value);
} }
}
}

/// <summary>
/// Allow for field specific soil data (as opposed to one type of soil being used for all fields on the farm)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using H.Core.Enumerations;
using H.Core.Models.LandManagement.Fields;
using H.Core.Models;
using System.Collections.Generic;
using System.Linq;
using H.Infrastructure;

namespace H.Core.Services.Initialization.Crops
{
Expand Down Expand Up @@ -129,6 +132,82 @@ public double CalculateAmountOfProductRequired(
return requiredAmountOfProduct;
}

public void InitializeManureApplicationMethod(CropViewItem viewItem, ManureApplicationViewItem manureApplicationViewItem, List<ManureApplicationTypes> validManureApplicationTypes)
{
if (manureApplicationViewItem.AnimalType.IsBeefCattleType())
{
var validBeefCattleApplicationMethods = new List<ManureApplicationTypes>()
{
ManureApplicationTypes.UntilledLandSolidSpread,
ManureApplicationTypes.TilledLandSolidSpread,
};

manureApplicationViewItem.AvailableManureApplicationTypes.UpdateItems(validBeefCattleApplicationMethods);

// Update the selected item based on the tillage type of the field
if (viewItem.TillageType == TillageType.NoTill)
{
manureApplicationViewItem.ManureApplicationMethod = ManureApplicationTypes.UntilledLandSolidSpread;
}
else
{
manureApplicationViewItem.ManureApplicationMethod = ManureApplicationTypes.TilledLandSolidSpread;
}
}
else if (manureApplicationViewItem.AnimalType.IsDairyCattleType())
{
var validDairyCattleApplicationMethods = new List<ManureApplicationTypes>()
{
ManureApplicationTypes.UntilledLandSolidSpread,
ManureApplicationTypes.TilledLandSolidSpread,
ManureApplicationTypes.SlurryBroadcasting,
ManureApplicationTypes.DropHoseBanding,
ManureApplicationTypes.ShallowInjection,
ManureApplicationTypes.DeepInjection,
};

// Can't use ObservableCollection.Clear(),
manureApplicationViewItem.AvailableManureApplicationTypes.UpdateItems(validDairyCattleApplicationMethods);

if (manureApplicationViewItem.ManureStateType.IsSolidManure())
{
if (viewItem.TillageType == TillageType.NoTill)
{
manureApplicationViewItem.ManureApplicationMethod = ManureApplicationTypes.UntilledLandSolidSpread;
}
else
{
manureApplicationViewItem.ManureApplicationMethod = ManureApplicationTypes.TilledLandSolidSpread;
}
}
else
{
manureApplicationViewItem.ManureApplicationMethod = ManureApplicationTypes.DeepInjection;
}
}
else
{
manureApplicationViewItem.AvailableManureApplicationTypes.UpdateItems(validManureApplicationTypes);

manureApplicationViewItem.ManureApplicationMethod = validManureApplicationTypes.FirstOrDefault();
}
}

public void InitializeFertilizerApplicationMethod(CropViewItem viewItem, FertilizerApplicationViewItem fertilizerApplicationViewItem)
{
if (viewItem != null && fertilizerApplicationViewItem != null)
{
if (viewItem.CropType.IsPerennial())
{
fertilizerApplicationViewItem.FertilizerApplicationMethodology = FertilizerApplicationMethodologies.Broadcast;
}
else
{
fertilizerApplicationViewItem.FertilizerApplicationMethodology = FertilizerApplicationMethodologies.IncorporatedOrPartiallyInjected;
}
}
}

#endregion
}
}
Loading

0 comments on commit 49106ce

Please sign in to comment.