SharpLibEar/Action.cs
author StephaneLenclud
Wed, 31 Aug 2016 20:20:32 +0200
changeset 265 82e87f4956ea
parent 264 4a08e1b7ba64
child 266 b11d7ebbdc7f
permissions -rw-r--r--
Actions can now have children.
     1 //
     2 
     3 
     4 using System;
     5 using System.Collections.Generic;
     6 using System.Diagnostics;
     7 using System.Linq;
     8 using System.Runtime.Serialization;
     9 using System.Threading;
    10 using System.Threading.Tasks;
    11 
    12 namespace SharpLib.Ear
    13 {
    14     [DataContract]
    15     [AttributeObject(Id = "Action", Name = "Action", Description = "An empty action.")]
    16     public class Action: Object
    17     {
    18         [DataMember]
    19         [AttributeObjectProperty
    20             (
    21             Id = "Action.Iterations",
    22             Name = "Iterations",
    23             Description = "Specifies the number of time this action should execute.",
    24             Minimum = "0",
    25             Maximum = "10000",
    26             Increment = "1"
    27             )
    28         ]
    29         public int Iterations { get; set; } = 1;
    30 
    31         /// <summary>
    32         /// 
    33         /// </summary>
    34         public bool Enabled
    35         {
    36             get { return Iterations > 0; }
    37         }
    38 
    39         /// <summary>
    40         /// Basic action just does nothing
    41         /// </summary>
    42         /// <returns></returns>
    43         protected virtual async Task DoExecute()
    44         {
    45             
    46         }
    47 
    48         /// <summary>
    49         /// Allows testing from generic edit dialog.
    50         /// </summary>
    51         public void Test()
    52         {
    53             Trace.WriteLine("Action test");
    54             Execute();
    55         }
    56 
    57         /// <summary>
    58         /// Execute our action N times.
    59         /// </summary>
    60         /// <returns></returns>
    61         public async Task Execute()
    62         {
    63             if (!IsValid())
    64             {
    65                 Trace.WriteLine("EAR: Action.Execute: WARNING: Action invalid, aborting execution: " + Brief());
    66                 return;
    67             }
    68 
    69             if (!Enabled)
    70             {
    71                 Trace.WriteLine("EAR: Action.Execute: Action disabled: " + Brief());
    72                 return;
    73             }
    74 
    75             for (int i = Iterations; i > 0; i--)
    76             {
    77                 Trace.WriteLine($"EAR: Action.Execute: [{Iterations - i + 1}/{Iterations}] - {BriefBase()}");
    78                 //For each iteration
    79                 //We first execute ourselves
    80                 await DoExecute();
    81 
    82                 //Then our children
    83                 foreach (Action a in Objects.OfType<Action>())
    84                 {
    85                     await a.Execute();
    86                 }
    87             }            
    88         }
    89 
    90         /// <summary>
    91         /// For inherited classes to override.
    92         /// </summary>
    93         /// <returns></returns>
    94         public virtual string BriefBase()
    95         {
    96             return Name;
    97         }
    98 
    99         /// <summary>
   100         /// Dynamic object description.
   101         /// </summary>
   102         /// <returns></returns>
   103         public sealed override string Brief()
   104         {
   105             return Iterations > 1 ? $"{Iterations} x " + BriefBase():BriefBase();
   106         }
   107 
   108 
   109 
   110 
   111 
   112 
   113     }
   114 
   115 
   116 }