Skip to content

Commit 222eba8

Browse files
author
Ruicong Zhu
committed
Merge branch 'add-new-system-tests-for-next-and-call' of https://github.com/nus-cs3203/team20-win-spa-20s1 into add-new-system-tests-for-next-and-call
2 parents e35f973 + 3248c1a commit 222eba8

File tree

7 files changed

+102
-79
lines changed

7 files changed

+102
-79
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4-
<LocalDebuggerCommandArguments> ..\Tests\Sample_source.txt ..\Tests\Sample_queries.txt ..\..\Tests20\out.xml</LocalDebuggerCommandArguments>
4+
<LocalDebuggerCommandArguments> ..\..\Tests20\iteration2\system_test_1_source.txt ..\..\Tests20\system_test_1_queries.txt ..\..\Tests20\out.xml</LocalDebuggerCommandArguments>
55
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
66
</PropertyGroup>
77
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8-
<LocalDebuggerCommandArguments> ..\Tests\Sample_source.txt ..\Tests\Sample_queries.txt ..\..\Tests20\out.xml</LocalDebuggerCommandArguments>
8+
<LocalDebuggerCommandArguments> ..\..\Tests20\iteration2\system_test_1_source.txt ..\..\Tests20\system_test_1_queries.txt ..\..\Tests20\out.xml</LocalDebuggerCommandArguments>
99
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
1010
</PropertyGroup>
11-
</Project>
11+
</Project>

