private void OnClickButton(object sender, EventArgs e)
        {
            Button button = sender as Button;
            // Get the state-list.
            String evalStr = "(find-all-facts ((?f state-list)) TRUE)";

            using (FactAddressValue f = (FactAddressValue)((MultifieldValue)clipsEnvironment.Eval(evalStr))[0])
            {
                string currentID = f.GetFactSlot("current").ToString();

                if (button.Tag.Equals("Next"))
                {
                    if (GetCheckedChoiceButton() == null)
                    {
                        clipsEnvironment.AssertString("(next " + currentID + ")");
                    }
                    else
                    {
                        clipsEnvironment.AssertString("(next " + currentID + " " +
                                                      (string)GetCheckedChoiceButton().Tag + ")");
                    }
                    NextUIState();
                }
                else if (button.Tag.Equals("Restart"))
                {
                    clipsEnvironment.Reset();
                    NextUIState();
                }
                else if (button.Tag.Equals("Prev"))
                {
                    clipsEnvironment.AssertString("(prev " + currentID + ")");
                    NextUIState();
                }
            }
        }
Example #2
0
        private void OnClickButton(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;

            if (button.Tag.Equals("Next"))
            {
                if (choicesPanel.Children.Count != 0)
                {
                    String theAnswer = (String)GetCheckedChoiceButton().Tag;
                    clips.AssertString("(" + relationAsserted + " " + theAnswer + ")");
                }
                else
                {
                    clips.AssertString("(start)");
                }
                clips.Run(1);
                NextUIState();
            }
            else if (button.Tag.Equals("Restart"))
            {
                clips.Reset();
                clips.Run(1);
                NextUIState();
            }
        }
        /* call this method from the fron end.
         * input = strButtonName = Next/Restart/Prev
         * initialFlag = call this for the first time
         * ChoiceOption = Yes / No
         */
        public void HandleRequest(string strButtonName, bool initialFlag, string ChoiceOption)
        {
            ReturnValueClass returnValue = new ReturnValueClass();
            String           evalStr     = "(find-all-facts ((?f state-list)) TRUE)";

            using (FactAddressValue f = (FactAddressValue)((MultifieldValue)clipsEnvironment.Eval(evalStr))[0])
            {
                string currentID = f.GetFactSlot("current").ToString();
                if (strButtonName.Equals("Next"))
                {
                    if (initialFlag)
                    {
                        clipsEnvironment.AssertString("(next " + currentID + ")");
                    }
                    else
                    {
                        clipsEnvironment.AssertString("(next " + currentID + " " + ChoiceOption + ")");
                    }
                    returnValue = getNextUIState();
                }
                else if (strButtonName.Equals("Restart"))
                {
                    clipsEnvironment.Reset();
                    returnValue = getNextUIState();
                }
                else if (strButtonName.Equals("Prev"))
                {
                    clipsEnvironment.AssertString("(prev " + currentID + ")");
                    returnValue = getNextUIState();
                }
            }
        }
