1 | using System.Collections.Generic;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Algorithms.IslandALPS {
|
---|
11 | /// <summary>
|
---|
12 | /// Extracts values from a scope into the current scope
|
---|
13 | /// </summary>
|
---|
14 | [Item("ResultsExtractor", "Extracts values from a scope into the current scope")]
|
---|
15 | [StorableClass]
|
---|
16 | public class ResultsExtractor : InstrumentedOperator {
|
---|
17 | [StorableConstructor]
|
---|
18 | public ResultsExtractor(bool deserializing) : base(deserializing) {}
|
---|
19 |
|
---|
20 | public ResultsExtractor(ResultsExtractor original, Cloner cloner) : base(original, cloner) {}
|
---|
21 |
|
---|
22 | public ResultsExtractor() {
|
---|
23 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the operator gets executed."));
|
---|
24 | Parameters.Add(new ValueLookupParameter<StringValue>("SourceName", "The Scope where the variable is."));
|
---|
25 | Parameters.Add(new ValueParameter<StringValue>("TargetName", "The name of the ResultCollection.", new StringValue("Results")));
|
---|
26 | }
|
---|
27 |
|
---|
28 | public ScopeParameter CurrentScopeParameter {
|
---|
29 | get { return (ScopeParameter) Parameters["CurrentScope"]; }
|
---|
30 | }
|
---|
31 |
|
---|
32 | public ValueLookupParameter<StringValue> SourceNameParameter {
|
---|
33 | get { return (ValueLookupParameter<StringValue>) Parameters["SourceName"]; }
|
---|
34 | set { Parameters["SourceName"].ActualValue = value; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public ValueParameter<StringValue> TargetNameParameter {
|
---|
38 | get { return (ValueParameter<StringValue>) Parameters["TargetName"]; }
|
---|
39 | set { Parameters["TargetName"].ActualValue = value; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | private IScope CurrentScope {
|
---|
43 | get { return CurrentScopeParameter.ActualValue; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | private IScope SourceScope {
|
---|
47 | get { return (IScope) CurrentScope.Variables[SourceNameParameter.Value.Value].Value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
51 | return new ResultsExtractor(this, cloner);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public override IOperation InstrumentedApply() {
|
---|
55 | var list = new List<ResultCollection>();
|
---|
56 | var key = TargetNameParameter.Value.Value;
|
---|
57 | for (var index = 0; index < SourceScope.SubScopes.Count; index++) {
|
---|
58 | var subScope = SourceScope.SubScopes[index];
|
---|
59 | if (subScope.Variables.ContainsKey(key))
|
---|
60 | list.Add(subScope.Variables[key].Value as ResultCollection);
|
---|
61 | }
|
---|
62 | if (!CurrentScope.Variables.ContainsKey(key))
|
---|
63 | CurrentScope.Variables.Add(new Variable(key));
|
---|
64 | CurrentScope.Variables[key].Value = new ItemArray<ResultCollection>(list);
|
---|
65 | return base.InstrumentedApply();
|
---|
66 | }
|
---|
67 | }
|
---|
68 | } |
---|