1 | | % (c) 2009-2019 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, |
6 | | max_reached/0, max_reached/1, |
7 | | succeed_max_call/2, succeed_max_call_id/3, |
8 | | succeed_max_initialise/0]). |
9 | | |
10 | | :- use_module(tools). |
11 | | |
12 | | :- use_module(module_information). |
13 | | :- module_info(group,infrastructure). |
14 | | :- module_info(description,'This module provides a meta call to retrieve a specified maximum number of solutions.'). |
15 | | |
16 | | :- use_module(extension('counter/counter')). |
17 | | |
18 | | :- dynamic max_reached/1. |
19 | | |
20 | ? | max_reached :- max_reached(_),!. |
21 | | |
22 | | assert_max_reached(Id) :- |
23 | | (max_reached(Id) -> true ; assert(max_reached(Id))). |
24 | | reset_max_reached :- retractall(max_reached(_)). |
25 | | |
26 | | :- meta_predicate succeed_max_call(0,-). |
27 | | :- meta_predicate succeed_max_call_id(+,0,-). |
28 | | |
29 | | succeed_max_call(Module:Call,MaxNrOfSols) :- |
30 | | succeed_max_call_id('$unknown',Module:Call,MaxNrOfSols). |
31 | | |
32 | | |
33 | | succeed_max_initialise :- counter_init,new_counter(inits_found),new_counter(constants_found),new_counter(ops_found). |
34 | | |
35 | | :- use_module(eventhandling,[register_event_listener/3]). |
36 | | :- register_event_listener(specification_initialised,succeed_max_initialise, |
37 | | 'Initialise sols_found counter.'). |
38 | | |
39 | | |
40 | | succeed_max_call_id(Id,Call,MaxNrOfSols) :- |
41 | ? | get_counter_name(Id,Counter), |
42 | ? | reset_counter(Counter), |
43 | ? | succeed_max_call_id_aux(Id,Counter,Call,MaxNrOfSols). |
44 | | |
45 | | succeed_max_call_id_aux(Id,_,_Call,MaxNrOfSols) :- |
46 | | MaxNrOfSols==0,!, |
47 | | assert_max_reached(Id),fail. |
48 | | succeed_max_call_id_aux(Id,Counter,Call,MaxNrOfSols) :- %print_message(max_call(Call,MaxnrOfSols)), |
49 | ? | call(Call), |
50 | | inc_counter(Counter,X1), |
51 | | % print_message(sol(X1)), % |
52 | | (X1>=MaxNrOfSols |
53 | | -> (!, %print_message(max_sol_found(Call,X1,MaxNrOfSols)), |
54 | | assert_max_reached(Id) |
55 | | ) |
56 | | ; true). |
57 | | |
58 | | % use different counters so that we can have at least one of each succeed_max_call pending without interaction |
59 | | get_counter_name(V,Name) :- var(V),!, Name=ops_found. |
60 | | get_counter_name('$initialise_machine',Name) :- !, Name=inits_found. |
61 | | get_counter_name('$setup_constants',Name) :- !,Name=constants_found. |
62 | | get_counter_name(_,ops_found). |
63 | | |