Esempio n. 1
0
        public IEnumerable <FailedExecutionInfo> ExecuteRules(RuleEvaluationRequest nextRequest, Processor processor)
        {
            EvaluationRequest evaluationRequest = new EvaluationRequest();

            evaluationRequest.Id = nextRequest.Id;
            //_context.StartEventLog("ExecuteRules start");
            _logger.Info("ExecuteRules start");
            IList <FailedExecutionInfo> failedRules = new List <FailedExecutionInfo>();
            Guid?            LocatorId        = null;
            EfileSchemaType  efileSchemaType  = null;
            string           logId            = "";
            string           diagnosticLog    = string.Empty;
            CurrentSelection currentSelection = new CurrentSelection(nextRequest.Jurisdiction, nextRequest.Year, nextRequest.ReturnType);

            try
            {
                LocatorId       = _logService.GetOrSaveLocator(nextRequest.Year, nextRequest.Jurisdiction, nextRequest.ReturnType, nextRequest.Locator).Id;
                efileSchemaType = _schemaTypeService.GetSchemaType(nextRequest.SchemaType);
                logId           = _logService.Save(nextRequest.Year, nextRequest.Jurisdiction, nextRequest.ReturnType, LocatorId, efileSchemaType, evaluationRequest, RequestStatusConstants.IN_PROCESS);

                var xmlDocumentToEvaluate = CreateXmlDocument(nextRequest.DocumentToEvaluate);
                var defaultNamespace      = ExtractDefaultNamespaceUri(xmlDocumentToEvaluate);
                var ruleExecutionElements = _ruleQueryExecuter.ExecuteRuleQuery(nextRequest.Year, nextRequest.ReturnType, nextRequest.Jurisdiction, nextRequest.SchemaType, defaultNamespace, processor).ToList();

                Stopwatch       sw = Stopwatch.StartNew();
                DocumentBuilder documentBuilder = processor.NewDocumentBuilder();
                documentBuilder.IsLineNumbering  = true;
                documentBuilder.WhitespacePolicy = WhitespacePolicy.PreserveAll;
                XdmNode _XdmNode = documentBuilder.Build(xmlDocumentToEvaluate);

                sw.Stop();
                _logger.Info(String.Format("End SaxonApi DocumentBuilder {0} - Time  {1:0.00}s", evaluationRequest.Id, sw.ElapsedMilliseconds / 1000));
                sw.Restart();
                _logger.Info(String.Format("Start loop rule execution elements RequestId: {0}", evaluationRequest.Id));

                Parallel.ForEach(ruleExecutionElements, (element) =>
                                 //foreach (var element in ruleExecutionElements)
                {
                    try
                    {
                        EFileDocument documentToEvaluate = new EFileDocument(xmlDocumentToEvaluate, true);
                        if (documentToEvaluate.IsDiagnosable)
                        {
                            documentToEvaluate.BeginDiagnostic();
                        }

                        var success = element.Evaluate(documentToEvaluate, element.useAtLeast, element.XQuery, currentSelection,
                                                       string.IsNullOrEmpty(element.AlternateText) ? element.RuleText : element.AlternateText,
                                                       processor, _XdmNode, element.Fields, element.CompiledXQueryExecutable);

                        if (element.useAtLeast > 0)
                        {
                            if (documentToEvaluate.NumberOfNoDiagnostics <= element.useAtLeast - 1)
                            {
                                FailedExecutionInfo failRule = GetFailRule(documentToEvaluate, element);
                                lock (failedRules)
                                {
                                    failedRules.Add(failRule);
                                }
                            }
                        }
                        else
                        {
                            if (documentToEvaluate.NumberOfDiagnostics > 0)
                            {
                                FailedExecutionInfo failRule = GetFailRule(documentToEvaluate, element);
                                lock (failedRules)
                                {
                                    failedRules.Add(failRule);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        string message = string.Empty;
                        if (e is ArgumentException)
                        {
                            message = "The fields from the rule cannot be used together.";
                            _logger.Error(
                                String.Format(
                                    "The fields from the rule cannot be used together RequestId: {0}, RuleId: {1}, RuleNumber: {2}",
                                    evaluationRequest.Id, element.RuleId, element.RuleNumber), e);
                        }
                        else
                        {
                            message = "Error while evaluating rule: " + e.Message;
                            _logger.Error(
                                String.Format(
                                    "Error while evaluating rule RequestId: {0}, RuleId: {1}, RuleNumber: {2}",
                                    evaluationRequest.Id, element.RuleId, element.RuleNumber), e);
                        }
                        lock (failedRules)
                        {
                            failedRules.Add(CreateFailRule(element.RuleId, element.RuleNumber, message, element.Categories, element.TaxProfileArea, element.LinkedQuery, 0));
                        }
                        _logger.Info(
                            String.Format(
                                "Catch loop rule execution elements end add rule RequestId: {0}, RuleId: {1}, RuleNumber: {2}",
                                evaluationRequest.Id, element.RuleId, element.RuleNumber));
                    }
                    //}
                });

                sw.Stop();
                _logger.Info(String.Format("End loop rule execution elements RequestId: {0}, Time:  {1:0.00}s ", evaluationRequest.Id, sw.ElapsedMilliseconds / 1000));

                diagnosticLog = string.Empty;
                if (failedRules.Count > 0)
                {
                    diagnosticLog = JsonConvert.SerializeObject(failedRules);
                }

                _logger.Info(String.Format("End Rule Execution Loop {0}", evaluationRequest.Id));

                _logService.Save(nextRequest.Year, nextRequest.Jurisdiction, nextRequest.ReturnType, LocatorId, efileSchemaType, evaluationRequest, RequestStatusConstants.SUCCESS, diagnosticLog, logId);

                _logger.Info(String.Format("Rule execution elements end save rule RequestId: {0}", evaluationRequest.Id));
            }
            catch (Exception ex)
            {
                _logger.Error("Error in ExecuteRules method", ex);
                _logService.Save(nextRequest.Year, nextRequest.Jurisdiction, nextRequest.ReturnType, LocatorId, efileSchemaType, evaluationRequest, RequestStatusConstants.FAILED, diagnosticLog, logId);
                throw new Exception("Error in ExecuteRules method", ex);
            }
            _logger.Info("Execute Rules Service Method end");
            //_context.EndEventLog();
            return(failedRules);
        }
 public string Save(string year, string jurisdiction, string returnType, Guid?locatorId, EfileSchemaType schemaType, EvaluationRequest evaluationRequest, string status, string DiagnosticInfo = null, string Id = null)
 {
     //_context.StartEventLog("Save start");
     _logger.Info("RuleExecutionLogService Save method start");
     try
     {
         RuleExecutionLog LogInformation = null;
         if (string.IsNullOrEmpty(Id))
         {
             LogInformation                   = new RuleExecutionLog();
             LogInformation.Locator           = new Locator();
             LogInformation.Locator.Id        = (Guid)locatorId;
             LogInformation.Id                = Guid.NewGuid();
             LogInformation.StartTime         = DateTime.Now;
             LogInformation.Status            = status;
             LogInformation.SchemaType        = schemaType;
             LogInformation.EvaluationRequest = evaluationRequest;
             LogInformation.Endtime           = DateTime.Now;
             // LogInformation.Jurisdiction = jurisdiction;
             _repository.Insert <RuleExecutionLog>(LogInformation);
         }
         else
         {
             LogInformation = _repository.Get <RuleExecutionLog>(new Guid(Id));
             if (LogInformation != null)
             {
                 LogInformation.Endtime           = DateTime.Now;
                 LogInformation.DiagnosticInfo    = DiagnosticInfo;
                 LogInformation.Status            = status;
                 LogInformation.EvaluationRequest = evaluationRequest;
                 _repository.Merge <RuleExecutionLog>(LogInformation);
             }
         }
         Id = (LogInformation != null) ? LogInformation.Id.ToString() : null;
     }
     catch (Exception ex)
     {
         _logger.Error("Error in RuleExecutionLogService Save Method ", ex);
     }
     _logger.Info("RuleExecutionLogService Save method End");
     //_context.EndEventLog();
     return(Id);
 }