forked from naturalog/tauchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tau.cpp
367 lines (308 loc) · 9.55 KB
/
tau.cpp
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
#include <stdio.h>
#include "cli.h"
#include <sstream>
#include "prover.h"
#ifdef with_marpa
#include "marpa_tau.h"
#endif
#include <tclap/CmdLine.h>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
extern int level;
bool autobt = false, _pause = false, __printkb = false, fnamebase = true, nocolor = false;
#ifdef DEBUG
auto dummy = []() {
return ( bool ) std::cin.tie ( &std::clog );
}();
#endif
#ifdef JSON
jsonld_options opts;
#endif
std::wostream& dout = std::wcout;
std::wostream& derr = std::wcerr;
/*
*
* return value of parse functions: 0: error, 1: incomplete input, 2: success
*
* */
#ifndef NOPARSER
int parse_nq(qdb &kb, qdb &q, std::wistream &f)
{
try {
readqdb(kb, f);
} catch (std::exception& ex) {
derr << L"Error reading quads: " << ex.what() << std::endl;
return 0;
}
try {
readqdb(q, f);
} catch (std::exception& ex) {
derr << L"Error reading quads: " << ex.what() << std::endl;
return 0;
}
return 2;
}
#endif
void parse(qdb &kb, qdb &q, std::wistream &f , std::string fn, std::string fmt)
{
boost::algorithm::to_lower(fmt);
std::vector<std::string> exts({"jsonld", "natural3", "natq", "n3", "nq"});
std::string fnl(fn);
boost::algorithm::to_lower(fnl);
if (fmt == "") // try to guess from file extension
{
for (auto x:exts)
if (boost::ends_with(fnl, x))
fmt = x;
}
if (fmt == "") // default
fmt = "natq";
#ifdef with_marpa
if(fmt == "natural3" || fmt == "n3")
parse_natural3(kb, q, f);
else
#endif
if(fmt == "natq" || fmt == "nq" || fmt == "nquads")
parse_nq(kb, q, f);
/* else
jsonld*/
else
throw std::runtime_error("unknown format");
}
typedef std::pair<std::string, std::string> fn_fmt;
int process_args(std::vector<std::string> args)
{
if (args.size() == 0)
{}//do interactive quads
std::vector<fn_fmt> inputs;
std::string fmt, fn;
for (std::string x: args) {
if (boost::starts_with(x, "--"))
fmt = std::string(x.begin() + 2, x.end());
else
fn = x;
if (fn != "") {
inputs.push_back(fn_fmt(fn, fmt));
fn = "";
}
}
//now that we have optionally collected a format
if (inputs.size() == 0)
{
qdb kb, query;
parse(kb, query, std::wcin, "", fmt);
prover prvr(kb);
TRACE(dout << "QUERYING" << std::endl);
prvr.query(query);
return 0;
}
uint pos = 0;
std::vector<qdb> kbs;
for (auto x: inputs) {
std::string fn = x.first;
std::string fmt = x.second;
qdb kb;
qdb query;
std::wifstream f(fn);
if (!f.is_open())
throw std::runtime_error("couldnt open file \"" + fn + "\"");
parse(kb, query, f, fn, fmt);
if(++pos == inputs.size())
{
if(query.first.size()) {
kbs.push_back(kb);
prover prvr(merge_qdbs(kbs));
TRACE(dout << "QUERYING" << std::endl);
prvr.query(query);
}
else {
if (kbs.size() == 0)
throw std::runtime_error("would use last file as query, but have no kb");
prover prvr(merge_qdbs(kbs));
TRACE(dout << "QUERYING" << std::endl);
prvr.query(kb);
}
}
else {
kbs.push_back(kb);
if (query.first.size())
dout << L"ignoring query in " << ws(fn) << std::endl;
}
}
return 0;
}
typedef std::map<TCLAP::SwitchArg*,bool*> tcFlags;
int main ( int argc, char** argv ) {
dict.init();
std::vector<std::string> args;
try {
TCLAP::CmdLine cmd("Tau-Chain by http://idni.org. ", ' ', "0.0");
TCLAP::ValueArg <int> tc_level("l", "level", "debug level", false, 1, "", cmd);
TCLAP::SwitchArg
tc_deref("d", "no-deref", "show integers only instead of strings", cmd, true),
tc_pause("P", "pause",
"pause on each trace and offer showing the backtrace. available under -DDEBUG only.", cmd,
false),
tc_shorten("s", "shorten", "on IRIs containing # show only what's after #", cmd, false),
tc_base("b", "base", "set file://<filename> as base in JsonLDOptions", cmd, true),
tc_nocolor("n", "nocolor", "disable color output", cmd, false);
TCLAP::UnlabeledMultiArg<std::string> multi("stuff", "file names, optionally prefixed with formats", false, "typedesc", cmd);
cmd.parse(argc, argv);
level = tc_level.getValue();
deref = tc_deref.getValue();
_pause = tc_pause.getValue();
shorten = tc_shorten.getValue();
fnamebase = tc_base.getValue();
nocolor = tc_nocolor.getValue();
//sorry stoopkid
args = multi.getValue();
} catch (TCLAP::ArgException &e) {
derr << "TCLAP error: " << e.what() << std::endl;
}
if (nocolor)
KNRM = KRED = KGRN = KYEL = KBLU = KMAG = KCYN = KWHT = L"";
return process_args(args);
}
#ifdef xxxxxxxxxxxxxxxxxxxxxxxxxxBLIMP
int process_args(args)
{
int pos = 0;
prover prvr;
for (std::string x: args) {
blimp(prvr, x, ++pos = args.size());
}
}
void blimp(prover &prvr, std::string x, int togo) {
static std::string fmt;
std::string fn;
static std::string block;
const int COMMANDS = 0;
const int KB = 1;
const int QUERY = 2;
const int WORK = 3;
static int phase = 0;
if (boost::starts_with(x, "--"))
{
/*
if (x == "--pass")
{
if(!togo)
dump
else error
} else
*/
fmt = std::string(x.begin() + 2, x.end());
}
else {
fn = x;
//try to load file, return
}
if (x == "fin." || x == "fin .") {
phase = phase + 1;
if (phase== WORK) { }
}
block += x + "\n";
if (fmt != "")
{
int r = parse(x, fmt);
}
/*
{
qdb xx = ;
prvr.add_qdb(load_file(x, fmt));
else {
qdb query;
if
load_file(query, query_format, query_fn);
}
fmt = "";
}
*/
/*
if (name=="-"// or name == "")
std::wistream* pis = &std::wcin;
else.
pis = new std::wifstream(ws(fname));
std::wistream& is = *pis;
ifstream blabla f(file.first);
load_file(kb,..
*/
}
/*
}
else
print_qdb(kb)
return rval;
*//*
}
} catch (std::exception& ex) { dout<<ex.what()<<std::endl; }
catch (...) { dout<<"generic exception."<<std::endl; }
sleep(1);
}
*/
/*
*
*
* chat
*
*
*
*
//exactly, if the rest of the command line logic were set up over this it would be
//what i described below. because from here i imagine something like a 'batch file'
//which would just be a really long command line line..stuck into a file and
//interpreted with some option like --batch
//i mean as far as simplicity and consistency goes i think its pretty solid,
//"everything works the same wherever you're doing it... basically"
//yeah it sounds good
//this will definitely be good when its more complete and we get to bigger more serious
//testing, especially the batch files. two commands :) i'm thinking of is --kb and
//--kb_append and maybe 3 with a --kb_clear or something
//also in interpeter mode what happens if you enter a bad line? does it fail out or
//does it just give you a warning and give you a new line? ofc if we have the
//integration between command line, interpreter and batch files then in some cases
//we'd want one, and in some cases we'd want the other, sounds like a command line option
//--fail_on_error or --continue_on_error or something like that
//also, what about merging multiple files into a single kb?
//--kb file.n3 file.json file.nq --query qfile1 qfile2 --kb-append file.n3 --query qfile3
// --kb-clear --kb_fail_on_error true --kb file.n3 file2.n3 and etc.......
// ^ supposed to all be one command line
//and the -- options are just what you would do in between "fin."s and starting the
//next 'activity' (--kb, --kb-append, --query) etc. etc.
//these 'activities' not really any different whether its cli, batch file, interpreter
//mode or IRC bot (and especially these last two are identical)
//ok, now i need to think it thru a bit more, so it will work more or less
//like now.................
//one thing i was thinking is that load_quads has it set up so that it will
//work over both files and stdin in the interpreter mode (which i've got
//prefixed with 'Tau>' now btw), was thinking whatever we set up the same things
//could be done in interpreter mode and it would be essentially the same interface
//command line commands essentially the same commands you'll give inside the
//interpreter to direct the course of things. like say i want to run another query
//after i'm done with my first one. right now Tau just exits.. but why?
//is essentially the same as just reading the next file and doing something with it
//from the commandline
//also brings up the idea of "quit" command :) which could actually have use
//already when i get into interpreter and didn't mean to.. it's cleaner and more
//considerate than making them Ctrl+Z, even though i personally have no problems
//with Ctrl+Z it's the thought that counts :) and could be a bigger thing later
//if we need more graceful tau shutdown, which i'm sure we would down the road.
//i agree down the road some sort of interpreter mode could be handy
//i figure that's already what we have and if we expanded the command line like
//we're talking about but kept it like it is now where it handles both files
//and 'interpreter', i.e. plain './tau', then that's exactly what we'd get :)
//tangential: we should bring back the IRC bot
//actually not completely tangential :)
//well..i will let you think about it:)
//command line commands, interpreter mode, irc mode(?), im not sure how
//to really approach any of it now
//basically just like we are now in the code except with the revisions of the command
//line like we've been talking about, and making these command line commands accessible
//in the interpreter
//basically, yeah
//basically an interpreter and the command line is no different except one has input
//from the user from the terminal and the other has input from files, and the commands/flags
//are all the same, and it all works exactly the same either way you do it
*/
#endif