Example #4
0
 private void OnClickButton(object sender, EventArgs e)
 {
     if ((string)nextButton.Tag == "Next")
     {
         if (choicesPanel.Controls.Count != 0)
         {
             if (isNumeric)
             {
                 var nud = choicesPanel.Controls.OfType <NumericUpDown>().First();
                 var xd  = clips.AssertString("(" + relationAsserted + " " + nud.Value + ")");
                 Console.WriteLine("(" + relationAsserted + " " + nud.Value + ") ");
             }
             else if (isCheckbox)
             {
                 var checkedButtons = choicesPanel.Controls.OfType <CheckBox>()
                                      .Where(r => r.Checked);
                 foreach (var box in checkedButtons)
                 {
                     var xd = clips.AssertString("(" + relationAsserted + " " + box.Tag + ")");
                     Console.WriteLine("(" + relationAsserted + " " + box.Tag + ") ");
                 }
             }
             else if (isYesNo)
             {
                 var checkedButton = choicesPanel.Controls.OfType <RadioButton>()
                                     .First(r => r.Checked);
                 if ((string)checkedButton.Tag == "yes")
                 {
                     var xd = clips.AssertString("(" + relationAsserted + ")");
                     Console.WriteLine("(" + relationAsserted + ")");
                 }
             }
             else
             {
                 var checkedButton = choicesPanel.Controls.OfType <RadioButton>()
                                     .First(r => r.Checked);
                 var xd = clips.AssertString("(" + relationAsserted + " " + checkedButton.Tag + ")");
                 Console.WriteLine("(" + relationAsserted + " " + checkedButton.Tag + ") ");
             }
         }
         else
         {
             clips.AssertString("(start)");
         }
         clips.Run(1);
         NextUIState();
     }
     else if ((string)nextButton.Tag == "Restart")
     {
         clips.Reset();
         clips.Run(1);
         NextUIState();
     }
 }
        private void NextBtn_OnClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;

            if (button == null)
            {
                return;
            }
            const string evalStr = "(find-all-facts ((?f state-list)) TRUE)";

            using (var f = (FactAddressValue)((MultifieldValue)_theEnv.Eval(evalStr))[0])
            {
                string currentId = f.GetFactSlot("current").ToString();
                switch (button.Content.ToString())
                {
                case "Dalej":
                    if (AnswersPanel.Children.Count == 0)
                    {
                        _theEnv.AssertString("(next " + currentId + ")");
                    }
                    else
                    {
                        var t = string.Format("(next {0} {1})", currentId, GetAnswer());
                        _theEnv.AssertString(t);
                    }
                    NextUiState();
                    break;

                case "Reset":
                    _theEnv.Reset();
                    NextUiState();
                    break;

                case "Powrót":
                    _theEnv.AssertString("(prev " + currentId + ")");
                    NextUiState();
                    break;
                }
            }
        }
        private void nextButton_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            // Get the state-list.
            String evalStr = "(find-all-facts ((?f state-list)) TRUE)";

            using (FactAddressValue f = (FactAddressValue)((MultifieldValue)Env.Eval(evalStr))[0])
            {
                string currentID = f.GetFactSlot("current").ToString();

                if (button.Tag.Equals("Next"))
                {
                    if (GetCheckedChoiceButton() == null)
                    {
                        Env.AssertString("(next " + currentID + ")");
                    }
                    else
                    {
                        string temp = (string)GetCheckedChoiceButton().Tag;
                        temp = temp.Replace(' ', '_');
                        //MessageBox.Show(temp);
                        Env.AssertString("(next " + currentID + " " +
                                         temp + ")");
                    }
                    NextUIState();
                }
                else if (button.Tag.Equals("Restart"))
                {
                    Env.Reset();
                    NextUIState();
                }
                else if (button.Tag.Equals("Prev"))
                {
                    Env.AssertString("(prev " + currentID + ")");
                    NextUIState();
                }
            }
        }
