public YAWLChatHandler(YAWLAgent agent, YAWLWorkflowProvider yawlWorkflowProvider) { _yawlAgent = agent; _yawlProvider = yawlWorkflowProvider; }
private void ReadMessages() { char[] sep = { ' ' }; while (_externalProcessor != null && _externalProcessor.Connected) { byte[] bytes = new byte[16384]; try { while (_externalProcessor != null && _externalProcessor.Available == 0) { Thread.Sleep(16); } if (_externalProcessor != null && _externalProcessor.Available > 0) { _externalProcessor.Receive(bytes); string[] strings = Encoding.GetString(bytes).Split('\n'); foreach (string xi in strings) { string x = xi.Replace("\r", ""); System.Diagnostics.Debug.WriteLine("RECV: " + x); Console.WriteLine(x); String action = x.Split(' ')[0]; int totalParams = x.Split(' ').Length; try { // Try - catch to process string processing errors, to not break the flow of the program // If a new active (bot-based) agent needs to be created, notify someone of this if (action == "ACTIVEAGENT" && totalParams == 4) { String first = x.Split(sep)[1]; String last = x.Split(sep)[2]; String yawlID = x.Split(sep)[3]; if (AllParticipants.ContainsKey(yawlID)) { if (!YawlToWorker.ContainsKey(yawlID)) { Guid newParticipant = Guid.NewGuid(); YawlToWorker.Add(yawlID, newParticipant.ToString()); WorkerToYawl.Add(newParticipant.ToString(), yawlID); // npcs[yawlid2uuid[yawlID]].SetYAWLReferences(this, AllYawlParticipants[yawlID]); OnParticipantCreated(new ParticipantEventArgs { FirstName = first, LastName = last, Id = newParticipant, WorkAgent = AllParticipants[yawlID], WorkflowProvider = this }); } } else { throw new Exception("No Agent Exists with this ID!"); } } // A new non-bot agent needs to be created. else if (action == "AGENT" && totalParams == 4) { String yawlID = x.Split(sep)[3]; String first = x.Split(sep)[1]; String last = x.Split(sep)[2]; if (!AllParticipants.ContainsKey(yawlID)) { YAWLAgent agent = new YAWLAgent { YawlId = yawlID }; AllParticipants.Add(yawlID, agent); } AllParticipants[yawlID].FirstName = first; AllParticipants[yawlID].LastName = last; } // There is a role being applied to an agent else if (action == "AGENTROLE" && totalParams >= 3) { String yawlID = x.Split(sep)[1]; String role = x.Split(sep, 3)[2]; if (!AllParticipants.ContainsKey(yawlID)) { YAWLAgent agent = new YAWLAgent(); agent.YawlId = yawlID; AllParticipants.Add(yawlID, agent); } AllParticipants[yawlID].Appearance = role; AllParticipants[yawlID].AddRole(role); } // There is a capabality being applied to an agent else if (action == "AGENTCAPABILITY" && totalParams >= 3) { String yawlID = x.Split(sep)[1]; String capability = x.Split(sep, 3)[2]; if (!AllParticipants.ContainsKey(yawlID)) { YAWLAgent agent = new YAWLAgent(); agent.YawlId = yawlID; AllParticipants.Add(yawlID, agent); } AllParticipants[yawlID].AddCapability(capability); } // A workitem is being added to a queue else if (action == "WORKITEM" && totalParams >= 7) { String currentQueue = x.Split(sep)[1]; String yawlID = x.Split(sep)[2]; String taskID = x.Split(sep)[3]; String tasks = x.Split(sep)[4]; String taskName = x.Split(sep)[5]; String goals = x.Split(sep)[6]; WorkItem work; if (!AllWorkItems.ContainsKey(taskID)) { work = new WorkItem(); work.taskID = taskID; AllWorkItems.Add(taskID, work); } else { work = AllWorkItems[taskID]; if (AllParticipants.ContainsKey(work.participant)) ((YAWLAgent)AllParticipants[work.participant]).GetQueueById(currentQueue).Remove(work); } work.participant = yawlID; work.taskVariables.Add("Tasks", tasks); work.taskVariables.Add("Goals", goals); work.taskName = taskName; work.taskQueue = currentQueue; // Add work to queue if work item doesnt exist in the queue already if (AllParticipants.ContainsKey(yawlID) && DoesNotContainWorkItem(yawlID, currentQueue, work)) { ((YAWLAgent)AllParticipants[yawlID]).AddToQueue(currentQueue, work); } // Add the work if it has not been completed already if (currentQueue == WorkAgent.STARTED && DoesNotContainWorkItem(yawlID, WorkAgent.COMPLETED, work)) { Workers[YawlToWorker[yawlID]].AddWork(work); } } // A work item name is being applied to a work item else if (action == "WORKITEMNAME" && totalParams >= 3) { String taskID = x.Split(sep)[1]; String taskName = x.Split(sep, 3)[2]; if (!AllWorkItems.ContainsKey(taskID)) { WorkItem work = new WorkItem(); work.taskID = taskID; AllWorkItems.Add(taskID, work); } AllWorkItems[taskID].taskName = taskName; } // A work item is being signalled to end else if (action == "TASKEND" && totalParams == 3) { String yawlID = x.Split(sep)[1]; String taskID = x.Split(sep)[2]; if (AllWorkItems.ContainsKey(taskID) && AllParticipants.ContainsKey(yawlID)) { WorkItem work = AllWorkItems[taskID]; if (work.participant == yawlID) { ((YAWLAgent)AllParticipants[yawlID]).GetQueueById(work.taskQueue).Remove(work); if (work.taskQueue == WorkAgent.STARTED) { Workers[YawlToWorker[yawlID]].StopTaskIfStarted(work); } AllWorkItems.Remove(taskID); } } } // A work item is being signalled to be suspended else if (action == "SUSPEND" && totalParams == 3) { String yawlID = x.Split(sep)[1]; String taskID = x.Split(sep)[2]; if (AllWorkItems.ContainsKey(taskID) && AllParticipants.ContainsKey(yawlID)) { WorkItem work = AllWorkItems[taskID]; if (work.participant == yawlID) { ((YAWLAgent)AllParticipants[yawlID]).GetQueueById(work.taskQueue).Remove(work); if (work.taskQueue == WorkAgent.STARTED) { Workers[YawlToWorker[yawlID]].StopTaskIfStarted(work); } ((YAWLAgent)AllParticipants[yawlID]).GetQueueById(WorkAgent.SUSPENDED).Add(work); } } } // A work item is being signalled to be unsuspended else if (action == "UNSUSPEND" && totalParams == 3) { String yawlID = x.Split(sep)[1]; String taskID = x.Split(sep)[2]; if (AllWorkItems.ContainsKey(taskID) && AllParticipants.ContainsKey(yawlID)) { WorkItem work = AllWorkItems[taskID]; if (work.participant == yawlID) { ((YAWLAgent)AllParticipants[yawlID]).GetQueueById(work.taskQueue).Remove(work); ((YAWLAgent)AllParticipants[yawlID]).GetQueueById(WorkAgent.STARTED).Add(work); Workers[YawlToWorker[yawlID]].AddWork(work); Workers[YawlToWorker[yawlID]].AddWork(work); } } } // A new case has been launched else if (action == "CASE" && totalParams == 3) { String caseId = x.Split(sep)[1]; String specificationId = x.Split(sep)[2]; String specificationName = string.Empty; if (!StartedCases.Any(c => c.CaseId.Equals(caseId))) { // Find the name of the specification if (AllSpecifications.ContainsKey(specificationId)) { specificationName = AllSpecifications[specificationId]; } // Add it to the list of started cases and "begin" the simulation // with a SyncAll() call StartedCases.Add(new Case { CaseId = caseId, SpecificationId = specificationId, SpecificationName = specificationName}); Thread.Sleep(1000); OnCaseStateChanged(new CaseStateEventArgs { State = CaseState.STARTED, CaseID = caseId, SpecificationID = specificationId }); SyncAll(); // TODO: do this in the event handler listener } } else if (action == "CASEEND" && totalParams == 3) { String caseId = x.Split(sep)[1]; String specificationId = x.Split(sep)[2]; if (StartedCases.Any(c => c.CaseId.Equals(caseId))) { StartedCases.Remove(StartedCases.FirstOrDefault(c => c.CaseId.Equals(caseId))); OnCaseStateChanged(new CaseStateEventArgs { State = CaseState.COMPLETED, CaseID = caseId, SpecificationID = specificationId }); } } else if (action == "SPECIFICATION" && totalParams == 3) { String specificationId = x.Split(sep)[1]; String specificationName = x.Split(sep)[2]; if (!AllSpecifications.ContainsKey(specificationId)) { AllSpecifications.Add(specificationId, specificationName); } } else if (x.Length > 1) { // Console.WriteLine("Unknown command " + x); } } catch (KeyNotFoundException e) { System.Diagnostics.Debug.WriteLine(e.Message); } } } } catch (Exception e) { //Do nothing System.Diagnostics.Debug.WriteLine(e.Message); } } }