public void getCMMatchesTest() { DbMethods.getInstance().clearTables(); int p1ID = 0; DbMethods.getInstance().InsertPatient(); DateTime now = DateTime.Now; DbMethods.getInstance().InsertVisit(p1ID, 1, 1, now); DbMethods.getInstance().InsertVisit(p1ID, 1, 1, now); DbMethods.getInstance().InsertVisit(p1ID, 1, 2, now); Patient p = new Patient(p1ID); p.Visits = DbMethods.getInstance().getPatientVisits(p1ID); ACV acv1 = new ACV(new List<Visit>(){ p.Visits[0], p.Visits[1] }); ACV acv2 = new ACV(new List<Visit>() { p.Visits[0], new Visit(1, 1, 3, now) }); List<ACV> matches = p.GetCMMatches(acv1); Assert.True(matches.Count == 1); matches = p.GetCMMatches(acv2); Assert.True(matches.Count == 0); }
public void getPatientMatchesCountTest() { int pID1 = this.insertPatient(); int pID2 = this.insertPatient(); int pID3 = this.insertPatient(); ACV acv1 = new ACV(new List<Visit>()); ACV acv2 = new ACV(new List<Visit>()); ACV acv3 = new ACV(new List<Visit>()); DateTime now = DateTime.Now; this.insertVisit(pID1, 1, 1, now); this.insertVisit(pID1, 1, 1, now); this.insertVisit(pID1, 1, 2, now); this.insertVisit(pID2, 1, 1, now); this.insertVisit(pID2, 1, 2, now); this.insertVisit(pID2, 1, 3, now); this.insertVisit(pID3, 1, 1, now.AddDays(1)); this.insertVisit(pID3, 1, 2, now.AddDays(2)); this.insertVisit(pID3, 1, 3, now.AddDays(3)); acv1.Add(new Visit(1, 1, 1, now)); acv1.Add(new Visit(1, 1, 1, now)); int count = DbMethods.getInstance().getPatientMatchesCount(acv1, pID1); Assert.True(count == 0); acv2.Add(new Visit(1, 1, 1, now)); acv2.Add(new Visit(1, 1, 2, now)); count = DbMethods.getInstance().getPatientMatchesCount(acv2, pID1); Assert.True(count == 1); acv3.Add(new Visit(1, 1, 1, now.AddDays(1))); count = DbMethods.getInstance().getPatientMatchesCount(acv3, pID1); Assert.True(count == 1); }
private void RenderMainMenu() { bool exit = false; string[] menuItems = new string[]{ "Show list of patients", "Show patient visits", "Work on one patient", "CM of visits 1, 5, and 9 of patient 1 matches", "Get list of safe patients with the client based algorithm", "Get list of safe patients with the query based algorithm", "Get list of unsafe patients with the client based algorithm", "Get list of unsafe patients with the query based algorithm", "Exit" }; int itemChoice = -1; while (!exit) { do { int counter = 1; Console.WriteLine(); Console.WriteLine("What would you like to do?"); foreach (string menuItem in menuItems) { Console.WriteLine(counter++ + ". " + menuItem); } try { itemChoice = int.Parse(Console.ReadLine()) - 1; } catch (Exception e) { itemChoice = -1; } } while (itemChoice < 0 || itemChoice >= menuItems.Length); Console.WriteLine(); switch (menuItems[itemChoice]) { case "Show list of patients": this.RenderPatients(); Console.WriteLine(); break; case "Show patient visits": this.RenderPatientVisits(); Console.WriteLine(); break; case "Work on one patient": this.RenderPatientMenu(); break; case "CM of visits 1, 5, and 9 of patient 1 matches": List<Visit> p1Visits = DbMethods.getInstance().getPatientVisits(DbMethods.getInstance().getPatients()[0].ID); ACV cm = new ACV(new List<Visit>() { p1Visits[0], p1Visits[4], p1Visits[8] }); this.RenderCMMatches(cm); break; case "Get list of safe patients with the client based algorithm": this.RenderSafePatients(0); break; case "Get list of safe patients with the query based algorithm": this.RenderSafePatients(1); break; case "Get list of unsafe patients with the client based algorithm": this.RenderUnsafePatients(0); break; case "Get list of unsafe patients with the query based algorithm": this.RenderUnsafePatients(1); break; case "Exit": exit = true; break; } } }
private void RenderCMMatches(int patientID) { int cmSize = 0; ACV CM = new ACV(new List<Visit>()); Patient p = new Patient(patientID); do { Console.WriteLine("Choose the size of the CM : "); cmSize = int.Parse(Console.ReadLine()); } while (cmSize <= 0); for (int i = 0; i < cmSize; i++) { int year; int month; int day; int rational; do { Console.WriteLine("CM no : " + (i + 1)); Console.WriteLine("Month : "); month = int.Parse(Console.ReadLine()); } while (month <= 0); do { Console.WriteLine("Day : "); day = int.Parse(Console.ReadLine()); } while (day <= 0); do { Console.WriteLine("Year : "); year = int.Parse(Console.ReadLine()); } while (year <= 0); do { Console.WriteLine("Rational : "); Console.WriteLine("1. New"); Console.WriteLine("2. Checkup"); Console.WriteLine("3. Follow-up"); Console.WriteLine("4. Referral"); Console.WriteLine("5. Emergency"); rational = int.Parse(Console.ReadLine()); } while (rational < 1 && rational > 5); CM.Add(new Visit(-1, -1, rational, new DateTime(year, month, day))); } //this._controller.matchCM(patientID, CM, false); List<ACV> matches = p.GetCMMatches(CM); int matchCounter = 1; foreach (ACV acv in matches) { Console.WriteLine(); Console.WriteLine("Match : " + matchCounter++); Console.WriteLine(acv.ToString()); } Console.WriteLine(); Console.WriteLine("Combination to match : "); Console.WriteLine(CM.ToString()); Console.WriteLine("Number of matches : " + matches.Count); Console.WriteLine(); }
private void RenderCMMatches(ACV cm) { int patientID = this.AskForPatientID(); Patient p = new Patient(patientID); List<ACV> matches = p.GetCMMatches(cm); int matchCounter = 1; foreach (ACV acv in matches) { Console.WriteLine("Match : " + matchCounter++); Console.WriteLine(acv.ToString()); } Console.WriteLine(); Console.WriteLine("Combination to match : "); Console.WriteLine(cm.ToString()); Console.WriteLine("Number of matches : " + matches.Count); Console.WriteLine(); }
//checks if the patient has an ACV that matches the specified ACV public bool MatchesACV(ACV acv) { if (this._visits.Count == 0) { this._visits = DbMethods.getInstance().getPatientVisits(this.ID); } List<Visit> toMatch = new List<Visit>(this._visits); int indexToRemove = -1; int matchCount = 0; foreach (Visit v1 in acv.Visits) { for(int i = 0; i < toMatch.Count; i++) { if (v1.equals(toMatch[i])) { indexToRemove = i; matchCount++; break; } } if (indexToRemove >= 0) { toMatch.RemoveAt(indexToRemove); indexToRemove = -1; } } if (matchCount == acv.Visits.Count) { return true; } return false; }
public ACVFoundArgs(ACV acv) { this._acv = acv; }
//get all ACVs that match the specified cm public List<ACV> GetCMMatches(ACV cm) { List<ACV> cmMatches = new List<ACV>(); this.FindACVs(cm.Visits.Count, (Patient sender, ACVFoundArgs args) => { ACV acv = args.ACV; ACV tempCM = new ACV(cm.Visits); bool acvMatch = true; foreach (Visit v1 in acv.Visits) { bool visitMatch = false; Visit toRemove = null; foreach (Visit v2 in tempCM.Visits) { if (v1.equals(v2)) { toRemove = v2; visitMatch = true; break; } } if (!visitMatch) { acvMatch = false; break; } else { if (toRemove != null) { tempCM.Visits.Remove(toRemove); } } } if (acvMatch) { cmMatches.Add(acv); } }); return cmMatches; }
//returns list of ACV for a patient public List<ACV> getPatientACV(int patientID, int acvSize) { SqlCeCommand selectCommand = new SqlCeCommand("SELECT DISTINCT * FROM Visit v0", this._conn); SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(selectCommand); DataTable dt = new DataTable(); List<ACV> acvs = new List<ACV>(); for (int i = 1; i < acvSize; i++) { selectCommand.CommandText += " JOIN Visit v" + i + " ON v" + (i - 1) + ".FK_patient = v" + i + ".FK_patient"; } selectCommand.CommandText += " WHERE v0.FK_patient = @patientID"; selectCommand.Parameters.AddWithValue("@patientID", patientID); for (int i = 0; i < acvSize - 1; i++) { selectCommand.CommandText += " AND v" + i + ".id < v" + (i + 1) + ".id"; } dataAdapter.Fill(dt); foreach (DataRow row in dt.Rows) { ACV acv = new ACV(new List<Visit>()); for (int i = 0; i < acvSize; i++) { if (i == 0) { acv.Add(new Visit((int)row["ID"], (int)row["FK_Professional"], (int)row["FK_Rational"], (DateTime)row["date"])); } else { acv.Add(new Visit((int)row["ID" + i], (int)row["FK_Professional" + i], (int)row["FK_Rational" + i], (DateTime)row["date" + i])); } } acvs.Add(acv); } return acvs; }
//returns the number patient matches for a specified ACV public int getPatientMatchesCount(ACV acv, int patientID) { Dictionary<Visit, int> visits = new Dictionary<Visit, int>(); bool isAdded = false; SqlCeCommand selectCommand = new SqlCeCommand(); selectCommand.Connection = this._conn; string query = "SELECT COUNT(*) " + "FROM (SELECT DISTINCT(FK_Patient) FROM Visit) Visit"; string where = " WHERE "; Stopwatch sw = new Stopwatch(); sw.Start(); foreach (Visit v in acv.Visits) { isAdded = false; foreach (KeyValuePair<Visit, int> pair in visits) { if (pair.Key.equals(v)) { visits[pair.Key]++; isAdded = true; break; } } if (!isAdded) { visits.Add(v, 1); } } int counter = 0; foreach (KeyValuePair<Visit, int> pair in visits) { if (counter > 0) { where += " AND "; } where += "Visit.FK_Patient IN ("+ "SELECT Visit.FK_Patient "+ "FROM Visit "+ "WHERE ( Visit.FK_Rational = @rationalID" + counter + " AND date = @date" + counter + ") "+ "GROUP BY FK_Patient "+ "HAVING count( Visit.FK_Patient ) >= @havingCount" + counter + ")"; selectCommand.Parameters.AddWithValue("@rationalID" + counter, pair.Key.RationalID); selectCommand.Parameters.AddWithValue("@date" + counter, pair.Key.Date); selectCommand.Parameters.AddWithValue("@havingCount" + counter, pair.Value); counter++; } selectCommand.CommandText = query + where + " AND FK_patient != " + patientID; int count = (int)selectCommand.ExecuteScalar(); sw.Stop(); //Console.WriteLine("DbMethods.getPatientMatchesCount() : " + sw.ElapsedTicks + " ticks"); return count; }