Package expert

Class PrologJavaRunner

java.lang.Object
expert.PrologJavaRunner

public class PrologJavaRunner
extends java.lang.Object
Class implementing the medical expert system featuring an interface written in the object-oriented language Java and a knowledge base written in the logic language Prolog.

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 adult
    double 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 base
    double 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 probing
    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 considered
    java.lang.String displayInquiry()
    Returns the question asked by this expert system during probing
    double getAge()
    Returns the age of the patient
    java.lang.String getAns()
    Returns the response of the user to the probing question
    boolean getEmergency()
    Returns true if a symptom indicative of an emergency has been reported; false, otherwise
    double 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 system
    int getI()
    Returns the index pertinent to the current disease being considered, relative to the Prolog list in the knowledge base
    java.lang.String getName()
    Returns the name of the patient
    java.lang.String getSex()
    Returns the sex of the patient
    java.lang.String getSymptom()
    Returns the current symptom being considered
    boolean isHighConfidence()
    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
    boolean isLowConfidence()
    Returns true if the certainty factor for the disease currently being considered falls below the threshold (ruling it out); false, otherwise
    void loadConstantsFromKB()
    Initializes the constants used by the expert system by retrieving their values from the Prolog knowledge base
    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 returns true if the consultation is successful or false, otherwise
    void moveToNextDisease()
    Considers the next disease in the knowledge base
    boolean removeChildSymptoms()
    Returns true if the patient is not a child (that is, of age 19 years and above) or false, otherwise, and removes symptoms in the knowledge base that are exclusive to pediatric (child) patients to make probing more targeted
    boolean removeFemaleSymptoms()
    Returns true if the patient is male or false, otherwise, and removes symptoms in the knowledge base that are exclusive to female patients to make probing more targeted
    void setAge​(double age)
    Sets the age of the patient to the given value
    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")
    void setName​(java.lang.String name)
    Sets the name of the patient to the given value
    void 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 patient
    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)

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • NUM_DISEASES

      public int NUM_DISEASES
      Number of diseases in the knowledge base
    • ADULT_AGE

      public int ADULT_AGE
      Lower bound for the age of an adult
    • FEVER_EMERGENCY_TEMP

      public double FEVER_EMERGENCY_TEMP
      A 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_RATE
      A heart rate equal to or slower than this is considered an emergency.
    • FEVER_DIAGNOSIS_TEMP

      public double FEVER_DIAGNOSIS_TEMP
      A 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_DIAGNOSIS
      A 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_DIAGNOSIS
      A 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_REMOVE
      A disease whose certainty factor falls below this value is ruled out.
    • CF_VALUE_CONCLUDE

      public double CF_VALUE_CONCLUDE
      A 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 Prolog

      Since initialization of attributes depends on the personal information of the patient, it is deferred and delegated to the setters, particularly to setName, setAge, and setSex.

  • 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()
      Returns true 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 returns true if the consultation is successful or false, otherwise

      Prolog 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 patient

      Under the hood, this function operates through a sequence of retractall/1 and asserta/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 base

      In 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()
      Returns true if the patient is male or false, 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()
      Returns true if the patient is not a child (that is, of age 19 years and above) or false, 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()
      Returns true 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()
      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
      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 considered
      answer - response of the patient
      age - age of the patient
      currIndex - 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 system

      The 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 considered

      The format of the returned string is as follows:

      • Symptom: most recent symptom considered
      • Certainty factors:
      • Name of disease (uppercase): certainty factor (in decimal)
      Each entry is separated by a newline.
      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