forked from conceptsinmotion/thea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owl2_shell.pl
342 lines (290 loc) · 10.9 KB
/
owl2_shell.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
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
/* -*- Mode: Prolog -*- */
:- use_module(owl2_model).
:- use_module(owl2_plsyn).
:- use_module(owl2_util).
:- use_module(owl2_reasoner).
:- use_module(owl2_text_display).
:- use_module(owl2_popl,[popl_translate/1]).
:- use_module(owl2_dotty).
:- op(1100,xfy,--).
:- op(1090,fx,new).
:- op(1090,fx,add).
:- op(1090,fx,init).
:- op(1090,fx,rm).
:- op(1090,fx,m).
:- op(1090,fx,q).
:- op(1090,fx,qi).
:- op(1090,fx,pq).
:- op(1090,fx,v).
:- op(1090,fx,t).
:- op(1090,fx,l).
:- op(1090,fx,set).
:- op(1090,fx,unset).
:- op(1100,xfy,===>). % from POPL
:- op(1000,xfy,where).
:- op(800,xfy,to).
:- op(800,fx,(?)).
:- dynamic transaction/1.
:- dynamic redo_stack/1.
:- discontiguous cmd_doc/4.
:- multifile edit_template/2.
:- initialization(shell_init).
:- redefine_system_predicate('>'(_,_)).
:- redefine_system_predicate(help).
user:file_search_path(home, Home) :-
getenv('HOME',Home).
rcfile(F) :- absolute_file_name(home('.thearc'),F).
shell_init :-
set_prolog_flag(verbose,normal),
print_message(banner,thea_shell_welcome),
RDFS_Label='http://www.w3.org/2000/01/rdf-schema#label',
assert_axiom(annotationProperty(RDFS_Label)),
assert_axiom(annotationAssertion(RDFS_Label,RDFS_Label,literal(lang(en,label)))),
consult_rc.
consult_rc :-
rcfile(F),
exists_file(F),
!,
consult(F).
consult_rc.
save_settings :-
rcfile(F),
open(F,write,S,[]),
Q=settings(_,_),
forall(Q,format(S,'~q.~n',[Q])),
close(S).
% EDIT CMDS
cmd_doc(edit,new,[axiom],'Add axiom to current ontology').
cmd_doc(edit,add,[axiom],'Add axiom to current ontology').
cmd_doc(edit,rm,[axiom],'Retract axiom to current ontology').
cmd_doc(edit,undo,[],'Undo last add/rm').
cmd_doc(edit,redo,[],'Redo last undo').
edit_op(Op,AxiomIn) :- current_ontology(Ont),!,tr(AxiomIn,Axiom), Act =.. [Op,Axiom,Ont],Act,print_message(informational,Act),asserta(transaction(Act)).
edit_op(_,_) :- print_message(error,no_current_ontology).
add AxiomIn where Goal :- !, forall(Goal,add(AxiomIn)).
add AxiomIn :- edit_op(assert_axiom,AxiomIn).
rm AxiomIn :- edit_op(retract_axiom,AxiomIn).
undo :- transaction(Act),undo(Act),print_message(informational,undo(Act)),asserta(redo_stack(Act)),retract(transaction(Act)).
undo(assert_axiom(A,O)) :- retract_axiom(A,O).
undo(retract_axiom(A,O)) :- assert_axiom(A,O).
redo :- redo_stack(Act),Act,print_message(informational,redo(Act)),retract(redo_stack(Act)).
new Ont :- assert_axiom(ontology(Ont)),nb_setval(ontology,Ont).
'===>'(AIn,BIn where G) :- !,tr(AIn,A),tr(BIn,B),popl_translate(A ===> B where G).
'===>'(AIn,BIn) :- tr(AIn,A),tr(BIn,B),popl_translate(A ===> B).
% EDIT TEMPLATES
+(TN) :-
current_opts(Opts),
T=..[TN,IRI],
% example: template(X), [iri(prefix(obo)),class(X),N-annotationAssertion(label,X,N)]
edit_template(T,[iri(IRI_Template)|Fields]),
get_field_info(iri(IRI_Template),IRI),
%get_field_axiom_list(Fields,IRI,Axs_1),
findall(Ax,(member(Field,Fields),
get_field_axiom(Field,IRI,Ax)),
Axs_1),
flatten(Axs_1,Axs),
nl,
writeln('% ------------------'),
writeln('% AXIOMS TO ADD:'),
writeln('% ------------------'),
( member(display(combined),Opts)
-> Opts_2=Opts
; Opts_2=[display(combined)|Opts]),
forall(member(Ax,Axs),
( tr(Ax,Ax_2),
display_term(Ax_2,Opts_2))),
input('% OK? [enter for yes, any other char for no]',Ok),
nl,
( Ok=''
-> maplist(add,Axs),
writeln('% AXIOMS ADDED. Type "undo." to retract.')
; print_message(informational,not_added)).
/*
get_field_axiom_list([],_,[]).
get_field_axiom_list([Field|Fields],IRI,[Ax|Axs]) :-
get_field_axiom(Field,IRI,Ax),
get_field_axiom_list(Fields,IRI,Axs).
*/
get_field_info(iri(obo(Prefix)),IRI) :-
!,
get_field_info(iri(obo(Prefix,7)),IRI).
get_field_info(iri(obo(IDSpace,NumDigits)),IRI) :-
!,
concat_atom(['http://purl.obolibrary.org/obo/',IDSpace,'_'],Prefix),
get_field_info(iri(prefix(Prefix,NumDigits)),IRI).
get_field_info(iri(prefix(Prefix,NumDigits)),IRI) :-
findall(X,(entity(E),concat_atom([Prefix,X],E),is_numeric_atom(X)),Xs),
sort(Xs,SXs),
( reverse(SXs,[Last|_])
-> true
; Last='-1'),
atom_number(Last,LastID),
ID is LastID+1,
concat_atom(['~',NumDigits,'d'],Fmt),
format(atom(A1),Fmt,[ID]),sub_atom(A1,2,_,0,PaddedID),
atom_concat(Prefix,PaddedID,IRI).
get_field_axiom(multi(T),_,AxiomsOut) :-
!,
repeat,
( get_field_axiom(T,_,AxiomsOut)
-> true
; !,
fail).
get_field_axiom(Val-Axioms,_,AxiomsOut) :-
!,
repeat,
nl,
write('% Template: '),writeln(Axioms),
tr(Axioms,AxiomsOut),
write('% Enter value: '),write(Val),
input(' >> ',Val),
( Val=''
-> !,
fail
; \+ is_valid_axiom_list(AxiomsOut)
-> print_message(error,not_valid_axioms(AxiomsOut)),
fail
; format('% Val=~w~n',[Val]),
tr(AxiomsOut,AxiomsOut_2),
show(AxiomsOut_2),
!,
true).
get_field_axiom(Axiom,_,Axiom) :-
Axiom\=_-_,
Axiom\=multi(_),
!,
show(Axiom).
is_numeric_atom(A) :- catch(atom_number(A,_),_,fail).
input(Prompt,Val) :-
write(Prompt),
read_line_to_codes(user_input,Codes),
atom_codes(Val,Codes).
is_valid_axiom_list(L) :- is_list(L),!,forall(member(A,L),is_valid_axiom(A)).
is_valid_axiom_list(A) :- is_valid_axiom(A).
% DISPLAY CMDS
cmd_doc(display,t,[class],'Show a tree').
cmd_doc(display,q,[term],'Query axioms').
cmd_doc(display,v,[axiom],'Query axioms').
cmd_doc(display,--,[cmd],'pipe through unix shell. E.g. q _<_ -- \'grep neuron\'.').
t N :- label2iri(N,X), nb_setval(obj,X),current_opts(Opts), display_class_tree(X,Opts).
l N :-
label2iri(N,X),
nb_setval(obj,X),
current_opts(Opts),
setof(A,
( axiom_references(A,X),
axiom_type(A,T)),
As),
format('* Axiom Type: ~w~n',[T]),
forall(member(A,As),
display_term(A,Opts)),
fail.
l _.
%q :- tr(Query,Axiom), q(_).
trshow(XIn) :- tr(XIn,X),show(X).
show(X) :- current_opts(Opts),display_term(X,Opts).
v ObjIn :- tr(ObjIn,Obj),current_opts(Opts),visualize_obj(Obj,Opts).
'--'(GoalIn,Cmd) :-
tr(GoalIn,Goal),
open(pipe(Cmd),write,S,[]),
with_output_to(S,forall(Goal,true)),
close(S).
Cmd > File :- % TODO
open(File,write,S,[]),
with_output_to(S,Cmd),
close(S).
% LIST CMDS
cmd_doc(display,ls,[],'List ontologies').
cmd_doc(display,lsa,[],'List axioms in current ontology').
ls :-
forall(ontology(Ont),
( ( current_ontology(Ont)
-> write('*')
; write(' ')),
writeln(Ont))).
lsa :-
current_ontology(Ont),
!,
current_opts(Opts),
forall(ontologyAxiom(Ont,A),
display_term(A,Opts)).
lsa :-
!,
current_opts(Opts),
forall(axiom(A),
display_term(A,Opts)).
cmd_doc(display,stats,[],'Stats on current ontology').
stats :- current_ontology(Ont),stats(Ont).
stats(Ont) :-
forall(aggregate(count,A,Arity^(ontologyAxiom(Ont,A),functor(A,T,Arity)),Num),
format('#~w\t~w~n',[T,Num])).
% QUERY CMDS
%% gi(Axiom) - entailment query - translates Axiom to prolog axiom term and calls reasoner_ask/1
%% g(Axiom) - assertion query - translates Axiom to prolog goal and calls it directly
cmd_doc(query,gi,[axiom],'Entailment query - translates Axiom to native prolog axiom term and calls reasoner. as qi/1 with no result mapping.').
cmd_doc(query,g,[axiom],'Assertion query - translates Axiom to prolog goal and calls directly. As q/1 with no result mapping.').
gi(AxiomIn) :- tr(AxiomIn,Axiom),reasoner_ask(Axiom).
g(AxiomIn) :- tr(AxiomIn,Axiom),Axiom.
% select from asserted database
q SelectIn where QueryIn :- !,tr(QueryIn,Query),tr(SelectIn,Select), forall(Query,show(Select)).
q QueryIn :- !,tr(QueryIn,Query), forall(Query,show(Query)).
% select from asserted database, no translation
pq Select where Query :- !, forall(Query,show(Select)).
pq Query :- !, forall(Query,show(Query)).
% select from reasoned database
qi SelectIn where QueryIn :- !,tr(QueryIn,Query),tr(SelectIn,Select), forall(reasoner_ask(Query),show(Select)).
qi QueryIn :- !,tr(QueryIn,Query), forall(reasoner_ask(Query),show(Query)).
cmd_doc(reasoner,init,[name],'Initialize a reasoner on current ont. E.g. "reasoner pellet.". Stores current reasoner in global variable.').
init RN :- initialize_reasoner(RN,_,[]).
% HELP
cmd_doc(help,?,[cmd],'Get help about a command.').
cmd_doc(help,h,[],'Show history.').
? X :- usage(X).
help :- usage.
set (P to V) :- !,unset(P), assert(settings(P,V)),save_settings.
set P + V :- !,assert(settings(P,V)),save_settings.
set P - V :- !,retractall(settings(P,V)),save_settings.
unset P :- retractall(settings(P,_)),save_settings.
settings :- q(settings(_,_)).
%tr(In,Out) :- plsyn_owl(In,Out),!.
tr(In,Out) :- plsyn_owl(In,X),map_IRIs(get_IRI_from_label,X,Out).
:- multifile settings/2.
:- dynamic settings/2.
opt_doc(display,'options for tuning screen display of axioms').
opt_value(display,combined,'show both prolog and pretty').
opt_value(display,tabular,'TSV for axiom arguments').
opt_value(display,prolog,'show prolog syntax rather than pretty').
opt_value(display,plsyn,'show prolog plsyn syntax').
opt_value(display,labels,'show labels not IRIs').
current_opts(Opts) :-
findall(Opt,
( settings(P,V),
atom(P),
Opt =.. [P,V]),
Opts).
current_ontology(Ont) :- nb_current(ontology,Ont),!.
current_ontology(Ont) :-
ontology(Ont),
!,
set_current_ontology(Ont).
set_current_ontology(Ont) :-
nb_setval(ontology,Ont),
print_message(informational,shell('Set ontology to ~w'-[Ont])).
label2iri(Label,Obj) :- labelAnnotation_value(Obj,Label),!.
label2iri(X,X).
prolog:message(thea_shell_welcome) -->
['% ::: Welcome to Posh, the Prolog OWL Shell :::'].
prolog:message(redo(Act)) --> ['Redo: '],prolog:message(Act).
prolog:message(undo(Act)) --> ['Undo: '],prolog:message(Act).
prolog:message(assert_axiom(A,Ont)) --> ['Asserting '],prolog:message(ax(A)),[' into ',Ont].
prolog:message(retract_axiom(A,Ont)) --> ['Retracting '],prolog:message(ax(A)),[' into ',Ont].
prolog:message(ax(Ax)) --> [Atom],{current_opts(Opts),with_output_to(atom(Atom),display_term(Ax,Opts))}.
prolog:message(shell(M)) --> [M].
usage :-
writeln('BOMB Shell Commands'),
forall(cmd_doc(_,C,_,Doc),
format('~w\t~w~n',[C,Doc])),
nl.
usage(C) :-
cmd_doc(_,C,_,Doc),
writeln(Doc).