Example #7
0
        public override DecisionResult IdentifyTreatment()
        {
            List <Treatment> treatments = new List <Treatment>();
            List <Action>    actions    = new List <Action>();
            List <Reason>    reasons    = new List <Reason>();

            string currentGFR = "";
            string GFRTrend   = "unknown";
            bool   Dialysis   = false;

            double       latest    = testData.Offsets().Min();
            List <Value> GfrValues = testData.Values("gfr");
            double       slope     = 0;

            if (GfrValues.Count() == 0)
            {
                Action action = new Action();
                action.text = "Please provide at least one GFR value.";
                actions.Add(action);
            }
            else if (GfrValues.Count() > 1)
            {
                List <double> weights = new List <double>();
                List <double> offsets = new List <double>();
                List <double> values  = new List <double>();
                foreach (Value value in GfrValues)
                {
                    double days       = (value.offset - latest) / 86400;
                    double new_weight = Math.Pow((1 - test_trend_decay_per_day), (double)value.value);
                    offsets.Add(value.offset);
                    values.Add((double)value.value);
                    weights.Add(Math.Max(0.125, new_weight));
                }
                double[] lsrResults = LeastSquaresWeightedBestFitLine1(offsets.ToArray(), values.ToArray(), weights.ToArray());
                //intercept = results[0];
                slope      = -lsrResults[1];
                currentGFR = testData.Value("gfr", latest).value.ToString();
                if (currentGFR == "-1")
                {
                    currentGFR = "";
                    GFRTrend   = "";
                    Dialysis   = true;
                }
                else if (GfrValues.Count > 1 && currentGFR != "")
                {
                    // double diff = (y_values[0] - intercept) / intercept;
                    GFRTrend = "stable";
                    if (slope / (double)testData.Value("gfr", latest).value > 0.1)
                    {
                        GFRTrend = "increasing";
                    }
                    if (slope / (double)testData.Value("gfr", latest).value < -0.1)
                    {
                        GFRTrend = "decreasing";
                    }
                }
            }
            else // Single GFR value
            {
                if (GfrValues.Count() > 0)
                {
                    currentGFR = testData.Value("gfr", latest).value.ToString();
                }
                if (currentGFR == "-1")
                {
                    currentGFR = "";
                    GFRTrend   = "";
                    Dialysis   = true;
                }
                else
                {
                    GFRTrend = "stable";
                }
            }

            Value  urineProduction      = testData.Value("urine production", latest);
            double urineProductionValue = 0;

            if (urineProduction.value == null || !(urineProduction.value is double))
            {
                Action action = new Action();
                action.text = "Please provide urine production as a number.";
                actions.Add(action);
            }
            else
            {
                urineProductionValue = (double)urineProduction.value;
            }
            Value  potassium      = testData.Value("potassium", latest);
            double potassiumValue = 0;

            if (potassium.value == null || !(potassium.value is double))
            {
                Action action = new Action();
                action.text = "Please provide potassium as a number.";
                actions.Add(action);
            }
            else
            {
                potassiumValue = (double)potassium.value;
            }
            Value  magnesium      = testData.Value("magnesium", latest);
            double magnesiumValue = 0;

            if (magnesium.value == null || !(magnesium.value is double))
            {
                Action action = new Action();
                action.text = "Please provide magnesium as a number.";
                actions.Add(action);
            }
            else
            {
                magnesiumValue = (double)magnesium.value;
            }
            Value  phosphorus      = testData.Value("phosphorus", latest);
            double phosphorusValue = 0;

            if (phosphorus.value == null || !(phosphorus.value is double))
            {
                Action action = new Action();
                action.text = "Please provide phosphorus as a number.";
                actions.Add(action);
            }
            else
            {
                phosphorusValue = (double)phosphorus.value;
            }
            Value  calcium      = testData.Value("calcium, ionized", latest);
            double calciumValue = 0;

            if (calcium.value == null || !(calcium.value is double))
            {
                Action action = new Action();
                action.text = "Please provide calcium as a number.";
                actions.Add(action);
            }
            else
            {
                calciumValue = (double)calcium.value;
            }
            Value  diarrhea      = testData.Value("diarrhea", latest);
            string diarrheaValue = diarrhea.value as string;

            if (diarrheaValue == null || diarrheaValue == "")
            {
                diarrheaValue = "X";
            }
            else
            {
                diarrheaValue = diarrheaValue.ToUpper();
                if (diarrheaValue[0] != 'N' && diarrheaValue[0] != 'Y')
                {
                    Action action = new Action();
                    action.text = "Please provide diarrhea as Yes, Y, No or N.";
                    actions.Add(action);
                }
                else
                {
                    diarrheaValue = diarrheaValue.Substring(0, 1);
                }
            }

            double recentKCL            = 0;
            int    recentKCLtreatements = 0;

            List <Value> kclTreatments = treatmentData.Values("kcl");

            foreach (Value kclTreatment in kclTreatments)
            {
                if (kclTreatment.offset <= 2 * 86400) // 2 days
                {
                    recentKCL += (double)kclTreatment.value;
                    recentKCLtreatements++;
                }
            }

            if (actions.Count == 0) // no errors
            {
                // Clear old values
                //clips.Eval("(clear)");
                //clips.Load("Hypokalemia.clp");
                //clips.Eval("(reset)"); // load facts from deffacts constructs.
                clips.Clear();
                clips.Load("Hypokalemia.clp");
                clips.Reset(); // load facts from deffacts constructs.

                // Set values in CLIPS
                clips.AssertString("(diagnosis (patient " + patientID + ") (name HYPOKALEMIA))");
                if (Dialysis)
                {
                    clips.AssertString("(test-value (patient " + patientID + ") (name Dialysis) (value TRUE))");
                }
                if (currentGFR != "")
                {
                    clips.AssertString("(test-value (patient " + patientID + ") (name GFR) (value " + currentGFR + "))");
                }
                if (GFRTrend != "")
                {
                    clips.AssertString("(test-value (patient " + patientID + ") (name GFR-trend) (value " + GFRTrend.ToUpper() + "))");
                }
                if (urineProduction != null)
                {
                    clips.AssertString("(test-value (patient " + patientID + ") (name Urine-production) (value " + urineProductionValue + "))");
                }
                if (potassium != null)
                {
                    clips.AssertString("(test-value (patient " + patientID + ") (name Potassium) (value " + potassiumValue + "))");
                }

                if (magnesium != null)
                {
                    clips.AssertString("(test-value (patient " + patientID + ") (name Magnesium) (value " + magnesiumValue + "))");
                }
                if (phosphorus != null)
                {
                    clips.AssertString("(test-value (patient " + patientID + ") (name Phosphorus) (value " + phosphorusValue + "))");
                }
                if (calcium != null)
                {
                    clips.AssertString("(test-value (patient " + patientID + ") (name Calcium) (value " + calciumValue + "))");
                }
                if (diarrhea != null)
                {
                    clips.AssertString("(test-value (patient " + patientID + ") (name Diarrhea) (value " + diarrheaValue + "))");
                }

                Value EnteralRoute = testData.Value("enteralroute", latest);
                if (EnteralRoute != null && (bool)EnteralRoute.value)
                {
                    clips.AssertString("(available-routes (patient " + patientID + ") (type ENTERAL))");
                }
                Value CentralRoute = testData.Value("centralroute", latest);
                if (CentralRoute != null && (bool)CentralRoute.value)
                {
                    clips.AssertString("(available-routes (patient " + patientID + ") (type CENTRAL-IV))");
                }
                Value PeripheralRoute = testData.Value("peripheralroute", latest);
                if (PeripheralRoute != null && (bool)PeripheralRoute.value)
                {
                    clips.AssertString("(available-routes (patient " + patientID + ") (type PERIPHERAL-IV))");
                }

                clips.AssertString("(test-value (patient " + patientID + ") (name Recent-KCL) (value " + recentKCL + "))");
                clips.AssertString("(test-value (patient " + patientID + ") (name Recent-KCL-Treatements) (value " + recentKCLtreatements + "))");

                //clips.Reset();
                clips.Run();

                // Get values from CLIPS
                MultifieldValue treatmentFacts = clips.Eval("(find-all-facts ((?treatment treatment)) TRUE)") as MultifieldValue;
                foreach (FactAddressValue fv in treatmentFacts)
                {
                    Treatment treatment = new Treatment();
                    //treatment.index = (int)((NumberValue)fv.GetFactSlot("index")).GetIntegerValue();
                    //treatment.type = ((LexemeValue)fv.GetFactSlot("type")).GetLexemeValue();
                    //treatment.med = ((LexemeValue)fv.GetFactSlot("med")).GetLexemeValue();
                    //treatment.quantity = ((NumberValue)fv.GetFactSlot("quantity")).GetFloatValue();
                    //treatment.units = ((LexemeValue)fv.GetFactSlot("units")).GetLexemeValue();
                    //treatment.route = ((LexemeValue)fv.GetFactSlot("route")).GetLexemeValue();
                    object tempIndex = fv.GetFactSlot("index");
                    if (tempIndex is FloatValue)
                    {
                        FloatValue tempValue = (FloatValue)tempIndex;
                        treatment.index = (float)(tempValue);
                    }
                    else
                    {
                        IntegerValue tempValue = (IntegerValue)tempIndex;
                        treatment.index = (int)(tempValue);
                    }
                    treatment.type = (string)((SymbolValue)fv.GetFactSlot("type"));
                    treatment.med  = (string)((SymbolValue)fv.GetFactSlot("med"));
                    PrimitiveValue quantitySlot = fv.GetFactSlot("quantity");
                    if (quantitySlot is FloatValue)
                    {
                        treatment.quantity = (double)((FloatValue)quantitySlot);
                    }
                    else
                    {
                        treatment.quantity = (int)((IntegerValue)quantitySlot);
                    }
                    treatment.units = (string)((SymbolValue)fv.GetFactSlot("units"));
                    treatment.route = (string)((SymbolValue)fv.GetFactSlot("route"));
                    treatments.Add(treatment);
                }

                MultifieldValue actionFacts = clips.Eval("(find-all-facts ((?action action)) TRUE)") as MultifieldValue;
                foreach (FactAddressValue fv in actionFacts)
                {
                    Action action = new Action();
                    action.text = ((SymbolValue)fv.GetFactSlot("text")).ToString().Replace("GFR-trend", "Previous GFR");
                    actions.Add(action);
                }

                MultifieldValue reasonFacts = clips.Eval("(find-all-facts ((?reason reason)) TRUE)") as MultifieldValue;
                foreach (FactAddressValue fv in reasonFacts)
                {
                    Reason reason = new Reason(
                        (int)((IntegerValue)fv.GetFactSlot("level")),
                        ((SymbolValue)fv.GetFactSlot("rule")).ToString(),
                        ((SymbolValue)fv.GetFactSlot("text")).ToString().Replace("GFR-trend", "Previous GFR"));
                    reasons.Add(reason);
                }
            }

            return(new DecisionResult(treatments, actions, reasons));
        }
