public bool SaveSepsisResult(string encounterID, SepsisResult sepsisResult)
        {
            bool canSave = false;
            Guid encounterIdGuid = Guid.Parse(encounterID);

            using (_treatSepsisContext = new TreatSepsisEntities())
            {
                var mortyResult = (from a in _treatSepsisContext.tMortyResult
                                   where a.EncounterID == encounterIdGuid
                                   select a);

                foreach (var result in mortyResult)
                {
                    _treatSepsisContext.tMortyResult.Remove(result);
                }

                try
                {
                    _treatSepsisContext.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    canSave = false;
                }

                tMortyResult res = new tMortyResult
                {

                    MortyResultID = Guid.NewGuid(),
                    EncounterID = encounterIdGuid,
                    CalibrationDatabaseVersion = null,
                    MortyVersion = null,
                    HuginVersion = null,
                    OverallProbabilityOfMortality = -1f,
                    SepsisProbabilityNo = sepsisResult.No,
                    SepsisProbabilityMild = sepsisResult.Mild,
                    SepsisProbabilityModerate = sepsisResult.Moderate,
                    SepsisProbabilitySevere = sepsisResult.Severe,
                    SepsisProbabilityCritical = sepsisResult.Critical,
                    OverallProbabilityOfBacteremia = sepsisResult.Bacterimia
                };

                _treatSepsisContext.tMortyResult.Add(res);

                try
                {
                    _treatSepsisContext.SaveChanges();
                    canSave = true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    canSave = false;
                }

                return canSave;
            }
        }
        public List<PatientInfo> GetPatients()
        {
            using (_treatSepsisContext = new TreatSepsisEntities())
            {
                List<PatientInfo> patients = new List<PatientInfo>();
                //Get patients from tPatient
                var patientquery = (from p in _treatSepsisContext.tPatient
                                    select new
                                    {
                                        CprNr = p.CPR,
                                        PatientID = p.PatientID
                                    });
                //Get all encounters for each patient
                foreach (var p in patientquery)
                    patients.Add(new PatientInfo
                    {
                        CprNr = p.CprNr,
                        Encounters = (from ep in _treatSepsisContext.tEpisode
                                      join en in _treatSepsisContext.tEncounter on ep.EpisodeID equals en.EpisodeID
                                      where ep.PatientID.Equals(p.PatientID)
                                      orderby en.EncounterTime
                                      select new
                                      {
                                          en.EncounterID,
                                          en.EncounterTime
                                      }).AsEnumerable()
              .Select(x => new EncounterInfo { EncounterID = x.EncounterID.ToString(), EncounterTime = x.EncounterTime.ToString() }).ToList()
                    });

                return patients;
            }
        }
 public void TestInitialize()
 {
     Console.Out.WriteLine("SepsisControllerTestInitialize called");
     testSepsisController = new SepsisControllerBLL();
     _treatSepsisContext = new TreatSepsisEntities();
 }
        public SepsisResult GetNodeIds(string encounterId)
        {
            //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Hugin\CPN.hkb");

            //Load CPN to HUGIN
            //Domain dom = new Domain(path);
            Domain dom = _domain;
            //dom.SaveAsKB("d:\\CPNNetwork.hkb");
            //Domain dom = (Domain)_domain.Clone();

            //Initial propagation of CPN - Prepares it for manipulation
            Propagate(dom);

            using (_treatSepsisContext = new TreatSepsisEntities())
            {
                Guid encounterIdGuid = Guid.Parse(encounterId);

                //List<SepsisFinder> sepsisInfo = (from sepsis in _treatSepsisContext.SepsisFinder
                //                                 where sepsis.EncounterID.Equals(encounterIdGuid)
                //                                 select sepsis).ToList();
                SepsisFinder sepsisInfo = _treatSepsisContext.SepsisFinder.Where(x => x.EncounterID.Equals(encounterIdGuid)).SingleOrDefault();

                //Get all nodes with a DesignID
                var nodes = (from n in _treatSepsisContext.aMapToNode
                             join d in _treatSepsisContext.ktUIDesign on n.DesignID equals d.DesignID
                             join cpnNode in _treatSepsisContext.aCpnNode on n.NodeID equals cpnNode.NodeID
                             where d.DesignID.Equals(n.DesignID)
                             select new
                             {
                                 n.NodeID,
                                 n.DesignID,
                                 cpnNode.NodeTypeID,
                                 d.DatabaseFieldName
                             }).ToList();

                List<aDiscreteNodeLookup> aDiscreteNodeLookUpList = (from b in _treatSepsisContext.aDiscreteNodeLookup
                                                                     select b).ToList();

                List<aDiscreteNodeRange> aDiscreteNodeRangeList = (from c in _treatSepsisContext.aDiscreteNodeRange
                                                                   select c).ToList();

                List<aModifyCpnValue> aModifyCpnValueList = (from c in _treatSepsisContext.aModifyCpnValue
                                                             select c).ToList();

                List<aModifyType> aModifyTypeList = (from c in _treatSepsisContext.aModifyType
                                                     select c).ToList();

                //Initialize age node related to Sepsis in CPN
                int age = CalculateAge(encounterIdGuid);

                DiscreteRangeNodeInitializeStatesAge(age, dom, aDiscreteNodeRangeList);

                //Initialize all other nodes related to Sepsis in CPN
                bool skipImmunomodulatoryTherapy = false;
                foreach (var node in nodes)
                {
                    switch (node.NodeTypeID)
                    {
                        case 0: //Discrete Node
                            //Get DiscreteNode from CPN based on NodeID
                            DiscreteChanceNode dNode = (DiscreteChanceNode)dom.GetNodeByName(node.NodeID);

                            //If the DesignID is 83, and the Immunodeficiency value is 1,2,3,4 or 6, the node is 100% in the "yes" state
                            if (node.DesignID == 83)
                            {
                                skipImmunomodulatoryTherapy = DiscreteNodeInitializeStates(node.NodeID, node.DatabaseFieldName, dNode, sepsisInfo, aDiscreteNodeRangeList, aDiscreteNodeLookUpList);
                            }
                            //If the Immunodeficiency value is not 1,2,3,4 or 6, then check DesignID 84 and the ImmunomodulatoryTerapy value.
                            //If this value is 0 then the node is 100% in the "no" state
                            //If this value is 2 then the node is 100% in the "yes" state
                            else if (node.DesignID == 84 && !skipImmunomodulatoryTherapy)
                            {
                                DiscreteNodeInitializeStates(node.NodeID, node.DatabaseFieldName, dNode, sepsisInfo, aDiscreteNodeRangeList, aDiscreteNodeLookUpList);
                            }
                            else
                            {
                                DiscreteNodeInitializeStates(node.NodeID, node.DatabaseFieldName, dNode, sepsisInfo, aDiscreteNodeRangeList, aDiscreteNodeLookUpList);
                            }

                            break;
                        case 1: //Continuous Node
                            ContinuousChanceNode cNode = (ContinuousChanceNode)dom.GetNodeByName(node.NodeID);
                            ContinuousNodeEnterValue(node.NodeID, node.DatabaseFieldName, encounterIdGuid, cNode, aModifyCpnValueList, aModifyTypeList, sepsisInfo);

                            break;
                    }
                }
                //Propagate CPN after entering findings and values for discrete and continuous nodes related to sepsis respectively
                Propagate(dom);

                //Save the network to disk
                //Only works if the program has administrator rights
                //dom.SaveAsKB("c:\\CPNNetwork.hkb");

                //Gets beliefs on sepsis node (i.e. read the degree of sepsis)
                return GetBeliefsOnSepsisNode(dom);
            }
        }
 public void TestCleanup()
 {
     Console.Out.WriteLine("SepsisControllerTestCleanup called");
     testSepsisController = null;
     _treatSepsisContext = null;
 }