10.44.6.2 Application Servers

If you want to get your Prolog application to be accessible from an intranet or the Internet you can use this package to embed the Prolog programs into a Java application server such as Tomcat, WebSphere, etc.

An example of how to do this is provided in pbexamples(sessionsum). This example uses sessions to keep track of users so that the application can hold a state for a user session (as in the example below, remember the sum of all expressions evaluated in the session).

     <%@ page import = "se.sics.prologbeans.*" %>
     <html>
     <head><title>Sum Calculator</title></head>
     <body bgcolor="white">
     <font size=4>Prolog Sum Calculator, enter expression to evaluate:
     <form><input type=text name=query></form>
     <%
        PrologSession pSession =
        PrologSession.getPrologSession("prolog/PrologSession", session);
        pSession.connect();
     
        String evQuery = request.getParameter("query");
        String output = "";
        if (evQuery != null) {
          Bindings bindings = new Bindings().bind("E",evQuery + '.');
          QueryAnswer answer =
             pSession.executeQuery("sum(E,Sum,Average,Count)", bindings);
          PBTerm average = answer.getValue("Average");
          if (average != null) {
             PBTerm sum = answer.getValue("Sum");
             PBTerm count = answer.getValue("Count");
     
             output = "<h4>Average =" + average + ", Sum = "
             + sum + " Count = " + count + "</h4>";
          } else {
             output = "<h4>Error: " + answer.getError() + "</h4>";
          }
       }
     %>
     <%= output  %><br></font>
     <p><hr>Powered by SICStus Prolog
     </body></html>

The example shows the code of a JSP (Java Server Page). It makes use of the method PrologSession.getPrologSession(String jndiName, HTTPSession session), which uses JNDI to look up a registered PrologSession, which is connected to the Prolog server. The variable session is in a JSP bound to the current HTTPSession, and the variable request is bound to the current HTTPRequest. Since the HTTPSession object session is specified all queries to the Prolog server will contain a session id. The rest of the example shows how to send a query and output the answer.

Example usage of sessions (from the sessionsum example) is shown below, and is from pbexamples('sessionsum/sessionsum.pl'):

     :- module(sessionsum,[main/0,sum/5]).
     :- use_module(library(prologbeans)).
     :- use_module(library(codesio), [read_from_codes/2]).
     
     %% Register the acceptable queries (session based)
     main:-
         register_query(sum(C,Sum,Average,Count),
                        sum(C,Session,Sum,Average,Count),
                        Session),
         start.
     
     %% The sum predicate which gets the information from a session database,
     %% makes some updates and then stores it back in to the session store
     %% (and returns the information back to the application server)
     sum(ExprChars, Session, Sum, Average, Count) :-
         session_get(Session, sum, 0, OldSum),
         session_get(Session, count, 0, OldCount),
         read_from_codes(ExprChars, Expr),
         Val is Expr,
         Sum is OldSum + Val,
         Count is OldCount + 1,
         Average is Sum / Count,
         session_put(Session, sum, Sum),
         session_put(Session, count, Count).

In this example a query sum/4 is registered to call sum/5 where one of the variables, Session will be bound to the session id associated to the query. The sum/5 predicate uses the session_get/4 predicate to access stored information about the particular session, and then it performs the evaluation of the expression. Finally, it updates and stores the values for this session.


Send feedback on this subject.