Example #8
0
 public void CreateAssert(string assertString)
 {
     _theEnv.AssertString(assertString);
 }
Example #9
0
        private void OnClickButton(object sender, EventArgs e)
        {
            Button button = sender as Button;
            // Get the state-list.
            String evalStr = "(find-all-facts ((?f state-list)) TRUE)";

            using (FactAddressValue f = (FactAddressValue)((MultifieldValue)_theEnv.Eval(evalStr))[0]) {
                string currentID = f.GetFactSlot("current").ToString();

                if (button.Tag.Equals("Next"))
                {
                    if (GetCheckedChoiceButton() == null)
                    {
                        if (termometr)
                        {
                            if (goraczka)
                            {
                                _theEnv.AssertString("(next " + currentID + " " +
                                                     "Yes" + ")");
                            }
                            else
                            {
                                _theEnv.AssertString("(next " + currentID + " " +
                                                     "No" + ")");
                            }
                            termometr = false;
                        }
                        else
                        {
                            _theEnv.AssertString("(next " + currentID + ")");
                        }
                    }
                    else
                    {
                        switch ((string)GetCheckedChoiceButton().Tag)
                        {
                        case "Tak":
                            _theEnv.AssertString("(next " + currentID + " " +
                                                 "Yes" + ")");
                            break;

                        case "Nie":
                            _theEnv.AssertString("(next " + currentID + " " +
                                                 "No" + ")");
                            break;
                        }
                    }

                    NextUIState();
                }
                else if (button.Tag.Equals("Restart"))
                {
                    _theEnv.Reset();
                    NextUIState();
                }
                else if (button.Tag.Equals("Prev"))
                {
                    _theEnv.AssertString("(prev " + currentID + ")");
                    NextUIState();
                }
            }
        }
        public override DecisionResult IdentifyTreatment()
        {
            List <Treatment> treatments = new List <Treatment>();
            List <Action>    actions    = new List <Action>();
            List <Reason>    reasons    = new List <Reason>();

            //// Step 1
            //string potassiumValue = null;

            //List<Value> Values = testData.Values("potassium");
            //if (Values.Count > 0)
            //{
            //    potassiumValue = Values[0].ToString();
            //}

            //// Step 2
            //string Dialysis = "NO";

            //Values = testData.Values("dialysis");
            //if (Values.Count() > 0)
            //{
            //    Dialysis = Values[0].ToString().ToUpper();
            //}

            //// Step 3
            //string currentGFR = "";
            //string GFRTrend = "Unknown";

            //Values = testData.Values("gfr");
            //double slope = 0;
            //if (Values.Count() > 0)
            //{
            //    currentGFR = Values[0].ToString();
            //}
            //if (Values.Count() > 1)
            //{
            //    List<double> weights = new List<double>();
            //    List<double> offsets = new List<double>();
            //    List<double> values = new List<double>();
            //    foreach (Value value in Values)
            //    {
            //        double days = value.offset / 86400;
            //        offsets.Add(days);
            //        values.Add((double)value.value);
            //        double new_weight = Math.Pow((1 - test_trend_decay_per_day), days);
            //        weights.Add(Math.Max(0.125, new_weight));
            //    }
            //    double[] lsrResults = LeastSquaresWeightedBestFitLine1(offsets.ToArray(), values.ToArray(), weights.ToArray());
            //    //intercept = results[0];
            //    slope = -lsrResults[1];
            //    // double diff = (y_values[0] - intercept) / intercept;
            //    GFRTrend = "Stable";
            //    if (slope / (double)Values[0].value > 0.1)
            //    {
            //        GFRTrend = "Improving";
            //    }
            //    if (slope / (double)Values[0].value < -0.1)
            //    {
            //        GFRTrend = "Worsening";
            //    }
            //}

            //Values = testData.Values("urine production");
            //string urineProduction = null;
            //if (Values.Count() > 0)
            //{
            //    double urineProductionValue = (double)Values[0].value;
            //    if (urineProductionValue > 0.5) urineProduction = "Normal";
            //    else if (urineProductionValue >= 0.4) urineProduction = "Marginal";
            //    else if (urineProductionValue >= 0.1) urineProduction = "Oliguric";
            //    else urineProduction = "Anuric";
            //}
            //else
            //{
            //    urineProduction = "Unknown";
            //}

            //// Step 4.
            //string TTTstate = "NONE";
            //string timeToRewarming = "over-4-hours";

            //Values = testData.Values("TTT-state");
            //if (Values.Count() > 0)
            //{
            //    TTTstate = Values[0].ToString().ToUpper();
            //}

            //Values = testData.Values("time-to-rewarming");
            //if (Values.Count() > 0)
            //{
            //    timeToRewarming = Values[0].ToString();
            //}

            //// Step 5.
            //string pastLoopDiuretic = "NO";

            //Values = testData.Values("past-loop-diuretic");
            //if (Values.Count() > 0)
            //{
            //    pastLoopDiuretic = Values[0].ToString().ToUpper();
            //}

            //// Step 6.
            //string diarrheaValue = "NO";

            //Values = testData.Values("past-diarrhea");
            //if (Values.Count() > 0)
            //{
            //    diarrheaValue = Values[0].ToString().ToUpper();
            //}

            //// Step 7.
            //string PastSupplementation = "NONE";
            //string PotassiumTrend = "Stable";

            //Values = treatmentData.Values("supplement24hrs");
            //if (Values.Count() > 0 && Values[0].ToString() != "0")
            //{
            //    if(Values[0].value is double)
            //    {
            //        double value = (double)Values[0].value;
            //        if (value == 0) PastSupplementation = "NONE";
            //        else if(value <= 30) PastSupplementation = "20mEq";
            //        else if (value <= 50) PastSupplementation = "40mEq";
            //        else PastSupplementation = "60mEq";
            //    }
            //}

            //Values = testData.Values("potassium");
            //if (Values.Count() > 1)
            //{
            //    if((double)Values[0].value - (double)Values[1].value >= 0.3)
            //    {
            //        PotassiumTrend = "Increasing";
            //    }
            //    if ((double)Values[0].value - (double)Values[1].value <= -0.3)
            //    {
            //        PotassiumTrend = "Decreasing";
            //    }
            //}

            //// Step 8.
            //Values = testData.Values("potassium");
            //string potassiumRange = null;
            //if (Values.Count > 0)
            //{
            //    double potassium = (double)Values[0].value;
            //    if (3.8 <= potassium) potassiumRange = "HIGH";
            //    else if (3.4 <= potassium && potassium < 3.8) potassiumRange = "MED";
            //    else if (3.0 <= potassium && potassium < 3.4) potassiumRange = "LOW";
            //    else potassiumRange = "VLOW";
            //}

            //// Step 9.
            //// Removed by Tzvi

            //// Step 10.
            //string phosphorusRange = "UNKNOWN";

            //Values = testData.Values("phosphorus");
            //if (Values.Count > 0)
            //{
            //    double potassium = (double)Values[0].value;
            //    if (2.0 <= potassium) phosphorusRange = "HIGH";
            //    else if (1.5 < potassium && potassium < 2.0) phosphorusRange = "MED";
            //    else phosphorusRange = "LOW";
            //}

            //// Step 11.
            //string calciumValue = "1.0";

            //Values = testData.Values("calcium, ionized");
            //if (Values.Count() > 0)
            //{
            //    calciumValue = Values[0].ToString();
            //}

            //if (actions.Count == 0) // no errors
            //{
            // Clear old values
            //clips.Eval("(clear)");
            //clips.Load("Hypokalemia.clp");
            //clips.Eval("(reset)"); // load facts from deffacts constructs.
            clips.Clear();
            clips.Load("HypokalemiaV2.clp");
            clips.Reset();     // load facts from deffacts constructs.

            // Set values in CLIPS

            List <string> dataValues = DataValues();

            foreach (string dataValue in dataValues)
            {
                clips.AssertString(dataValue);
            }
////                clips.AssertString("(diagnosis (patient " + patientID + ") (name Hypokalemia))");
//                if (Dialysis != "")
//                {
//                    //(data-value (name dialysis) (patient 123456) (value NO))
//                    string dataValue = "(data-value (patient " + patientID + ") (name dialysis) (value " + Dialysis + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (currentGFR != "")
//                {
//                    //(data-value (name GFR) (patient 123456) (value 45))
//                    string dataValue = "(data-value (patient " + patientID + ") (name GFR) (value " + currentGFR + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (GFRTrend != "")
//                {
//                    //(data-value (name GFR-trend) (patient 123456) (value Stable))
//                    string dataValue = "(data-value (patient " + patientID + ") (name GFR-trend) (value " + GFRTrend + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (urineProduction != null)
//                {
//                    //(data-value (name Urine-production) (patient 123456) (value Normal))
//                    string dataValue = "(data-value (patient " + patientID + ") (name Urine-production) (value " + urineProduction + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (potassiumValue != null)
//                {
//                    //(data-value (name potassium) (patient 123456) (value 3.5))
//                    string dataValue = "(data-value (patient " + patientID + ") (name potassium) (value " + potassiumValue + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (potassiumRange != null)
//                {
//                    //(data-value (name potassium) (patient 123456) (value 3.5))
//                    string dataValue = "(data-value (patient " + patientID + ") (name potassium-range) (value " + potassiumRange + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (phosphorusRange != null)
//                {
//                    //(data-value (name phosphorus) (patient 123456) (value 2.5))
//                    string dataValue = "(data-value (patient " + patientID + ") (name phosphorus-range) (value " + phosphorusRange + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (calciumValue != null)
//                {
//                    //(data-value (name calcium) (patient 123456) (value 2.0))
//                    string dataValue = "(data-value (patient " + patientID + ") (name calcium) (value " + calciumValue + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (diarrheaValue != null)
//                {
//                    //(data-value (name past-diarrhea) (patient 123456) (value NO))
//                    string dataValue = "(data-value (patient " + patientID + ") (name past-diarrhea) (value " + diarrheaValue + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (TTTstate != null)
//                {
//                    //(data-value (name TTT-state) (patient 123456) (value COOLING))
//                    string dataValue = "(data-value (patient " + patientID + ") (name TTT-state) (value " + TTTstate + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (timeToRewarming != null)
//                {
//                    //(data-value (name time-to-rewarming) (patient 123456) (value over-4-hours))
//                    string dataValue = "(data-value (patient " + patientID + ") (name time-to-rewarming) (value " + timeToRewarming + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (pastLoopDiuretic != null)
//                {
//                    //(data-value (name past-loop-diuretic) (patient 123456) (value NO))
//                    string dataValue = "(data-value (patient " + patientID + ") (name past-loop-diuretic) (value " + pastLoopDiuretic + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (PastSupplementation != null)
//                {
//                    //(data-value (name Past-Supplementation) (patient 123456) (value NONE))
//                    string dataValue = "(data-value (patient " + patientID + ") (name Past-Supplementation) (value " + PastSupplementation + "))";
//                    clips.AssertString(dataValue);
//                }
//                if (PotassiumTrend != null)
//                {
//                    //(data-value (name Potassium-trend) (patient 123456) (value Stable))
//                    string dataValue = "(data-value (patient " + patientID + ") (name Potassium-trend) (value " + PotassiumTrend + "))";
//                    clips.AssertString(dataValue);
//                }

            clips.AssertString("(state (patient " + patientID + ") (name Step-1))");

            clips.Run();

            MultifieldValue stateFacts = clips.Eval("(find-all-facts ((?fact state)) TRUE)") as MultifieldValue;

            foreach (FactAddressValue fv in stateFacts)
            {
                string name = ((SymbolValue)fv.GetFactSlot("name"));
            }

            // Get values from CLIPS
            MultifieldValue treatmentFacts = clips.Eval("(find-all-facts ((?treatment treatment)) TRUE)") as MultifieldValue;

            foreach (FactAddressValue fv in treatmentFacts)
            {
                Treatment treatment = new Treatment();
                //treatment.index = (int)((NumberValue)fv.GetFactSlot("index")).GetIntegerValue();
                //treatment.type = ((LexemeValue)fv.GetFactSlot("type")).GetLexemeValue();
                //treatment.med = ((LexemeValue)fv.GetFactSlot("med")).GetLexemeValue();
                //treatment.quantity = ((NumberValue)fv.GetFactSlot("quantity")).GetFloatValue();
                //treatment.units = ((LexemeValue)fv.GetFactSlot("units")).GetLexemeValue();
                //treatment.route = ((LexemeValue)fv.GetFactSlot("route")).GetLexemeValue();
                object tempIndex = fv.GetFactSlot("index");
                if (tempIndex is FloatValue)
                {
                    FloatValue tempValue = (FloatValue)tempIndex;
                    treatment.index = (float)(tempValue);
                    treatment.type  = (string)((SymbolValue)fv.GetFactSlot("type"));
                }
                else if (tempIndex is IntegerValue)
                {
                    IntegerValue tempValue = (IntegerValue)tempIndex;
                    treatment.index = (int)(tempValue);
                    treatment.type  = (string)((SymbolValue)fv.GetFactSlot("type"));
                }
                else
                {
                    treatment.type = tempIndex.ToString();
                }
                //treatment.med = (string)((SymbolValue)fv.GetFactSlot("med"));
                //PrimitiveValue quantitySlot = fv.GetFactSlot("quantity");
                //if (quantitySlot is FloatValue)
                //{
                //    treatment.quantity = (double)((FloatValue)quantitySlot);
                //}
                //else
                //{
                //    treatment.quantity = (int)((IntegerValue)quantitySlot);
                //}
                //treatment.units = (string)((SymbolValue)fv.GetFactSlot("units"));
                treatment.route = ((SymbolValue)fv.GetFactSlot("routes"));
                treatments.Add(treatment);
            }

            MultifieldValue actionFacts = clips.Eval("(find-all-facts ((?action action)) TRUE)") as MultifieldValue;

            foreach (FactAddressValue fv in actionFacts)
            {
                Action action     = new Action();
                string actionText = ((SymbolValue)fv.GetFactSlot("text")).ToString();
                if (!actions.Any(a => a.text == actionText))
                {
                    action.text = actionText;
                    actions.Add(action);
                }
            }

            MultifieldValue reasonFacts = clips.Eval("(find-all-facts ((?reason reason)) TRUE)") as MultifieldValue;

            foreach (FactAddressValue fv in reasonFacts)
            {
                Reason reason = new Reason(
                    0,     // (int)((IntegerValue)fv.GetFactSlot("level")),
                    ((SymbolValue)fv.GetFactSlot("rule")).ToString(),
                    ReplaceReasonText(((SymbolValue)fv.GetFactSlot("text")).ToString()));
                reasons.Add(reason);
            }
            //}

            return(new DecisionResult(treatments, actions, reasons));
        }