Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Communication.Operators/StateTransistor.cs @ 5313

Last change on this file since 5313 was 592, checked in by abeham, 16 years ago

Put the GPL license in the files from the communication framework and simulation optimization project (branch 3.1)

File size: 5.9 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 HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Operators.Programmable;
29using HeuristicLab.Communication.Data;
30
31namespace HeuristicLab.Communication.Operators {
32  public class StateTransistor : OperatorBase {
33    public override string Description {
34      get { return @"Evaluates the state transition conditions one by one and advances to the state specified as target state in the first condition that results true.
35If the choice on the transition was made and there are suboperators attached, it will call its first suboperator upon successful transition, otherwise the second suboperator is the next to be executed."; }
36    }
37
38    public StateTransistor()
39      : base() {
40      AddVariableInfo(new VariableInfo("CurrentState", "", typeof(ProtocolState), VariableKind.In | VariableKind.Out | VariableKind.New));
41      AddVariableInfo(new VariableInfo("ConditionIndex", "", typeof(IntData), VariableKind.New | VariableKind.In | VariableKind.Out | VariableKind.Deleted));
42      AddVariableInfo(new VariableInfo("Result", "", typeof(BoolData), VariableKind.New | VariableKind.In | VariableKind.Out | VariableKind.Deleted));
43    }
44
45    public override IOperation Apply(IScope scope) {
46      ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
47      // Terminate as soon as an accepting state is reached
48      if (currentState.AcceptingState.Data) return null;
49
50      ItemList<StateTransition> transitionCondition = currentState.StateTransitions;
51      if ((transitionCondition == null || transitionCondition.Count < 1) && !currentState.AcceptingState.Data)
52        throw new InvalidOperationException("ERROR: A dead-end state (" + currentState.Name.Data + ") has been reached that is not an accepting state");
53
54      IVariableInfo conditionIndexInfo = GetVariableInfo("ConditionIndex");
55      IntData conditionIndex = GetVariableValue<IntData>("ConditionIndex", scope, false, false);
56      if (conditionIndex == null) {
57        Variable conditionIndexVar = null;
58        if (conditionIndexInfo.Local) {
59          conditionIndexVar = new Variable(conditionIndexInfo.ActualName, new IntData(0));
60          AddVariable(conditionIndexVar);
61        } else {
62          conditionIndexVar = new Variable(scope.TranslateName(conditionIndexInfo.FormalName), new IntData(0));
63          scope.AddVariable(conditionIndexVar);
64        }
65        conditionIndex = (IntData)conditionIndexVar.Value;
66      }
67
68      BoolData result = GetVariableValue<BoolData>("Result", scope, false, false);
69      if (result != null) {
70        bool done = false;
71        if (result.Data) {
72          IVariable csVar = scope.GetVariable(scope.TranslateName("CurrentState"));
73          if (csVar == null) scope.AddVariable(new Variable(scope.TranslateName("CurrentState"), ((StateTransition)transitionCondition[conditionIndex.Data - 1]).TargetState));
74          else csVar.Value = ((StateTransition)transitionCondition[conditionIndex.Data - 1]).TargetState;
75          done = true;
76        }
77        if (done || conditionIndex.Data == transitionCondition.Count) {
78          //conditionIndex.Data = 0;
79          IVariableInfo resultInfo = GetVariableInfo("Result");
80          if (resultInfo.Local) RemoveVariable(resultInfo.ActualName);
81          else scope.RemoveVariable(scope.TranslateName(resultInfo.FormalName));
82          if (conditionIndexInfo.Local) RemoveVariable(conditionIndexInfo.ActualName);
83          else scope.RemoveVariable(scope.TranslateName(conditionIndexInfo.FormalName));
84        }
85        if (!done && conditionIndex.Data == transitionCondition.Count) {
86          if (SubOperators.Count > 1) return new AtomicOperation(SubOperators[1], scope);
87          else return null;
88        } else {
89          if (SubOperators.Count > 0) return new AtomicOperation(SubOperators[0], scope);
90          else return null;
91        }
92      }
93      SequentialProcessor sp = new SequentialProcessor();
94      ProgrammableOperator tmp = (ProgrammableOperator)((StateTransition)transitionCondition[conditionIndex.Data]).TransitionCondition;
95      tmp.GetVariableInfo("Result").ActualName = GetVariableInfo("Result").ActualName;
96      sp.AddSubOperator(tmp);
97
98      StateTransistor nextTransistor = new StateTransistor();
99      nextTransistor.GetVariableInfo("CurrentState").ActualName = GetVariableInfo("CurrentState").ActualName;
100      nextTransistor.GetVariableInfo("Result").ActualName = GetVariableInfo("Result").ActualName;
101      if (conditionIndexInfo.Local) {
102        nextTransistor.AddVariable(new Variable(conditionIndexInfo.ActualName, conditionIndex));
103        nextTransistor.GetVariableInfo("ConditionIndex").Local = true;
104      }
105      nextTransistor.GetVariableInfo("ConditionIndex").ActualName = conditionIndexInfo.ActualName;
106      conditionIndex.Data++;
107
108      for (int i = 0 ; i < SubOperators.Count ; i++) {
109        nextTransistor.AddSubOperator(SubOperators[i]);
110      }
111
112      sp.AddSubOperator(nextTransistor);
113      return new AtomicOperation(sp, scope);
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.