static bool EvaluateWorkflowIsReadyForTransition(GenericExampleModel model) { var readyForTransition = false; var status = JsonConvert.DeserializeObject <JToken>(model.Status); var state = (WorkflowState)status["state"].ToObject <int>(); switch (state) { case WorkflowState.ToDo: // all "preliminary" tasks must be completed before being considered "in progress" readyForTransition = model.Tasks.Where(t => t.Variety == GenericExampleTaskVariety.Preliminary).All(t => t.Completed); break; case WorkflowState.Doing: // all "communicative" tasks must be completed readyForTransition = model.Tasks.Where(t => t.Variety == GenericExampleTaskVariety.Communicative).All(t => t.Completed); break; case WorkflowState.Waiting: // all "administrative" tasks must be completed readyForTransition = model.Tasks.Where(t => t.Variety == GenericExampleTaskVariety.Administrative).All(t => t.Completed); break; } return(readyForTransition); }
static void ProcessModel(GenericExampleModel model, bool pauseExecution = false) { var nodes = DeserializeNodes(); var currentNode = JsonConvert.DeserializeObject <JToken>(model.Status); var matchingCurrent = nodes.Find(n => JToken.DeepEquals(currentNode, n)); var indexOfCurrent = nodes.IndexOf(matchingCurrent); var nextNode = nodes[indexOfCurrent + 1]; if (JToken.DeepEquals(nextNode["state"], currentNode["state"])) { model.Status = nextNode.ToString(); model.StatusDisplay = nextNode["statusName"].ToString(); } else { var ready = EvaluateWorkflowIsReadyForTransition(model); if (ready) { var success = PopNextWorkflow((WorkflowState)currentNode["state"].ToObject <int>(), pauseExecution); if (success) { model.Status = nextNode.ToString(); model.StatusDisplay = nextNode["statusName"].ToString(); } } } Console.WriteLine($"current status: {model.StatusDisplay}"); }
static void Main(string[] args) { var model = new GenericExampleModel(); InitModel(model); Console.WriteLine("initiating sequence:"); Console.WriteLine($"current status: {model.StatusDisplay}"); Console.ReadLine(); // assign model ProcessModel(model); Console.ReadLine(); // commence work ProcessModel(model); Console.ReadLine(); // waiting on 3rd party ProcessModel(model, true); Console.ReadLine(); // compiling results ProcessModel(model); Console.ReadLine(); // completion ProcessModel(model); Console.ReadLine(); Console.WriteLine("sequence completed."); Console.ReadLine(); }
static void InitModel(GenericExampleModel model) { model.Id = 1; model.Tasks = new List <GenericExampleTaskModel>(); InitTasks(model.Tasks); // intitialize status for model var workflowNodes = DeserializeNodes(); if (workflowNodes.Any()) { var initialStatus = workflowNodes.First(); model.StatusDisplay = initialStatus["statusName"].ToString(); // storing the whole workflow node as the status model.Status = initialStatus.ToString(); } }