-
Notifications
You must be signed in to change notification settings - Fork 2
/
ailp.pl
executable file
·164 lines (154 loc) · 7.6 KB
/
ailp.pl
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env swipl
/*
* ailp.pl
*
* AI and Logic Programming assignment runner.
* Loads and runs specified assignmentN_library.pl file from
* assignmentN folder.
*/
test_wiki_links :-
bagof(A, wp:actor(A), As),
test_wiki_link(As).
test_wiki_link([A|As]) :-
( wp:actor_links(A, _) -> write(A), write(' OK\n')
; otherwise -> write(A), write(' not working\n')
), test_wiki_link(As).
test_wiki_link([]) :- halt.
test_wiki_deduction :-
( wp:subset_links(_, _) -> write('Undistinguishable actors! Please update wikipedia identities.\n\n')
; otherwise -> write('All actors can be uniquely identified.\n\n')).
find_submission(Assignment, Submission) :-
directory_files('./', Files),
( Assignment='assignment1' -> find_file('assignment1_', Files, Submission)
; Assignment='oscar' -> find_file('assignment2_', Files, Submission)
; Assignment='wp' -> find_file('assignment2_wp_', Files, Submission)
).
find_file(_, [], _) :- fail.
find_file(Prefix, [F|Files], Submission) :-
( check_file(Prefix, F) -> F=Submission
; otherwise -> find_file(Prefix, Files, Submission)
).
check_file(Prefix, File) :-
atom_concat(Basename, '.pl', File),
atom_concat(Prefix, Candidate, Basename),
atom_number(Candidate, _).
translate_input_options(Args, Assignment_name, Part) :-
( Args=['assignment1'|[]] -> Assignment_name='assignment1',Part=0
; Args=['assignment2','part1'|[]] -> Assignment_name='oscar',Part=1
; Args=['assignment2','part2'|[]] -> Assignment_name='wp',Part=2
; Args=['assignment2','part3'|[]] -> Assignment_name='oscar',Part=3
; Args=['assignment2','part4'|[]] -> Assignment_name='oscar',Part=4
; Args=['test_wiki_links'|[]] -> Assignment_name='oscar',Part=99
; otherwise -> fail
).
read_in_choice(Assignment_name, Part) :-
nl, write('Please input `> assignment_name [assignment_part]`'),nl,
write(' e.g. `assignment1` or `assignment2 part2`'),nl,
write('> '), read_line_to_codes(user_input, InputCodes),
atom_codes(InputAtom, InputCodes),
atomic_list_concat(InputList, ' ', InputAtom),
( translate_input_options(InputList, Assignment_name, Part) -> true
; read_in_choice(Assignment_name, Part)
).
:- dynamic
user:prolog_file_type/2,
user:file_search_path/2.
:- multifile
user:prolog_file_type/2,
user:file_search_path/2.
:- % parse command line arguments
current_prolog_flag(argv, Args),
% take first argument to be the name of the assignment folder
( translate_input_options(Args, Assignment_name, Part) -> true
; % missing argument, so display syntax and halt
nl,
write('Syntax:'),nl,
nl,
write(' ./ailp.pl <assignment_name> [part]'),nl,
write('e.g. ./ailp.pl assignment1'),nl,
write('or'),nl,
write('e.g. ./ailp.pl assignment2 part1'),nl,
write('Alternatively, to test the wikipedia links and identities do:'),nl,
write(' ./ailp.pl test_wiki_links'),nl,
nl,
write('~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+'),nl,
nl,
read_in_choice(Assignment_name, Part)
),
assert(assignment(Assignment_name)),
assert(sub_assignment(Part)).
get_assignment_details :- % get assignment details
assignment(Assignment_name),
sub_assignment(Assignment_part),
atom_concat(Assignment_name, '_library', Assignment_library_name),
%
% define a module path assignment_root()
prolog_load_context(directory, Sys),
(\+ user:file_search_path(assignment_root, Sys)
-> asserta(user:file_search_path(assignment_root, Sys))
),
%
% define our own ailp path assignment_ailp()
atom_concat(Sys, '/ailp', Ailp),
(\+ user:file_search_path(assignment_ailp, Ailp)
-> asserta(user:file_search_path(assignment_ailp, Ailp))
),
%
% define our own Library path assignment_library()
atom_concat(Ailp, '/library', Lib),
(\+ user:file_search_path(assignment_library, Lib)
-> asserta(user:file_search_path(assignment_library, Lib))
),
%
% find candidate submission
find_submission(Assignment_name, Submission),
%
% load files
load_files(
[ %assignment_ailp(command_channel),
%assignment_library(Assignment_library_name),
assignment_root(Submission)
],
[ silent(true)
]
),
%
% load files and set up dependencies
( Assignment_part=0 -> use_module(assignment_library(Assignment_library_name))
; Assignment_part=1 -> use_module(assignment_library('wp_library')),
use_module(assignment_library(Assignment_library_name), [api_/1,part_module/1,shell/0,start/0,stop/0,
query_world/2,possible_query/2,my_agent/1,
leave_game/0,join_game/1,start_game/0,
reset_game/0,map_adjacent/3,map_distance/3,say/2]
),
find_submission('wp', WpSubmission),
load_files([assignment_root(WpSubmission)], [silent(true)])
; Assignment_part=2 -> use_module(assignment_library(Assignment_library_name), [wp/1,wp/2,wt_link/2,actor/1,link/1,
init_identity/0,test/0]
),
use_module(assignment_library('oscar_library'), [api_/1,part_module/1]),
use_module(assignment_library('game_predicates'), [agent_ask_oracle/4]),
retract(part_module(1)), assertz(part_module(2)),
game_predicates:ailp_reset
; Assignment_part=3 -> use_module(assignment_library('wp_library')),
use_module(assignment_library(Assignment_library_name), [api_/1,part_module/1,shell/0,start/0,stop/0,
query_world/2,possible_query/2,my_agent/1,
leave_game/0,join_game/1,start_game/0,
reset_game/0,map_adjacent/3,map_distance/3,say/2]
),
find_submission('wp', WpSubmission),
load_files([assignment_root(WpSubmission)], [silent(true)]),
retract(part_module(1)), assertz(part_module(3))
; Assignment_part=4 -> use_module(assignment_library('wp_library')),
use_module(assignment_library(Assignment_library_name), [api_/1,part_module/1,shell/0,start/0,stop/0,
query_world/2,possible_query/2,my_agent/1,
leave_game/0,join_game/1,start_game/0,
reset_game/0,map_adjacent/3,map_distance/3,say/2]
),
find_submission('wp', WpSubmission),
load_files([assignment_root(WpSubmission)], [silent(true)]),
retract(part_module(1)), assertz(part_module(4))
; Assignment_part=99 -> use_module(assignment_library('wp_library')),test_wiki_deduction,test_wiki_links
; otherwise -> true
).
:-get_assignment_details.