Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.3/ConditionalBranch.cs @ 2757

Last change on this file since 2757 was 2757, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • worked on parameters and operators
File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Operators {
32  /// <summary>
33  /// A branch of two operators whose executions depend on a condition.
34  /// </summary>
35  [Item("ConditionalBranch", "A branch of two operators whose executions depend on a boolean condition.")]
36  [Creatable("Test")]
37  [EmptyStorableClass]
38  public class ConditionalBranch : SingleSuccessorOperator {
39    public LookupParameter<BoolData> ConditionParameter {
40      get { return (LookupParameter<BoolData>)Parameters["Condition"]; }
41    }
42    protected OperatorParameter TrueBranchParameter {
43      get { return (OperatorParameter)Parameters["TrueBranch"]; }
44    }
45    protected OperatorParameter FalseBranchParameter {
46      get { return (OperatorParameter)Parameters["FalseBranch"]; }
47    }
48    public IOperator TrueBranch {
49      get { return TrueBranchParameter.Value; }
50      set { TrueBranchParameter.Value = value; }
51    }
52    public IOperator FalseBranch {
53      get { return FalseBranchParameter.Value; }
54      set { FalseBranchParameter.Value = value; }
55    }
56
57    public ConditionalBranch()
58      : base() {
59      Parameters.Add(new LookupParameter<BoolData>("Condition", "A boolean variable which defines which branch is executed."));
60      Parameters.Add(new OperatorParameter("TrueBranch", "The operator which is executed if the condition is true."));
61      Parameters.Add(new OperatorParameter("FalseBranch", "The operator which is executed if the condition is false."));
62    }
63
64    public override IExecutionContext Apply() {
65      ExecutionContextCollection next = new ExecutionContextCollection(base.Apply());
66      if (ConditionParameter.ActualValue.Value) {
67        if (TrueBranch != null) next.Insert(0, new ExecutionContext(ExecutionContext.Parent, TrueBranch, ExecutionContext.Scope));
68      } else {
69        if (FalseBranch != null) next.Insert(0, new ExecutionContext(ExecutionContext.Parent, FalseBranch, ExecutionContext.Scope));
70      }
71      return next;
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.