-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmodelsBridge.cpp
234 lines (214 loc) · 8.69 KB
/
SmodelsBridge.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
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <sys/wait.h>
#include <string>
#include "SmodelsBridge.h"
#include "c++11_warning.h"
using namespace std;
unordered_set<Literal> SmodelsBridge::getAnswerSet(const Program &p) {
stringstream smodelsProgram, smodelsResponseBuf;
createSmodelsProgram(smodelsProgram, p);
string programString = smodelsProgram.str();
#ifdef PRINT_DEBUG
cout << "---\nDEBUG:\n" << programString;
#endif
pid_t pid;
int inputpipe[2], outputpipe[2];
char buf[1024];
if (pipe(inputpipe) == -1 || pipe(outputpipe) == -1) perror("Error creating pipe");
pid = fork();
if (pid == 0) {
// Child
dup2(inputpipe[0], STDIN_FILENO);
dup2(outputpipe[1], STDOUT_FILENO);
close(inputpipe[0]);
close(inputpipe[1]);
close(outputpipe[0]);
close(outputpipe[1]);
execl(smodelsExecPath, smodelsExecPath, (char*) NULL);
exit(1);
}
close(inputpipe[0]);
close(outputpipe[1]);
if (write(inputpipe[1], programString.c_str(), programString.size())==-1) {
perror("Error writing to smodels");
exit(1);
}
close(inputpipe[1]);
int status;
waitpid(pid, &status, 0);
while (int c = read(outputpipe[0], buf, sizeof(buf)-1)) {
if (c == -1) {
perror("Error reading response from smodels");
exit(1);
}
buf[c]=0;
smodelsResponseBuf << buf;
}
close(outputpipe[0]);
#ifdef PRINT_DEBUG
cout << "---\nDEBUG:\n" << smodelsResponseBuf.str();
#endif
unordered_set<Literal> answerSet;
string line;
getline(smodelsResponseBuf, line); //Smodels version
getline(smodelsResponseBuf, line);
if (line == "False")
throw false;
else if (line == "Answer: 1") {
getline(smodelsResponseBuf, line);
stringstream asStream(line);
string atom;
char colon = 0;
while (colon != ':')
asStream >> colon;
asStream >> atom;
while (!asStream.eof()) {
answerSet.insert(nameToLiteral(atom.c_str()));
asStream >> atom;
}
} else {
cerr << "Error parsing smodels output:" << line << endl;
exit(1);
}
return answerSet;
}
Literal SmodelsBridge::nameToLiteral(const char *n) const {
return atoi(n+1); //Skip one character and convert to integer
}
bool SmodelsBridge::existsAnswerSet(const Program &p) {
try {
getAnswerSet(p);
return true;
} catch (bool e) {
return false;
}
}
void SmodelsBridge::createSmodelsProgram(stringstream &smodelsProgram, const Program &p) {
countLiterals(p);
for (auto rule = p.rulesBeginIterator(); rule != p.rulesEndIterator(); rule++) {
/* Ugly code. Conversion should be implicit. */
switch ((*rule)->getType()) {
case Rule::RuleType::BASIC:
createSmodelsRule(smodelsProgram, dynamic_cast<BasicRule *>(rule->get()));
break;
case Rule::RuleType::CHOICE:
createSmodelsRule(smodelsProgram, dynamic_cast<ChoiceRule *>(rule->get()));
break;
case Rule::RuleType::CONSTRAINT:
createSmodelsRule(smodelsProgram, dynamic_cast<ConstraintRule *>(rule->get()));
break;
case Rule::RuleType::WEIGHTCONSTRAINT:
createSmodelsRule(smodelsProgram, dynamic_cast<WeightRule *>(rule->get()));
break;
}
}
smodelsProgram << "0" << endl;
for (int i = 2; i< maxLiteral; i++)
smodelsProgram << i << ' ' << 'a' << i << endl;
smodelsProgram << "0" << endl;
smodelsProgram << "B+" << endl;
for (auto it = p.positiveTargets.begin(); it != p.positiveTargets.end(); it++)
smodelsProgram << *it << endl;
smodelsProgram << "0" << endl;
smodelsProgram << "B-" << endl;
for (auto it = p.negativeTargets.begin(); it != p.negativeTargets.end(); it++)
smodelsProgram << *it << endl;
smodelsProgram << "0" << endl;
smodelsProgram << "1" << endl;
}
void SmodelsBridge::createSmodelsRule(stringstream &smodelsProgram, const BasicRule *rule) {
smodelsProgram << "1 " << rule->head << ' ' << (rule->positiveBody.size() + rule->negativeBody.size()) << ' ' << rule->negativeBody.size();
for (auto it = rule->negativeBody.begin(); it != rule->negativeBody.end(); it++)
smodelsProgram << ' ' << *it;
for (auto it = rule->positiveBody.begin(); it != rule->positiveBody.end(); it++)
smodelsProgram << ' ' << *it;
smodelsProgram << endl;
}
void SmodelsBridge::createSmodelsRule(stringstream &smodelsProgram, const ChoiceRule *rule) {
smodelsProgram << "3 " << rule->head.size();
for (auto it = rule->head.begin(); it != rule->head.end(); it++)
smodelsProgram << ' ' << *it;
smodelsProgram << (rule->positiveBody.size() + rule->negativeBody.size()) << ' ' << rule->negativeBody.size();
for (auto it = rule->negativeBody.begin(); it != rule->negativeBody.end(); it++)
smodelsProgram << ' ' << *it;
for (auto it = rule->positiveBody.begin(); it != rule->positiveBody.end(); it++)
smodelsProgram << ' ' << *it;
smodelsProgram << endl;
}
void SmodelsBridge::createSmodelsRule(stringstream &smodelsProgram, const ConstraintRule *rule) {
smodelsProgram << "1 1 " << (rule->positiveBody.size() + rule->negativeBody.size()) << ' ' << rule->negativeBody.size();
for (auto it = rule->negativeBody.begin(); it != rule->negativeBody.end(); it++)
smodelsProgram << ' ' << *it;
for (auto it = rule->positiveBody.begin(); it != rule->positiveBody.end(); it++)
smodelsProgram << ' ' << *it;
smodelsProgram << endl;
}
void SmodelsBridge::createSmodelsRule(stringstream &smodelsProgram, const WeightRule *rule) {
long negativeWeights = 0;
stringstream positive, negative, weightsNeg, weightsPos;
unsigned int numNegatives = 0;
for (auto it = rule->positiveBody.begin(); it != rule->positiveBody.end(); it++) {
if (it->second < 0) {
numNegatives++;
negative << ' ' << it->first;
weightsNeg << ' ' << -it->second;
negativeWeights -= it->second;
} else {
positive << ' ' << it->first;
weightsPos << ' ' << it->second;
}
}
Literal newLit = ++maxLiteral;
smodelsProgram << "1 1 1 1 " << newLit << endl;
smodelsProgram << "5 " << newLit << ' ' << (rule->upperLimit + negativeWeights + 1) << ' ' <<
rule->positiveBody.size() << ' ' << numNegatives << negative.str() << positive.str() <<
weightsNeg.str() << weightsPos.str() << endl;
}
/* TODO optimize this */
void SmodelsBridge::countLiterals(const Program &p) {
for (auto rule = p.rulesBeginIterator(); rule != p.rulesEndIterator(); rule++) {
/* Ugly code. Conversion should be implicit. */
switch ((*rule)->getType()) {
case Rule::RuleType::BASIC:
countLiterals(dynamic_cast<BasicRule *>(rule->get()));
break;
case Rule::RuleType::CHOICE:
countLiterals(dynamic_cast<ChoiceRule *>(rule->get()));
break;
case Rule::RuleType::CONSTRAINT:
countLiterals(dynamic_cast<ConstraintRule *>(rule->get()));
break;
case Rule::RuleType::WEIGHTCONSTRAINT:
countLiterals(dynamic_cast<WeightRule *>(rule->get()));
break;
}
}
}
void SmodelsBridge::countLiterals(const BasicRule *rule) {
if (rule->head > maxLiteral) maxLiteral = rule->head;
for (auto it = rule->positiveBody.begin(); it != rule->positiveBody.end(); it++)
if (*it > maxLiteral) maxLiteral = (*it);
for (auto it = rule->negativeBody.begin(); it != rule->negativeBody.end(); it++)
if (*it > maxLiteral) maxLiteral = (*it);
}
void SmodelsBridge::countLiterals(const ChoiceRule *rule) {
for (auto it = rule->head.begin(); it != rule->head.end(); it++)
if (*it > maxLiteral) maxLiteral = (*it);
for (auto it = rule->positiveBody.begin(); it != rule->positiveBody.end(); it++)
if (*it > maxLiteral) maxLiteral = (*it);
for (auto it = rule->negativeBody.begin(); it != rule->negativeBody.end(); it++)
if (*it > maxLiteral) maxLiteral = (*it);
}
void SmodelsBridge::countLiterals(const ConstraintRule *rule) {
for (auto it = rule->positiveBody.begin(); it != rule->positiveBody.end(); it++)
if (*it > maxLiteral) maxLiteral = (*it);
for (auto it = rule->negativeBody.begin(); it != rule->negativeBody.end(); it++)
if (*it > maxLiteral) maxLiteral = (*it);
}
void SmodelsBridge::countLiterals(const WeightRule *rule) {
for (auto it = rule->positiveBody.begin(); it != rule->positiveBody.end(); it++)
if (it->first > maxLiteral) maxLiteral = it->first;
}