Node:Jasper Package Class Reference, Next:, Previous:Calling Prolog from Java, Up:Mixing Java and Prolog



Jasper Package Class Reference

Detailed documentation of the classes in the jasper package can be found in the HTML documentation installed with SICStus and also at the SICStus documentation page (http://www.sics.se/sicstus/docs/).

Please note: None of the se.sics.jasper methods are thread safe, unless explicitly mentioned, they can only be called from the thread that created the SICStus object. (This is different from how se.sics.jasper worked in SICStus 3.8)

However, Jasper in SICStus 3.9 supports multi threaded mode. Several Java threads can access SICStus runtime through a server thread which does the actual calls.

The API is defined by three interfaces: Prolog, Query and Term. The methods of these interfaces are implemented by inner classes of the Jasper server. Instances of these inner classes are returned by methods of the class Jasper and can then be used from multiple threads by the Java programmer.

In multi threaded mode the Java programmer obtains an object implementing the interface Prolog. That interface has methods similar to the methods of the SICStus class described below. Interface Query and interface Term have the same relations to class SPQuery and class SPTerm, respectively. In addition the SICStus class, the SPQuery class and the SPTerm class all implement the above interfaces. The methods of the interfaces are preferred over the old methods.

See the HTML documentation for details on the methods of the interfaces.

See Jasper Notes, for limitations in multi threaded Jasper.

boolean query (String module, String name, SPTerm args[]) Method on SICStus
Call name with args (a vector of SPTerm objects). Like once(Module:Name(Args...)).

Returns true if the call succeeded, false if the call failed, i.e. there were no solutions.

Introduced in SICStus 3.8.5.

boolean query (String goal, Map varMap) Method on SICStus
Call a goal specified as a string.

goal
The textual representation of the goal to execute, with terminating period.
varMap
A map from variable names to SPTerm objects. Used both for passing variable bindings into the goal and to obtain the bindings produced by the goal. May be null.

On success, the values of variables with names that do not start with underscore (_) will be added to the map.

Returns true if the call succeeded, false if the call failed, i.e. there were no solutions.

HashMap varMap = new HashMap();

varMap.put("File", new SPTerm(sp, "datafile.txt"));

if (sp.query("see(File),do_something(Result),seen.", varMap)) {
   System.out.println("Result==" + ((SPTerm)varMap.get("Result")).toString());
} else {
   System.out.println("Failed);
}

Introduced in SICStus 3.8.5.

boolean query (SPPredicate pred, SPTerm args[]) Method on SICStus
Obsolescent version of SICStus::query() above.

boolean queryCutFail (String module, String name, SPTerm args[]) Method on SICStus
Call name with args for side effect only.

As SICStus.query() it only finds the first solution, and then it cuts away all other solutions and fails.

It corresponds roughly to the following Prolog code:

   ( \+ call(Module:Name(Args...)) -> fail; true )
Introduced in SICStus 3.8.5.

boolean queryCutFail (String goal, Map varMap) Method on SICStus
Call a goal specified as a string, for side effect only. The map is only used for passing variable bindings into the goal. See query for details

Introduced in SICStus 3.8.5.

boolean queryCutFail (SPPredicate pred, SPTerm args[]) Method on SICStus
Obsolescent version of queryCutFail above.

SPQuery openQuery (String module, String name, SPTerm args[]) Method on SICStus
Sets up a query (an object of class SPQuery) which can later be asked to produce solutions. You must close an opened query when no more solutions are required; see below.

It corresponds roughly to the following Prolog code:

   ( true     % just calling openQuery does not call the predicate

   ; % failing (nextSolution) will backtrack for more solutions
     call(Module:Name(Args...))
   )

Introduced in SICStus 3.8.5.

boolean openQuery (String goal, Map varMap) Method on SICStus
Sets up a query specified as a string. See openQuery and query for details.

Introduced in SICStus 3.8.5.

SPQuery openQuery (SPPredicate pred, SPTerm args[]) Method on SICStus
Obsolescent version of openQuery above.

The following methods are used to obtain solutions from an opened query and to tell the SICStus run-time system that no more answers are required.

boolean nextSolution () Method on SPQuery
Obtain the next solution. Returns true on success and false if there were no more solutions. When you are no longer interested in any more solutions, you should call SPQuery.close or SPQuery.cut to close the query.

Returns true if a new solution was produced, false if there were no more solutions. This corresponds roughly to fail/0. See SPTerm and Memory, for additional details.

close () Method on SPQuery
Cut and fail away any previous solution to the query. After closing a query object, you should not use it anymore. This corresponds roughly to !, fail. See SPTerm and Memory, for additional details.

cut () Method on SPQuery
Cut, but do not fail away, any previous solution. After closing a query object with cut, you should not use it anymore. This corresponds roughly to !. See SPTerm and Memory, for additional details.