-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASPTranslate.h
More file actions
296 lines (237 loc) · 7.72 KB
/
ASPTranslate.h
File metadata and controls
296 lines (237 loc) · 7.72 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
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
/***************************************************************************
* Copyright (C) 2014 by Mushthofa *
* Mushthofa.Mushthofa@Ugent.be *
* *
* 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. *
***************************************************************************/
/*
* ASPTranslate.h
*
* Created on: Feb 2014
* Author: mush
*/
#ifndef ASPTRANSLATE_H_
#define ASPTRANSLATE_H_
#include "Program.h"
class ASPTranslate
{
public:
/* Types of standard rules:
* 1) ONEHB: one (real) predicate in the head and body, e.g.:
* a <- p(X,Y)*X<Y.
*
* 2) FACT, e.g.
* a <- #c
*
* 3) CONSTR, e.g.
* #c <- a.
*
* 4) BTNORM, t-norm in the body, one head atom, e.g. (may contain infix)
* [also include when one of the body literals is NAF]
* a <- b(X,Y) * c * X<Y
*
* 5) BCOTNORM, co-tnorm in the body, one head atom, e.g.
* [also include when one of the body literals is NAF]
* a <- b+c.
*
* 6) BMAX, max in the body, e.g.
* [also include when one of the body literals is NAF]
* a <- b&c
*
* 7) BMIN, min in the body, e.g.
* [also include when one of the body literals is NAF]
* a <- b^c
*
* 8) HTNORM, tnorm in the head, one literal in the body, e.g.
* a*b <- c
*
* 9)HCOTNORM, cotnorm in the head, one literal in the body, e.g.
* a+b <- p(X,Y)*X<Y.
*
* 10) HMAX, max in the head, e.g.
* a&b <- c.
*
* 11) HMIN, min in the head, e.g.
* a^b <- c.
*
* 12) BNAF, negation in the body, e.g.
* a <- not b
*
* 13) INV, invalid/unhandled type.
*
*/
typedef enum {ONEHB, FACT, CONSTR, BTNORM, BCOTNORM, BMAX, BMIN,
HTNORM, HCOTNORM, HMAX, HMIN, BNAF, INV} RuleType_t;
ASPTranslate(const Program& p, int kk)
:program(p), needmincheck(false), anydisj(false), k(kk)
{
// Compute all non-SRCF Atoms
getNonSRCFAtoms();
if(nonSRCFAtoms.size()>0 && anydisj)
needmincheck = true;
// Do shift for disj rules with SRCF atoms
//std::cout<<"Non SRCF = "<<nonSRCFAtoms.size()<<std::endl;
//std::set<AtomPtr>::iterator it;
//for(it=nonSRCFAtoms.begin(); it!=nonSRCFAtoms.end(); ++it)
//std::cout<<*(*it)<<std::endl;
doShift();
doTranslate(); // translate rules
addConsRule(); // add rules p_0 <- , p_i <- p_{i+1}
}
std::string getProgram() const
{
return os.str();
}
bool needMinCheck()
{
return needmincheck;
}
protected:
void getNonSRCFAtoms()
{
// Get all the non-SRCF Atoms;
// by finding all atoms that are disjunct in the head
// and disjunct in the body as well.
std::set<AtomPtr> headDisj;
std::set<AtomPtr> bodyDisj;
Program::const_iterator it;
for(it=program.begin(); it!=program.end(); ++it)
{
HeadExpr_t head = (*it)->getHead();
BodyExpr_t body = (*it)->getBody();
HeadList_t hl = head.first;
BodyList_t bl = body.first;
if(hl.size()==2 && head.second == CO_TNORM)
{
anydisj = true;
//
//if(hl[0]->getPredicateName().find("_NEW_") == std::string::npos)
headDisj.insert(hl[0]);
//if(hl[1]->getPredicateName().find("_NEW_") == std::string::npos)
headDisj.insert(hl[1]);
}
if(bl.size() == 2 && body.second == CO_TNORM)
{
//
//if(bl[0]->getAtom()->getPredicateName().find("_NEW_") == std::string::npos)
bodyDisj.insert(bl[0]->getAtom());
//if(bl[1]->getAtom()->getPredicateName().find("_NEW_") == std::string::npos)
bodyDisj.insert(bl[1]->getAtom());
}
}
// Take intersection;
std::set_intersection(headDisj.begin(), headDisj.end(), bodyDisj.begin(), bodyDisj.end(),
std::inserter(nonSRCFAtoms, nonSRCFAtoms.begin()));
}
void doShift()
{
/*
* Do shifting on all disjunctive rules
*/
//std::cout<<"Do shifting "<<std::endl;
Program::iterator it = program.begin();
while(it!=program.end())
{
Program::iterator now = it++;
HeadExpr_t head = (*now)->getHead();
BodyExpr_t body = (*now)->getBody();
HeadList_t hl = head.first;
BodyList_t bl = body.first;
//cout<<"Rule "<<(*(*now))<<std::endl;
//cout<<"Head size "<<hl.size()<<" operator = "<<head.second<<std::endl;
// Check if disjunctive rule
if(hl.size()!=2 || head.second!=CO_TNORM)
continue;
if(nonSRCFAtoms.find(hl[0])!=nonSRCFAtoms.end()
|| nonSRCFAtoms.find(hl[1])!=nonSRCFAtoms.end())
continue;
// Replace a+b <- c
// into
// a <- c x p1
// b <- c x p2
// p1 <- not b
// p2 <- not a
AtomPtr p1 (new Atom(Program::genNextPred(), Tuple()));
AtomPtr p2 (new Atom(Program::genNextPred(), Tuple()));
AtomPtr a = hl[0];
AtomPtr b = hl[1];
// Create new body lists
BodyList_t b1 = bl, b2 = bl;
b1.push_back(LiteralPtr(new Literal(p1)));
b2.push_back(LiteralPtr(new Literal(p2)));
BodyExpr_t body1, body2;
body1.first = b1;
body2.first = b2;
body1.second = TNORM;
body2.second = TNORM;
// Set up the heads;
HeadList_t hl1, hl2;
hl1.push_back(b);
hl2.push_back(a);
//std::cout<<"Deleting rule "<<*(*now)<<std::endl;
program.deleteRule(now);
RulePtr r1 (new Rule(std::make_pair(hl1, MAX), body1, "_NEW_", program.size()));
RulePtr r2 (new Rule(std::make_pair(hl2, MAX), body2, "_NEW_", program.size()));
//std::cout<<"adding rule "<<*r1<<"and "<<*r2<<std::endl;
program.addRule(r1);
program.addRule(r2);
hl1.clear();
hl2.clear();
b1.clear();
b2.clear();
hl1.push_back(p1);
hl2.push_back(p2);
b1.push_back(LiteralPtr(new Literal(a, true)));
b2.push_back(LiteralPtr(new Literal(b, true)));
body1.first = b1;
body2.first = b2;
RulePtr r3 (new Rule(std::make_pair(hl1, MAX), body1, "_VIRT_", program.size()));
RulePtr r4 (new Rule(std::make_pair(hl2, MAX), body2, "_VIRT_", program.size()));
//std::cout<<"adding rule "<<*r3<<"and "<<*r4<<std::endl;
program.addRule(r3);
program.addRule(r4);
}
}
/* During translate, rules are converted to string rep
* and accumulated in os,
* doTranslate() recaps all the translation
* addConsRule() adds the auxiliary rules a_i <- a_i+1}
* translateXYX() are rules that perform translation for rules of type XYZ
*/
void doTranslate();
void translateRule(RulePtr r);
void addConsRule();
void translateONEHB(RulePtr r);
void translateBNAF(RulePtr r);
void translateBTNORM(RulePtr r);
void translateBCOTNORM(RulePtr r);
void translateFACT(RulePtr);
void translateCONSTR(RulePtr);
void translateBMIN(RulePtr);
void translateBMAX(RulePtr);
void translateHTNORM(RulePtr );
void translateHCOTNORM(RulePtr);
void translateHMAX(RulePtr);
void translateHMIN(RulePtr);
RuleType_t detType(RulePtr r);
std::ostringstream os;
Program program;
std::set<AtomPtr> nonSRCFAtoms;
bool needmincheck;
bool anydisj;
int k;
};
#endif /* ASPTRANSLATE_H_ */