Esempio n. 1
0
        public async Task <Option <EstimativaPorBayesViewModel> > Handle(
            EstimativaBayesPorTextoCommand request,
            CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                return(Option.None <EstimativaPorBayesViewModel>());
            }
            var eventos = (await eventoReadOnlyRepository.ObterEventosPorTexto(request.Texto))
                          .ValueOr(Enumerable.Empty <EventoViewModel>());

            var estimativas = eventos.Select(x => Task.Factory.StartNew(() =>
                                                                        Estimativa.CriarEstimativaPorTeoremaBayes(
                                                                            x.ProbabilidadeAcontecer,
                                                                            x.ProbabilidadeNaoAcontecer)));

            await Task.WhenAll(estimativas);

            var tarefaParaObterMediaPriory = Task.Run(() => estimativas
                                                      .Select(x => x.Result.Priory).Average());
            var tarefaParaObterMediaPosteriory = Task.Run(() => estimativas
                                                          .Select(x => x.Result.Posteriory).Average());
            var tarefaParaObterMediaProbabilidade = Task.Run(() => estimativas
                                                             .Select(x => x.Result.Probabilidade).Average());

            await Task.WhenAll(
                tarefaParaObterMediaPriory,
                tarefaParaObterMediaPosteriory,
                tarefaParaObterMediaProbabilidade);

            var estimativa = Estimativa.CriarEstimativaPorTeoremaBayes(
                tarefaParaObterMediaPriory.Result,
                tarefaParaObterMediaPosteriory.Result);

            await estimativaDomainRepository.Add(estimativa);

            if (!await Commit())
            {
                return(Option.None <EstimativaPorBayesViewModel>());
            }

            return(Option.Some(
                       EstimativaPorBayesViewModel.Criar(
                           tarefaParaObterMediaPriory.Result,
                           tarefaParaObterMediaPosteriory.Result,
                           tarefaParaObterMediaProbabilidade.Result)));
        }
 public async Task <EstimativaPorBayesViewModel> EstimativaPorBayesUsandoTexto(string texto) =>
 (await bus.SendCommand <EstimativaBayesPorTextoCommand, Option <EstimativaPorBayesViewModel> >(
      EstimativaBayesPorTextoCommand.Criar(texto))
 ).Match(
     some: estimativa => estimativa,
     none: EstimativaPorBayesViewModel.Empty);