Class PrologJavaRunner
public class PrologJavaRunner
extends java.lang.Object
Since the knowledge base is written in Prolog, a programming language characterized by its strong support for logical rules and backward chaining, the bidirectional open-source library JPL is used to connect the Java and Prolog components through the SWI Prolog Foreign Language Interface and the Java Native Interface.
JPL is bundled with SWI Prolog since version 5.4. It is used in this machine project in accordance with the terms in its Simplified BSD License.
-
Field Summary
Fields Modifier and Type Field Description int
ADULT_AGE
Lower bound for the age of an adultdouble
CF_VALUE_CONCLUDE
A disease that meets this certainty factor is immediately reported as the diagnosis.double
CF_VALUE_REMOVE
A disease whose certainty factor falls below this value is ruled out.double
FEVER_DIAGNOSIS_TEMP
A body temperature (in degree Celsius) equal to or higher than this is indicative of fever.double
FEVER_EMERGENCY_TEMP
A body temperature (in degree Celsius) equal to or higher than this is considered an emergency.int
NUM_DISEASES
Number of diseases in the knowledge basedouble
SLOW_HEART_RATE_ADULT_DIAGNOSIS
A heart rate equal to or slower tha this is indicative of slow heart rate (brachycardia) for an adult.double
SLOW_HEART_RATE_CHILD_DIAGNOSIS
A heart rate equal to or slower tha this is indicative of slow heart rate (brachycardia) for a child.double
SLOW_HEART_RATE_EMERGENCY_RATE
A heart rate equal to or slower than this is considered an emergency. -
Constructor Summary
Constructors Constructor Description PrologJavaRunner()
Constructor for the medical expert system featuring an interface written in the object-oriented language Java and a knowledge base written in the logic language Prolog -
Method Summary
Modifier and Type Method Description void
deleteSymptom()
Removes a symptom from the list of symptoms associated with a certain disease in order to prevent redundant asking of questions during probingjava.lang.String
displayCFvals(java.lang.String symptom)
Returns the confidence values per disease for display in the graphical user interface of this expert system, given the most recent symptom consideredjava.lang.String
displayInquiry()
Returns the question asked by this expert system during probingdouble
getAge()
Returns the age of the patientjava.lang.String
getAns()
Returns the response of the user to the probing questionboolean
getEmergency()
Returnstrue
if a symptom indicative of an emergency has been reported;false
, otherwisedouble
getFinalCF()
Returns the final certainty factor (that is, the highest recorded certainty factor for the diseases in the database)java.lang.String
getFinalDiagnosis()
Return the final diagnosis of this expert systemint
getI()
Returns the index pertinent to the current disease being considered, relative to the Prolog list in the knowledge basejava.lang.String
getName()
Returns the name of the patientjava.lang.String
getSex()
Returns the sex of the patientjava.lang.String
getSymptom()
Returns the current symptom being consideredboolean
isHighConfidence()
Returnstrue
if the certainty factor for the disease currently being considered falls meets the threshold for immediate diagnosis (eliminating the need to consider the subsequent diseases in the knowledge base);false
, otherwiseboolean
isLowConfidence()
Returnstrue
if the certainty factor for the disease currently being considered falls below the threshold (ruling it out);false
, otherwisevoid
loadConstantsFromKB()
Initializes the constants used by the expert system by retrieving their values from the Prolog knowledge baseboolean
loadKnowledgeBase(java.lang.String knowledgeBase)
Connects this expert system to the knowledge base written in Prolog given the filename (together with the file extension) of this knowledge base and returnstrue
if the consultation is successful orfalse
, otherwisevoid
moveToNextDisease()
Considers the next disease in the knowledge baseboolean
removeChildSymptoms()
Returnstrue
if the patient is not a child (that is, of age 19 years and above) orfalse
, otherwise, and removes symptoms in the knowledge base that are exclusive to pediatric (child) patients to make probing more targetedboolean
removeFemaleSymptoms()
Returnstrue
if the patient is male orfalse
, otherwise, and removes symptoms in the knowledge base that are exclusive to female patients to make probing more targetedvoid
setAge(double age)
Sets the age of the patient to the given valuevoid
setAns(java.lang.String ans)
Sets the user's response to the probing question to the given value (which is either a"yes"
or a"no"
)void
setName(java.lang.String name)
Sets the name of the patient to the given valuevoid
setSex(java.lang.String sex)
Sets the sex of the patient to the given value (which is either biologically"male"
or *"female"
)void
unloadKnowledgeBase()
Resets connection with the knowledge base when the expert system is restarted, effectively deleting all data associated with the previous patientdouble
updateCF(java.lang.String symptom, java.lang.String answer, double age, int currIndex)
Computes the certainty factor and sets the corresponding attribute, given the current symptom being considered, the patient's response, the patient's age, and the index of the current disease being considered (relative to the Prolog list in the knowledge base)
-
Field Details
-
NUM_DISEASES
public int NUM_DISEASESNumber of diseases in the knowledge base -
ADULT_AGE
public int ADULT_AGELower bound for the age of an adult -
FEVER_EMERGENCY_TEMP
public double FEVER_EMERGENCY_TEMPA body temperature (in degree Celsius) equal to or higher than this is considered an emergency. -
SLOW_HEART_RATE_EMERGENCY_RATE
public double SLOW_HEART_RATE_EMERGENCY_RATEA heart rate equal to or slower than this is considered an emergency. -
FEVER_DIAGNOSIS_TEMP
public double FEVER_DIAGNOSIS_TEMPA body temperature (in degree Celsius) equal to or higher than this is indicative of fever. -
SLOW_HEART_RATE_CHILD_DIAGNOSIS
public double SLOW_HEART_RATE_CHILD_DIAGNOSISA heart rate equal to or slower tha this is indicative of slow heart rate (brachycardia) for a child. -
SLOW_HEART_RATE_ADULT_DIAGNOSIS
public double SLOW_HEART_RATE_ADULT_DIAGNOSISA heart rate equal to or slower tha this is indicative of slow heart rate (brachycardia) for an adult. -
CF_VALUE_REMOVE
public double CF_VALUE_REMOVEA disease whose certainty factor falls below this value is ruled out. -
CF_VALUE_CONCLUDE
public double CF_VALUE_CONCLUDEA disease that meets this certainty factor is immediately reported as the diagnosis.
-
-
Constructor Details
-
PrologJavaRunner
public PrologJavaRunner()Constructor for the medical expert system featuring an interface written in the object-oriented language Java and a knowledge base written in the logic language PrologSince initialization of attributes depends on the personal information of the patient, it is deferred and delegated to the setters, particularly to
setName
,setAge
, andsetSex
.
-
-
Method Details
-
getName
public java.lang.String getName()Returns the name of the patient- Returns:
- name of the patient
-
getAge
public double getAge()Returns the age of the patient- Returns:
- age of the patient
-
getSex
public java.lang.String getSex()Returns the sex of the patient- Returns:
- sex of the patient
-
getAns
public java.lang.String getAns()Returns the response of the user to the probing question- Returns:
- response of the user to the probing question
-
getI
public int getI()Returns the index pertinent to the current disease being considered, relative to the Prolog list in the knowledge base- Returns:
- index pertinent to the current disease being considered, relative to the Prolog list in the knowledge base
-
getFinalCF
public double getFinalCF()Returns the final certainty factor (that is, the highest recorded certainty factor for the diseases in the database)- Returns:
- highest recorded certainty factor for the diseases in the database
-
getSymptom
public java.lang.String getSymptom()Returns the current symptom being considered- Returns:
- current symptom being considered
-
getEmergency
public boolean getEmergency()Returnstrue
if a symptom indicative of an emergency has been reported;false
, otherwise- Returns:
true
if a symptom indicative of an emergency has been reported;false
, otherwise
-
setAns
public void setAns(java.lang.String ans)Sets the user's response to the probing question to the given value (which is either a"yes"
or a"no"
)- Parameters:
ans
- response of the user to the probing question
-
setName
public void setName(java.lang.String name)Sets the name of the patient to the given value- Parameters:
name
- name of the patient
-
setAge
public void setAge(double age)Sets the age of the patient to the given value- Parameters:
age
- age of the patient
-
setSex
public void setSex(java.lang.String sex)Sets the sex of the patient to the given value (which is either biologically"male"
or *"female"
)- Parameters:
sex
- sex of the patient
-
loadKnowledgeBase
public boolean loadKnowledgeBase(java.lang.String knowledgeBase)Connects this expert system to the knowledge base written in Prolog given the filename (together with the file extension) of this knowledge base and returnstrue
if the consultation is successful orfalse
, otherwiseProlog files are typically associated with the file extension
.pl
.- Parameters:
knowledgeBase
- Prolog knowledge base where the pertinent facts and rules are stored- Returns:
true
if the consultation is successful;false
, otherwise
-
unloadKnowledgeBase
public void unloadKnowledgeBase()Resets connection with the knowledge base when the expert system is restarted, effectively deleting all data associated with the previous patientUnder the hood, this function operates through a sequence of
retractall/1
andasserta/1
to reset the terms that have been dynamically altered during previous sessions of the expert system. -
loadConstantsFromKB
public void loadConstantsFromKB()Initializes the constants used by the expert system by retrieving their values from the Prolog knowledge baseIn particular, these include the following attributes:
- Number of diseases in the knowledge base
- Minimum age for a patient to be considered an adult
- Minimum temperature for a body temperature to be considered indicative of an emergency
- Upper bound for a slow heart rate to be considered indicative of an emergency
- Minimum temperature for a body temperature to be considered indicative of a fever
- Upper bound for a slow heart rate (brachycardia) for a child
- Upper bound for a slow heart rate (brachycardia) for an adult
- Threshold for a disease to be ruled out
- Threshold for a diagnosis to be immediately given
-
removeFemaleSymptoms
public boolean removeFemaleSymptoms()Returnstrue
if the patient is male orfalse
, otherwise, and removes symptoms in the knowledge base that are exclusive to female patients to make probing more targeted- Returns:
true
if the patient is male;false
, otherwise
-
removeChildSymptoms
public boolean removeChildSymptoms()Returnstrue
if the patient is not a child (that is, of age 19 years and above) orfalse
, otherwise, and removes symptoms in the knowledge base that are exclusive to pediatric (child) patients to make probing more targeted- Returns:
true
if the patient is not a child;false
, otherwise
-
moveToNextDisease
public void moveToNextDisease()Considers the next disease in the knowledge base -
isLowConfidence
public boolean isLowConfidence()Returnstrue
if the certainty factor for the disease currently being considered falls below the threshold (ruling it out);false
, otherwise- Returns:
true
if the certainty factor for the disease currently being considered falls below the threshold (ruling it out);false
, otherwise
-
isHighConfidence
public boolean isHighConfidence()Returnstrue
if the certainty factor for the disease currently being considered falls meets the threshold for immediate diagnosis (eliminating the need to consider the subsequent diseases in the knowledge base);false
, otherwise- Returns:
true
if the certainty factor for the disease currently being considered falls meets the threshold for immediate diagnosis (eliminating the need to consider the subsequent diseases in the knowledge base);false
, otherwise
-
updateCF
public double updateCF(java.lang.String symptom, java.lang.String answer, double age, int currIndex)Computes the certainty factor and sets the corresponding attribute, given the current symptom being considered, the patient's response, the patient's age, and the index of the current disease being considered (relative to the Prolog list in the knowledge base)The computation is expressed in the Prolog rules
confidenceFactor/3
and adjustedWeight/3 (which adjusts the weight depending on whether an affirmative or negative response is received from the patient). This function also asserts the updated certainty factors back into the knowledge base, superseding previous values.It is patterned after the ad-hoc formulae used by the pioneering medical expert system MYCIN; a more detailed description is given in the technical report.
- Parameters:
symptom
- current symptom being consideredanswer
- response of the patientage
- age of the patientcurrIndex
- index of the current disease being considered (relative to the Prolog list)- Returns:
- updated certainty factor for the pertinent disease
-
deleteSymptom
public void deleteSymptom()Removes a symptom from the list of symptoms associated with a certain disease in order to prevent redundant asking of questions during probing -
displayInquiry
public java.lang.String displayInquiry()Returns the question asked by this expert system during probing- Returns:
- question asked by this expert system during probing
-
getFinalDiagnosis
public java.lang.String getFinalDiagnosis()Return the final diagnosis of this expert systemThe final diagnosis contains the following details:
- Most probable disease
- Qualitative description of the certainty factor
- Final certainty factor, expressed in percentage (%)
If no disease meets the 0.2 threshold, then the patient is asked to consult with a larger hospital for a more thorough diagnosis. This rule is overridden if the patient reports a symptom that is indicative of an emergency.
- Returns:
- final diagnosis of this expert system
-
displayCFvals
public java.lang.String displayCFvals(java.lang.String symptom)Returns the confidence values per disease for display in the graphical user interface of this expert system, given the most recent symptom consideredThe format of the returned string is as follows:
- Symptom: most recent symptom considered
- Certainty factors:
- Name of disease (uppercase): certainty factor (in decimal)
- Parameters:
symptom
- most recent symptom considered- Returns:
- confidence values per disease for display in the graphical user interface of this expert system, given the most recent symptom considered
-