public void UpdateCompoundPropertiesIn(Simulation simulation) { var allExistingCompoundProperties = simulation.CompoundPropertiesList.ToList(); var allExistingProtocols = simulation.AllBuildingBlocks <Protocol>().ToList(); simulation.Properties.ClearCompoundPropertiesList(); //add existing ones back foreach (var compound in simulation.Compounds) { //using name comparison here and not references because references might not be uptodate. var compoundProperties = allExistingCompoundProperties.Find(x => string.Equals(x.Compound.Name, compound.Name)); if (compoundProperties == null) { continue; } compoundProperties.Compound = compound; updateUsedProtocol(compoundProperties, allExistingProtocols); simulation.Properties.AddCompoundProperties(compoundProperties); } //create new ones simulation.AllBuildingBlocks <Compound>().Where(c => simulation.CompoundPropertiesFor(c) == null) .Each(c => simulation.Properties.AddCompoundProperties(_compoundPropertiesMapper.MapFrom(c))); //update protocol to previsous selected one in case of a one to one mapping (e.g. switching from compound A to compound B) if (!isUpdatingOneCompoundWithAnotherOne(simulation, allExistingCompoundProperties)) { return; } var compoundPropertiesForSingleCompound = simulation.CompoundPropertiesFor(simulation.Compounds[0]); if (compoundPropertiesForSingleCompound.IsAdministered) { return; } compoundPropertiesForSingleCompound.ProtocolProperties = allExistingCompoundProperties[0].ProtocolProperties; updateUsedProtocol(compoundPropertiesForSingleCompound, allExistingProtocols); }
protected override void Context() { _compound1 = new Compound().WithId("1").WithName("Compound1"); _compound2 = new Compound().WithId("2").WithName("Compound2"); _simulation = new IndividualSimulation { Properties = new SimulationProperties() }; _simulation.AddUsedBuildingBlock(new UsedBuildingBlock("Temp1", PKSimBuildingBlockType.Compound) { BuildingBlock = _compound1 }); _simulation.AddUsedBuildingBlock(new UsedBuildingBlock("Temp2", PKSimBuildingBlockType.Compound) { BuildingBlock = _compound2 }); _compoundPropertiesMapper = A.Fake <ICompoundToCompoundPropertiesMapper>(); A.CallTo(() => _compoundPropertiesMapper.MapFrom(A <Compound> ._)).ReturnsLazily(x => new CompoundProperties { Compound = x.GetArgument <Compound>(0) }); sut = new CompoundPropertiesUpdater(_compoundPropertiesMapper); }