File: test_text.cpp

package info (click to toggle)
clasp 3.3.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,716 kB
  • sloc: cpp: 69,712; ansic: 207; xml: 182; sh: 92; makefile: 28
file content (458 lines) | stat: -rw-r--r-- 15,898 bytes parent folder | download | duplicates (4)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//
// Copyright (c) 2016-2017 Benjamin Kaufmann
//
// This file is part of Potassco.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//

#include <catch/catch.hpp>
#include "test_common.h"
#include <potassco/aspif_text.h>
#include <potassco/aspif.h>
#include <sstream>
namespace Potassco {
namespace Test {
namespace Text {

bool read(AspifTextInput& in, std::stringstream& str) {
	return in.accept(str) && in.parse();
}
TEST_CASE("Text reader ", "[text]") {
	std::stringstream input, output;
	AspifOutput out(output);
	AspifTextInput prg(&out);

	SECTION("read empty") {
		REQUIRE(read(prg, input));
		REQUIRE(output.str() == "asp 1 0 0\n0\n");
	}
	SECTION("read empty integrity constraint") {
		input << ":- .";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("1 0 0 0 0") != std::string::npos);
	}
	SECTION("read fact") {
		input << "x1.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("1 0 1 1 0 0") != std::string::npos);
	}
	SECTION("read basic rule") {
		input << "x1 :- not   x2.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("1 0 1 1 0 1 -2") != std::string::npos);
	}
	SECTION("read choice rule") {
		input << "{x1} :- not x2.\n";
		input << "{x2, x3}.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("1 1 1 1 0 1 -2") != std::string::npos);
		REQUIRE(output.str().find("1 1 2 2 3 0 0") != std::string::npos);
	}
	SECTION("ready empty choice rule") {
		input << "{}.\n";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("1 1 0 0 0") != std::string::npos);
	}
	SECTION("read disjunctive rule") {
		input << "x1 | x2 :- not x3.";
		input << "x1 ; x2 :- not x4.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("1 0 2 1 2 0 1 -3") != std::string::npos);
		REQUIRE(output.str().find("1 0 2 1 2 0 1 -4") != std::string::npos);
	}
	SECTION("read weight rule") {
		input << "x1 :- 2 {x2, x3=2, not x4 = 3, x5}.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("1 0 1 1 1 2 4 2 1 3 2 -4 3 5 1") != std::string::npos);
	}
	SECTION("read alternative atom names") {
		input << "a :- not b, x_3.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("1 0 1 1 0 2 -2 3") != std::string::npos);
	}
	SECTION("read integrity constraint") {
		input << ":- x1, not x2.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("1 0 0 0 2 1 -2") != std::string::npos);
	}
	SECTION("read minimize constraint") {
		input << "#minimize {x1, x2, x3}.\n";
		input << "#minimize {not x1=2, x4, not x5 = 3}@1.\n";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("2 0 3 1 1 2 1 3 1") != std::string::npos);
		REQUIRE(output.str().find("2 1 3 -1 2 4 1 -5 3") != std::string::npos);
	}
	SECTION("read project") {
		input << "#project {a,x2}.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("3 2 1 2") != std::string::npos);
	}
	SECTION("read empty project") {
		input << "#project {}.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("3 0") != std::string::npos);
	}
	SECTION("read output") {
		input << "#output foo (1, \"x1, x2\") : x1, x2.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("4 15 foo(1,\"x1, x2\") 2 1 2") != std::string::npos);
	}
	SECTION("read external") {
		input << "#external x1.\n";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("5 1 2") != std::string::npos);
	}
	SECTION("read external with value") {
		input << "#external x2. [true]\n";
		input << "#external x3. [false]\n";
		input << "#external x4. [free]\n";
		input << "#external x5. [release]\n";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("5 2 1") != std::string::npos);
		REQUIRE(output.str().find("5 3 2") != std::string::npos);
		REQUIRE(output.str().find("5 4 0") != std::string::npos);
		REQUIRE(output.str().find("5 5 3") != std::string::npos);
	}
	SECTION("read external with unknown value") {
		input << "#external x2. [open]\n";
		REQUIRE_THROWS(read(prg, input));
	}
	SECTION("read assume") {
		input << "#assume {a, not x2}.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("6 2 1 -2") != std::string::npos);
	}
	SECTION("read empty assume") {
		input << "#assume {}.";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("6 0") != std::string::npos);
	}
	SECTION("read heuristic") {
		input << "#heuristic x1. [1, level]";
		input << "#heuristic x2 : x1. [2@1, true]";
		input << "#heuristic x3 :. [1,level]";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("7 0 1 1 0 0") != std::string::npos);
		REQUIRE(output.str().find("7 4 2 2 1 1 1") != std::string::npos);
		REQUIRE(output.str().find("7 0 3 1 0 0") != std::string::npos);
	}
	SECTION("read edge") {
		input << "#edge (1,2) : x1.";
		input << "#edge (2,1).";
		REQUIRE(read(prg, input));
		REQUIRE(output.str().find("8 1 2 1 1") != std::string::npos);
		REQUIRE(output.str().find("8 2 1 0") != std::string::npos);
	}
	SECTION("read incremental") {
		input << "#incremental.\n";
		input << "{x1}.\n";
		input << "#step.\n";
		input << "{x2}.\n";
		REQUIRE(read(prg, input));
		REQUIRE(prg.parse());
		REQUIRE(output.str() ==
			"asp 1 0 0 incremental\n"
			"1 1 1 1 0 0\n"
			"0\n"
			"1 1 1 2 0 0\n"
			"0\n");
	}
	SECTION("read error") {
		input << "#incremental.\n";
		input << "#foo.\n";
		try { read(prg, input); REQUIRE(false); }
		catch (const std::logic_error& e) {
			REQUIRE(std::strstr(e.what(), "parse error in line 2: ") != 0);
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////
// AspifTextOutput
/////////////////////////////////////////////////////////////////////////////////////////
TEST_CASE("Text writer ", "[text]") {
	std::stringstream input, output;
	AspifTextOutput out(output);
	AspifTextInput  prg(&out);
	SECTION("empty program is empty") {
		REQUIRE(read(prg, input));
		REQUIRE(output.str() == "");
	}
	SECTION("simple fact") {
		input << "x1.";
		read(prg, input);
		REQUIRE(output.str() == "x_1.\n");
	}
	SECTION("named fact") {
		input << "x1.\n#output foo : x1.";
		read(prg, input);
		REQUIRE(output.str() == "foo.\n");
	}
	SECTION("simple choice") {
		input << "{x1,x2}.\n#output foo : x1.";
		read(prg, input);
		REQUIRE(output.str() == "{foo;x_2}.\n");
	}
	SECTION("integrity constraint") {
		input << ":- x1,x2.\n#output foo : x1.";
		read(prg, input);
		REQUIRE(output.str() == ":- foo, x_2.\n");
	}
	SECTION("empty integrity constraint") {
		input << ":-.";
		read(prg, input);
		REQUIRE(output.str() == ":- .\n");
	}
	SECTION("basic rule") {
		input << "x1 :- x2, not x3, x4.\n#output foo : x1.\n#output bar : x3.";
		read(prg, input);
		REQUIRE(output.str() == "foo :- x_2, not bar, x_4.\n");
	}
	SECTION("choice rule") {
		input << "{x1,x2} :- not x3, x4.\n#output foo : x1.\n#output bar : x3.";
		read(prg, input);
		REQUIRE(output.str() == "{foo;x_2} :- not bar, x_4.\n");
	}
	SECTION("disjunctive rule") {
		input << "x1;x2 :- not x3, x4.\n#output foo : x1.\n#output bar : x3.";
		read(prg, input);
		REQUIRE(output.str() == "foo|x_2 :- not bar, x_4.\n");
	}
	SECTION("cardinality rule") {
		input << "x1;x2 :- 1{not x3, x4}.\n#output foo : x1.\n#output bar : x3.";
		read(prg, input);
		REQUIRE(output.str() == "foo|x_2 :- 1{not bar; x_4}.\n");
	}
	SECTION("sum rule") {
		input << "x1;x2 :- 3{not x3=2, x4, x5=1,x6=2}.\n#output foo : x1.\n#output bar : x3.";
		read(prg, input);
		REQUIRE(output.str() == "foo|x_2 :- 3{not bar=2; x_4=1; x_5=1; x_6=2}.\n");
	}
	SECTION("convert sum rule to cardinality rule") {
		input << "x1;x2 :- 3{not x3=2, x4=2, x5=2,x6=2}.\n#output foo : x1.\n#output bar : x3.";
		read(prg, input);
		REQUIRE(output.str() == "foo|x_2 :- 2{not bar; x_4; x_5; x_6}.\n");
	}
	SECTION("minimize rule") {
		input << "#minimize{x1,x2=2,x3}.\n#minimize{not x1=3,not x2,not x3}@1.";
		read(prg, input);
		REQUIRE(output.str() == "#minimize{x_1=1; x_2=2; x_3=1}@0.\n#minimize{not x_1=3; not x_2=1; not x_3=1}@1.\n");
	}
	SECTION("output statements") {
		input << "{x1;x2}.\n#output foo.\n#output bar : x1.\n#output \"Hello World\" : x2, not x1.";
		read(prg, input);
		REQUIRE(output.str() == "{bar;x_2}.\n#show foo.\n#show \"Hello World\" : x_2, not bar.\n");
	}
	SECTION("write external - ") {
		SECTION("default") {
			input << "#external x1.";
			read(prg, input);
			REQUIRE(output.str() == "#external x_1.\n");
		}
		SECTION("false is default") {
			input << "#external x1. [false]";
			read(prg, input);
			REQUIRE(output.str() == "#external x_1.\n");
		}
		SECTION("with value") {
			input << "#external x1. [true]";
			input << "#external x2. [free]";
			input << "#external x3. [release]";
			read(prg, input);
			REQUIRE(output.str() == "#external x_1. [true]\n#external x_2. [free]\n#external x_3. [release]\n");
		}
	}
	SECTION("assumption directive") {
		input << "#assume{x1,not x2,x3}.";
		read(prg, input);
		REQUIRE(output.str() == "#assume{x_1, not x_2, x_3}.\n");
	}
	SECTION("projection directive") {
		input << "#project{x1,x2,x3}.";
		read(prg, input);
		REQUIRE(output.str() == "#project{x_1, x_2, x_3}.\n");
	}
	SECTION("edge directive") {
		input << "#edge (0,1) : x1, not x2.";
		input << "#edge (1,0).";
		read(prg, input);
		REQUIRE(output.str() == "#edge(0,1) : x_1, not x_2.\n#edge(1,0).\n");
	}

	SECTION("heuristic directive -") {
		SECTION("simple") {
			input << "#heuristic a. [1,true]";
			read(prg, input);
			REQUIRE(output.str() == "#heuristic x_1. [1, true]\n");
		}
		SECTION("simple with priority") {
			input << "#heuristic a. [1@2,true]";
			read(prg, input);
			REQUIRE(output.str() == "#heuristic x_1. [1@2, true]\n");
		}
		SECTION("with condition") {
			input << "#heuristic a : b, not c. [1@2,true]";
			read(prg, input);
			REQUIRE(output.str() == "#heuristic x_1 : x_2, not x_3. [1@2, true]\n");
		}
	}
	SECTION("incremental program") {
		input << "#incremental.\n";
		input << "{x1;x2}.";
		input << "#external x3.";
		input << "#output a(1) : x1.";
		input << "#step.";
		input << "x3 :- x1.";
		input << "#step.";
		input << "x4 :- x2,not x3.";
		read(prg, input);
		prg.parse();
		prg.parse();
		REQUIRE(output.str() ==
			"% #program base.\n"
			"{a(1);x_2}.\n"
			"#external x_3.\n"
			"% #program step(1).\n"
			"x_3 :- a(1).\n"
			"% #program step(2).\n"
			"x_4 :- x_2, not x_3.\n");
	}
}

TEST_CASE("Text writer writes theory", "[text]") {
	std::stringstream output;
	AspifTextOutput out(output);
	out.initProgram(false);
	out.beginStep();
	SECTION("write empty atom") {
		out.theoryAtom(0, 0, Potassco::toSpan<Id_t>());
		out.theoryTerm(0, Potassco::toSpan("t"));
		out.endStep();
		REQUIRE(output.str() == "&t{}.\n");
	}
	SECTION("write operators") {
		out.theoryTerm(0, Potassco::toSpan("t"));
		out.theoryTerm(1, Potassco::toSpan("x"));
		out.theoryTerm(2, Potassco::toSpan("y"));
		out.theoryTerm(3, Potassco::toSpan("^~\\?."));
		std::vector<Id_t> ids;
		out.theoryTerm(4, 3, Potassco::toSpan(ids ={1,2}));
		out.theoryElement(0, Potassco::toSpan(ids ={4}), Potassco::toSpan<Lit_t>());
		out.theoryAtom(0, 0, Potassco::toSpan(ids ={0}));
		out.endStep();
		REQUIRE(output.str() == "&t{x ^~\\?. y}.\n");
	}
	SECTION("write complex atom") {
		out.theoryTerm(1, 200);
		out.theoryTerm(3, 400);
		out.theoryTerm(6, 1);
		out.theoryTerm(11, 2);
		out.theoryTerm(0, Potassco::toSpan("diff"));
		out.theoryTerm(2, Potassco::toSpan("<="));
		out.theoryTerm(4, Potassco::toSpan("-"));
		out.theoryTerm(5, Potassco::toSpan("end"));
		out.theoryTerm(8, Potassco::toSpan("start"));
		std::vector<Id_t> ids;
		out.theoryTerm(7, 5, Potassco::toSpan(ids = {6}));
		out.theoryTerm(9, 8, Potassco::toSpan(ids = {6}));
		out.theoryTerm(10, 4, Potassco::toSpan(ids = {7, 9}));
		out.theoryTerm(12, 5, Potassco::toSpan(ids = {11}));
		out.theoryTerm(13, 8, Potassco::toSpan(ids = {11}));
		out.theoryTerm(14, 4, Potassco::toSpan(ids = {12, 13}));

		out.theoryElement(0, Potassco::toSpan(ids = {10}), Potassco::toSpan<Lit_t>());
		out.theoryElement(1, Potassco::toSpan(ids = {14}), Potassco::toSpan<Lit_t>());

		out.theoryAtom(0, 0, Potassco::toSpan(ids = {0}), 2, 1);
		out.theoryAtom(0, 0, Potassco::toSpan(ids = {1}), 2, 3);
		out.endStep();
		REQUIRE(output.str() ==
			"&diff{end(1) - start(1)} <= 200.\n"
			"&diff{end(2) - start(2)} <= 400.\n");
	}
	SECTION("Use theory atom in rule") {
		Atom_t head = 2;
		Lit_t  body = 1;
		out.rule(Head_t::Disjunctive, toSpan(&head, 1), toSpan(&body, 1));
		out.theoryTerm(0, Potassco::toSpan("atom"));
		out.theoryTerm(1, Potassco::toSpan("x"));
		out.theoryTerm(2, Potassco::toSpan("y"));
		std::vector<Id_t> ids;
		out.theoryElement(0, Potassco::toSpan(ids = {1, 2}), Potassco::toSpan<Lit_t>());
		out.theoryElement(1, Potassco::toSpan(ids = {2}), Potassco::toSpan<Lit_t>());
		out.theoryAtom(1, 0, Potassco::toSpan(ids = {0, 1}));
		out.endStep();
		REQUIRE(output.str() == "x_2 :- &atom{x, y; y}.\n");
	}
	SECTION("Theory element with condition") {
		std::vector<Atom_t> head;
		std::vector<Id_t> ids;
		std::vector<Lit_t> body;
		out.rule(Head_t::Choice, toSpan(head = {1, 2}), toSpan<Lit_t>());
		out.rule(Head_t::Disjunctive, toSpan(head = {4}), toSpan(body = {3}));
		out.output(toSpan("y"), toSpan(body = {1}));
		out.output(toSpan("z"), toSpan(body = {2}));
		out.theoryTerm(0, Potassco::toSpan("atom"));
		out.theoryTerm(1, Potassco::toSpan("elem"));
		out.theoryTerm(2, Potassco::toSpan("p"));
		out.theoryElement(0, Potassco::toSpan(ids = {1}), Potassco::toSpan(body = {1, -2}));
		out.theoryElement(1, Potassco::toSpan(ids = {2}), Potassco::toSpan(body = {1}));
		out.theoryAtom(3, 0, Potassco::toSpan(ids = {0, 1}));
		out.endStep();
		REQUIRE(output.str() ==
			"{y;z}.\n"
			"x_4 :- &atom{elem : y, not z; p : y}.\n");
	}
	SECTION("write complex atom incrementally") {
		out.initProgram(true);
		out.beginStep();
		out.theoryTerm(1, 200);
		out.theoryTerm(6, 1);
		out.theoryTerm(11, 2);
		out.theoryTerm(0, Potassco::toSpan("diff"));
		out.theoryTerm(2, Potassco::toSpan("<="));
		out.theoryTerm(4, Potassco::toSpan("-"));
		out.theoryTerm(5, Potassco::toSpan("end"));
		out.theoryTerm(8, Potassco::toSpan("start"));
		std::vector<Id_t> ids;
		out.theoryTerm(7, 5, Potassco::toSpan(ids = {6}));
		out.theoryTerm(9, 8, Potassco::toSpan(ids = {6}));
		out.theoryTerm(10, 4, Potassco::toSpan(ids = {7, 9}));

		out.theoryElement(0, Potassco::toSpan(ids = {10}), Potassco::toSpan<Lit_t>());
		out.theoryAtom(0, 0, Potassco::toSpan(ids = {0}), 2, 1);
		out.endStep();
		REQUIRE(output.str() ==
			"% #program base.\n"
			"&diff{end(1) - start(1)} <= 200.\n");
		output.str("");
		out.beginStep();
		out.theoryTerm(1, 600);
		out.theoryTerm(12, 5, Potassco::toSpan(ids = {11}));
		out.theoryTerm(13, 8, Potassco::toSpan(ids = {11}));
		out.theoryTerm(14, 4, Potassco::toSpan(ids = {12, 13}));
		out.theoryElement(0, Potassco::toSpan(ids = {14}), Potassco::toSpan<Lit_t>());
		out.theoryAtom(0, 0, Potassco::toSpan(ids = {0}), 2, 1);
		out.endStep();
		REQUIRE(output.str() ==
			"% #program step(1).\n"
			"&diff{end(2) - start(2)} <= 600.\n");
	}
}
}}}