protected void ScenariosChanged(object sender, NotifyCollectionChangedEventArgs e)
{
var scenariosNew = (e.NewItems ?? new object[0]).Cast<Scenario>();
var scenariosOld = (e.OldItems ?? new object[0]).Cast<Scenario>();
lock (_scenariosInternal)
{
// Remove all.
if (e.Action == NotifyCollectionChangedAction.Reset)
{
_scenariosInternal.Clear();
}
// Remove individuals.
else if (e.Action == NotifyCollectionChangedAction.Remove
|| e.Action == NotifyCollectionChangedAction.Replace)
{
foreach (var old in scenariosOld)
{
var oldRecord =
_scenariosInternal
.FirstOrDefault(o => o.Scenario == old);
_scenariosInternal.Remove(oldRecord);
}
}
// Add new.
else if (e.Action == NotifyCollectionChangedAction.Add
|| e.Action == NotifyCollectionChangedAction.Replace)
{
foreach (var @new in scenariosNew)
{
@new.Workbench = this;
FObservation evidence = @new.Evidence;
InferenceQuery query = new InferenceQuery(
@new.BayesianNetwork, evidence);
query.WarmupSize = this.InferenceWarmupSize;
query.ParticleSeparation = this.InferenceParticleSeparation;
ScenarioRecord record = new ScenarioRecord(
@new,
query);
_scenariosInternal.Add(record);
}
}
}
}