-
Notifications
You must be signed in to change notification settings - Fork 9
/
tau.cpp
171 lines (164 loc) · 4.62 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
#include <stdio.h>
#include "cli.h"
#include <sstream>
#include "prover.h"
#ifdef with_marpa
#include "marpa_tau.h"
#endif
bool autobt = false, _pause = false, __printkb = false, fnamebase = true, quad_in = false, nocolor = false;
#ifdef DEBUG
auto dummy = []() {
return ( bool ) std::cin.tie ( &std::clog );
}();
#endif
#ifdef JSON
jsonld_options opts;
#endif
//void menu();
//#include <boost/asio.hpp>
//#include <boost/asio/ip/tcp.hpp>
std::wostream& dout = std::wcout;
std::wostream& derr = std::wcerr;
//#endif
/*
class listen_cmd : public cmd_t {
virtual std::string desc() const { return "Listen to incoming connections and answer queries."; }
virtual std::string help() const { return "Behave as a server. Usage: tau listen <port>"; }
virtual int operator() ( const strings& args ) {
if (args.size() != 3) { derr<<help()<<std::endl; return 1; }
using namespace boost::asio;
using boost::asio::ip::tcp;
io_service ios;
ip::tcp::endpoint endpoint(tcp::v4(), stoi(args[2]));
ip::tcp::acceptor acceptor(ios, endpoint);
for (;;) {
ip::tcp::iostream& stream = *new ip::tcp::iostream;
acceptor.accept(*stream.rdbuf());
stream.expires_from_now(boost::posix_time::seconds(60));
threads.push_back(new thread([&stream](){
string line;
while (stream) {
stream >> line;
dout << line << endl;
}
}));
}
return 0;
}
};
*/
#define ever ;;
#ifndef NOPARSER
class prove_cmd : public cmd_t {
public:
virtual std::string desc() const {
return "Run a query against a knowledgebase.";
}
virtual std::string help() const {
std::stringstream ss ( "Usage:" );
ss << std::endl << "\ttau prove [JSON-LD kb filename] [JSON-LD query filename]" << "\tAnswers the query." << std::endl;
return ss.str();
}
virtual int operator() ( const strings& args ) {
if ( ( args.size() == 3 && args[1] == L"help" ) || ( args.size() != 2 && args.size() != 4 ) ) {
dout << ws(help());
return 1;
}
try {
#ifdef IRC
for(ever) { try {
#endif
auto kb = load_quads(L"");
if (kb) {
dout << "kb input done." << std::endl;
auto query = load_quads(L"");
dout << "query loaded." << std::endl;
if (query) {
prover pr( *kb );
pr.query( *query );
dout << "Ready." << std::endl;
}
}
#ifdef IRC
} catch (std::exception& ex) { dout<<ex.what()<<std::endl; }
catch (...) { dout<<"generic exception."<<std::endl; }
sleep(1);
}
#endif
return 0;
} catch ( std::exception& ex ) {
derr << ex.what() << std::endl;
return 1;
}
return 0;
}
};
#endif
int main ( int argc, char** argv ) {
// segment = new boost::interprocess::managed_heap_memory(long(1024*1024*1024)*1);
// alloc = new allocator_t(segment->get_segment_manager());
dict.init();
cmds_t& cmds = *new cmds_t;
cmds = { {
#ifdef with_marpa
{ string ( L"load_n3" ) , new load_n3_cmd },
#endif
#ifdef JSON
{ string ( L"convert" ) , new convert_cmd },
{ string ( L"prove" ) , new prove_cmd }
#endif
}, {
{ { L"--no-deref", L"show integers only instead of strings" }, &deref },
{ { L"--pause", L"pause on each trace and offer showing the backtrace. available under -DDEBUG only." }, &_pause },
{ { L"--shorten", L"on IRIs containig # show only what after #" }, &shorten },
// { { "--printkb", "print predicates, rules and frames at the end of prove command" }, &__printkb },
{ { L"--base", L"set file://<filename> as base in JsonLDOptions" }, &fnamebase },
{ { L"--quads", L"set input format for prove command as quads" }, &quad_in },
{ { L"--nocolor", L"disable color output" }, &nocolor }
}
};
strings& args = *new strings;
for ( int n = 0; n < argc; ++n ) args.push_back ( ws(std::string(argv[n])) );
for ( int n = 0; n < argc; ++n )
if (args[n] == L"--level") {
if (n + 1 == argc) {
derr<<"please specify log level."<<std::endl;
exit(1);
}
level = std::stoi(args[n+1]);
args.erase(args.begin() + n + 1);
args.erase(args.begin() + n);
break;
}
process_flags ( cmds, args );
if (nocolor)
KNRM = KRED = KGRN = KYEL = KBLU = KMAG = KCYN = KWHT = L"";
argc = args.size();
try {
prover::unittest();
} catch (std::exception& ex) {
dout << "unit test failed: " << ex.what() << endl;
exit(1);
}
#ifndef NOPARSER
if ( argc == 1 ) {
prove_cmd p;
quad_in = true;
return p({L"",L"",L"",L""});
}
if (( cmds.first.find ( args[1] ) == cmds.first.end() && args[1] != L"help" ) ) {
print_usage ( cmds );
return 1;
}
if ( args[1] == L"help" && argc == 3 ) {
dout << ws(cmds.first[string ( args[2] )]->help());
return 0;
}
if ( args[1] == L"help" && argc == 2 ) {
print_usage ( cmds );
return 0;
}
int rval = ( *cmds.first[args[1]] ) ( args );
return rval;
#endif
}