aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/ProjectRunner.cs
blob: 51863079285296055b5e957dc802033e5aff728a (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Scripting.Basic;

namespace Tango.FSE.Procedures
{
    public class ProjectRunner : ExtendedObject
    {
        private ProjectSession<ProcedureContext> _currentSession;

        public event EventHandler<ProjectRunnerState> StateChanged;

        private ProjectRunnerState _state;
        public ProjectRunnerState State
        {
            get { return _state; }
            private set
            {
                _state = value;
                RaisePropertyChangedAuto();
                RaisePropertyChanged(nameof(IsRunning));
                RaisePropertyChanged(nameof(IsCompiling));
                RaisePropertyChanged(nameof(CanRun));
                RaisePropertyChanged(nameof(CanCompile));

                StateChanged?.Invoke(this, value);
            }
        }

        public bool IsRunning
        {
            get { return State == ProjectRunnerState.Running; }
        }

        public bool IsCompiling
        {
            get { return State == ProjectRunnerState.Compiling; }
        }

        public bool CanCompile
        {
            get { return State != ProjectRunnerState.Running && State != ProjectRunnerState.Compiling; }
        }

        public bool CanRun
        {
            get { return State != ProjectRunnerState.Running && State != ProjectRunnerState.Compiling; }
        }

        public ProcedureProject Project { get; private set; }

        public ProjectRunner(ProcedureProject project)
        {
            Project = project;
        }

        public async Task<CompilationResult> Compile()
        {
            try
            {
                State = ProjectRunnerState.Compiling;

                if (Project.Inputs.GroupBy(x => x.Key).Any(x => x.Count() > 1))
                {
                    throw new InvalidOperationException("Project inputs contain duplicate keys.");
                }

                var result = await Project.Compile();
                State = ProjectRunnerState.None;
                return result;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                State = ProjectRunnerState.None;
            }
        }

        public async Task Run(ProcedureContext context)
        {
            if (!CanRun)
            {
                throw new InvalidOperationException("Project is already running.");
            }

            try
            {
                State = ProjectRunnerState.Compiling;
                _currentSession = await Project.Run(context);
            }
            catch (Exception ex)
            {
                State = ProjectRunnerState.None;
                throw ex;
            }

            State = ProjectRunnerState.Running;

            try
            {
                var result = await _currentSession.WaitForCompletion();
                State = ProjectRunnerState.Completed;
            }
            catch (OperationCanceledException)
            {
                State = ProjectRunnerState.Aborted;
                throw;
            }
            catch (Exception)
            {
                State = ProjectRunnerState.Failed;
                throw;
            }
        }

        public void Stop()
        {
            _currentSession?.Abort();
            State = ProjectRunnerState.Aborted;
        }
    }
}