Skip to content

Commit

Permalink
Improve CLI help message
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaMuravjov committed Feb 24, 2024
1 parent bf5ab5a commit 4b7110e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 30 deletions.
34 changes: 26 additions & 8 deletions cli/run_all_pairs_cflr.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,32 @@ def run_all_pairs_cflr(

def main(raw_args: List[str]):
parser = argparse.ArgumentParser(
description="Measures running time for solving all-pairs reachability")
parser.add_argument('-algo', dest='algo', required=True, choices=ALL_PAIRS_CFL_REACHABILITY_ALGO_NAMES,
help='Algorithm implementation that will be run')
parser.add_argument('-graph', dest='graph', required=True, help='Graph file to query on in POCR format')
parser.add_argument('-grammar', dest='grammar', required=True,
help='Context-free grammar file to query with in POCR format')
parser.add_argument('-time-limit', dest='time_limit', default=None, help='Time limit in seconds')
parser.add_argument('-out', dest='out', default=None, help='Output file for saving vertex pairs')
description="Solves the Context-Free Language Reachability (CFL-R) problem for all vertex pairs.",
)
parser.add_argument(dest='algo', choices=ALL_PAIRS_CFL_REACHABILITY_ALGO_NAMES,
help='Specifies the algorithm to use.')
parser.add_argument(dest='graph',
help='Specifies the graph file in POCR format. The line format is: '
'`<EDGE_SOURCE> <EDGE_DESTINATION> <EDGE_LABEL> [LABEL_INDEX]`, '
'with values separated by whitespace characters. '
'[LABEL_INDEX] is optional. '
'Indexed label names should end with `_i`.')
parser.add_argument(dest='grammar',
help='Specifies the grammar file in POCR format. '
'Non-empty lines (except the last two) denote grammar rules: '
'complex rules (`<NON_TERMINAL> <SYMBOL_1> <SYMBOL_2>`), '
'simple rules (`<NON_TERMINAL> <TERMINAL>`), and epsilon rules (`<NON_TERMINAL>`), '
'with values separated by whitespace characters. '
'Indexed symbol names should end with `_i`. '
'The final two lines define the starting non-terminal as: '
'`Count:\\n <START_NON_TERMINAL>`.')
parser.add_argument('--time-limit', dest='time_limit', default=None, help='Sets a time limit in seconds.')
parser.add_argument('--out', dest='out', default=None,
help='Specifies the output file for saving vertex pairs. '
'The line format is: `[START_VERTEX]\t[END_VERTEX]`. '
'Each line indicates a path exists from [START_VERTEX] to [END_VERTEX], '
'labels along which spell a word from the specified Context-Free Language (CFL).'
)
settings_manager = AlgoSettingsManager()
settings_manager.add_args(parser)
args = parser.parse_args(raw_args)
Expand Down
10 changes: 8 additions & 2 deletions src/algo_setting/preprocessor_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ def __repr__(self):

@property
def flag_name(self) -> str:
return "-" + self.var_name.replace('_', '-')
return "--disable-optimize-block-matrix"

@property
def var_name(self) -> str:
return "explode_indexes"

def add_arg(self, parser: ArgumentParser):
parser.add_argument(self.flag_name, dest=self.var_name, default=False, action="store_true")
parser.add_argument(
self.flag_name,
dest=self.var_name,
default=False,
action="store_true",
help="Turns off block matrix optimization."
)