Team20/Code20/UnitTesting/TestPQLEvaluator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ TEST_METHOD(TestEvaluationTable_Slice) {
393393
| 1 |
394394
*/
395395
EvaluationTable expected = EvaluationTable(new TABLE({{"s", {"1"}}}));
396-
EvaluationTable actual = table.slice({"s"});
396+
EvaluationTable actual = table.sliceSymbols({"s"});
397397
Assert::IsTrue(expected == actual);
398398

399399
/* Two columns, one unique entry
@@ -402,7 +402,7 @@ TEST_METHOD(TestEvaluationTable_Slice) {
402402
| 1 | 2 |
403403
*/
404404
expected = EvaluationTable(new TABLE({{"s", {"1"}}, {"a", {"2"}}}));
405-
actual = table.slice({"s", "a"});
405+
actual = table.sliceSymbols({"s", "a"});
406406
Assert::IsTrue(expected == actual);
407407

408408
/* Three columns, multiple unique entries
@@ -413,7 +413,7 @@ TEST_METHOD(TestEvaluationTable_Slice) {
413413
*/
414414
expected = EvaluationTable(
415415
new TABLE({{"s", {"1", "1"}}, {"a", {"2", "2"}}, {"v", {"w", "x"}}}));
416-
actual = table.slice({"s", "a", "v"});
416+
actual = table.sliceSymbols({"s", "a", "v"});
417417
Assert::IsTrue(expected == actual);
418418

419419
/* Three columns, multiple unique entries
@@ -426,7 +426,7 @@ TEST_METHOD(TestEvaluationTable_Slice) {
426426
expected = EvaluationTable(new TABLE({{"s", {"1", "1", "1"}},
427427
{"v", {"w", "x", "x"}},
428428
{"p", {"gonna", "give", "up"}}}));
429-
actual = table.slice({"s", "v", "p"});
429+
actual = table.sliceSymbols({"s", "v", "p"});
430430
Assert::IsTrue(expected == actual);
431431
}
432432

Team20/Code20/source/DispatcherGraph.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ void DispatcherGraph::addDispatcher(ClauseDispatcher *dispatcher) {
1313
if (contains(dispatcher)) {
1414
throw "Error: dispatcher already exists in graph";
1515
}
16-
if (!symbols.empty()) {
16+
if (!symbolsToClauseDispatchersMap.empty()) {
1717
ensureHasOneCommonSymbolWith(dispatcher);
1818
}
1919
adjacencyList[dispatcher] = EDGES();
2020
for (const auto &symbol : dispatcher->getSymbols()) {
2121
if (!contains(symbol)) {
22-
symbols[symbol] = CLAUSE_DISPATCHER_SET();
22+
symbolsToClauseDispatchersMap[symbol] = CLAUSE_DISPATCHER_SET();
2323
}
24-
for (const auto &associatedDispatcher : symbols[symbol]) {
24+
for (const auto &associatedDispatcher :
25+
symbolsToClauseDispatchersMap[symbol]) {
2526
int weight = countCommonSymbols(dispatcher, associatedDispatcher) * 2;
2627
adjacencyList.at(dispatcher)
2728
.insert(Edge{
@@ -35,7 +36,8 @@ void DispatcherGraph::addDispatcher(ClauseDispatcher *dispatcher) {
3536
weight,
3637
});
3738
}
38-
symbols.at(symbol).insert(dispatcher);
39+
symbolsToClauseDispatchersMap.at(symbol).insert(dispatcher);
40+
symbols.insert(symbol);
3941
}
4042
totalPriority += dispatcher->dispatchPriority();
4143
}
@@ -53,7 +55,7 @@ int DispatcherGraph::countCommonSymbols(ClauseDispatcher *first,
5355

5456
void DispatcherGraph::ensureSymbolsNotContainedInOtherGraph(
5557
const DispatcherGraph *otherGraph) const {
56-
for (const auto &symbolPair : symbols) {
58+
for (const auto &symbolPair : symbolsToClauseDispatchersMap) {
5759
if (otherGraph->contains(symbolPair.first)) {
5860
throw "ERROR: There exists common symbol between the two graphs";
5961
}
@@ -63,7 +65,7 @@ void DispatcherGraph::ensureSymbolsNotContainedInOtherGraph(
6365
void DispatcherGraph::ensureHasOneCommonSymbolWith(
6466
ClauseDispatcher *dispatcher) const {
6567
for (const auto &symbol : dispatcher->getSymbols()) {
66-
if (mapContains(symbols, symbol)) {
68+
if (mapContains(symbolsToClauseDispatchersMap, symbol)) {
6769
return;
6870
}
6971
}
@@ -80,15 +82,18 @@ void DispatcherGraph::merge(DispatcherGraph &otherGraph,
8082
for (const auto &[clause, edges] : otherGraph.adjacencyList) {
8183
adjacencyList[clause].insert(edges.begin(), edges.end());
8284
}
83-
for (const auto &[symbol, clauses] : otherGraph.symbols) {
84-
symbols[symbol].insert(clauses.begin(), clauses.end());
85+
for (const auto &[symbol, clauses] :
86+
otherGraph.symbolsToClauseDispatchersMap) {
87+
symbolsToClauseDispatchersMap[symbol].insert(clauses.begin(),
88+
clauses.end());
8589
}
8690
totalPriority += otherGraph.totalPriority;
91+
symbols.insert(otherGraph.symbols.begin(), otherGraph.symbols.end());
8792
addDispatcher(dispatcher);
8893
}
8994

9095
bool DispatcherGraph::contains(SYMBOL symbol) const {
91-
return mapContains(symbols, symbol);
96+
return mapContains(symbolsToClauseDispatchersMap, symbol);
9297
}
9398

9499
bool DispatcherGraph::contains(ClauseDispatcher *clauseDispatcher) const {
@@ -97,7 +102,8 @@ bool DispatcherGraph::contains(ClauseDispatcher *clauseDispatcher) const {
97102

98103
int DispatcherGraph::priority() { return totalPriority; }
99104

100-
EvaluationTable DispatcherGraph::evaluate() {
105+
EvaluationTable
106+
DispatcherGraph::evaluate(std::unordered_set<SYMBOL> selectedSymbols) {
101107
typedef std::pair<int, ClauseDispatcher *> PQ_NODE;
102108
auto compare = [](PQ_NODE &node1, PQ_NODE &node2) {
103109
return node1.first < node2.first;
@@ -135,6 +141,22 @@ EvaluationTable DispatcherGraph::evaluate() {
135141
if (table.rowCount() == 0) {
136142
return table;
137143
}
144+
auto symbolDeleted = false;
145+
for (const auto &symbol : dispatcher->getSymbols()) {
146+
if (mapContains(symbolsToClauseDispatchersMap, symbol) &&
147+
setContains(symbolsToClauseDispatchersMap.at(symbol), dispatcher)) {
148+
symbolsToClauseDispatchersMap.at(symbol).erase(dispatcher);
149+
if (symbolsToClauseDispatchersMap.at(symbol).empty() &&
150+
!setContains(selectedSymbols, symbol)) {
151+
symbols.erase(symbol);
152+
symbolDeleted = true;
153+
symbolsToClauseDispatchersMap.erase(symbol);
154+
}
155+
}
156+
}
157+
if (symbolDeleted)
158+
table = table.sliceSymbols(symbols);
159+
138160
for (const auto &e : adjacencyList.at(dispatcher)) {
139161
int pqWeight = e.weight + e.neighbour->dispatchPriority();
140162
pq.push(PQ_NODE{pqWeight, e.neighbour});
@@ -145,5 +167,6 @@ EvaluationTable DispatcherGraph::evaluate() {
145167
};
146168

147169
bool DispatcherGraph::operator==(const DispatcherGraph &other) const {
148-
return adjacencyList == other.adjacencyList && symbols == other.symbols;
170+
return adjacencyList == other.adjacencyList &&
171+
symbolsToClauseDispatchersMap == other.symbolsToClauseDispatchersMap;
149172
}

Team20/Code20/source/DispatcherGraph.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ struct DispatcherGraph {
5252
/** @brief Evaluates the `ClauseDispatcher`s in an efficient manner.
5353
* Clauses are evaluated based on their estimated ease of evaluation and their
5454
* common synonyms with clauses that have already already evaluated.
55+
* @param selectedSymbols symbols that are selected in the query
5556
* @return evaluation table that is the result from evaluating the clauses.
5657
*/
57-
EvaluationTable evaluate();
58+
EvaluationTable evaluate(std::unordered_set<SYMBOL> selectedSymbols);
5859

5960
bool DispatcherGraph::operator==(const DispatcherGraph &other) const;
6061

@@ -63,8 +64,10 @@ struct DispatcherGraph {
6364
typedef std::unordered_map<SYMBOL, CLAUSE_DISPATCHER_SET> SYMBOLS_MAP;
6465
typedef std::unordered_set<Edge> EDGES;
6566
typedef std::unordered_map<ClauseDispatcher *, EDGES> ADJACACENCY_LIST;
67+
typedef std::unordered_set<SYMBOL> SYMBOL_SET;
6668
ADJACACENCY_LIST adjacencyList;
67-
SYMBOLS_MAP symbols;
69+
SYMBOLS_MAP symbolsToClauseDispatchersMap;
70+
SYMBOL_SET symbols;
6871
int totalPriority;
6972

7073
int countCommonSymbols(ClauseDispatcher *first, ClauseDispatcher *second);

0 commit comments

Comments
 (0)