Interpreter Stub

The solution will replace the query interpreter by a stub. This stub will invoke the Prolog agent. We might ask why we invoke the Prolog agent here and do not directly go to the web server. This could be archive solely from within Java by for example using the URL class. The reason is simple, we want to show with this example how Prolog code could be used on the client side. Jekejeke Prolog has built-in support for streams that are opened via an URL. In the current case this is enough to implement a Prolog agent.

The interpreter stub has thus the simple responsibility of invoking the Prolog agent. We have already manually invoked the Prolog agent from development environment for testing purpos-es. The interpreter stub will generate the exact same query we have already seen. The inter-face between the client and the interpreter stub will be the same as between the standalone and the query interpreter. The Prolog text of the Prolog agent will be consulted in the method initKnowledgebase(). The query with the given functor will then be dynamically built by the method makeQuery().The corresponding code reads as follows:

     * <p>Create the query term.</p>
     * @param vars The query variables.
     * @return The query term.
public AbstractTerm makeQuery(TermVar[] vars) {
Object[] args = new Object[7];
args[0] = firstname;
args[1] = name;
args[2] = agefrom;
args[3] = ageto;
args[4] = salaryfrom;
args[5] = salaryto;
args[6] = new TermCompound(inter, "employee",
return new TermCompound(inter, functor, args);

As can be seen the above code will generate a query which consists of a single predicate in-vocation. The implementation of the rest of the interface parallels the implementation of the query interpreter. More details are found in the appendix. We have also placed the Java meth-od for the encode_parameter/2 foreign predicate in the interpreter stub. The method is static, since currently we can only declare static Java methods as foreign predicates. The method has a String return type since it directly returns the result:

     * <p>Java foreign predicate url_encode_utf8/2.</p>
     * @param s The string.
* @return The encoded string.
public static String encodeParameter(String s) {
return ForeignUri.encode(s, false,
ForeignUri.NEEDS_COMP, ForeignUri.ENCODING_UTF8);

As can be seen our Java foreign function interface does allow a lot of automatism. One autom-atism is the checking and conversion of input parameters. A second automatism is that output parameters and exceptions are also mapped. The Prolog Java interface allows for extension of the Prolog system. So instead of using the already defined open/3 predicate, we could also provide new built-ins for socket communication that match another web server and let the Prolog agent use these means. More information about the Prolog Java interface can be found in the programming interface documentation.