-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.pl
More file actions
128 lines (100 loc) · 6.53 KB
/
configuration.pl
File metadata and controls
128 lines (100 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
:- module(configuration,
[
deserialise_configuration/2,
configuration_planner_command/2, configuration_domain_filename/2, configuration_problem_filename/2,
configuration_result_filename/2, configuration_nb_tests/2, configuration_generators/2,
configuration_heuristic/2, configuration_metamorphic_relation/2, configuration_run_all_tests/2
]).
:- use_module(read_file).
:- use_module(library(plunit)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CONFIGURATION STRUCTURE AND ACCESSORS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% configuration(_PlannerCommand, _DomainFilename, _ProblemFilename, _ResultFilename, _MetamorphicRelation, _NumberOfTests, _RunAllTests, _GeneratorsPredicates, _HeuristicPredicate).
configuration_planner_command(configuration(PlannerCommand, _, _, _, _, _, _, _, _), PlannerCommand).
configuration_domain_filename(configuration(_, DomainFilename, _, _, _, _, _, _, _), DomainFilename).
configuration_problem_filename(configuration(_, _, ProblemFilename, _, _, _, _, _, _), ProblemFilename).
configuration_result_filename(configuration(_, _, _, ResultFilename, _, _, _, _, _), ResultFilename).
configuration_metamorphic_relation(configuration(_, _, _, _, MetamorphicRelation, _, _, _, _), MetamorphicRelation).
configuration_nb_tests(configuration(_, _, _, _, _, NumberOfTests, _, _, _), NumberOfTests).
configuration_run_all_tests(configuration(_, _, _, _, _, _, RunAllTests, _, _), RunAllTests).
configuration_generators(configuration(_, _, _, _, _, _, _, GeneratorsPredicates, _), GeneratorsPredicates).
configuration_heuristic(configuration(_, _, _, _, _, _, _, _, HeuristicPredicate), HeuristicPredicate).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CONFIGURATION PREDICATES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% deserialise_configuration(+Filename, -Configuration).
deserialise_configuration(Filename, Configuration) :- deserialise_configuration(Filename, Configuration, []).
%% deserialise_configuration(+Filename, -Configuration, -RestOfFile).
deserialise_configuration(Filename, Configuration, RestOfFile) :-
read_file(Filename, List),
json_configuration(Configuration, List, RestOfFile),
% grounds free variables of the configuration (in case of optional parameters)
instantiate_optional_parameters(Configuration).
json_configuration(configuration(PC, DF, PF, RF, MR, NbTests, RAT, Gen, H))
--> ['{'], key_string_value(command, PC), [','],
key_string_value(domain, DF), [','],
key_string_value(problem, PF), [','],
(key_string_value(result, RF), [','] ; []), % result filename is optional
key_string_value(metamorphic_relation, MR), [','],
(key_integer_value(number_of_tests, NbTests), [','] ; []), % number of tests optional
(key_boolean_value(run_all_tests, RAT), [','] ; []), % run_all_tests is optional
key_objects(generators, argument, Gen), [','],
key_object_value(heuristic, argument, H),
['}'].
key_string_value(Key, StringValue) --> [Key], [':'], string_value(StringValue).
key_integer_value(Key, IntegerValue) --> [Key], [':'], integer_value(IntegerValue).
key_boolean_value(Key, BooleanValue) --> [Key], [':'], boolean_value(BooleanValue).
key_object_value(Key, Object, ObjectValue) --> [Key], [':'], object_value(Object, ObjectValue).
key_objects(Key, Object, Objects) --> [Key], [':'], ['['], maybe_more(Object, Objects), [']'].
object_value(ObjectName, ObjectValue, Input, Output) :-
Rule =.. [ObjectName, ObjectValue, Input, Output],
Rule.
maybe_more(_Object, []) --> [].
maybe_more(Object, [Element|Tail]) --> object_value(Object, Element), [','], maybe_more(Object, Tail).
maybe_more(Object, [Element]) --> object_value(Object, Element).
%% rules called by object_value/2 must return atom and not list !
argument(Argument) --> ['{'], key_string_value(name, Name), [','], key_objects(arguments, element, Args), ['}'], {Argument =.. [Name|Args]}.
argument(ArgumentWithoutParameter) --> ['{'], key_string_value(name, ArgumentWithoutParameter), ['}'].
element(Element) --> [Element].
%% tokens
boolean_value(true) --> [true].
boolean_value(false) --> [false].
integer_value(I) --> [I], {integer(I)}.
string_value(V) --> [V], {integer(V), !, fail}.
string_value(V) --> [V], {float(V), !, fail}.
string_value(V) --> [V].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CONFIGURATION HELPER PREDICATES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
instantiate_optional_parameters(Configuration) :-
configuration_result_filename(Configuration, ResultFilename),
(ground(ResultFilename) -> true ; ResultFilename = 'test.csv'),
configuration_nb_tests(Configuration, NumberOfTests),
(ground(NumberOfTests) -> true ; NumberOfTests = 15),
configuration_run_all_tests(Configuration, RAT),
(ground(RAT) -> true ; RAT = true).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PLUNIT TESTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- begin_tests(basic_configuration).
get_configuration(configuration('planner_command', 'domain_filename', 'problem_filename', 'result_filename', mr0, 42, false, [generator0(2), generator1(0,2)], heuristic0)).
test(planner_command_accessor, [setup(get_configuration(Config))]) :-
configuration_planner_command(Config, 'planner_command').
test(domain_filename_accessor, [setup(get_configuration(Config))]) :-
configuration_domain_filename(Config, 'domain_filename').
test(problem_filename_accessor, [setup(get_configuration(Config))]) :-
configuration_problem_filename(Config, 'problem_filename').
test(result_filename_accessor, [setup(get_configuration(Config))]) :-
configuration_result_filename(Config, 'result_filename').
test(metamorphic_relation_accessor, [setup(get_configuration(Config))]) :-
configuration_metamorphic_relation(Config, mr0).
test(nb_tests_accessor, [setup(get_configuration(Config))]) :-
configuration_nb_tests(Config, 42).
test(run_all_tests_accessor, [setup(get_configuration(Config))]) :-
configuration_run_all_tests(Config, false).
test(generators_accessor, [setup(get_configuration(Config))]) :-
configuration_generators(Config, [generator0(2),generator1(0,2)]).
test(heuristic_accessor, [setup(get_configuration(Config))]) :-
configuration_heuristic(Config, heuristic0).
:- end_tests(basic_configuration).