Example #1
0
        /// <summary>
        /// Derange values
        /// </summary>
        /// <param name="useOriginalValue">whether to use original value to derange</param>
        public void Derange(bool useOriginalValue = false)
        {
            if (useOriginalValue)
            {
                valueArray = originalValues?.ToArray() ?? new T[0];
            }
            if (valueArray.IsNullOrEmpty())
            {
                return;
            }
            lastIndex = 0;
            var size = valueArray.Length;

            for (var i = size - 1; i >= 0; --i)
            {
                var nextIndex = RandomNumberHelper.GetRandomNumber(i);
                var nextValue = valueArray[nextIndex];
                valueArray[nextIndex] = valueArray[i];
                valueArray[i]         = nextValue;
            }
        }
Example #2
0
        /// <summary>
        /// Seed Events, meanwhile seed clinical problem instances
        /// </summary>
        void SeedEvent()
        {
            var encounter = db.Encounter.FirstOrDefault();

            if (encounter == null)
            {
                return;
            }

            Event  evt;
            Report report;

            // make report & fact sample data using test suite dict
            //
            // 1st event
            //
            evt = new Event()
            {
                Name      = "心肌疾病的实验诊断",
                Encounter = encounter,
                EventType = EnumEventType.化验单.ToString(),
                TimeStamp = DateTime.Now
            };
            db.Event.Add(evt);

            report = new Report()
            {
                Event      = evt,
                ReportType = EnumReportType.化验单.ToString(),
                TimeStamp  = DateTime.Now,
                URL        = "/KnowledgeBase/report/心肌酶.jpg"
            };
            db.Report.Add(report);

            var item = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "肌酸激酶CK-MB测定");

            if (item != null)
            {
                var fact = new Fact()
                {
                    Report                = report,
                    NumericValue          = 6.0,
                    Confidence            = RandomNumberHelper.GetRandomNumber(98, 100),
                    ContextItemDefinition = item,
                    IsAbnormal            = false,
                    LifeSpan              = EnumLifeSpan.Temporary.ToString()
                };
                db.UpdateFactCache(encounter.Id, fact);
            }

            item = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "α羟基丁酸脱氢酶测定");
            if (item != null)
            {
                var fact = new Fact()
                {
                    Report                = report,
                    NumericValue          = 120,
                    Confidence            = RandomNumberHelper.GetRandomNumber(98, 100),
                    ContextItemDefinition = item,
                    IsAbnormal            = false,
                    LifeSpan              = EnumLifeSpan.Temporary.ToString()
                };
                db.UpdateFactCache(encounter.Id, fact);
            }

            item = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "肌钙蛋白T测定");
            if (item != null)
            {
                var fact = new Fact()
                {
                    Report                = report,
                    NumericValue          = 1.1,
                    Confidence            = RandomNumberHelper.GetRandomNumber(98, 100),
                    ContextItemDefinition = item,
                    IsAbnormal            = true,
                    LifeSpan              = EnumLifeSpan.Temporary.ToString()
                };
                db.UpdateFactCache(encounter.Id, fact);
            }

            item = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "肌钙蛋白I测定");
            if (item != null)
            {
                var fact = new Fact()
                {
                    Report                = report,
                    NumericValue          = 11,
                    Confidence            = RandomNumberHelper.GetRandomNumber(98, 100),
                    ContextItemDefinition = item,
                    IsAbnormal            = true,
                    LifeSpan              = EnumLifeSpan.Temporary.ToString()
                };
                db.UpdateFactCache(encounter.Id, fact);
            }

            item = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "肌红蛋白测定");
            if (item != null)
            {
                var fact = new Fact()
                {
                    Report                = report,
                    NumericValue          = 80,
                    Confidence            = RandomNumberHelper.GetRandomNumber(98, 100),
                    ContextItemDefinition = item,
                    IsAbnormal            = false,
                    LifeSpan              = EnumLifeSpan.Temporary.ToString()
                };
                db.UpdateFactCache(encounter.Id, fact);
            }

            item = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "缺血修饰白蛋白(IMA)测定");
            if (item != null)
            {
                var fact = new Fact()
                {
                    Report                = report,
                    NumericValue          = 90,
                    Confidence            = RandomNumberHelper.GetRandomNumber(98, 100),
                    ContextItemDefinition = item,
                    IsAbnormal            = false,
                    LifeSpan              = EnumLifeSpan.Temporary.ToString()
                };
                db.UpdateFactCache(encounter.Id, fact);
            }

            item = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "脑钠肽(BNP)测定");
            if (item != null)
            {
                var fact = new Fact()
                {
                    Report                = report,
                    NumericValue          = 70,
                    Confidence            = RandomNumberHelper.GetRandomNumber(98, 100),
                    ContextItemDefinition = item,
                    IsAbnormal            = false,
                    LifeSpan              = EnumLifeSpan.Temporary.ToString()
                };
                db.UpdateFactCache(encounter.Id, fact);
            }

            item = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "N端-前脑钠肽(NT-ProBNP)测定");
            if (item != null)
            {
                var fact = new Fact()
                {
                    Report                = report,
                    NumericValue          = 66,
                    Confidence            = RandomNumberHelper.GetRandomNumber(98, 100),
                    ContextItemDefinition = item,
                    IsAbnormal            = false,
                    LifeSpan              = EnumLifeSpan.Temporary.ToString()
                };
                db.UpdateFactCache(encounter.Id, fact);
            }

            item = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "妊娠相关蛋白测定");
            if (item != null)
            {
                var fact = new Fact()
                {
                    Report                = report,
                    StringValue           = "阴性(-)",
                    Confidence            = RandomNumberHelper.GetRandomNumber(98, 100),
                    ContextItemDefinition = item,
                    IsAbnormal            = false,
                    LifeSpan              = EnumLifeSpan.Temporary.ToString()
                };
                db.UpdateFactCache(encounter.Id, fact);
            }

            item = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "同型半胱氨酸测定");
            if (item != null)
            {
                var fact = new Fact()
                {
                    Report                = report,
                    NumericValue          = 9,
                    Confidence            = RandomNumberHelper.GetRandomNumber(98, 100),
                    ContextItemDefinition = item,
                    IsAbnormal            = false,
                    LifeSpan              = EnumLifeSpan.Temporary.ToString()
                };
                db.UpdateFactCache(encounter.Id, fact);
            }
            db.SaveChanges();

            //
            // 2nd event
            //
            evt = new Event()
            {
                Name      = "肾脏疾病的实验诊断",
                Encounter = db.Encounter.FirstOrDefault(),
                EventType = EnumEventType.化验单.ToString(),
                TimeStamp = DateTime.Now
            };
            db.Event.Add(evt);

            report = new Report()
            {
                Event      = evt,
                ReportType = EnumReportType.化验单.ToString(),
                TimeStamp  = DateTime.Now,
                URL        = "/KnowledgeBase/report/肝肾.jpg"
            };
            db.Report.Add(report);

            //
            // 3rd events
            // Generate a series of continuous BP facts to demostrate the
            //
            for (int i = 0; i < 5; i++)
            {
                evt = new Event()
                {
                    Name        = "第" + (i + 1).ToString() + "次" + EnumEventType.体征数据.ToString(),
                    Encounter   = db.Encounter.FirstOrDefault(),
                    Description = "",
                    EventType   = EnumEventType.体征数据.ToString(),
                    TimeStamp   = DateTime.Now.Subtract(TimeSpan.FromDays(10 - i))
                };

                report = new Report()
                {
                    Event      = evt,
                    ReportType = EnumReportType.住院病历.ToString(),
                    TimeStamp  = DateTime.Now.Subtract(TimeSpan.FromDays(10 - i)),
                    URL        = "/KnowledgeBase/report/BP.jpg"
                };
                db.Report.Add(report);
            }

            //
            // 4th event
            //
            evt = new Event()
            {
                Name        = EnumEventType.入院.ToString(),
                Encounter   = db.Encounter.FirstOrDefault(),
                Description = "",
                EventType   = EnumEventType.入院.ToString(),
                TimeStamp   = DateTime.Now.Subtract(TimeSpan.FromDays(20))
            };
            db.Event.Add(evt);

            report = new Report()
            {
                Event      = evt,
                ReportType = EnumReportType.入院记录.ToString(),
                TimeStamp  = DateTime.Now.Subtract(TimeSpan.FromDays(20)),
                URL        = "/KnowledgeBase/report/入院记录.htm"
            };
            db.Report.Add(report);

            db.UpdateFactCache(encounter.Id, new Fact()
            {
                Report                = report,
                BooleanValue          = true,
                Confidence            = 100,
                ContextItemDefinition = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "急性冠脉综合征"),
                LifeSpan              = EnumLifeSpan.Temporary.ToString()
            });

            db.SaveChanges();

            //
            // 5th event
            //
            evt = new Event()
            {
                Name        = EnumEventType.诊断.ToString(),
                Encounter   = db.Encounter.FirstOrDefault(),
                Description = "",
                EventType   = EnumEventType.诊断.ToString(),
                TimeStamp   = DateTime.Now.Subtract(TimeSpan.FromDays(20))
            };

            report = new Report()
            {
                Event      = evt,
                ReportType = EnumReportType.门诊诊断.ToString(),
                TimeStamp  = DateTime.Now.Subtract(TimeSpan.FromDays(20)),
                URL        = "/KnowledgeBase/report/RIM.png"
            };
            db.Report.Add(report);

            // in real application, facts are retrived by GetFactsFromreport(report obj/url) I/F.
            db.UpdateFactCache(encounter.Id, new Fact()
            {
                Report                = report,
                BooleanValue          = true,
                IsAbnormal            = true,
                Confidence            = 100,
                ContextItemDefinition = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "心房扑动"),
                LifeSpan              = EnumLifeSpan.Temporary.ToString()
            });
            db.UpdateFactCache(encounter.Id, new Fact()
            {
                Report                = report,
                BooleanValue          = true,
                IsAbnormal            = true,
                Confidence            = 100,
                ContextItemDefinition = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "肺动脉高压"),
                LifeSpan              = EnumLifeSpan.Temporary.ToString()
            });
            db.UpdateFactCache(encounter.Id, new Fact()
            {
                Report                = report,
                BooleanValue          = true,
                IsAbnormal            = true,
                Confidence            = 100,
                ContextItemDefinition = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "肥胖"),
                LifeSpan              = EnumLifeSpan.Temporary.ToString()
            });
            db.UpdateFactCache(encounter.Id, new Fact()
            {
                Report                = report,
                BooleanValue          = true,
                IsAbnormal            = true,
                Confidence            = 100,
                ContextItemDefinition = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "鼻炎"),
                LifeSpan              = EnumLifeSpan.Temporary.ToString()
            });
            db.UpdateFactCache(encounter.Id, new Fact()
            {
                Report                = report,
                BooleanValue          = true,
                IsAbnormal            = true,
                Confidence            = 100,
                ContextItemDefinition = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "远视"),
                LifeSpan              = EnumLifeSpan.Temporary.ToString()
            });
            db.UpdateFactCache(encounter.Id, new Fact()
            {
                Report                = report,
                BooleanValue          = true,
                IsAbnormal            = true,
                Confidence            = 100,
                ContextItemDefinition = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "精神分裂症"),
                LifeSpan              = EnumLifeSpan.Temporary.ToString()
            });
            db.UpdateFactCache(encounter.Id, new Fact()
            {
                Report                = report,
                BooleanValue          = true,
                IsAbnormal            = true,
                Confidence            = 100,
                ContextItemDefinition = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "手术并发症"),
                LifeSpan              = EnumLifeSpan.Temporary.ToString()
            });
            db.SaveChanges();
        }
        /// <summary>
        /// used for demo purpose only
        /// </summary>
        /// <param name="id"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public IEnumerable <Fact> GetFactsFromReportMock(int id, KBEntities db)
        {
            var facts = new List <Fact>();

            if (db == null)
            {
                return(facts);
            }

            var report = db.Report.Find(id);

            if (report == null)
            {
                return(facts);
            }

            if (report.Event.EventType == EnumEventType.体征数据.ToString())
            // && db.Fact.Where(x=>x.Report.Id==report.Id && x.LifeSpan == EnumLifeSpan.Temporary.ToString()).Count()<=0)
            {
                var contextItem = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "肱动脉收缩压");
                if (contextItem != null)
                {
                    facts.Add(new Fact()
                    {
                        Report                = report,
                        NumericValue          = 160 + RandomNumberHelper.GetRandomNumber(-20, 20),
                        IsAbnormal            = true,
                        Confidence            = 100,
                        ContextItemDefinition = contextItem,
                        LifeSpan              = EnumLifeSpan.Temporary.ToString()
                    });
                }

                contextItem = db.ContextItemDefinition.FirstOrDefault(x => x.Name == "肱动脉舒张压");
                if (contextItem != null)
                {
                    facts.Add(new Fact()
                    {
                        Report                = report,
                        NumericValue          = 120 + RandomNumberHelper.GetRandomNumber(-20, 20),
                        IsAbnormal            = true,
                        Confidence            = 100,
                        ContextItemDefinition = contextItem,
                        LifeSpan              = EnumLifeSpan.Temporary.ToString()
                    });
                }
            }
            else
            {
                foreach (var x in db.Fact)
                {
                    if (x.Report.Id == id &&
                        x.LifeSpan == EnumLifeSpan.Temporary.ToString())
                    {
                        facts.Add(x);
                    }
                }
            }

            return(facts);
        }