Power JavaDeveloping Intelligent Systems With Java and Prolog

Listing 4


/* Java 'wrapper' for astar.pro
 * Loads astar.pro and provides foreign (java) predicates
 */

class GraphSearch
{
   /**
    * the logic engine
    */
   LogicServer ls;

   /**
    * main program method. creates a JavaProlog object and passes
    * control to it
    */
   public static void main(String args[])
   {
      GraphSearch gs = new GraphSearch();
      gs.run();
   }

   /**
    * runs the prolog engine
    */
   public void run()
   {
      try
      {
         ls = new LogicServer();
         ls.Init("");

         ls.AddPred("result", 1, "GraphSearch", "result", this);
         ls.AddPred("distance_calc", 5, "GraphSearch", "distCalc", this);
         
         ls.Load("amzi4.xpl");
         ls.ExecStr("consult(astar)");
         ls.ExecStr("astar_search(a, h)");
      }
      catch(LSException e)
      {
         e.printStackTrace();
         System.exit(1);
      }
   }

   /**
    * prints the term passed to this predicate
    * @return true if the printing succeeds
    */
   public boolean result()
   {
      // System.out.println("--> result");
      String answer;
      boolean tf;

      try
      {
         long term = ls.GetParm(1);
         String result = ls.TermToStr(term, 200);
         System.out.println(result);
         
         // System.out.println("<-- result");
         return true;

      }
      catch (LSException e)
      {
         System.out.println(e.GetMsg());
         e.printStackTrace();
         return false;
      }
   }

   /**
    * This method calculates the distance between two points
    * on the map. The x,y coordinates are obtained by making
    * calls to the LogicServer
    * @return true if the calculation succeeds
    */
   public boolean distCalc()
   {
      // System.out.println("--> distCalc");
      // Called from prolog like this:
      // distance_calc(X1, Y1, X2, Y2, Dist).

      try
      {
         // get (x,y) for first point
         long term = ls.GetParm(1);
         String result = ls.TermToStr(term, 80);
         double x1 = Double.parseDouble(result);
         
         term = ls.GetParm(2);
         result = ls.TermToStr(term, 80);
         double y1 = Double.parseDouble(result);

         // get (x,y) for second point
         term = ls.GetParm(3);
         result = ls.TermToStr(term, 80);
         double x2 = Double.parseDouble(result);

         term = ls.GetParm(4);
         result = ls.TermToStr(term, 80);
         double y2 = Double.parseDouble(result);

         // calculate distance between the two points
         double dist = Math.sqrt(((x1-x2) * (x1-x2)) + ((y1-y2) * (y1-y2)));
            
         String predicateResult = new Double(dist).toString();
         long  predRes = ls.StrToTerm(predicateResult);

         // Unify the fifth argument with the result
         // When the call to the external predicate returns,
         // the fifth argument contains the distance between
         // the two points specified by the first four args.
         ls.UnifyParm(5, predRes);

         // System.out.println("<-- distCalc");
         return true;
      }
      catch (LSException e)
      {
         System.out.println(e.GetMsg());
         return false;
      }
   }
}

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.