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