-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add concrete tests for cfpq algorithms * Add concrete tests for rpq algorithms Also rename TestCaseCFPQ -> CaseCFPQ and fix docs * Fix linter errors
- Loading branch information
Showing
9 changed files
with
256 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from typing import Callable | ||
from pyformlang.cfg import CFG | ||
from copy import copy | ||
from pyformlang.rsa import RecursiveAutomaton | ||
from networkx import MultiDiGraph | ||
import graphs | ||
|
||
|
||
class CaseCFPQ: | ||
""" | ||
class that contains all information about test case for cfpq algorithms | ||
""" | ||
|
||
def __init__( | ||
self, | ||
graph: MultiDiGraph, | ||
query: CFG, | ||
actual_answer: set[tuple[int, int]], | ||
start_nodes: set[int] = None, | ||
final_nodes: set[int] = None, | ||
): | ||
self.graph = copy(graph) | ||
self.query = copy(query) | ||
self.expected_answer = copy(actual_answer) | ||
self.start_nodes = copy(start_nodes) if start_nodes else graph.nodes | ||
self.final_nodes = copy(final_nodes) if final_nodes else graph.nodes | ||
|
||
def check_answer_cfg( | ||
self, | ||
function: Callable[ | ||
[CFG, MultiDiGraph, set[int], set[int]], | ||
set[tuple[int, int]], | ||
], | ||
): | ||
""" | ||
assertion function for algorithms with cfg | ||
:param function: the function under test (*hellings_based_cfpq* or *matrix_based_cfpq*) | ||
:return: assertion | ||
""" | ||
actual_res = function( | ||
self.query, self.graph, self.start_nodes, self.final_nodes | ||
) | ||
assert actual_res == self.expected_answer | ||
|
||
def check_answer_rsm( | ||
self, | ||
function: Callable[ | ||
[RecursiveAutomaton, MultiDiGraph, set[int], set[int]], set[tuple[int, int]] | ||
], | ||
cfg_to_rsm: Callable[[CFG], RecursiveAutomaton], | ||
): | ||
""" | ||
assertion function for algorithms with rsm | ||
:param function: the function under test (*tensor_based_cfpq* or *gll_based_cfpq*) | ||
:param cfg_to_rsm: function that convert CFG to RecursiveAutomaton | ||
:return: assertion | ||
""" | ||
actual_res = function( | ||
cfg_to_rsm(self.query), self.graph, self.start_nodes, self.final_nodes | ||
) | ||
assert actual_res == self.expected_answer | ||
|
||
def __str__(self): | ||
return ( | ||
f"expected result: {self.expected_answer}\n" | ||
+ f"query: {self.query.to_text()}" | ||
+ f"graph: {self.graph.edges(data=True)}" | ||
) | ||
|
||
|
||
CASES_CFPQ = [ | ||
CaseCFPQ(graphs.point_graph, CFG.from_text("S -> a"), set()), | ||
CaseCFPQ(graphs.point_graph, CFG.from_text("S -> S a | $"), {(1, 1)}), | ||
CaseCFPQ( | ||
graphs.set_of_vertices_without_edges, | ||
CFG.from_text("S -> S a | $"), | ||
{(0, 0), (2, 2), (1, 1)}, | ||
), | ||
CaseCFPQ(graphs.b_graph, CFG.from_text("S -> a"), set()), | ||
CaseCFPQ(graphs.b_graph, CFG.from_text("S -> b"), {(0, 1)}), | ||
CaseCFPQ(graphs.b_graph, CFG.from_text("S -> S b | $"), {(0, 1), (0, 0), (1, 1)}), | ||
CaseCFPQ( | ||
graphs.bbb_graph, | ||
CFG.from_text("S -> S b b | $"), | ||
{(0, 1), (0, 0), (0, 2), (1, 1), (1, 2), (1, 0), (2, 2), (2, 1), (2, 0)}, | ||
), | ||
CaseCFPQ( | ||
graphs.bab_graph, | ||
CFG.from_text("S -> S a | S b | $"), | ||
{(0, 1), (0, 0), (0, 2), (1, 1), (1, 2), (1, 0), (2, 2), (2, 1), (2, 0)}, | ||
), | ||
CaseCFPQ( | ||
graphs.baa_graph, CFG.from_text("S -> a S b | $"), {(0, 0), (1, 1), (1, 0)} | ||
), | ||
CaseCFPQ(graphs.baa_graph, CFG.from_text("S -> a S b | a b"), {(0, 0), (1, 0)}), | ||
CaseCFPQ( | ||
graphs.set_of_vertices_without_edges, | ||
CFG.from_text("S -> $"), | ||
{(0, 0), (2, 2), (1, 1)}, | ||
), | ||
CaseCFPQ( | ||
graphs.aaa_graph, | ||
CFG.from_text("S -> a | S S"), | ||
{(0, 1), (0, 0), (0, 2), (1, 1), (1, 2), (1, 0), (2, 2), (2, 1), (2, 0)}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from networkx import MultiDiGraph | ||
from constants import LABEL | ||
|
||
point_graph = MultiDiGraph() | ||
point_graph.add_node(1) | ||
|
||
set_of_vertices_without_edges = MultiDiGraph() | ||
set_of_vertices_without_edges.add_nodes_from([0, 1, 2]) | ||
|
||
b_graph = MultiDiGraph() | ||
b_graph.add_edges_from([(0, 1, {LABEL: "b"})]) | ||
|
||
bbb_graph = MultiDiGraph() | ||
bbb_graph.add_edges_from( | ||
[(0, 1, {LABEL: "b"}), (1, 2, {LABEL: "b"}), (2, 0, {LABEL: "b"})] | ||
) | ||
|
||
bab_graph = MultiDiGraph() | ||
bab_graph.add_edges_from( | ||
[(0, 1, {LABEL: "b"}), (1, 2, {LABEL: "a"}), (2, 0, {LABEL: "b"})] | ||
) | ||
|
||
baa_graph = MultiDiGraph() | ||
baa_graph.add_edges_from( | ||
[(0, 0, {LABEL: "b"}), (0, 1, {LABEL: "a"}), (1, 0, {LABEL: "a"})] | ||
) | ||
|
||
aaa_graph = MultiDiGraph() | ||
aaa_graph.add_edges_from( | ||
[(0, 1, {LABEL: "a"}), (1, 2, {LABEL: "a"}), (2, 0, {LABEL: "a"})] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from typing import Callable | ||
|
||
import graphs | ||
from copy import copy | ||
from helper import rpq_dict_to_set | ||
from networkx import MultiDiGraph | ||
|
||
|
||
class CaseRPQ: | ||
""" | ||
class that contains all information about test case for rpq algorithms | ||
""" | ||
|
||
def __init__( | ||
self, | ||
graph: MultiDiGraph, | ||
regex: str, | ||
actual_answer: set[tuple[int, int]], | ||
start_nodes: set[int] = None, | ||
final_nodes: set[int] = None, | ||
): | ||
self.graph = copy(graph) | ||
self.regex = copy(regex) | ||
self.expected_answer = copy(actual_answer) | ||
self.start_nodes = copy(start_nodes) if start_nodes else graph.nodes | ||
self.final_nodes = copy(final_nodes) if final_nodes else graph.nodes | ||
|
||
def check_answer_regex( | ||
self, | ||
function: Callable[ | ||
[MultiDiGraph, set[int], set[int], str], list[tuple[int, int]] | ||
], | ||
): | ||
""" | ||
assertion function for algorithms with regex | ||
:param function: the function under test (*tensor_based_rpq*) | ||
:return: assertion | ||
""" | ||
assert ( | ||
set(function(self.graph, self.start_nodes, self.final_nodes, self.regex)) | ||
== self.expected_answer | ||
) | ||
|
||
def check_answer_automata( | ||
self, | ||
function, | ||
fa, | ||
constraints_fa, | ||
): | ||
""" | ||
assertion function for algorithms with automata | ||
:param function: the function under test (*ms_bfs_based_rpq*) | ||
:param fa: automata by graph | ||
:param constraints_fa: automata by regex | ||
:return: assertion | ||
""" | ||
assert rpq_dict_to_set(function(fa, constraints_fa)) == self.expected_answer | ||
|
||
def __str__(self): | ||
return ( | ||
f"expected result: {self.expected_answer}\n" | ||
+ f"regex: {self.regex}" | ||
+ f"graph: {self.graph.edges(data=True)}" | ||
) | ||
|
||
|
||
CASES_RPQ = [ | ||
CaseRPQ(graphs.point_graph, "a", set()), | ||
CaseRPQ(graphs.point_graph, "a*", {(1, 1)}), | ||
CaseRPQ(graphs.set_of_vertices_without_edges, "a*", {(0, 0), (2, 2), (1, 1)}), | ||
CaseRPQ(graphs.b_graph, "a", set()), | ||
CaseRPQ(graphs.b_graph, "b", {(0, 1)}), | ||
CaseRPQ(graphs.b_graph, "b*", {(0, 1), (0, 0), (1, 1)}), | ||
CaseRPQ( | ||
graphs.bbb_graph, | ||
"(b b)*", | ||
{(0, 1), (0, 0), (0, 2), (1, 1), (1, 2), (1, 0), (2, 2), (2, 1), (2, 0)}, | ||
), | ||
CaseRPQ( | ||
graphs.bab_graph, | ||
"(a | b)*", | ||
{(0, 1), (0, 0), (0, 2), (1, 1), (1, 2), (1, 0), (2, 2), (2, 1), (2, 0)}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters