Add argument with extra info to callback
This commit is contained in:
@@ -205,20 +205,20 @@ instruction_match(Term, VarList) :-
|
||||
submit_query_and_print_results(Term, VarList)
|
||||
).
|
||||
|
||||
%% run_query(+QueryChars, +Callback_1, +Options)
|
||||
%% run_query(+QueryChars, +Callback_2, +Options)
|
||||
%
|
||||
% Runs a query from a string of chars, calling `Callback_1` on each leaf answer.
|
||||
% Runs a query from a string of chars, calling `Callback_2` on each leaf answer.
|
||||
% See `run_query_goal/4` for details.
|
||||
run_query(QueryChars, Callback_1, Options) :-
|
||||
run_query(QueryChars, Callback_2, Options) :-
|
||||
read_term_from_chars(QueryChars, QueryGoal, [variable_names(VarNames)]),
|
||||
run_query_goal(QueryGoal, VarNames, Callback_1, Options).
|
||||
run_query_goal(QueryGoal, VarNames, Callback_2, Options).
|
||||
|
||||
%% run_query_goal(+QueryGoal, +VarNames, +Callback_1, +Options)
|
||||
%% run_query_goal(+QueryGoal, +VarNames, +Callback_2, +Options)
|
||||
%
|
||||
% Run a query from a goal, calling `Callback_1 on each leaf answer.
|
||||
% Run a query from a goal, calling `Callback_2` on each leaf answer.
|
||||
% `VarNames` needs to have the same format as the one from the `variable_names(-VarNames)`
|
||||
% option in `read_term/3`. That is, a list of terms of the form `Name=Var`, where `Name`
|
||||
% is an atom and `Var` is a variable. The possible arguments to `Callback_1` are:
|
||||
% is an atom and `Var` is a variable. The possible first arguments to `Callback_2` are:
|
||||
%
|
||||
% - `final(false)`
|
||||
% - `final(exception(Exception))`, where `Exception` is the exception thrown
|
||||
@@ -237,9 +237,11 @@ run_query(QueryChars, Callback_1, Options) :-
|
||||
% The variants with principal functor `final/1` mean that there will be no more leaf answers,
|
||||
% and the ones with `pending/1` mean that there will be more leaf answers.
|
||||
%
|
||||
% The second argument is a list with extra information that can be activated with options.
|
||||
%
|
||||
% `Option` is a list of options. There are none currently, but in the future support for
|
||||
% inference limits and timeouts may be implemented.
|
||||
run_query_goal(QueryGoal, VarNames, Callback_1, _) :-
|
||||
run_query_goal(QueryGoal, VarNames, Callback_2, _) :-
|
||||
% The b value in the WAM basically represents which choicepoint we are at.
|
||||
% By recording it before and after we can then compare the values to know
|
||||
% if we are still inside the query or not.
|
||||
@@ -256,11 +258,11 @@ run_query_goal(QueryGoal, VarNames, Callback_1, _) :-
|
||||
),
|
||||
( Excepted == true ->
|
||||
!,
|
||||
call(Callback_1, final(exception(Exception)))
|
||||
call(Callback_2, final(exception(Exception)), [])
|
||||
; ( VarNames == [], ResGoals == [] ->
|
||||
( Pending == true ->
|
||||
call(Callback_1, pending(true))
|
||||
; call(Callback_1, final(true))
|
||||
call(Callback_2, pending(true), [])
|
||||
; call(Callback_2, final(true), [])
|
||||
)
|
||||
; copy_term([Vars1, ResVars], [Vars1, ResVars], ResGoals),
|
||||
term_variables(ResGoals, ResGoalVars),
|
||||
@@ -272,16 +274,16 @@ run_query_goal(QueryGoal, VarNames, Callback_1, _) :-
|
||||
term_variables(Vars3, Vars4), % deduplicate vars of Vars1 but preserve their order.
|
||||
charsio:extend_var_list(Vars4, VarNames, NewVarNames1, fabricated),
|
||||
( Pending == true ->
|
||||
call(Callback_1, pending(leaf_answer(Bindings, ResGoals, NewVarNames1)))
|
||||
; call(Callback_1, final(leaf_answer(Bindings, ResGoals, NewVarNames1)))
|
||||
call(Callback_2, pending(leaf_answer(Bindings, ResGoals, NewVarNames1)), [])
|
||||
; call(Callback_2, final(leaf_answer(Bindings, ResGoals, NewVarNames1)), [])
|
||||
)
|
||||
)
|
||||
).
|
||||
run_query_goal(_, _, Callback_1, _) :-
|
||||
run_query_goal(_, _, Callback_2, _) :-
|
||||
% If the whole query failed or we didn't cut in the previous definition of
|
||||
% run_query_goal/4 (which means we are still in the query but it has failed)
|
||||
% then we get here so we have a (tail) false.
|
||||
call(Callback_1, final(false)).
|
||||
call(Callback_2, final(false), []).
|
||||
|
||||
submit_query_and_print_results(QueryTerm, VarNames) :-
|
||||
bb_put('$answer_count', 0),
|
||||
@@ -304,12 +306,12 @@ increment_answer_count :-
|
||||
Count is Count0 + 1,
|
||||
bb_put('$answer_count', Count).
|
||||
|
||||
toplevel_query_callback(pending(LeafAnswer)) :-
|
||||
toplevel_query_callback(pending(LeafAnswer), _) :-
|
||||
handle_first_answer,
|
||||
increment_answer_count,
|
||||
write_leaf_answer(LeafAnswer, []),
|
||||
read_input(LeafAnswer).
|
||||
toplevel_query_callback(final(LeafAnswer)) :-
|
||||
toplevel_query_callback(final(LeafAnswer), _) :-
|
||||
( exception(Exception) = LeafAnswer ->
|
||||
print_exception(Exception)
|
||||
; handle_first_answer,
|
||||
|
||||
Reference in New Issue
Block a user