-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLVASPSolver.cpp
More file actions
233 lines (198 loc) · 5.71 KB
/
DLVASPSolver.cpp
File metadata and controls
233 lines (198 loc) · 5.71 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
/***************************************************************************
* 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. *
***************************************************************************/
/**
* @file DLVASPSolver.h
* @author Roman Schindlauer, Mushthofa
*
* @brief ASP solver class implemented using dlv.
*
*/
#include "DLVASPSolver.h"
#include "DLVResultParser.h"
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string/trim.hpp>
DLVASPSolver::DLVASPSolver(ProgramBuilder* progbuilder, ASPResultParser* rp, std::set<std::string>* filter)
:ASPSolver(progbuilder, rp), checkOnly(false), checkSatisfy(false)
{
/* Get dlv's path and arguments from command line */
lpcommand = Globals::Instance()->strOption("DLVPath");
boost::tokenizer<> args (Globals::Instance()->strOption("DLVArgs"));
lpargs.push_back(lpcommand);
lpargs.push_back("-silent");
lpargs.push_back("--");
std::string filter_arg = "";
if(filter != 0 && filter->size()>0)
{
filter_arg += "-filter=";
std::set<std::string>::iterator f_it;
unsigned n(0);
for(f_it = filter->begin(); f_it !=filter->end(); ++f_it)
{
filter_arg += *f_it;
if(n<filter->size() - 1)
filter_arg += ",";
}
lpargs.push_back(filter_arg);
}
for(boost::tokenizer<>::iterator args_it = args.begin(); args_it!=args.end(); ++args_it)
{
lpargs.push_back(*args_it);
}
pb = new ProcessBuf();
/*
answerSets.clear();
answerSetIndex = answerSets.end();
*/
}
DLVASPSolver::~DLVASPSolver()
{
delete pb;
}
void DLVASPSolver::callSolver(const Program& prog, const AtomSet& facts, bool checkonly)
{
//answerSets.clear();
checkOnly = checkonly;
int retcode = 0;
if(pb->isOpen())
retcode = pb->close();
try
{
pb->open(lpargs);
std::iostream iopipe(pb);
iopipe.exceptions(std::ios_base::badbit);
std::string prg;
programBuilder->clearString();
programBuilder->buildProgram(prog);
programBuilder->buildFacts(facts);
prg = programBuilder->getString();
try
{
/*
if(checkonly)
{
std::cout<<"Calling dlv with program "<<std::endl;
std::cout<<prg<<std::endl;
}
*/
iopipe<<prg<<std::endl;
}
catch(std::ios_base::failure e)
{
std::ostringstream oserr;
oserr << "Error executing command ";
for(std::vector<std::string>::iterator it = lpargs.begin(); it!=lpargs.end(); ++it)
oserr<<*it<<" ";
std::cout<<"I/O stream failure: "<<e.what()<<std::endl;
oserr<<"Program is "<<std::endl<<prg<<std::endl;
throw FatalError(oserr.str());
}
pb->endoffile();
/*std::cout<<"DLV Result: "<<std::endl;
*/
std::string outputline;
if(checkonly)
{
std::getline(iopipe, outputline);
if(outputline.size() >= 2) // atleast {}
checkSatisfy = true;
else
checkSatisfy = false;
}
else
{
//std::cout<<"parsing dlv result"<<outputline<<std::endl;
if(std::getline(iopipe, outputline))
{
try
{
resultParser->parseLine(outputline);
}
catch(GeneralError& e)
{
std::ostringstream err;
err << e.getErrorMsg() << std::endl;
programBuilder->clearString();
programBuilder->buildProgram(prog);
programBuilder->buildFacts(facts);
err << " Program was : " <<std::endl << programBuilder->getString() <<std::endl;
throw FatalError(err.str());
}
currentAnswer= resultParser->getAnswerSet();
answersetsleft = true;
}
else
answersetsleft = false;
}
//retcode = pb.close();
}
catch(FatalError& e)
{
throw e;
}
catch(GeneralError& e)
{
throw FatalError(e.getErrorMsg());
}
catch (std::exception& e)
{
throw FatalError(e.what());
}
if(retcode==127)
{
throw FatalError("DLV is not found in the specified path");
}
}
//
AtomSet DLVASPSolver::getNextAnswerSet()
{
if(checkOnly)
throw FatalError("Cannot get next answer set: Solver was called with 'check only' flag!");
AtomSet resultAS = currentAnswer;
std::string outputline;
std::iostream iopipe(pb);
iopipe.exceptions(std::ios_base::badbit);
if(std::getline(iopipe, outputline))
{
resultParser->parseLine(outputline);
currentAnswer = resultParser->getAnswerSet();
answersetsleft = true;
}
else
answersetsleft = false;
return resultAS;
}
bool DLVASPSolver::answerSetsLeft() const
{
if(checkOnly)
{
return checkSatisfy;
}
return answersetsleft;
}
DLVASPSolverEngine::DLVASPSolverEngine()
{
}
DLVASPSolver* DLVASPSolverEngine::createSolver(ProgramBuilder* pb, std::set<std::string>* filter)
{
ASPResultParser* rp = new DLVResultParser();
DLVASPSolver* new_solver = new DLVASPSolver(pb, rp, filter);
return new_solver;
}
//End