def read_arg(self, args: Namespace):
if args.explode_indexes:
Expand Down
20 changes: 10 additions & 10 deletions src/grammar/cnf_grammar_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ def read_from_pocr_cnf_file(path: Union[Path, str]) -> "CnfGrammarTemplate":
Reads a CNF grammar from a file and constructs a CnfGrammarTemplate object.
The file format is expected to be as follows:
- Each non-blank line represents a rule, except the last two lines.
- Complex rules are in the format: <NON_TERMINAL> <SYMBOL_1> <SYMBOL_2>
- Simple rules are in the format: <NON_TERMINAL> <TERMINAL>
- Epsilon rules are in the format: <NON_TERMINAL>
- Indexed symbols names must end with suffix '_i'.
- Each non-empty line represents a rule, except the last two lines.
- Complex rules are in the format: `<NON_TERMINAL> <SYMBOL_1> <SYMBOL_2>`
- Simple rules are in the format: `<NON_TERMINAL> <TERMINAL>`
- Epsilon rules are in the format: `<NON_TERMINAL>`
- Indexed symbols names must end with suffix `_i`.
- Whitespace characters are used to separate values on one line
- The last two lines specify the starting non-terminal in the format:
'''
```
Count:
<START_NON_TERMINAL>
'''
```
"""
with open(path, 'r') as file:
lines = [line.strip() for line in file.readlines() if line.strip()]
Expand Down Expand Up @@ -91,9 +91,9 @@ def read_from_pocr_cnf_file(path: Union[Path, str]) -> "CnfGrammarTemplate":
complex_rules.append((Symbol(parts[0]), Symbol(parts[1]), Symbol(parts[2])))
else:
raise ValueError(
f"Invalid rule format: '{line}' in file '{path}'. "
f"Expected formats are '<NON_TERMINAL> <SYMBOL_1> <SYMBOL_2>' for complex rules, "
f"'<NON_TERMINAL> <TERMINAL>' for simple rules, and '<NON_TERMINAL>' for epsilon rules."
f"Invalid rule format: `{line}` in file `{path}`. "
f"Expected formats are `<NON_TERMINAL> <SYMBOL_1> <SYMBOL_2>` for complex rules, "
f"`<NON_TERMINAL> <TERMINAL>` for simple rules, and `<NON_TERMINAL>` for epsilon rules."
)

return CnfGrammarTemplate(start_nonterm, epsilon_rules, simple_rules, complex_rules)
8 changes: 4 additions & 4 deletions src/graph/label_decomposed_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def read_from_pocr_graph_file(path: Union[Path, str]) -> "LabelDecomposedGraph":
)
except IndexOutOfBound as e:
raise ValueError(
f"Failed to create adjacency matrix for label '{symbol.label}'.\n"
f"This issue is usually caused by using indexes for label without '_i' suffix.\n"
f"Consider adding suffix '_i' to label '{symbol.label}'."
f"Failed to create adjacency matrix for label `{symbol.label}`.\n"
f"This issue is usually caused by using indexes for label without `_i` suffix.\n"
f"Consider adding `_i` suffix to label '{symbol.label}'."
) from e

return LabelDecomposedGraph(
Expand All @@ -123,7 +123,7 @@ def read_from_pocr_graph_file(path: Union[Path, str]) -> "LabelDecomposedGraph":
"```\n"
"Whitespace characters should be used to separate values on one line.\n"
"[LABEL_INDEX] is optional.\n"
"Indexed labels names must end with '_i'."
"Indexed labels names must end with `_i`."
) from e

def __sizeof__(self) -> int:
Expand Down
27 changes: 25 additions & 2 deletions src/matrix/matrix_optimizer_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ def __init__(self):

@property
def flag_name(self) -> str:
return "-disable-" + self.var_name.replace("_", "-")
return "--disable-" + self.var_name.replace("_", "-")

@property
@abstractmethod
def help(self) -> str:
pass

def __repr__(self):
return f"{self.__class__.__name__}(is_enabled={self.is_enabled})"

def add_arg(self, parser: ArgumentParser):
parser.add_argument(self.flag_name, dest=self.var_name, default=False, action="store_true")
parser.add_argument(
self.flag_name,
dest=self.var_name,
default=False,
action="store_true",
help=self.help
)

def read_arg(self, args: Namespace):
if args.__getattribute__(self.var_name) is True:
Expand Down Expand Up @@ -63,6 +74,10 @@ def _wrap_matrix_unconditionally(self, base_matrix: OptimizedMatrix) -> Optimize
def var_name(self) -> str:
return "optimize_empty"

@property
def help(self) -> str:
return "Turns off empty matrix optimization."


class OptimizeFormatMatrixSetting(MatrixOptimizerSetting):
def _wrap_matrix_unconditionally(self, base_matrix: OptimizedMatrix) -> OptimizedMatrix:
Expand All @@ -72,6 +87,10 @@ def _wrap_matrix_unconditionally(self, base_matrix: OptimizedMatrix) -> Optimize
def var_name(self) -> str:
return "optimize_format"

@property
def help(self) -> str:
return "Turns off matrix format optimization."


class LazyAddMatrixSetting(MatrixOptimizerSetting):
def _wrap_matrix_unconditionally(self, base_matrix: OptimizedMatrix) -> OptimizedMatrix:
Expand All @@ -80,3 +99,7 @@ def _wrap_matrix_unconditionally(self, base_matrix: OptimizedMatrix) -> Optimize
@property
def var_name(self) -> str:
return "lazy_add"

@property
def help(self) -> str:
return "Turns off lazy addition optimization."
8 changes: 4 additions & 4 deletions test/cli/test_run_all_pairs_cflr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def test_cli_run_all_pairs_cflr_out():
expected_path = os.path.join(data_path, 'expected_all_pairs_reachability.txt')
run_all_pairs_cflr.main(
[
'-algo', 'IncrementalAllPairsCFLReachabilityMatrix',
'-graph', find_graph_file(data_path),
'-grammar', find_grammar_file(data_path),
'-out', actual_path
'IncrementalAllPairsCFLReachabilityMatrix',
find_graph_file(data_path),
find_grammar_file(data_path),
'--out', actual_path
]
)
with open(actual_path, 'r') as actual_file:
Expand Down

0 comments on commit 4b7110e

Please sign in to comment.