Skip to content

Commit 3b949bb

Browse files
authored
Support returning boolean value in evaluator (#228)
1 parent 18e5b29 commit 3b949bb

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

Team20/Code20/IntegrationTesting/TestPQLEvaluator.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -451,5 +451,60 @@ TEST_CLASS(TestPqlEvaluator) {
451451
Pql::evaluate(pq, pkb.getQueryInterface(), actual);
452452
Assert::IsTrue(expected == actual);
453453
}
454+
455+
TEST_METHOD(TestEvaluateParsedQuery_SelectBoolean) {
456+
/* Boolean true
457+
Select BOOLEAN such that Follows(1, 2)
458+
*/
459+
ParsedQuery pq = {{},
460+
{PqlResultType::Boolean},
461+
{{TokenType::FOLLOWS,
462+
{TokenType::NUMBER, "1"},
463+
{TokenType::NUMBER, "2"}}}};
464+
std::list<VALUE> expected = {"TRUE"};
465+
std::list<VALUE> actual;
466+
Pql::evaluate(pq, pkb.getQueryInterface(), actual);
467+
Assert::IsTrue(expected == actual);
468+
469+
/* Boolean false
470+
Select BOOLEAN such that Follows(1, 3)
471+
*/
472+
pq = {{},
473+
{PqlResultType::Boolean},
474+
{{TokenType::FOLLOWS,
475+
{TokenType::NUMBER, "1"},
476+
{TokenType::NUMBER, "3"}}}};
477+
expected = {"FALSE"};
478+
actual.clear();
479+
Pql::evaluate(pq, pkb.getQueryInterface(), actual);
480+
Assert::IsTrue(expected == actual);
481+
482+
/* No values (indirect false)
483+
read r; if i; Select BOOLEAN such that Parent*(i, r)
484+
*/
485+
pq = {{{"r", TokenType::READ}, {"i", TokenType::IF}},
486+
{PqlResultType::Boolean},
487+
{{TokenType::PARENT, {TokenType::IF, "i"}, {TokenType::READ, "r"}}}};
488+
expected = {"FALSE"};
489+
actual.clear();
490+
Pql::evaluate(pq, pkb.getQueryInterface(), actual);
491+
Assert::IsTrue(expected == actual);
492+
493+
/* With values (indirect true)
494+
assign a; read r; call c; Select BOOLEAN such that Follows(r, a)
495+
*/
496+
pq = {{{"a", TokenType::ASSIGN},
497+
{"r", TokenType::READ},
498+
{"c", TokenType::CALL}},
499+
{PqlResultType::Boolean},
500+
{{TokenType::FOLLOWS,
501+
{TokenType::READ, "r"},
502+
{TokenType::ASSIGN, "a"}}}};
503+
expected = {"TRUE"};
504+
actual.clear();
505+
Pql::evaluate(pq, pkb.getQueryInterface(), actual);
506+
Logger::WriteMessage(actual.front().c_str());
507+
Assert::IsTrue(expected == actual);
508+
}
454509
};
455510
} // namespace IntegrationTesting

Team20/Code20/source/PQLEvaluator.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ void Pql::evaluate(ParsedQuery pq, PkbQueryInterface *queryHandler,
1919
// Early termination if clause evaluates to false
2020
if (!dispatcher->booleanDispatch()) {
2121
delete dispatcher;
22+
if (pq.results.resultType == PqlResultType::Boolean) {
23+
result.push_back(FALSE_RESULT);
24+
}
2225
return;
2326
}
2427
} else {
@@ -36,6 +39,9 @@ void Pql::evaluate(ParsedQuery pq, PkbQueryInterface *queryHandler,
3639
// Early termination if clause evaluates to false
3740
if (!dispatcher->booleanDispatch()) {
3841
delete dispatcher;
42+
if (pq.results.resultType == PqlResultType::Boolean) {
43+
result.push_back(FALSE_RESULT);
44+
}
3945
return;
4046
}
4147
} else {
@@ -47,6 +53,9 @@ void Pql::evaluate(ParsedQuery pq, PkbQueryInterface *queryHandler,
4753

4854
// Early termination if table contains synonyms, but has no values
4955
if (!table.empty() && table.rowCount() == 0) {
56+
if (pq.results.resultType == PqlResultType::Boolean) {
57+
result.push_back(FALSE_RESULT);
58+
}
5059
return;
5160
}
5261

@@ -72,6 +81,15 @@ void Pql::evaluate(ParsedQuery pq, PkbQueryInterface *queryHandler,
7281
delete dispatcher;
7382
filtered.merge(clauseResult);
7483
}
84+
85+
if (pq.results.resultType == PqlResultType::Boolean) {
86+
if (!filtered.empty() && filtered.rowCount() == 0) {
87+
result.push_back(FALSE_RESULT);
88+
} else {
89+
result.push_back(TRUE_RESULT);
90+
}
91+
return;
92+
}
7593
filtered.flatten(pq.results.results, result);
7694
}
7795

Team20/Code20/source/PQLEvaluator.h

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ typedef std::string SYMBOL;
1111
typedef std::string VALUE;
1212
typedef std::unordered_map<SYMBOL, std::vector<VALUE>> TABLE;
1313

14+
constexpr auto TRUE_RESULT = "TRUE";
15+
constexpr auto FALSE_RESULT = "FALSE";
16+
1417
/** @brief Represents the table of possible query results.
1518
* As clauses are evaluated, values from the PkbTables are pushed into
1619
* this table.

0 commit comments

Comments
 (0)