Remote Procedure Call

JSON Remote Procedure Call (RPC)-based web services

PSP provides the required predicates to send queries and receive responses within Prolog across the web, which is provided by the integration between PSP and JSON-based bidirectional communication via HTTP POST. The PSP web services exchange simple messages containing a single parameter storing the plain Prolog term. Users interested in the translation between Prolog and JSON can take a look to SWI Prolog pengines (http://pengines.swi-prolog.org/). The present implementation is based on JSON-RPC 2.0 (http://jsonrpc.org/), which is much simpler and less verbose than XML. As an illustration, a Prolog call is translated to:

{
  "jsonrpc" : "2.0" ,
  "method" : "prolog" ,
  "params" : "[ ... ]",
  "id" : "{Prolog Session ID}"
}

where ... is the string representation of the goal. Similarly, the response will be:

{
  "jsonrpc" : "2.0" ,
  "result" : "[ ... ]" | null,
  "error" : null | [ "code" : -32000, "message" : "{Prolog Error Term}" ],
  "id" : "{Prolog Session ID}"
}

where ... is the string representation of the solutions to the goal.

At the present stage, it is required that the client is able to generate a Prolog call and examine a Prolog term as the reply. A request is normally sent using the HTTP POST method on a specific PSP page. The request is handled regardless of the content of the PSP page. However, the latter can be used to define predicates useful to handling of the request (e.g., authentication predicates) that are eventually called by the Prolog goal sent through JSON-RPC.

When the RPC request is sent directly from PSP the Reply term is unified either with a (possibly empty) list of solutions or the first error term raised during the call. Thus, it depends on the PSP page that is handling the request to properly process the reply. For RPC requests sent from other environments (e.g., from PHP) any error is recorded in the message field of the JSON reply. Currently, the message is identical to the Prolog error term, except that it is a string and hence it does require proper parsing.

Continue reading with some examples
Last update: Mon Nov 02 00:38:42 2015