forked from miroiu/nodify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMachineRunnerViewModel.cs
More file actions
230 lines (185 loc) · 7.17 KB
/
StateMachineRunnerViewModel.cs
File metadata and controls
230 lines (185 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Collections.Generic;
using System.Linq;
namespace Nodify.StateMachine
{
public class StateMachineRunnerViewModel : ObservableObject
{
private StateMachine? _stateMachine;
private StateViewModel? _activeState;
private TransitionViewModel? _activeTransition;
private readonly DebugBlackboardDecorator _debugger = new DebugBlackboardDecorator();
private readonly Blackboard _original = new Blackboard();
protected StateMachineViewModel StateMachineViewModel { get; }
private MachineState _state;
public MachineState State
{
get => _state;
protected set => SetProperty(ref _state, value);
}
private int _nodesVisited;
public int NodesVisited
{
get => _nodesVisited;
protected set => SetProperty(ref _nodesVisited, value);
}
public StateMachineRunnerViewModel(StateMachineViewModel stateMachineViewModel)
{
StateMachineViewModel = stateMachineViewModel;
_debugger.ValueChanged += OnBlackboardKeyValueChanged;
}
private void OnBlackboardKeyValueChanged(BlackboardKey key, object? newValue)
{
if (_stateMachine != null && _stateMachine.State != MachineState.Stopped)
{
var existing = StateMachineViewModel.Blackboard.Keys.FirstOrDefault(k => k.Name == key.Name && k.Type == key.Type);
if (existing != null)
{
existing.Value = newValue;
}
}
}
#region State Machine Actions
public async void Start()
{
NodesVisited = 0;
_stateMachine = new StateMachine(StateMachineViewModel.States[0].Id, CreateStates(StateMachineViewModel.States), CreateBlackboard(StateMachineViewModel.Blackboard));
_stateMachine.StateTransition += HandleStateTransition;
_stateMachine.StateChanged += HandleStateChange;
await _stateMachine.Start();
}
public void Stop()
{
_stateMachine?.Stop();
_stateMachine = null;
}
private void HandleStateTransition(Guid from, Guid to)
{
NodesVisited++;
SetActiveStateAndTransition(false);
_activeTransition = StateMachineViewModel.Transitions.FirstOrDefault(t => t.Source.Id == from);
_activeState = StateMachineViewModel.States.FirstOrDefault(st => st.Id == to);
SetActiveStateAndTransition(true);
}
private void SetActiveStateAndTransition(bool value)
{
if (_activeState != null)
{
_activeState.IsActive = value;
}
if (_activeTransition != null)
{
_activeTransition.IsActive = value;
}
}
private void HandleStateChange(MachineState newState)
{
if (newState == MachineState.Stopped)
{
SetActiveStateAndTransition(false);
ResetBlackboardToOriginal();
}
State = newState;
}
private void ResetBlackboardToOriginal()
{
var keys = StateMachineViewModel.Blackboard.Keys;
for (int i = 0; i < keys.Count; i++)
{
var key = keys[i];
key.Value = _original.GetObject(key.Name);
}
}
public void TogglePause()
{
if (State == MachineState.Paused)
{
_stateMachine?.Unpause();
}
else if (State != MachineState.Stopped)
{
_stateMachine?.Pause();
}
}
#endregion
#region Initialize State Machine
private IEnumerable<State> CreateStates(IEnumerable<StateViewModel> states)
=> states.Select(s => new DebugStateDecorator(new State(s.Id, CreateTransitions(s), CreateAction(s.Action))));
private IEnumerable<Transition> CreateTransitions(StateViewModel state)
{
var transitions = StateMachineViewModel.Transitions.Where(t => t.Source == state).ToList();
var result = new List<Transition>(transitions.Count);
for (int i = 0; i < transitions.Count; i++)
{
var transition = transitions[i];
var tr = new Transition(transition.Source.Id, transition.Target.Id, CreateCondition(transition.Condition));
result.Add(new DebugTransitionDecorator(tr));
}
return result;
}
private IBlackboardCondition? CreateCondition(BlackboardItemViewModel? condition)
{
if (condition?.Type != null && typeof(IBlackboardCondition).IsAssignableFrom(condition.Type))
{
// TODO: DI Container
var result = (IBlackboardCondition?)Activator.CreateInstance(condition.Type);
InitializeKeys(condition.Input, result, condition.Type);
return result;
}
return default;
}
private IBlackboardAction? CreateAction(BlackboardItemViewModel? action)
{
if (action?.Type != null && typeof(IBlackboardAction).IsAssignableFrom(action.Type))
{
// TODO: DI Container
var result = (IBlackboardAction?)Activator.CreateInstance(action.Type);
InitializeKeys(action.Input, result, action.Type);
InitializeKeys(action.Output, result, action.Type);
return result;
}
return default;
}
private void InitializeKeys(NodifyObservableCollection<BlackboardKeyViewModel> keys, object? instance, Type type)
{
for (int i = 0; i < keys.Count; i++)
{
var vm = keys[i];
var key = CreateActionValue(vm);
// TODO: Property cache
if (vm.PropertyName != null)
{
var prop = type.GetProperty(vm.PropertyName);
if (prop?.CanWrite ?? false)
{
prop.SetValue(instance, key);
}
}
}
}
private Blackboard CreateBlackboard(BlackboardViewModel blackboard)
{
Blackboard result = new Blackboard();
for (int i = 0; i < blackboard.Keys.Count; i++)
{
var key = blackboard.Keys[i];
if (!string.IsNullOrWhiteSpace(key.Name))
{
result.Set(new BlackboardKey(key.Name, key.Type), key.Value);
}
}
result.CopyTo(_original);
_debugger.Attach(result);
return _debugger;
}
private BlackboardProperty CreateActionValue(BlackboardKeyViewModel key)
{
if (key.Value is BlackboardKeyViewModel bkv)
{
return new BlackboardProperty(new BlackboardKey(bkv.Name, bkv.Type));
}
return new BlackboardProperty(key.Value);
}
#endregion
}
}