using System.Collections.Generic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Algorithms.IslandALPS {
///
/// Extracts values from a scope into the current scope
///
[Item("ResultsExtractor", "Extracts values from a scope into the current scope")]
[StorableClass]
public class ResultsExtractor : InstrumentedOperator {
[StorableConstructor]
public ResultsExtractor(bool deserializing) : base(deserializing) {}
public ResultsExtractor(ResultsExtractor original, Cloner cloner) : base(original, cloner) {}
public ResultsExtractor() {
Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the operator gets executed."));
Parameters.Add(new ValueLookupParameter("SourceName", "The Scope where the variable is."));
Parameters.Add(new ValueParameter("TargetName", "The name of the ResultCollection.", new StringValue("Results")));
}
public ScopeParameter CurrentScopeParameter {
get { return (ScopeParameter) Parameters["CurrentScope"]; }
}
public ValueLookupParameter SourceNameParameter {
get { return (ValueLookupParameter) Parameters["SourceName"]; }
set { Parameters["SourceName"].ActualValue = value; }
}
public ValueParameter TargetNameParameter {
get { return (ValueParameter) Parameters["TargetName"]; }
set { Parameters["TargetName"].ActualValue = value; }
}
private IScope CurrentScope {
get { return CurrentScopeParameter.ActualValue; }
}
private IScope SourceScope {
get { return (IScope) CurrentScope.Variables[SourceNameParameter.Value.Value].Value; }
}
public override IDeepCloneable Clone(Cloner cloner) {
return new ResultsExtractor(this, cloner);
}
public override IOperation InstrumentedApply() {
var list = new List();
var key = TargetNameParameter.Value.Value;
for (var index = 0; index < SourceScope.SubScopes.Count; index++) {
var subScope = SourceScope.SubScopes[index];
if (subScope.Variables.ContainsKey(key))
list.Add(subScope.Variables[key].Value as ResultCollection);
}
if (!CurrentScope.Variables.ContainsKey(key))
CurrentScope.Variables.Add(new Variable(key));
CurrentScope.Variables[key].Value = new ItemArray(list);
return base.InstrumentedApply();
}
}
}