import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import jess.*;

public class Blocks extends HttpServlet {
   public void doPost( HttpServletRequest request, HttpServletResponse response )
      throws ServletException, IOException
   {
      response.setContentType( "text/html" );

      String root = getInitParameter("root");      // root defined in zone.properties
      String debug = request.getParameter("debug");

      String move = request.getParameter("move");
      String on_top_of = request.getParameter("on-top-of");
      PrintWriter out = response.getWriter();

      out.println("<html>");
      out.println("<meta http-equiv=\"Content-Type\" content=\"text/html\" >");
      out.println("<head><title>Blocks World</title></head>");
      out.println("<body bgcolor=\"#E3CA95\" text=\"#000000\" link=\"#800000\" vlink=\"#666633\" alink=\"#1445A1\">");
      out.println("<h2>Blocks World</h2>");
      out.println("<pre><b><font face=\"Courier, Arial, sans-serif\" size=+1>");

      Rete rete = new Rete();

      rete.addOutputRouter("t", out);
      rete.addOutputRouter("WSTDOUT", out);
      rete.addOutputRouter("WSTDERR", out);

      try {
        BufferedReader br = new BufferedReader(new FileReader(root + "/sylee/courses/jess/examples/blocks.clp"));

        Jesp j = new Jesp(br, rete);
        try
        {
            j.parse(false);
            rete.reset();
            if ("yes".equals(debug)) rete.executeCommand("(watch all)");

            Fact f = new Fact("goal", rete);
            f.setSlotValue("move", new Value(move, RU.ATOM));
            f.setSlotValue("on-top-of", new Value(on_top_of, RU.ATOM));
            rete.assertFact(f);

            rete.run();
        }
        catch (JessException re)
        {
            re.printStackTrace(rete.getErrStream());
        }

    }catch(IOException e){}

    out.println("</font></b></pre></body></html>");
    out.close();

    }
}
