import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*;
import java.io.*;
import java.util.*;
import java.net.*;
import jess.*;

public class Cholesterol extends HttpServlet {
   int getInput( HttpServletRequest request, String param)
      throws ServletException, IOException
   {
	  int retValue;
      String str = request.getParameter(param);
      if (!"".equals(str)||(null!=str))
      		retValue = Integer.parseInt(str);
      		else retValue = -1;
      return retValue;
   }

   public void doPost( HttpServletRequest request, HttpServletResponse response )
      throws ServletException, IOException
   {
      response.setContentType( "text/html; charset=Big5" );

      String root = getInitParameter("root");             // root defined in zone.properties
      String jessDir = getInitParameter("jessDir");
      String name = request.getParameter("name");
      String sex = request.getParameter("sex");
      String h_ldl = request.getParameter("h-ldl");
      String htn = request.getParameter("htn");
      String smoking = request.getParameter("smoking");
      String dm = request.getParameter("dm");
      String chd = request.getParameter("chd");
      String h_chd = request.getParameter("h-chd");
      String et = request.getParameter("et");
      String pm = request.getParameter("pm");
      int age = getInput(request, "age");
      int hdl = getInput(request, "hdl");
      //int hdl_date = Integer.parseInt(request.getParameter("hdl-date"));
      int ldl = getInput(request, "ldl");
      //int ldl_date = Integer.parseInt(request.getParameter("ldl-date"));
      int chol = getInput(request, "chol");
      //int chol_date = Integer.parseInt(request.getParameter("chol-date"));
      String treatment = request.getParameter("treatment");
      //int treatment_date = Integer.parseInt(request.getParameter("treatment-date"));

      PrintWriter responseOutput = response.getWriter();

      responseOutput.println("<html>");
   responseOutput.println("<meta http-equiv=\"Content-Type\" content=\"text/html\"; charset=\"big5\">");
      responseOutput.println("<head><title>±ÀÂËµ²ªG</title></head>");
      responseOutput.println("<body bgcolor=\"#E3CA95\" text=\"#000000\" link=\"#800000\" vlink=\"#666633\" alink=\"#1445A1\">");

    try {
        BufferedReader br = new BufferedReader(new FileReader(jessDir + "choleste.clp"));

        Rete rete = new Rete();

        rete.addOutputRouter("t", responseOutput);
        rete.addOutputRouter("WSTDOUT", responseOutput);
        rete.addOutputRouter("WSTDERR", responseOutput);

        Jesp j = new Jesp(br, rete);
        try
        {
            j.parse(false);
            //rete.reset();
            //rete.run();
        rete.executeCommand("(reset)");

      Fact f = new Fact("patient1", rete);
      f.setSlotValue("sex", new Value(sex, RU.STRING));
      f.setSlotValue("name", new Value(name, RU.STRING));
      f.setSlotValue("h-chd", new Value(h_chd, RU.STRING));
      f.setSlotValue("chd", new Value(chd, RU.STRING));
      f.setSlotValue("smoking", new Value(smoking, RU.STRING));
      f.setSlotValue("dm", new Value(dm, RU.STRING));
      f.setSlotValue("et", new Value(et, RU.STRING));
      f.setSlotValue("pm", new Value(pm, RU.STRING));
      f.setSlotValue("age", new Value(age, RU.INTEGER));
      f.setSlotValue("hdl", new Value(hdl, RU.INTEGER));
      f.setSlotValue("chol", new Value(chol, RU.INTEGER));
      rete.assertFact(f);
        rete.executeCommand("(run)");
        }
        catch (JessException re)
        {
            re.printStackTrace(rete.getErrStream());
        }

    }catch(IOException e){}

    responseOutput.println("</pre></body></html>");
    responseOutput.close();

    }
}
