package org.mitre.midiki.workshop;
import java.io.*;
import java.io.IOException;
import java.util.*;
/**
* Simple test class for dialogue workshop backend. Allows a user to run
* tests against the disease database from the command line. Run with no
* arguments to get the syntax for specific tests. It should be easy for
* a Java programmer to adapt this class to the needs of a dialogue system
* with a Java interface.
*
* @author Carl Burke
* @version 1.0
* @since 1.0
*/
public class WorkshopDatabaseTest
{
/**
* Checks the container for a copy of this string, ignoring case.
* If the item is found, the canonical version is returned.
*
* @param c a Container
value
* @param s a String
value
* @return a String
value
*/
static public String containsIgnoreCase(Collection c, String s)
{
Iterator it = c.iterator();
while (it.hasNext()) {
String sit = (String)it.next();
if (sit.equalsIgnoreCase(s)) return sit;
}
return null;
}
/**
* Parses a symptom specification of the form "symptom"/ into
* a Symptom object. The test component of the symptom is not specified.
* If the component is not specified, it is assumed to be "yes".
*
parseSymptom("headache/yes") yields Symptom(headache,"",yes)
*
parseSymptom("\"headache\"/no") yields Symptom(headache,"",no)
*
parseSymptom("headache") yields Symptom(headache,"",yes)
*
* @param s a String
value
* @param wd a WorkshopDatabase
in which to search for the symptom name
* @return a Symptom
value
*/
static public Symptom parseSymptom(String s,
WorkshopDatabase wd)
{
int slash = s.lastIndexOf("/");
String presence = null;
if (slash==-1) {
presence = "yes";
} else {
presence = s.substring(slash+1);
s = s.substring(0,slash);
}
if (s.startsWith("\"")) {
s = s.substring(1,s.length()-1);
}
s = containsIgnoreCase(wd.symptomList,wd.resolveSynonyms(s));
if (s==null) return null;
return new Symptom(s,true,presence.equals("yes"));
}
/**
* Parses a symptom specification of the form "symptom"/ into
* a DiagnosticTest object. The test and result components of the test
* are not specified.
* If the component is not specified, it is assumed to be "yes".
*@see parseSymptom
*
* @param s a String
value
* @param wd a WorkshopDatabase
in which to search for the test name
* @return a DiagnosticTest
value
*/
static public DiagnosticTest parseTest(String s,
WorkshopDatabase wd)
{
int slash = s.lastIndexOf("/");
String presence = null;
if (slash==-1) {
presence = "yes";
} else {
presence = s.substring(slash+1);
s = s.substring(0,slash);
}
if (s.startsWith("\"")) {
s = s.substring(1,s.length()-1);
}
s = containsIgnoreCase(wd.testList,wd.resolveSynonyms(s));
if (s==null) return null;
return new DiagnosticTest(s,"","",true,presence.equals("yes"));
}
/**
* Parses a symptom specification of the form "symptom"/ into
* a MedicalHistory object.
* If the component is not specified, it is assumed to be "yes".
*@see parseSymptom
*
* @param s a String
value
* @param wd a WorkshopDatabase
in which to search for the history item name
* @return a MedicalHistory
value
*/
static public MedicalHistory parseHistory(String s,
WorkshopDatabase wd)
{
int slash = s.lastIndexOf("/");
String presence = null;
if (slash==-1) {
presence = "yes";
} else {
presence = s.substring(slash+1);
s = s.substring(0,slash);
}
if (s.startsWith("\"")) {
s = s.substring(1,s.length()-1);
}
s = containsIgnoreCase(wd.historyList,wd.resolveSynonyms(s));
if (s==null) return null;
return new MedicalHistory(s,true,presence.equals("yes"));
}
/**
* Reads arguments and tests queries against the specified database.
*
* @param args a String[]
value
*/
public static void main(String[] args) {
WorkshopDatabase wd = null;
if (args.length >= 2) {
try {
FileInputStream fos = new FileInputStream(args[0]);
ObjectInputStream oos = new ObjectInputStream(fos);
wd = (WorkshopDatabase)oos.readObject();
fos.close();
System.out.println("Read database from "+args[0]);
} catch (Exception e) {
System.out.println("*** Couldn't read disease database ***");
e.printStackTrace();
return;
}
switch (args[1].charAt(0)) {
case 'd':
System.out.println("*** GET DISEASE NAMES ***");
System.out.println(wd.getAllDiseases());
break;
case 's':
System.out.println("*** GET SYMPTOM NAMES ***");
System.out.println(wd.getAllSymptoms());
break;
case 't':
System.out.println("*** GET TEST NAMES AND SYMPTOMS ***");
System.out.println(wd.getAllTests());
break;
case 'h':
System.out.println("*** GET HISTORY ITEM NAMES ***");
System.out.println(wd.getAllHistoryItems());
break;
case 'n':
System.out.println("*** GET SYNONYMS ***");
System.out.println(wd.getAllSymptomSynonyms());
break;
case 'r':
System.out.println("*** GET DISEASE DESCRIPTION ***");
if (args.length < 3) {
System.out.println("Error: no disease name specified");
return;
}
Disease descr = wd.getDiseaseDescription(args[2]);
if (descr==null) {
System.out.println(args[2]+" not stored in database");
} else {
System.out.println(descr);
}
break;
case 'g':
System.out.println("*** DIAGNOSE DISEASE FROM SPECIFIED SYMPTOMS ***");
LinkedList symp = new LinkedList();
LinkedList test = new LinkedList();
LinkedList hist = new LinkedList();
for (int i=2; i ");
System.out.println("where");
System.out.println(" - typically 'diseasedb.ser'");
System.out.println(" ");
System.out.println(" d - get all disease names");
System.out.println(" s - get all symptom names");
System.out.println(" t - get all testable symptoms and test names");
System.out.println(" h - get all history item names");
System.out.println(" n - get all symptom synonyms");
System.out.println(" r - get description of named disease");
System.out.println(" g - diagnose given the named findings");
System.out.println(" each finding is of the form \"name\"/");
System.out.println(" ( is either yes or no)");
System.out.println(" Separate multiple findings with spaces.");
System.out.println(" Findings obtained via diagnostic tests are allowed,");
System.out.println(" as are items of medical history, as well as symptoms");
System.out.println(" reported by the patient.");
}
}
}