-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodule.lgt
More file actions
374 lines (312 loc) · 11.3 KB
/
Copy pathmodule.lgt
File metadata and controls
374 lines (312 loc) · 11.3 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
:- object(banpipe_module_path).
:- info([
version is 1:0:0,
author is 'Christian Theil Have',
date is 2012-11-09,
comment is 'Manages the module directory search paths.',
remarks is [
'environment_variable $BANPIPE_MODULE_PATH' - 'Search paths are obtained from the BANPIPE_MODULE_PATH (paths separated by : (unix) or ; (windows)',
'explicit paths in scripts' - 'module directories can be added explicitly from scripts, see include_directory/1']
]).
:- private(path_dir/1).
:- dynamic(path_dir/1).
:- public(get_paths/1).
:- info(get_paths/1, [
comment is 'Extract module directories from the BANPIPE_MODULE_PATH environment variable.',
argnames is ['Paths']
]).
get_paths(Paths) :-
( os::environment_variable('BANPIPE_MODULE_PATH',PathAtom) ->
atom::split(PathAtom, ':', EnvPaths0),
meta::map(os::absolute_file_name,EnvPaths0,EnvPaths)
; EnvPaths = []
),
findall(Dir,::path_dir(Dir),AdditionalDirs),
meta::map(os::absolute_file_name,AdditionalDirs,ExpandAdditionalDirs),
list::append(ExpandAdditionalDirs,EnvPaths,Paths).
:- public(check_set/0).
:- info(check_set/0, [
comment is 'Check that BANPIPE_MODULE_PATH is set.'
]).
check_set :-
report_unless(os::environment_variable('BANPIPE_MODULE_PATH',_))::warning('BANPIPE_MODULE_PATH not set.').
:- public(include_directory/1).
:- info(include_directory/1, [
comment is 'Programmatically appends a directory to the module search path',
argnames is ['Directory']
]).
include_directory(Directory) :-
( ::path_dir(Directory) ->
true
; ::assertz(path_dir(Directory))
).
:- end_object.
:- object(module(_Name)).
:- info([
version is 1:0:0,
author is 'Christian Theil Have',
date is 2012-11-09,
comment is 'A proxy object which represent a particular banpipe module.',
parnames is ['Name']
]).
:- uses(user, [
atomic_list_concat/2
]).
:- public(interface_file/2).
interface_file(File,prolog) :-
parameter(1,ModuleName),
banpipe_module_path::get_paths(Paths),
list::member(Path,Paths),
atomic_list_concat([Path, '/', ModuleName, '/', 'interface.pl'],File),
file(File)::exists.
interface_file(File,generic) :-
parameter(1,ModuleName),
banpipe_module_path::get_paths(Paths),
list::member(Path,Paths),
atomic_list_concat([Path, '/', ModuleName, '/', 'interface.banpipe'],File),
file(File)::exists.
:- public(builtin/0).
:- info(builtin/0, [
comment is 'True if module is a builtin module'
]).
builtin :-
parameter(1,Module),
implements_protocol(Module,banpipe_builtin_module).
:- public(module_type/1).
:- info(module_type/1, [
comment is 'True if module is a builtin module'
]).
module_type(builtin) :-
self(Self),
Self::builtin.
module_type(prolog) :-
interface_file(_,prolog).
module_type(generic) :-
interface_file(_,generic).
:- public(available/0).
:- info(available/0, [
comment is 'True if a module with the given name is available.'
]).
available :-
::builtin.
available :-
::interface_file(_,_).
:- end_object.
:- object(module_task(_Module,_Task)).
:- info([
version is 1:0:0,
author is 'Christian Theil Have',
date is 2012-11-09,
comment is 'Represents a task in a module',
parnames is ['Module', 'Task']
]).
:- public(valid/1).
:- info(valid/1, [
comment is 'True if module+task exists, have a declaration+implementation in the interface file and Options matches (is a subset of) declared options.',
argnames is ['Options']
]).
valid(Options) :-
parameter(1,Module),
parameter(2,Task),
% Check that the task is declared
(::has_declaration -> true ; reporting::error(no_task_declaration(Module,Task))),
(::has_implementation -> true ; reporting::error(no_task_implementation(Module,Task))),
::options(DeclaredOptions),
(forall(list::member(Opt,Options), (Opt =.. [F,_], MatchOpt =.. [F,_],list::member(MatchOpt,DeclaredOptions))) ->
true
;
reporting::error(interface(model_called_with_undeclared_options(Module,Options)))).
:- public(expand_options/2).
:- info(expand_options/2, [
comment is 'Options are expanded to SortedExpandedOptions: A sorted list which include Options+declared default options, except if an option in Options use same functor. ',
argnams is [ 'Options', 'SortedExpandedOptions' ]
]).
expand_options(Options,SortedExpandedOptions) :-
::options(DefaultOptions),
::expand_options(Options,DefaultOptions,SortedExpandedOptions).
:- public(expand_options/3).
expand_options(Options,DefaultOptions,SortedExpandedOptions) :-
::valid(Options),
meta::map([OptIn,OptOut]>>((OptIn=..[F,X],OptOut=..[F,X], list::member(OptOut,Options) -> true
; (OptIn=..[F,_],OptOut=..[F,_],list::member(OptOut,Options)) -> true
; OptOut=OptIn)),
DefaultOptions,ExpandedOptions),
list::sort(ExpandedOptions,SortedExpandedOptions).
:- public(has_declaration/0).
:- info(has_declaration/0, [comment is 'True if the task has a declaration.']).
has_declaration :-
::declaration(_).
:- public(has_implementation/0).
:- info(has_implementation/0, [comment is 'True if the there is an implementation predicate for the task.']).
% has_implementation/0 for built-in modules
has_implementation :-
parameter(1,Module),
module(Module)::builtin,
!,
parameter(2,Task),
TaskGoal =.. [ Task, _, _, _ ],
Module::predicate_property(TaskGoal,(public)).
% has_implementation/0 for external prolog-based banpipe modules
has_implementation :-
parameter(1,Module),
parameter(2,Task),
module(Module)::interface_file(File,prolog),
prolog_file(File)::read_terms(Terms),
term_extras::has_rule_with_head(Terms,Task,3).
% has_implementation/0 for external generic banpipe modules
% In this case we do not check for implementation (implementation is program/shell command)
has_implementation :-
parameter(1,Module),
parameter(2,Task),
module(Module)::interface_file(File,generic),
prolog_file(File)::member(invoke(Task,_Method)).
:- public(declaration/1).
:- info(declaration/1,
[ comment is 'Declaration is the task declation, e.g, taskname(input_types,options,output_types).',
argnames is ['Declaration']
]).
declaration(Declaration) :-
parameter(1,Module),
module(Module)::builtin,
!,
parameter(2,Task),
Module::task(Declaration),
functor(Declaration, Task, _).
declaration(Declaration) :-
parameter(1,Module),
parameter(2,Task),
module(Module)::interface_file(InterfaceFile,prolog),
prolog_file(InterfaceFile)::member(TaskMatcher),
TaskMatcher = (:- task(Declaration)),
functor(Declaration, Task, _).
declaration(Declaration) :-
parameter(1,Module),
parameter(2,Task),
module(Module)::interface_file(InterfaceFile,generic),
prolog_file(InterfaceFile)::member(TaskMatcher),
TaskMatcher = task(Declaration),
functor(Declaration, Task, _).
:- public(options/1).
:- info(options/1, [
comment is 'Options is the list of declared options for task.',
argnames is ['Options']
]).
options(Options) :-
::declaration(Decl),
Decl =.. [ _ , _, OptionsDecl, _ ],
( list::member(version(_),OptionsDecl) ->
Options = OptionsDecl
; Options = [version(na)|OptionsDecl]
).
:- public(input_types/1).
:- info(input_types/1, [
comment is 'InputTypes is a sequence of file types accepted as input to the task.',
argnames is ['InputTypes']
]).
input_types(InputTypes) :-
::declaration(Decl),
Decl =.. [ _ , InputTypes, _, _ ].
:- public(output_types/1).
:- info(output_types/1, [
comment is 'OutputTypes is a sequence of file types produced by the task.',
argnames is ['OutputTypes']
]).
output_types(OutputTypes) :-
::declaration(Decl),
Decl =.. [ _ , _, _, OutputTypes ].
:- end_object.
:- object(task(Module,Task,_InputFiles,_Options), extends(module_task(Module,Task))).
:- info([
version is 1:0:0,
author is 'Christian Theil Have',
date is 2012-11-09,
comment is '.',
parnames is ['Module', 'Task', 'InputFiles', 'Options']
]).
:- public(run/1).
:- info(run/1,[
comment is 'Runs the task(Module,Task,InputFiles,Options) using an invoker (see invoker/1) if the task is not available on file(s). OutputFiles is a list of names of resulting files.',
argnames is ['OutputFiles']
]).
run(OutputFilesList) :-
self(Self),
parameter(1,Module),
parameter(2,Task),
parameter(3,InputFiles),
parameter(4,Options),
report_unless(module(Module)::available)::error(module_not_available(Module)),
report_unless(Self::has_declaration)::error(no_task_declaration(Module::Task)),
report_unless(Self::has_implementation)::error(no_task_implementation(Module::Task)),
::output_types(OutputTypes),
% If the output-types are not declared as a list,
% assume task predicate takes a single file argument
(list::valid(OutputTypes) ->
OutputTypesList = OutputTypes,
list::length(OutputTypesList,N),
list::length(OutputFilesList,N),
OutputFiles=OutputFilesList
;
OutputTypesList = [OutputTypes],
OutputFilesList = [OutputFiles]),
config::get(file_manager,FileManager),
::expand_options(Options,ExpandedOptions),
meta::map([X,Y]>>(file(X)::canonical(Y)),InputFiles,InputFilesCanonical),
% If the input-types are not declared as a list,
% assume that task predicate takes a single file as argument
::input_types(InputTypes),
(list::valid(InputTypes) ->
InputFilesCanonicalAdapt = InputFilesCanonical
;
% Strip the list if it is not declared in the argument
[InputFilesCanonicalAdapt] = InputFilesCanonical),
Goal =.. [ Task, InputFilesCanonicalAdapt, ExpandedOptions, OutputFiles ],
write(Goal), nl,
(FileManager::result_files(Module,Task,InputFilesCanonical,ExpandedOptions,OutputFilesList) ->
true % The task has allready been run
;
FileManager::result_files_allocate(Module,Task,InputFilesCanonical,ExpandedOptions,OutputFilesList),
(module(Module)::builtin ->
Module::Goal
;
::invoker(Invoker),
module(Module)::interface_file(InterfaceFile,_),
Invoker::run(InterfaceFile,Goal)),
!,
FileManager::result_files_commit(Module,Task,InputFilesCanonical,ExpandedOptions)).
:- public(typecheck/1).
:- info(typecheck/1,[
comments is 'Check that supplied types of the input types are valid and unify OutputTypes to the resulting output types.',
argnames is ['OutputTypes']
]).
% FIXME: Typecheck needs adaptation to non-list types
typecheck(OutputTypes) :-
parameter(2,Task),
parameter(3,SuppliedInputTypes),
parameter(4,Options),
::declaration(TaskDeclaration),
TaskDeclaration =.. [ Task, InputTypes, DeclOptions, OutputTypes ],
::expand_options(Options,DeclOptions,_),
term::subsumes(InputTypes,SuppliedInputTypes).
:- public(invoker/1).
:- info(invoker/1, [
comment is 'Determine InvokerObject to use: module may declare invoke_with/1; otherwise use banpipe_config default_invoker',
argnames is ['InvokerObject']
]).
invoker(builtin) :-
parameter(1,Module),
module(Module)::builtin.
invoker(generic_invoker) :-
parameter(1,Module),
module(Module)::interface_file(_,generic).
invoker(InvokerName) :-
parameter(1,Module),
module(Module)::interface_file(InterfaceFile,prolog),
prolog_file(InterfaceFile)::member(TaskMatcher),
TaskMatcher = (:- invoke_with(PrologName)),
atom_concat(PrologName,'_invoker',InvokerName),
!.
invoker(Invoker) :-
config::get(default_invoker,Invoker).
% TODO: Eventually, I might move guard invokation to run/1 (rather than in script interpreter)
% to allow unifying variables from the default option settings
:- end_object.