Exemplo n.º 1
0
        public void BuildInferComboReports(PumpSystem ppSys)
        {
            using (var context = new PumpSystemContext()) {
                var reportsToSave = new List <InferComboReport>();
                foreach (var comp in ppSys)
                {
                    foreach (var icItem in comp.InferComboItems.Where(ic => ic.IsHappening))
                    {
                        GradedCriterion.ForEachValidGradeRange(gradeRange =>
                        {
                            var passedGcts =
                                icItem.ExpressionCts.Where(ct => ct is GradedCriterion)
                                .Where(gct => gct.IsHappening)
                                .Cast <GradedCriterion>().ToList();

                            //假如range是3的话,那么通过的里面至少有个3,且只能有3,不能有1,2
                            //假如range是2,3的话, 那么通过的里面至少有个2,且只能有2,3
                            //假如range是1,2,3的话, 那么通过的里面至少有个1,且只能有1,2,3

                            var checkPass = passedGcts.Any();
                            checkPass    &= passedGcts.Exists(g => (int)g.HappeningGrade == gradeRange[0]);
                            checkPass    &= passedGcts.All(g => gradeRange.Contains((int)g.HappeningGrade));

                            if (!checkPass)
                            {
                                return;
                            }

                            var newRpt = new InferComboReport();

                            //设置报告时间
                            var happenTime    = passedGcts.Max(ct => ct.Time);
                            newRpt.LibId      = icItem.Id;
                            newRpt.FirstTime  = happenTime;
                            newRpt.LatestTime = happenTime;

                            //设置报告组件
                            newRpt.CompCode = comp.Code;

                            //设置报告故障内容和建议
                            //                        var faultItem = Repo.FaultItems.First(fi => fi.IsSameFaultItem(icItem));
                            newRpt.DisplayText = icItem.FaultResult;
                            newRpt.EventMode   = icItem.EventMode;
                            newRpt.Expression  = icItem.Expression;
                            newRpt.RtDatas     = string.Join(Repo.Separator,
                                                             icItem.ExpressionCts.Select(ct => $"{ct.LibId}:{(ct.IsHappening ? 1 : 0)}"));
                            //                        newRpt.Advise = faultItem.Advise;

                            newRpt.HappenCount = 1;

                            //remark2 作为所用分档
                            newRpt.Remark2 = "Grade:" + string.Join(",", gradeRange);

                            //把通过的(为1的)分别是几档写到remark3里
                            newRpt.Remark3 = "GradeRefer: " + string.Join(Repo.Separator,
                                                                          passedGcts.Select(g => $"{g.LibId}:{(int) g.HappeningGrade}"));


                            var icReports = context.InferComboReports.ToList();

                            //判断是否存在报告,不存在则添加,存在则更新
                            var existRpts = icReports.Where(rptRecord => rptRecord.LibId == newRpt.LibId &&
                                                            rptRecord.EventMode == newRpt.EventMode &&
                                                            rptRecord.Expression == newRpt.Expression &&
                                                            rptRecord.Remark2 == newRpt.Remark2).ToArray();
                            if (!existRpts.Any())
                            {
                                var intersects = icReports.Select(r => r.LibId).Intersect(icItem.PrevIds).ToList();
                                if (intersects.Any())
                                {
                                    newRpt.DisplayText += "(小概率)";
                                }
                                reportsToSave.Add(newRpt);
                            }
                            else
                            {
                                var latestTime       = existRpts.Max(rr => rr.LatestTime);
                                var latestRpt        = existRpts.First(r => r.LatestTime == latestTime);
                                latestRpt.LatestTime = newRpt.FirstTime;
                                latestRpt.HappenCount++;
                            }
                        });
                    }
                }

                if (reportsToSave.Any())
                {
                    context.InferComboReports.AddRange(reportsToSave);
                }

                context.SaveChanges();
            }
        }
        private void ParseGradedCriterion()
        {
            var gct = _ct as GradedCriterion;

            if (gct == null)
            {
                return;
            }

            string outputDetail = string.Empty;

            //先尝试判断判据能否正常解析
            string parsedExp;

            if (!TryParseVariables(out parsedExp))
            {
                return;
            }

            var curIsPassed = true;

            //判据无误能解析,那就进行分级解析
            GradedCriterion.ForEachValidGradeRange(lvs =>
            {
                //取一个最小的作为迭代条件
                var lv = lvs.Min();

                //当前等级没通过的话,之后的等级也就不用判断了,跳出循环
                if (!curIsPassed)
                {
                    return;
                }

                var gradePercent = Convert.ToDouble(lv) / Convert.ToDouble(GradedCriterion.GradeCount);
                //解析判据
                TryParseVariables(out parsedExp, gradePercent);
                curIsPassed = EvaluateExpression(parsedExp);

                if (curIsPassed)
                {
                    gct.HappeningGrade = (SeverityGrade)lv;
                }

                //不管通过与否,都要记录和输出。 通过的话,设置级别要在记录之前;没通过的话,跳出要在记录之后
                RecordRtData();
                outputDetail = $"  组件:{_compCode} 事件模式:{gct.EventMode}  实时数据: {gct.RtDataStr}";
            });

            var notHappenAction = new Action(() =>
            {
                gct.IsHappening = false;
                gct.Time        = null;
                Log.Inform($"---- 分级判据{_ct.LibId} 未发生 {outputDetail}");
            });

            var happenAction = new Action(() =>
            {
                gct.IsHappening = true;
                gct.Time        = TdUpdateTime;
                Log.Inform($">>>> 分级判据{_ct.LibId} 已发生 {outputDetail}");
            });

            //都没通过,判据没发生
            if (gct.HappeningGrade == SeverityGrade.NotHappen)
            {
                notHappenAction();
            }
            else
            {
                //                if (Repo.ValidGrades.Contains((int) gct.HappeningGrade)) {
                //                    happenAction();
                //                } else {
                //                    notHappenAction();
                //                }
                happenAction();
            }
        }