json_method(+Methods)- Callable
:Reply - Here, Callable is a Prolog callable term whose name and number of argument match a predicate in this module. The arguments are JSON Schema types and Reply is a JSON Schema type.
- Callable
- Callable is as above, but there is no return value. This implements JSON RPC notifications, i.e., asynchronously processed messages for which we do not wait for a reply.
For example:
:- json_method
subtract(#{type:number}, #{type:number}): #{type:number}.
subtract(A, B, R) :- R is A-B.
Methods with named arguments can be implemented
using a single argument that is an object with specified properties. For
example, the program below implements a depositing to a bank account.
The method takes an account and amount
parameter and returns the new balance. The json_rpc_error/2
throws a JSON RPC application error.
:- json_method
deposit(#{ properties:
#{ account: #{type:string},
amount: #{type:number}
}}): #{type:number},
deposit(Request, Reply),
#{account: Account, amount: Amount} :< Request =>
transaction(( retract(account(Account, Old))
-> New is Old+Amount,
asserta(account(Account, New))
; json_rpc_error(2, "Account does not exist")
)),
Reply = New.