-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpa.cpp
321 lines (269 loc) · 8.58 KB
/
bpa.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
/***************************************************************************
* Copyright (C) 2009 by Mushthofa *
* unintendedchoice@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include "SpiritProgramParser.h"
#include "DLVASPSolver.h"
#include "XSBPrologEngine.h"
#include "EvalComp.h"
#include "BoostComponentFinder.h"
#include "GraphBuilder.h"
#include "GraphProcessor.h"
#include "BoostHCFDetector.h"
#include "SimpleMethodSelector.h"
#include "OutputBuilder.h"
#include <ctime>
void showUsage(char* prog)
{
std::cout <<"BPA"<<std::endl;
std::cout <<"Answer set semantics for programs with Bounded Predicate Arities." <<std::endl;
std::cout <<"USAGE : ";
std::cout <<prog<<" [OPTIONS] [FILE] "<<std::endl<<std::endl;
std::cout << "Reads program from file FILE and evaluate the program under answer set semantics" <<std::endl;
std::cout << "If no filename is given, reads from standard input instead "<<std::endl;
std::cout << "Supported options are the following: "<<std::endl;
std::cout << "-m=M\t selects evaluation method, where M can be 1, 2 or 3 "<<std::endl;
std::cout << "\t note that if (a component of) the program is non-HCF, "<<std::endl
<< "\t method 3 will be used to evaluate the component regardless "<<std::endl
<< "\t of the method selected " <<std::endl
<< "\t if not given, an internal method selection heuristic will determine" <<std::endl
<< "\t the methods used "<<std::endl;
std::cout << "-n=N\t limits the number of answer set printed to N "<<std::endl;
std::cout << "-filter=p1[,p2[...]]\n\t filters the answer set to contain only " <<std::endl
<< "\t extensions of predicates p1, p2,..." <<std::endl;
std::cout << "-ruleml\t output the answer set in RuleML format " <<std::endl;
//std::cout << "-no-scc\t disables SCC decomposition (can make evaluation very slow) " <<std::endl;//
std::cout << "-grmax=N\t sets the maximum number of ground rules/1000 a rule may have "<<std::endl;
std::cout << "\t to be considered 'small'. this option affects the model generations and " <<std::endl;
std::cout << "\t minimality checking. defaults to 10 " <<std::endl;
std::cout << "-vmax=N\t sets the minimum number of variables a rule may have "<<std::endl;
std::cout << "\t to be considered 'big'. this option affects the model generations and " <<std::endl;
std::cout << "\t minimality checking. defaults to 4 " <<std::endl;
std::cout << "-v, -V\t be verbose (prepare for a long output)." <<std::endl;
std::cout << "-h, --help\t display this help text "<<std::endl;
std::cout << std::endl;
}
int main(int argc, char *argv[])
{
GetPot cl(argc, argv);
Globals::Instance()->processArgs(cl);
if(Globals::Instance()->boolOption("help"))
{
showUsage(argv[0]);
return EXIT_SUCCESS;
}
AtomSet EDB;
Program IDB;
/* Read and parse input */
try
{
ProgramParser* parser = new SpiritProgramParser();
EDB = parser->getEDB();
IDB = parser->getIDB();
delete parser;
}
catch(GeneralError& e)
{
std::cerr << e.getErrorMsg() << std::endl;
return EXIT_FAILURE;
}
time_t start = time(NULL);
OutputBuilder* ob;
std::string filters = Globals::Instance()->strOption("filter");
if(Globals::Instance()->boolOption("ruleml"))
ob = new OutputXMLBuilder(filters);
else
ob = new OutputTextBuilder(filters);
/* Program is empty, just check consistency of the facts */
if(IDB.empty())
{
if(EDB.isConsistent())
{
ob->buildPre();
ob->buildOutput(EDB);
ob->buildPost();
std::cout << ob->getString();
}
delete ob;
return EXIT_SUCCESS;
}
/* Framework component classes for evaluation */
/* Here it is possible to customize bpa to use different ASP solvers, Prolog engine etc. */
ProgramBuilder* pb = new DLVProgramBuilder();
ASPSolverEngine* se = new DLVASPSolverEngine();
PrologEngine* pe = new XSBPrologEngine(XSBHOME);
/* This is currently experimental, some dependency preprocessing is needed in the evaluation steps
so using no-scc might give incorrect result */
if(Globals::Instance()->boolOption("no-scc"))
{
EvalComp* evalcomp;
unsigned method = Globals::Instance()->intOption("EvalMethod");
bool limitAS = Globals::Instance()->boolOption("limitAS");
unsigned long long maxNumAS = Globals::Instance()->intOption("maxNumAS");
unsigned foundAS = 0;
switch(method)
{
case 0:
evalcomp = new EvalDirect(se, pb, maxNumAS, limitAS);
break;
case 1:
evalcomp = new EvalMethod1(se, pb, pe, maxNumAS, limitAS);
break;
case 2:
evalcomp = new EvalMethod2(se, pb, pe, maxNumAS, limitAS);
break;
case 3:
evalcomp = new EvalDisjunctive(se, pb, pe, maxNumAS, limitAS);
break;
default:
evalcomp = new EvalDirect(se, pb, maxNumAS, limitAS);
break;
}
try
{
evalcomp->setInput(IDB, EDB);
ob->buildPre();
while(evalcomp->answerSetsLeft())
{
foundAS++;
AtomSet currentAS = evalcomp->getNextAnswerSet();
ob->buildOutput(currentAS);
std::cout << ob->getString() <<std::endl;
if( Globals::Instance()->boolOption("limitAS") &&
(foundAS >= Globals::Instance()->intOption("maxNumAS") ))
break;
}
ob->buildPost();
std::cout << ob->getString();
}
catch(GeneralError& e)
{
std::cerr <<e.getErrorMsg()<<std::endl;
delete evalcomp;
delete pb;
delete se;
delete pe;
delete ob;
return EXIT_FAILURE;
}
delete evalcomp;
delete pb;
delete se;
delete pe;
delete ob;
return EXIT_SUCCESS;
}
/* Strategy classes for dependency analysis/SCC */
HCFDetector* hcfd = new BoostHCFDetector();
MethodSelector* ms = new SimpleMethodSelector(hcfd);
GraphBuilder gb;
NodeGraph nodegraph;
try
{
/* Build the graph of dependency between atoms */
gb.run(IDB, nodegraph);
}
catch (GeneralError& e)
{
std::cerr << e.getErrorMsg() << std::endl << std::endl;
delete ms;
delete hcfd;
delete pb;
delete se;
delete pe;
delete ob;
return EXIT_FAILURE;
}
/*
gb.dumpGraph(nodegraph, std::cout);
return EXIT_SUCCESS;
*/
ComponentFinder* cf;
//
// The DependencyGraph identifies and creates the graph components that will
// be processed by the GraphProcessor.
//
DependencyGraph* dg;
try
{
cf = new BoostComponentFinder();
//
// Initializing the DependencyGraph. Its constructor uses the
// ComponentFinder to find relevant graph
// properties for the subsequent processing stage.
//
dg = new DependencyGraph(nodegraph, cf);
}
catch (GeneralError &e)
{
std::cerr << e.getErrorMsg() << std::endl << std::endl;
delete cf;
delete dg;
delete ms;
delete hcfd;
delete pb;
delete se;
delete pe;
delete ob;
return EXIT_FAILURE;
}
/* Create the graph processors to perform evaluation along SCCs */
GraphProcessor* gp = new GraphProcessor(dg, se, pb, pe, ms, ob);
/* Run it */
ob->buildPre();
try
{
gp->run(EDB);
}
catch(GeneralError& e)
{
std::cerr << e.getErrorMsg() << std::endl;
delete gp;
delete cf;
delete dg;
delete ms;
delete hcfd;
delete pb;
delete se;
delete pe;
delete ob;
return EXIT_FAILURE;
}
ob->buildPost();
std::cout << ob->getString();
/* computation ends */
time_t end = time(NULL);
std::cout << "BPA done.."<<std::endl;
std::cout << "Time: " <<(end-start) << "s" << std::endl;
delete gp;
delete cf;
delete dg;
delete ms;
delete hcfd;
delete pb;
delete se;
delete pe;
delete ob;
/* We are done */
return EXIT_SUCCESS;
}