Skip to content

Commit 18e5b29

Browse files
pengnamafterdusk
andauthored
Refactor ParsedQuery object with new result and WITH parsing (#222)
* Add support for AND parsing for relationship and pattern * Add relationship specifications for Next,Affect,Call * Refactor for proper pattern and parsing * Add while and if pattern parsing * Add multiple result parsing * Change lexer delimiter due to stmt# usage in attrRef * Add with statement parsing * Add definitions for new PqlResult * Define definitions for ref types * Add initial draft * Fix bugs * Use LJ's version of evaluator * Remove test files * Reset files * Revert erroneous changes from merge * Fix incorrect merge conflict resolution * Update flatten to accept TUPLE * Add simple integration test for new changes * Add simple endtoend integration test * Fix typo * Update Affects Co-authored-by: Liang Jun <[email protected]> Co-authored-by: Evan (Liang Jun) <[email protected]>
1 parent 2cdc334 commit 18e5b29

14 files changed

+227
-88
lines changed

Team20/Code20/IntegrationTesting/IntegrationTesting.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
<ClInclude Include="targetver.h" />
165165
</ItemGroup>
166166
<ItemGroup>
167+
<ClCompile Include="TestPqlEndToEnd.cpp" />
167168
<ClCompile Include="TestPqlParserLexerIntegration.cpp" />
168169
<ClCompile Include="stdafx.cpp">
169170
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>

Team20/Code20/IntegrationTesting/IntegrationTesting.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@
3535
<ClCompile Include="TestPqlEvaluator.cpp">
3636
<Filter>Source Files</Filter>
3737
</ClCompile>
38+
<ClCompile Include="TestPqlEndToEnd.cpp">
39+
<Filter>Source Files</Filter>
40+
</ClCompile>
3841
</ItemGroup>
3942
</Project>

Team20/Code20/IntegrationTesting/TestPQLEvaluator.cpp

+39-22
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ TEST_CLASS(TestPqlEvaluator) {
228228
TEST_METHOD(TestEvaluateParsedQuery_SingleSuchThatClause) {
229229
// stmt s; Select s such that Follows(3, s)
230230
ParsedQuery pq = {{{"s", TokenType::STMT}},
231-
{"s"},
231+
{PqlResultType::Tuple, {{"s", AttributeRefType::NONE}}},
232232
{{TokenType::FOLLOWS,
233233
{TokenType::NUMBER, "3"},
234234
{TokenType::STMT, "s"}}}};
@@ -239,7 +239,7 @@ TEST_CLASS(TestPqlEvaluator) {
239239

240240
// read r1; read r2; Select r2 such that Follows(r1, r2)
241241
pq = {{{"r1", TokenType::READ}, {"r2", TokenType::READ}},
242-
{"r2"},
242+
{PqlResultType::Tuple, {{"r2", AttributeRefType::NONE}}},
243243
{{TokenType::FOLLOWS,
244244
{TokenType::READ, "r1"},
245245
{TokenType::READ, "r2"}}}};
@@ -252,7 +252,7 @@ TEST_CLASS(TestPqlEvaluator) {
252252

253253
// call c; Select c such that Follows(_, c)
254254
pq = {{{"c", TokenType::CALL}},
255-
{"c"},
255+
{PqlResultType::Tuple, {{"c", AttributeRefType::NONE}}},
256256
{{TokenType::FOLLOWS,
257257
{TokenType::UNDERSCORE},
258258
{TokenType::CALL, "c"}}}};
@@ -268,7 +268,7 @@ TEST_CLASS(TestPqlEvaluator) {
268268
{{"w", TokenType::WHILE},
269269
{"i", TokenType::IF},
270270
{"p", TokenType::PRINT}},
271-
{"p"},
271+
{PqlResultType::Tuple, {{"p", AttributeRefType::NONE}}},
272272
{{TokenType::FOLLOWS, {TokenType::WHILE, "w"}, {TokenType::IF, "i"}}}};
273273
expected = {"6", "21", "22", "26"};
274274
actual.clear();
@@ -281,7 +281,7 @@ TEST_CLASS(TestPqlEvaluator) {
281281
pq = {{{"a", TokenType::ASSIGN},
282282
{"p", TokenType::PRINT},
283283
{"s", TokenType::STMT}},
284-
{"s"},
284+
{PqlResultType::Tuple, {{"s", AttributeRefType::NONE}}},
285285
{{TokenType::FOLLOWS,
286286
{TokenType::ASSIGN, "a"},
287287
{TokenType::PRINT, "p"}}}};
@@ -297,7 +297,7 @@ TEST_CLASS(TestPqlEvaluator) {
297297
PkbTables::AST const1 = TNode("1");
298298
spec.value = &const1;
299299
ParsedQuery pq = {{{"a", TokenType::ASSIGN}},
300-
{"a"},
300+
{PqlResultType::Tuple, {{"a", AttributeRefType::NONE}}},
301301
{},
302302
{ParsedPattern{PqlToken{TokenType::ASSIGN, "a"},
303303
PqlToken{TokenType::STRING, "q"}, spec}}};
@@ -311,8 +311,9 @@ TEST_CLASS(TestPqlEvaluator) {
311311

312312
TEST_METHOD(TestEvaluateParsedQuery_SelectStatementNoClause) {
313313
// while w; if i; Select i
314-
ParsedQuery pq = {
315-
{{"w", TokenType::WHILE}, {"i", TokenType::IF}}, {"i"}, {}};
314+
ParsedQuery pq = {{{"w", TokenType::WHILE}, {"i", TokenType::IF}},
315+
{PqlResultType::Tuple, {{"i", AttributeRefType::NONE}}},
316+
{}};
316317
std::list<std::string> expected = {"15", "19"};
317318
std::list<std::string> actual;
318319
Pql::evaluate(pq, pkb.getQueryInterface(), actual);
@@ -321,7 +322,9 @@ TEST_CLASS(TestPqlEvaluator) {
321322
Assert::IsTrue(expected == actual);
322323

323324
// call c; Select c
324-
pq = {{{"c", TokenType::CALL}}, {"c"}, {}};
325+
pq = {{{"c", TokenType::CALL}},
326+
{PqlResultType::Tuple, {{"c", AttributeRefType::NONE}}},
327+
{}};
325328
expected = {"7", "11", "25"};
326329
actual.clear();
327330
Pql::evaluate(pq, pkb.getQueryInterface(), actual);
@@ -332,7 +335,9 @@ TEST_CLASS(TestPqlEvaluator) {
332335

333336
TEST_METHOD(TestEvaluateParsedQuery_SelectProcedureNoClause) {
334337
// procedure p; Select p
335-
ParsedQuery pq = {{{"p", TokenType::PROCEDURE}}, {"p"}, {}};
338+
ParsedQuery pq = {{{"p", TokenType::PROCEDURE}},
339+
{PqlResultType::Tuple, {{"p", AttributeRefType::NONE}}},
340+
{}};
336341
std::list<std::string> expected = {"main", "extra", "complicate", "aux"};
337342
std::list<std::string> actual;
338343
Pql::evaluate(pq, pkb.getQueryInterface(), actual);
@@ -343,7 +348,9 @@ TEST_CLASS(TestPqlEvaluator) {
343348

344349
TEST_METHOD(TestEvaluateParsedQuery_SelectVariableNoClause) {
345350
// variable v; Select v
346-
ParsedQuery pq = {{{"v", TokenType::VARIABLE}}, {"v"}, {}};
351+
ParsedQuery pq = {{{"v", TokenType::VARIABLE}},
352+
{PqlResultType::Tuple, {{"v", AttributeRefType::NONE}}},
353+
{}};
347354
std::list<std::string> expected = {{"x", "y", "r", "m", "q", "t", "k"}};
348355
std::list<std::string> actual;
349356
Pql::evaluate(pq, pkb.getQueryInterface(), actual);
@@ -354,7 +361,9 @@ TEST_CLASS(TestPqlEvaluator) {
354361

355362
TEST_METHOD(TestEvaluateParsedQuery_SelectConstantNoClause) {
356363
// constant c; Select c
357-
ParsedQuery pq = {{{"c", TokenType::CONSTANT}}, {"c"}, {}};
364+
ParsedQuery pq = {{{"c", TokenType::CONSTANT}},
365+
{PqlResultType::Tuple, {{"c", AttributeRefType::NONE}}},
366+
{}};
358367
std::list<std::string> expected = {
359368
"0", "1", "5", "11111111111111111111111111111111111111"};
360369
std::list<std::string> actual;
@@ -366,9 +375,11 @@ TEST_CLASS(TestPqlEvaluator) {
366375

367376
TEST_METHOD(TestEvaluateParsedQuery_SelectMultipleNoClause) {
368377
// constant c; procedure p; Select <p, c>
369-
ParsedQuery pq = {{{"c", TokenType::CONSTANT}, {"p", TokenType::PROCEDURE}},
370-
{"p", "c"},
371-
{}};
378+
ParsedQuery pq = {
379+
{{"c", TokenType::CONSTANT}, {"p", TokenType::PROCEDURE}},
380+
{PqlResultType::Tuple,
381+
{{"p", AttributeRefType::NONE}, {"c", AttributeRefType::NONE}}},
382+
{}};
372383
std::list<std::string> expected = {
373384
"main 0", "main 1",
374385
"main 5", "main 11111111111111111111111111111111111111",
@@ -391,11 +402,13 @@ TEST_CLASS(TestPqlEvaluator) {
391402
/* All seen synonyms
392403
read r1; read r2; Select <r2, r1> such that Follows(r1, r2)
393404
*/
394-
ParsedQuery pq = {{{"r1", TokenType::READ}, {"r2", TokenType::READ}},
395-
{"r2", "r1"},
396-
{{TokenType::FOLLOWS,
397-
{TokenType::READ, "r1"},
398-
{TokenType::READ, "r2"}}}};
405+
ParsedQuery pq = {
406+
{{"r1", TokenType::READ}, {"r2", TokenType::READ}},
407+
{PqlResultType::Tuple,
408+
{{"r2", AttributeRefType::NONE}, {"r1", AttributeRefType::NONE}}},
409+
{{TokenType::FOLLOWS,
410+
{TokenType::READ, "r1"},
411+
{TokenType::READ, "r2"}}}};
399412
std::list<VALUE> expected = {"2 1", "9 8", "13 12"};
400413
std::list<VALUE> actual;
401414
Pql::evaluate(pq, pkb.getQueryInterface(), actual);
@@ -410,7 +423,10 @@ TEST_CLASS(TestPqlEvaluator) {
410423
{{"w", TokenType::WHILE},
411424
{"i", TokenType::IF},
412425
{"p", TokenType::PRINT}},
413-
{"w", "p", "i"},
426+
{PqlResultType::Tuple,
427+
{{"w", AttributeRefType::NONE},
428+
{"p", AttributeRefType::NONE},
429+
{"i", AttributeRefType::NONE}}},
414430
{{TokenType::FOLLOWS, {TokenType::WHILE, "w"}, {TokenType::IF, "i"}}}};
415431
expected = {"17 6 19", "17 21 19", "17 22 19", "17 26 19"};
416432
actual.clear();
@@ -425,7 +441,8 @@ TEST_CLASS(TestPqlEvaluator) {
425441
pq = {{{"a", TokenType::ASSIGN},
426442
{"r", TokenType::READ},
427443
{"c", TokenType::CALL}},
428-
{"c", "a"},
444+
{PqlResultType::Tuple,
445+
{{"c", AttributeRefType::NONE}, {"a", AttributeRefType::NONE}}},
429446
{{TokenType::FOLLOWS,
430447
{TokenType::READ, "r"},
431448
{TokenType::ASSIGN, "a"}}}};

Team20/Code20/IntegrationTesting/TestPQLParserLexerIntegration.cpp

+43-4
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,97 @@ TEST_CLASS(TestPqlParserLexerIntegration){
1111
const std::string input = "procedure p;\nSelect p";
1212
const auto actualResult = Pql::parse(Pql::lex(input));
1313
const DECLARATIONS expectedDeclarations{{"p", TokenType::PROCEDURE}};
14-
const RESULTS expectedResults{{"p"}};
14+
const RESULTS expectedResults{PqlResultType::Tuple,
15+
{{"p", AttributeRefType::NONE}}};
1516
const RELATIONSHIPS expectedRelationships{};
1617
const PATTERNS expectedPatterns{};
18+
const WITHS expectedWiths{};
1719
Assert::IsTrue(actualResult.declarations == expectedDeclarations);
1820
Assert::IsTrue(actualResult.results == expectedResults);
1921
Assert::IsTrue(actualResult.relationships == expectedRelationships);
2022
Assert::IsTrue(actualResult.patterns == expectedPatterns);
23+
Assert::IsTrue(actualResult.withs == expectedWiths);
2124
} // namespace IntegrationTesting
2225

2326
TEST_METHOD(TestLexAndParse_SuchThatFollowsStarNoPattern) {
2427
const std::string input = "stmt s;\n\nSelect s such that Follows* (6, s)";
2528
const auto actualResult = Pql::parse(Pql::lex(input));
2629
const DECLARATIONS expectedDeclarations{{"s", TokenType::STMT}};
27-
const RESULTS expectedResults{{"s"}};
30+
const RESULTS expectedResults{PqlResultType::Tuple,
31+
{{"s", AttributeRefType::NONE}}};
2832
const RELATIONSHIPS expectedRelationships{
2933
{TokenType::FOLLOWS_T, {TokenType::NUMBER, "6"}, {TokenType::STMT, "s"}}};
3034
const PATTERNS expectedPatterns{};
35+
const WITHS expectedWiths{};
3136
Assert::IsTrue(actualResult.declarations == expectedDeclarations);
3237
Assert::IsTrue(actualResult.results == expectedResults);
3338
Assert::IsTrue(actualResult.relationships == expectedRelationships);
3439
Assert::IsTrue(actualResult.patterns == expectedPatterns);
40+
Assert::IsTrue(actualResult.withs == expectedWiths);
3541
} // namespace UnitTesting
3642
TEST_METHOD(TestLexAndParse_SuchThatUsesPattern) {
3743
const std::string input = "assign a; variable v;\n\nSelect a such that Uses "
3844
"(a, v) pattern a (v, _)";
3945
const auto actualResult = Pql::parse(Pql::lex(input));
4046
const DECLARATIONS expectedDeclarations{{"a", TokenType::ASSIGN},
4147
{"v", TokenType::VARIABLE}};
42-
const RESULTS expectedResults{{"a"}};
48+
const RESULTS expectedResults{PqlResultType::Tuple,
49+
{{"a", AttributeRefType::NONE}}};
4350
const RELATIONSHIPS expectedRelationships{
4451
{TokenType::USES, {TokenType::ASSIGN, "a"}, {TokenType::VARIABLE, "v"}}};
4552
const PATTERNS expectedPatterns{{{TokenType::ASSIGN, "a"},
4653
{TokenType::VARIABLE, "v"},
4754
{PatternMatchType::Any}}};
55+
const WITHS expectedWiths{};
4856
Assert::IsTrue(actualResult.declarations == expectedDeclarations);
4957
Assert::IsTrue(actualResult.results == expectedResults);
5058
Assert::IsTrue(actualResult.relationships == expectedRelationships);
5159
Assert::IsTrue(actualResult.patterns == expectedPatterns);
60+
Assert::IsTrue(actualResult.withs == expectedWiths);
5261
} // namespace UnitTesting
5362
TEST_METHOD(TestLexAndParse_SynonymDeclaredWithKeyword) {
5463
const std::string input = "if if; Select if such that Follows* (if, 16)";
5564
const auto actualResult = Pql::parse(Pql::lex(input));
5665
const DECLARATIONS expectedDeclarations{{"if", TokenType::IF}};
57-
const RESULTS expectedResults{{"if"}};
66+
const RESULTS expectedResults{PqlResultType::Tuple,
67+
{{"if", AttributeRefType::NONE}}};
5868
const RELATIONSHIPS expectedRelationships{
5969
{TokenType::FOLLOWS_T, {TokenType::IF, "if"}, {TokenType::NUMBER, "16"}}};
6070
const PATTERNS expectedPatterns{};
71+
const WITHS expectedWiths{};
6172
Assert::IsTrue(actualResult.declarations == expectedDeclarations);
6273
Assert::IsTrue(actualResult.results == expectedResults);
6374
Assert::IsTrue(actualResult.relationships == expectedRelationships);
6475
Assert::IsTrue(actualResult.patterns == expectedPatterns);
76+
Assert::IsTrue(actualResult.withs == expectedWiths);
6577
} // namespace IntegrationTesting
78+
79+
TEST_METHOD(TestLexAndParse_TupleResultWithWithClause) {
80+
const std::string input =
81+
"assign a; variable v;\n\nSelect <a, v.stmt#> such that Uses "
82+
"(a, v) pattern a (v, _) with a.procName = v.procName";
83+
const auto actualResult = Pql::parse(Pql::lex(input));
84+
const DECLARATIONS expectedDeclarations{{"a", TokenType::ASSIGN},
85+
{"v", TokenType::VARIABLE}};
86+
const RESULTS expectedResults{PqlResultType::Tuple,
87+
{
88+
{"a", AttributeRefType::NONE},
89+
{"v", AttributeRefType::STATEMENT_NUM},
90+
}};
91+
const RELATIONSHIPS expectedRelationships{
92+
{TokenType::USES, {TokenType::ASSIGN, "a"}, {TokenType::VARIABLE, "v"}}};
93+
const PATTERNS expectedPatterns{{{TokenType::ASSIGN, "a"},
94+
{TokenType::VARIABLE, "v"},
95+
{PatternMatchType::Any}}};
96+
const WITHS expectedWiths = {
97+
{Reference{Element{"a", AttributeRefType::PROCNAME}},
98+
Reference{Element{"v", AttributeRefType::PROCNAME}}}};
99+
Assert::IsTrue(actualResult.declarations == expectedDeclarations);
100+
Assert::IsTrue(actualResult.results == expectedResults);
101+
Assert::IsTrue(actualResult.relationships == expectedRelationships);
102+
Assert::IsTrue(actualResult.patterns == expectedPatterns);
103+
Assert::IsTrue(actualResult.withs == expectedWiths);
104+
} // namespace UnitTesting
66105
}
67106
;
68107
} // namespace IntegrationTesting
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "CppUnitTest.h"
2+
#include "stdafx.h"
3+
4+
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
5+
6+
namespace IntegrationTesting {
7+
TEST_CLASS(TestPqlEndToEnd) {
8+
public:
9+
Pkb pkb;
10+
SetUpTests setUpTests = SetUpTests::SetUpTests(pkb);
11+
12+
TEST_METHOD(TestPqlEndToEnd_TupleSelectWithFollows) {
13+
const auto query =
14+
"assign a; read r; call c; Select <c, a> such that Follows(r, a)";
15+
std::list<std::string> actual;
16+
Pql::evaluate(Pql::parse(Pql::lex(query)), pkb.getQueryInterface(), actual);
17+
std::list<std::string> expected = {"7 10", "11 10", "25 10"};
18+
Assert::IsTrue(actual == expected);
19+
}
20+
};
21+
} // namespace IntegrationTesting

Team20/Code20/IntegrationTesting/stdafx.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "CppUnitTest.h"
1212

1313
// TODO: reference additional headers your program requires here
14+
#include "PQL.h"
1415
#include "Pkb.h"
1516
#include "PqlEvaluator.h"
1617
#include "SetUpTests.h"

Team20/Code20/UnitTesting/TestPQLEvaluator.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ TEST_METHOD(TestEvaluationTable_Flatten) {
445445
EvaluationTable table(new TABLE({{"s", {"1", "2", "3", "4"}}}));
446446
std::list<VALUE> expected = {"1", "2", "3", "4"};
447447
std::list<VALUE> actual;
448-
table.flatten({"s"}, actual);
448+
table.flatten({{"s", AttributeRefType::NONE}}, actual);
449449
expected.sort();
450450
actual.sort();
451451
Assert::IsTrue(expected == actual);
@@ -470,7 +470,13 @@ TEST_METHOD(TestEvaluationTable_Flatten) {
470470
expected = {"gonna 5 1 2 w 2", "give 5 1 2 x 2", "up 5 1 2 x 2",
471471
"gonna 5 1 2 w 4", "give 5 1 2 x 4", "up 5 1 2 x 4"};
472472
actual.clear();
473-
table.flatten({"p", "i", "s", "a", "v", "w"}, actual);
473+
table.flatten({{"p", AttributeRefType::NONE},
474+
{"i", AttributeRefType::NONE},
475+
{"s", AttributeRefType::NONE},
476+
{"a", AttributeRefType::NONE},
477+
{"v", AttributeRefType::NONE},
478+
{"w", AttributeRefType::NONE}},
479+
actual);
474480
expected.sort();
475481
actual.sort();
476482
Assert::IsTrue(expected == actual);
@@ -494,7 +500,10 @@ TEST_METHOD(TestEvaluationTable_Flatten) {
494500
{"p", {"gonna", "give", "up", "gonna", "give", "up"}}}));
495501
expected = {"gonna 1 w", "give 1 x", "up 1 x"};
496502
actual.clear();
497-
table.flatten({"p", "s", "v"}, actual);
503+
table.flatten({{"p", AttributeRefType::NONE},
504+
{"s", AttributeRefType::NONE},
505+
{"v", AttributeRefType::NONE}},
506+
actual);
498507
expected.sort();
499508
actual.sort();
500509
Assert::IsTrue(expected == actual);

0 commit comments

Comments
 (0)