Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
project-3-draft [2021/04/22 11:06] roxana_elena.stiuca [Graph queries] |
project-3-draft [2021/04/25 16:07] (current) roxana_elena.stiuca prerequisite for taskset3 |
||
---|---|---|---|
Line 26: | Line 26: | ||
| Cartesian (Row -> Row -> Row) [String] Query Query | | Cartesian (Row -> Row -> Row) [String] Query Query | ||
| Projection [String] Query | | Projection [String] Query | ||
- | -- | forall a. Filter (FilterCondition a) Query | + | | forall a. FEval a => Filter (FilterCondition a) Query |
- | | Graph EdgeOp query | + | | Graph EdgeOp Query |
| | ||
-- where EdgeOp is defined: | -- where EdgeOp is defined: | ||
Line 34: | Line 34: | ||
**Don't worry about Graph or Filter queries yet.** | **Don't worry about Graph or Filter queries yet.** | ||
- | + | ==== Prerequisite ==== | |
+ | Add the following lines at the beginning of your .hs files: | ||
+ | <code haskell> | ||
+ | {-# LANGUAGE ExistentialQuantification #-} | ||
+ | {-# LANGUAGE FlexibleInstances #-} | ||
+ | </code> | ||
+ | |||
+ | The first line allows ''forall a''. | ||
+ | The second allows ''instance FEval String''. | ||
===== Query Evaluation ===== | ===== Query Evaluation ===== | ||
Line 128: | Line 137: | ||
A **graph** is a special kind of table which has precisely the following column names: ''["From", "To", "Value"]''. Each row defines a **weighted edge** between node ''From'' and node ''To''. | A **graph** is a special kind of table which has precisely the following column names: ''["From", "To", "Value"]''. Each row defines a **weighted edge** between node ''From'' and node ''To''. | ||
- | The query ''Graph edgeop query'': creates a graph starting from the result of the evaluation of ''query''. Say **T** is that table. | + | The query ''Graph edgeop query'': creates such a table starting from the result of the evaluation of ''query''. Suppose the query evaluates to a table **T**. |
- | * The nodes are the rows in table **T**. | + | * The nodes are the **rows** in table **T**. |
- | * The weight of an edge between 2 nodes is given by ''edgeop'', which returns a Maybe Value. If ''edgeop row1 row2'' returns ''Nothing'', then we don't have an edge between those 2 nodes. If it returns ''Just val'' then we have an edge between row1 and row2 of weight val. | + | * The weight of an edge between 2 nodes is given by ''edgeop'', which returns a ''Maybe Value''. If ''edgeop row1 row2'' returns ''Nothing'', then we don't have an edge between those 2 nodes. If it returns ''Just val'' then we have an edge between ''row1'' and ''row2'' of weight ''val''. |
- | * In the resulting table, a row describes an edge between node_i and node_j and will have the values: | + | * In the resulting table, each row describes an edge between node_i and node_j and will have the values: |
* "From" = first column from node_i | * "From" = first column from node_i | ||
* "To" = first column from node_j | * "To" = first column from node_j | ||
* "Value" = edgeop node_i node_j | * "Value" = edgeop node_i node_j | ||
- | The edge node_i-node_j is the same as node_j-node_i, so it should only appear once. "From" value should be lexicographically before "To". | + | The edge //node_i-node_j// is the same as //node_j-node_i//, so it should only appear once (graphs are unoriented). "From" value should be lexicographically before "To". |
+ | |||
+ | **Example:** Suppose **T** is the table shown below: | ||
+ | <code> | ||
+ | Name Grade Class | ||
+ | Mihai 9 321 | ||
+ | Andrei 8 322 | ||
+ | Stefan 10 321 | ||
+ | Ana 9 322 | ||
+ | </code> | ||
+ | |||
+ | If we would like to build a graph that connects all students in the same class, then: | ||
+ | <code haskell> | ||
+ | edgeop [_,_,z] [_,_,c] | ||
+ | | z == c = Just c | ||
+ | | otherwise = Nothing | ||
+ | </code> | ||
+ | |||
+ | and the resulting graph will be: | ||
+ | <code> | ||
+ | From To Value | ||
+ | Mihai Stefan 321 | ||
+ | Ana Andrei 322 | ||
+ | </code> | ||
+ | |||
+ | If we would like to build a graph that connects students with grades equal or with a difference of **at least** a point, then: | ||
+ | <code haskell> | ||
+ | edgeop [_,x,_] [_,y,_] | ||
+ | | abs $ (read x :: Int) - (read y :: Int) <= 1 = Just "similar" | ||
+ | | otherwise = Nothing | ||
+ | </code> | ||
+ | |||
+ | and the resulting graph is: | ||
+ | <code> | ||
+ | From To Value | ||
+ | Andrei Mihai similar | ||
+ | Mihai Stefan similar | ||
+ | Ana Mihai similar | ||
+ | Ana Andrei similar | ||
+ | Ana Stefan similar | ||
+ | </code> | ||
==== Similarities graph, using queries ==== | ==== Similarities graph, using queries ==== | ||
+ | |||
We want to check the similarities between students lecture points. | We want to check the similarities between students lecture points. | ||
* For that, we want to obtain a graph where "From" and "To" are students' emails and "Value" is the distance between the 2 students' points. | * For that, we want to obtain a graph where "From" and "To" are students' emails and "Value" is the distance between the 2 students' points. | ||
Line 145: | Line 196: | ||
* The edges in the resulting graph (the rows in the resulting table) should be sorted by the "Value" column. If email is missing, don't include that entry. | * The edges in the resulting graph (the rows in the resulting table) should be sorted by the "Value" column. If email is missing, don't include that entry. | ||
- | Your task is to write ''similarities_query'' as a sequence of queries, that once evaluated results in the graph described above. | + | Your task is to write ''similarities_query'' as a **sequence of queries**, that once evaluated results in the graph described above. |
+ | |||
+ | **Note**: ''similarities_query'' is a Query. The checker applies ''eval'' on it. | ||
===== TL;DR Tasks ===== | ===== TL;DR Tasks ===== | ||
- | - Enroll Query in class Eval (without Filter or Graph). 0.3p | + | - Enroll ''Query'' in class ''Eval'' (without ''Filter'' or ''Graph''). **0.3p** |
- | - Enroll FilterCondition in class FEval and implement eval for Filter query. 0.2p | + | - Enroll ''FilterCondition'' in class ''FEval'' and implement ''eval'' for ''Filter'' query. **0.2p** |
- | - Implement eval for Graph query. 0.2p | + | - Implement ''eval'' for ''Graph'' query. 0.2p |
- | - Get graph for similarities. 0.3p | + | - Extract similarity graph. 0.3p |
===== Checker ===== | ===== Checker ===== |