import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import java.util.List;

import CLIPSJNI.*;

public class Blocksjni 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 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>");

      System.load("c:/clips/CLIPSJNI/CLIPSJNI.dll");

      Environment clips = new Environment();
      clips.load(root + "/sylee/courses/clips/clipsjni/examples/blocks.clp");

      clips.reset();
      clips.assertString("(goal (move " + move + ") (on-top-of " + on_top_of + "))");

      clips.run();

      String evalStr = "(find-all-facts ((?f action)) TRUE)";
      MultifieldValue pv = (MultifieldValue) clips.eval(evalStr);
      int tNum = pv.listValue().size();
      if (tNum == 0) return;
      List theList = pv.listValue();
      FactAddressValue fv;
      for (int i=0; i<tNum; i++)
        {
         fv = (FactAddressValue) pv.listValue().get(i);
         move = fv.getFactSlot("move").toString();
         on_top_of = fv.getFactSlot("on-top-of").toString();
         out.println("<p>" + move + " moved on top of " + on_top_of + ".");
        }

    out.println("</font></b></pre></body></html>");
    out.close();
    }
}
