5 using System.Collections.Generic;
6 using System.Diagnostics;
8 using System.Runtime.Serialization;
9 using System.Threading;
10 using System.Threading.Tasks;
12 namespace SharpLib.Ear
15 [AttributeObject(Id = "Action", Name = "Action Group", Description = "Use it to group other actions together.")]
16 public class Action: Object
19 [AttributeObjectProperty
21 Id = "Action.Iterations",
23 Description = "Specifies the number of time this action should execute.",
29 public int Iterations { get; set; } = 1;
36 get { return Iterations > 0; }
40 public override bool IsValid()
42 // We don't want to override this behaviour for derived classes
43 if (GetType() == typeof(Action))
45 // Avoid having empty actions with no name
46 return !string.IsNullOrEmpty(Name);
49 return base.IsValid();
54 /// Basic action just does nothing
56 /// <returns></returns>
57 protected virtual async Task DoExecute()
63 /// Allows testing from generic edit dialog.
67 Trace.WriteLine("Action test");
72 /// Execute our action N times.
74 /// <returns></returns>
75 public async Task Execute()
79 Trace.WriteLine("EAR: Action.Execute: WARNING: Action invalid, aborting execution: " + Brief());
85 Trace.WriteLine("EAR: Action.Execute: Action disabled: " + Brief());
89 for (int i = Iterations; i > 0; i--)
91 Trace.WriteLine($"EAR: Action.Execute: [{Iterations - i + 1}/{Iterations}] - {BriefBase()}");
93 //We first execute ourselves
97 foreach (Action a in Objects.OfType<Action>())
105 /// For inherited classes to override.
107 /// <returns></returns>
108 public virtual string BriefBase()
110 return string.IsNullOrEmpty(Name)? AttributeName : Name;
114 /// Dynamic object description.
116 /// <returns></returns>
117 public sealed override string Brief()
119 return Iterations > 1 ? $"{Iterations} x " + BriefBase():BriefBase();