Beispiel #1
0
        static void Main(string[] args)
        {
            (int xBound, int yBound) = (100, 100);

            bool[][] matrix = ConstructMatrix(xBound, yBound, seed: 203);

            List <string> states = CreateStates(xBound, yBound, matrix);

            IStateMachine machine = CreateRoverMachine(matrix, states, "0,0,N");

            System.Console.WriteLine("Enter f, b, r, or l to navigate the plateau.");

            while (true)
            {
                var input = System.Console.ReadLine().ToLowerInvariant();

                if (string.Equals(input, "exit", StringComparison.OrdinalIgnoreCase))
                {
                    break;
                }

                if (string.IsNullOrWhiteSpace(input))
                {
                    continue;
                }

                try
                {
                    machine.FireAsync(input, null, CancellationToken.None).Wait();
                }
                catch (InvalidOperationException invalidException)
                {
                    System.Console.WriteLine(invalidException.Message);
                }
                catch (AggregateException exception)
                    when(exception.InnerException is InvalidOperationException invalidException)
                    {
                        System.Console.WriteLine(invalidException.Message);
                    }
            }
        }
Beispiel #2
0
        private void FireTrigger() =>
        Post("/{MachineId}/fire/{TriggerName}", async(parameters, ct) =>
        {
            var stateEngine = StateEngineFactory
                              .GetStateEngine(Context.CurrentUser?.GetApiKey());

            string machineId   = parameters.MachineId;
            string triggerName = parameters.TriggerName;
            var payload        = Request.Body.AsString();

            string contentType = null;
            if (Request.Headers.Keys.Contains("Content-Type"))
            {
                contentType = Request.Headers.ContentType;
            }

            var commitTagHeaders = Request.Headers.Where(h => h.Key == "X-REstate-CommitTag");

            var commitTagString = commitTagHeaders.Any() ? commitTagHeaders.First().Value.FirstOrDefault() : null;

            Guid commitTagGuid;
            Guid?commitTag = null;
            if (Guid.TryParse(commitTagString, out commitTagGuid))
            {
                commitTag = commitTagGuid;
            }

            InstanceRecord instanceRecord;

            IStateMachine machine = await stateEngine
                                    .GetMachine(machineId, ct);

            State resultantState;
            try
            {
                resultantState = await machine.FireAsync(
                    new Trigger(triggerName),
                    contentType,
                    payload,
                    commitTag,
                    ct);
            }
            catch (InvalidOperationException ex)
            {
                return(Negotiate
                       .WithStatusCode(400)
                       .WithReasonPhrase(ex.Message)
                       .WithModel(new ReasonPhraseResponse {
                    ReasonPhrase = ex.Message
                }));
            }
            catch (StateConflictException ex)
            {
                return(Negotiate
                       .WithStatusCode(409)
                       .WithReasonPhrase(ex.Message)
                       .WithModel(new ReasonPhraseResponse {
                    ReasonPhrase = ex.Message
                }));
            }
            catch (AggregateException ex)
                when(ex.InnerExceptions.First().GetType() == typeof(StateConflictException))
                {
                    return(Negotiate
                           .WithStatusCode(409)
                           .WithReasonPhrase(ex.InnerExceptions.First().Message)
                           .WithModel(new ReasonPhraseResponse {
                        ReasonPhrase = ex.InnerExceptions.First().Message
                    }));
                }

            instanceRecord = await stateEngine.GetMachineInfoAsync(machineId, ct);

            return(Negotiate
                   .WithModel(instanceRecord)
                   .WithAllowedMediaRange("application/json"));
        });