File: ActivityStateRecord.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (349 lines) | stat: -rw-r--r-- 12,696 bytes parent folder | download | duplicates (7)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities.Tracking
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Runtime;
    using System.Runtime.Serialization;
    using System.Collections;
    using System.Reflection;
    using System.Runtime.Diagnostics;

    [Fx.Tag.XamlVisible(false)]
    [DataContract]
    public sealed class ActivityStateRecord : TrackingRecord
    {
        IDictionary<string, object> variables;
        IDictionary<string, object> arguments;
        ActivityInfo activity;
        string state;

        static ReadOnlyCollection<string> wildcardCollection = new ReadOnlyCollection<string>(new List<string>(1) { "*" });

        internal ActivityStateRecord(Guid instanceId, ActivityInstance instance, ActivityInstanceState state)
            : this(instanceId, new ActivityInfo(instance), state)
        {
        }

        internal ActivityStateRecord(Guid instanceId, ActivityInfo activity, ActivityInstanceState state)
            : base(instanceId)
        {
            this.Activity = activity;

            switch (state)
            {
                case ActivityInstanceState.Executing:
                    this.State = ActivityStates.Executing;
                    break;
                case ActivityInstanceState.Closed:
                    this.State = ActivityStates.Closed;
                    break;
                case ActivityInstanceState.Canceled:
                    this.State = ActivityStates.Canceled;
                    break;
                case ActivityInstanceState.Faulted:
                    this.State = ActivityStates.Faulted;
                    break;
                default:
                    throw Fx.AssertAndThrow("Invalid state value");
            }
        }

        public ActivityStateRecord(
            Guid instanceId,
            long recordNumber,
            ActivityInfo activity,
            string state)
            : base(instanceId, recordNumber)
        {
            if (activity == null)
            {
                throw FxTrace.Exception.ArgumentNull("activity");
            }
            if (string.IsNullOrEmpty(state))
            {
                throw FxTrace.Exception.ArgumentNullOrEmpty("state");
            }

            this.Activity = activity;
            this.State = state;
        }

        ActivityStateRecord(ActivityStateRecord record)
            : base(record)
        {
            this.Activity = record.Activity;
            this.State = record.State;
            if (record.variables != null)
            {
                if (record.variables == ActivityUtilities.EmptyParameters)
                {
                    this.variables = ActivityUtilities.EmptyParameters;
                }
                else
                {
                    this.variables = new Dictionary<string, object>(record.variables);
                }
            }

            if (record.arguments != null)
            {
                if (record.arguments == ActivityUtilities.EmptyParameters)
                {
                    this.arguments = ActivityUtilities.EmptyParameters;
                }
                else
                {
                    this.arguments = new Dictionary<string, object>(record.arguments);
                }
            }
        }

        
        public ActivityInfo Activity
        {
            get
            {
                return this.activity;
            }
            private set
            {
                this.activity = value;
            }
        }
        
        public string State
        {
            get
            {
                return this.state;
            }
            private set
            {
                this.state = value;
            }
        }

        public IDictionary<string, object> Variables
        {
            get
            {
                if (this.variables == null)
                {
                    this.variables = GetVariables(wildcardCollection);
                    Fx.Assert(this.variables.IsReadOnly, "only readonly dictionary can be set for variables");
                }
                return this.variables;
            }

            internal set
            {
                Fx.Assert(value.IsReadOnly, "only readonly dictionary can be set for variables");
                this.variables = value;
            }
        }

        public IDictionary<string, object> Arguments
        {
            get
            {
                if (this.arguments == null)
                {
                    this.arguments = GetArguments(wildcardCollection);
                    Fx.Assert(this.arguments.IsReadOnly, "only readonly dictionary can be set for arguments");
                }
                return this.arguments;
            }

            internal set
            {
                Fx.Assert(value.IsReadOnly, "only readonly dictionary can be set for arguments");
                this.arguments = value;
            }
        }

        [DataMember(EmitDefaultValue = false, Name = "variables")]
        internal IDictionary<string, object> SerializedVariables
        {
            get { return this.variables; }
            set { this.variables = value; }
        }

        [DataMember(EmitDefaultValue = false, Name = "arguments")]
        internal IDictionary<string, object> SerializedArguments
        {
            get { return this.arguments; }
            set { this.arguments = value; }
        }

        [DataMember(Name = "Activity")]
        internal ActivityInfo SerializedActivity
        {
            get { return this.Activity; }
            set { this.Activity = value; }
        }

        [DataMember(Name = "State")]
        internal string SerializedState
        {
            get { return this.State; }
            set { this.State = value; }
        }

        protected internal override TrackingRecord Clone()
        {
            return new ActivityStateRecord(this);
        }


        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentCulture,
               "ActivityStateRecord {{ {0}, Activity {{ {1} }}, State = {2} }}",
                base.ToString(),
                this.Activity.ToString(),
                this.State);
        }

        internal IDictionary<string, object> GetVariables(ICollection<string> variables)
        {
            Dictionary<string, object> trackedVariables = null; // delay allocated through TrackData
            ActivityInstance currentInstance = this.Activity.Instance;

            if (currentInstance != null)
            {
                Activity currentElement = currentInstance.Activity;
                Activity startActivity = currentInstance.Activity;
                bool containsWildcard = variables.Contains("*");
                //count defines how many items we can get in this lookup. It represents the maximum number of items that can be extracted, 
                //if * is specified, any other names specified are expected to be variables defined in scope, not in the activity itself. 
                //if a variable name in the activity is specified, the lookup continues through the variables in scope. 
                int count = containsWildcard ? currentElement.RuntimeVariables.Count + variables.Count - 1 : variables.Count;

                IdSpace activityIdSpace = currentElement.MemberOf;

                while (currentInstance != null)
                {
                    //* only extracts variables of the current Activity and not variables in scope. 
                    bool useWildCard = containsWildcard && startActivity == currentElement;

                    // we only track public Variables, not ImplementationVariables
                    for (int i = 0; i < currentElement.RuntimeVariables.Count; i++)
                    {
                        Variable variable = currentElement.RuntimeVariables[i];
                        if (TrackData(variable.Name, variable.Id, currentInstance, variables, useWildCard, ref trackedVariables))
                        {
                            if (trackedVariables.Count == count)
                            {
                                return new ReadOnlyDictionaryInternal<string, object>(trackedVariables);
                            }
                        }
                    }

                    bool foundNext = false;

                    while (!foundNext)
                    {
                        currentInstance = currentInstance.Parent;

                        if (currentInstance != null)
                        {
                            currentElement = currentInstance.Activity;
                            foundNext = currentElement.MemberOf.Equals(activityIdSpace);
                        }
                        else
                        {
                            // We set foundNext to true to get out of our loop.
                            foundNext = true;
                        }
                    }
                }
            }

            if (trackedVariables == null)
            {
                return ActivityUtilities.EmptyParameters;
            }
            else
            {
                Fx.Assert(trackedVariables.Count > 0, "we should only allocate the dictionary if we're putting data in it");
                return new ReadOnlyDictionaryInternal<string, object>(trackedVariables);
            }
        }

        internal IDictionary<string, object> GetArguments(ICollection<string> arguments)
        {
            Dictionary<string, object> trackedArguments = null; // delay allocated through TrackData
            ActivityInstance currentInstance = this.Activity.Instance;

            if (currentInstance != null)
            {
                Activity currentElement = currentInstance.Activity;
                bool containsWildcard = arguments.Contains("*");
                int count = containsWildcard ? currentElement.RuntimeArguments.Count : arguments.Count;
                bool isActivityStateExecuting = ActivityStates.Executing.Equals(this.State, StringComparison.Ordinal);

                //look at arguments for this element. 
                for (int i = 0; i < currentElement.RuntimeArguments.Count; i++)
                {
                    RuntimeArgument argument = currentElement.RuntimeArguments[i];

                    // OutArguments will always start with default(T), so there is no need to track them when state == Executing
                    if (isActivityStateExecuting && argument.Direction == ArgumentDirection.Out)
                    {
                        continue;
                    }

                    if (TrackData(argument.Name, argument.Id, currentInstance, arguments, containsWildcard, ref trackedArguments))
                    {
                        if (trackedArguments.Count == count)
                        {
                            break;
                        }
                    }
                }
            }

            if (trackedArguments == null)
            {
                return ActivityUtilities.EmptyParameters;
            }
            else
            {
                Fx.Assert(trackedArguments.Count > 0, "we should only allocate the dictionary if we're putting data in it");
                return new ReadOnlyDictionaryInternal<string, object>(trackedArguments);
            }
        }


        bool TrackData(string name, int id, ActivityInstance currentInstance, ICollection<string> data, bool wildcard, ref Dictionary<string, object> trackedData)
        {
            if (wildcard || data.Contains(name))
            {
                Location location = currentInstance.Environment.GetSpecificLocation(id);
                if (location != null)
                {
                    if (trackedData == null)
                    {
                        trackedData = new Dictionary<string, object>(10);
                    }

                    string dataName = name ?? NameGenerator.Next();
                    trackedData[dataName] = location.Value;
                    if (TD.TrackingDataExtractedIsEnabled())
                    {
                        TD.TrackingDataExtracted(dataName, this.Activity.Name);
                    }

                    return true;
                }
            }
            return false;
        }
    }
}