protected override bool LocalExit(AgentContext context) { if (mSelectedTask != NONE && !mTasks[mSelectedTask].Exit(context)) // Current task is still // exiting. return false; mSelectedTask = NONE; return true; }
/// <summary> /// Processes the task based on its current state. /// </summary> /// <remarks> /// <para> /// Only performs a single action per call. So it will always take multiple calls for a /// task to be fully processed. /// </para> /// <para> /// The <paramref name="failure"/> flag indicates whether the task is in a failed state. /// (Note: It may still need processing.) /// </para> /// </remarks> /// <param name="task">The behaivor to process.</param> /// <param name="failure">True if the task is detected in a failed state.</param> /// <returns>True if the task needs more processing.</returns> public static bool Process(AgentContext context, ITask task, out bool failure) { failure = false; switch (task.State) { case TaskState.Active: task.Update(context); return true; case TaskState.Complete: return !task.Exit(context); case TaskState.Exiting: return !task.Exit(context); case TaskState.Failed: failure = true; return !task.Exit(context); case TaskState.Inactive: task.Update(context); return true; } return true; }
public Task <Result> Remove(int agencyId, int policyId, AgentContext agent) { return(GetAgencyPolicy(agencyId, policyId) .Map(DeletePolicy) .Tap(WriteAuditLog) .Bind(UpdateDisplayedMarkupFormula)); async Task <MarkupPolicy> DeletePolicy(MarkupPolicy policy) { _context.Remove(policy); await _context.SaveChangesAsync(); return(policy); } Task WriteAuditLog(MarkupPolicy policy) => _markupPolicyAuditService.Write(MarkupPolicyEventType.AgencyMarkupDeleted, new AgencyMarkupPolicyData(policy.Id, int.Parse(policy.SubjectScopeId)), agent.ToApiCaller()); }
public Task <AgentContext <object> > Run(object state, AgentCapability self, object message) { var context = new AgentContext <object>(state, self); if (message is AgentRootInitMessage rootInitMessage) { var cap = context.IssueCapability(new[] { typeof(PingPongMessage) }); context.CreateAgent("AgentTwoId", "AgentTwo", new PingPongMessage { AgentOne = cap }, null); } else if (message is PingPongMessage pingPongMessage) { context.SendMessage(pingPongMessage.AgentTwo, new PingPongMessage { AgentOne = pingPongMessage.AgentOne, AgentTwo = pingPongMessage.AgentTwo, Content = "Ping" }, null); } return(Task.FromResult(context)); }
public Task <Result> Remove(int policyId, AgentContext agent) { return(GetPolicy() .Bind(CheckPermissions) .Bind(DeletePolicy)); async Task <Result <MarkupPolicy> > GetPolicy() { var policy = await _context.MarkupPolicies.SingleOrDefaultAsync(p => p.Id == policyId); return(policy == null ? Result.Failure <MarkupPolicy>("Could not find policy") : Result.Success(policy)); } async Task <Result <MarkupPolicy> > CheckPermissions(MarkupPolicy policy) { var scopeType = policy.ScopeType; var scope = new MarkupPolicyScope(scopeType, policy.CounterpartyId ?? policy.AgencyId ?? policy.AgentId); var(_, isFailure, error) = await CheckUserManagePermissions(scope, agent); return(isFailure ? Result.Failure <MarkupPolicy>(error) : Result.Success(policy)); } async Task <Result> DeletePolicy(MarkupPolicy policy) { _context.Remove(policy); await _context.SaveChangesAsync(); return(Result.Success()); } }
public Task <Result> SendVoucher(int bookingId, string email, AgentContext agent, string languageCode) { return(_bookingDocumentsService.GenerateVoucher(bookingId, agent, languageCode) .Bind(voucher => { var voucherData = new VoucherData { Accommodation = voucher.Accommodation, AgentName = voucher.AgentName, BookingId = voucher.BookingId, DeadlineDate = DateTimeFormatters.ToDateString(voucher.DeadlineDate), NightCount = voucher.NightCount, ReferenceCode = voucher.ReferenceCode, RoomDetails = voucher.RoomDetails, CheckInDate = DateTimeFormatters.ToDateString(voucher.CheckInDate), CheckOutDate = DateTimeFormatters.ToDateString(voucher.CheckOutDate), MainPassengerName = voucher.MainPassengerName, BannerUrl = voucher.BannerUrl, LogoUrl = voucher.LogoUrl }; return SendEmail(email, _options.VoucherTemplateId, voucherData); })); }
/// <summary> /// Processes the task based on its current state, optimized to complete the task more /// quickly. /// </summary> /// <remarks> /// <para> /// The exit process will be triggered as soon as completion is dectected. So this /// method may perform two operations (update and exit) in a single call. /// </para> /// </remarks> /// <param name="task">The task to process.</param> /// <returns>True if the task needs more processing.</returns> public static bool ProcessImmediate(AgentContext context, ITask task) { switch (task.State) { case TaskState.Active: if (IsFinished(task.Update(context))) return !task.Exit(context); return true; case TaskState.Complete: return !task.Exit(context); case TaskState.Exiting: return !task.Exit(context); case TaskState.Failed: return !task.Exit(context); case TaskState.Inactive: if (IsFinished(task.Update(context))) return !task.Exit(context); return true; } return true; }
public bool EvaluateCondition(AgentContext context) { return true; }
protected override void LocalUpdate(AgentContext context) { if (state == TaskState.Inactive) { mSelectedTask = NONE; for (int i = 0; i < mConditions.Length; i++) { if (mConditions[i].EvaluateCondition(context)) { mSelectedTask = i; break; } } if (mSelectedTask == NONE) { // Could not find a task to run. state = TaskState.Complete; return; } state = TaskState.Active; } state = mTasks[mSelectedTask].Update(context); }
protected override void LocalUpdate(AgentContext context) { bool failed; TaskUtil.Process(context, rootTask, out failed); state = (failed ? TaskState.Failed : TaskState.Active); }
protected override bool LocalExit(AgentContext context) { return rootTask.Exit(context); }