| 1 | % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, | |
| 2 | % Heinrich Heine Universitaet Duesseldorf | |
| 3 | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) | |
| 4 | ||
| 5 | :- module(succeed_max,[reset_max_reached/0, reset_max_reached/1, | |
| 6 | max_reached/0, max_reached/1, | |
| 7 | succeed_max_call/2, succeed_max_call_id/3, | |
| 8 | succeed_max_initialise/0, | |
| 9 | assert_max_reached/1 % mainly for operation caching | |
| 10 | ]). | |
| 11 | ||
| 12 | :- use_module(self_check). | |
| 13 | :- use_module(debug,[debug_format/3]). | |
| 14 | ||
| 15 | :- use_module(module_information). | |
| 16 | :- module_info(group,infrastructure). | |
| 17 | :- module_info(description,'This module provides a meta call to retrieve a specified maximum number of solutions.'). | |
| 18 | ||
| 19 | :- use_module(extension('counter/counter'), | |
| 20 | [counter_init/0, new_counter/1, inc_counter/2, reset_counter/1]). | |
| 21 | ||
| 22 | :- dynamic max_reached/1. | |
| 23 | ||
| 24 | max_reached :- max_reached(_),!. | |
| 25 | ||
| 26 | % assert that for Id a maximum number of solutions was reached, | |
| 27 | % typically Id is an operation name; meaning that not all transitions were computed for it | |
| 28 | :- assert_must_succeed((succeed_max:reset_max_reached, succeed_max:assert_max_reached(_), | |
| 29 | succeed_max:max_reached('$unknown'), \+ succeed_max:max_reached(sm_test_op), | |
| 30 | succeed_max:reset_max_reached)). | |
| 31 | :- assert_must_succeed((succeed_max:reset_max_reached, succeed_max:assert_max_reached(sm_test_op), | |
| 32 | succeed_max:assert_max_reached(_), succeed_max:max_reached('$unknown'), | |
| 33 | succeed_max:reset_max_reached)). | |
| 34 | :- assert_must_succeed((succeed_max:reset_max_reached, | |
| 35 | findall(X,succeed_max:succeed_max_call_id(_,between:between(1,3,X),2),L), L==[1,2], | |
| 36 | succeed_max:max_reached('$unknown'), \+ succeed_max:max_reached(sm_test_op), | |
| 37 | succeed_max:reset_max_reached)). | |
| 38 | % an unbound Id is stored as '$unknown': a fact max_reached(_) would match every operation name | |
| 39 | assert_max_reached(Id) :- var(Id), !, assert_max_reached('$unknown'). | |
| 40 | assert_max_reached(Id) :- | |
| 41 | (max_reached(Id) -> true | |
| 42 | ; assertz(max_reached(Id)), | |
| 43 | debug_format(19,'Maximum solutions reached for ~w~n',[Id]) | |
| 44 | ). | |
| 45 | ||
| 46 | reset_max_reached(Id) :- retractall(max_reached(Id)). | |
| 47 | reset_max_reached :- | |
| 48 | ? | (max_reached(_) -> retractall(max_reached(_)) ; true). % retractall seems sometimes expensive in SICStus |
| 49 | ||
| 50 | :- meta_predicate succeed_max_call(0,-). | |
| 51 | :- meta_predicate succeed_max_call_id(+,0,-). | |
| 52 | ||
| 53 | succeed_max_call(Call,MaxNrOfSols) :- | |
| 54 | succeed_max_call_id('$unknown',Call,MaxNrOfSols). | |
| 55 | ||
| 56 | ||
| 57 | succeed_max_initialise :- counter_init,new_counter(inits_found),new_counter(constants_found),new_counter(ops_found). | |
| 58 | ||
| 59 | :- use_module(eventhandling,[register_event_listener/3]). | |
| 60 | :- register_event_listener(specification_initialised,succeed_max_initialise, | |
| 61 | 'Initialise sols_found counter.'). | |
| 62 | ||
| 63 | succeed_max_call_id(Id,Call,MaxNrOfSols) :- | |
| 64 | get_counter_name(Id,Counter), | |
| 65 | reset_counter(Counter), | |
| 66 | ? | succeed_max_call_id_aux(Id,Counter,Call,MaxNrOfSols). |
| 67 | ||
| 68 | :- assert_must_succeed((succeed_max:reset_max_reached(sm_test), | |
| 69 | findall(X,succeed_max:succeed_max_call_id(sm_test,between:between(1,3,X),3),L), L==[1,2,3], | |
| 70 | \+ succeed_max:max_reached(sm_test))). % exactly 3 solutions: max not reached | |
| 71 | :- assert_must_succeed((succeed_max:reset_max_reached(sm_test), | |
| 72 | findall(X,succeed_max:succeed_max_call_id(sm_test,between:between(1,3,X),2),L), L==[1,2], | |
| 73 | succeed_max:max_reached(sm_test), succeed_max:reset_max_reached(sm_test))). | |
| 74 | :- assert_must_succeed((succeed_max:reset_max_reached(sm_test), | |
| 75 | findall(X,succeed_max:succeed_max_call_id(sm_test,between:between(1,3,X),5),L), L==[1,2,3], | |
| 76 | \+ succeed_max:max_reached(sm_test))). | |
| 77 | :- assert_must_succeed((succeed_max:reset_max_reached(sm_test), % choicepoint left after the last solution: we cannot know | |
| 78 | findall(X,succeed_max:succeed_max_call_id(sm_test,(lists:member(X,[1,2]) ; fail),2),L), L==[1,2], | |
| 79 | succeed_max:max_reached(sm_test), succeed_max:reset_max_reached(sm_test))). | |
| 80 | :- assert_must_succeed((succeed_max:reset_max_reached(sm_test), | |
| 81 | \+ succeed_max:succeed_max_call_id(sm_test,true,0), | |
| 82 | succeed_max:max_reached(sm_test), succeed_max:reset_max_reached(sm_test))). | |
| 83 | succeed_max_call_id_aux(Id,_,_Call,MaxNrOfSols) :- | |
| 84 | MaxNrOfSols==0,!, | |
| 85 | assert_max_reached(Id),fail. | |
| 86 | succeed_max_call_id_aux(Id,Counter,Call,MaxNrOfSols) :- | |
| 87 | ? | call_cleanup(Call,Det=true), % Det is bound if Call has exited deterministically, i.e., has no further solutions |
| 88 | inc_counter(Counter,X1), | |
| 89 | (X1>=MaxNrOfSols | |
| 90 | -> % we have to check Det before the cut below; the cut would run the cleanup goal and bind Det | |
| 91 | (Det==true | |
| 92 | -> true % exactly MaxNrOfSols solutions exist: the maximum was not exceeded | |
| 93 | ; assert_max_reached(Id) % there may be further solutions | |
| 94 | ), | |
| 95 | ! | |
| 96 | ; true). | |
| 97 | ||
| 98 | % use different counters so that we can have at least one of each succeed_max_call pending without interaction | |
| 99 | get_counter_name(V,Name) :- var(V),!, Name=ops_found. | |
| 100 | get_counter_name('$initialise_machine',Name) :- !, Name=inits_found. | |
| 101 | get_counter_name('$setup_constants',Name) :- !,Name=constants_found. | |
| 102 | get_counter_name(_,ops_found). | |
| 103 |