jobshop2_parser now works, too

This commit is contained in:
Trolli Schmittlauch 2017-06-27 23:39:32 +02:00
parent 59cf66506f
commit 252e7d6ee0
2 changed files with 15 additions and 3 deletions

View file

@ -83,8 +83,13 @@ class JobShopVisitor(arpeggio.PTNodeVisitor):
def visit_problem_data(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> JobShopProblem: def visit_problem_data(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> JobShopProblem:
if self.debug: if self.debug:
print("problem_data\nchildren:", children) print("problem_data\nchildren:", children)
# filter out newlines or other strings
cleaned_data = list(filter(lambda x: type(x) is not str, children)) cleaned_data = list(filter(lambda x: type(x) is not str, children))
problem_data: List[List[Tuple(int, int)]] = [ cleaned_data[i] for i in range(2, len(cleaned_data))] problem_data: List[List[Tuple(int, int)]] = [ cleaned_data[i] for i in range(2, len(cleaned_data))]
return JobShopProblem(children[0], children[1], problem_data) problem = JobShopProblem(children[0], children[1], problem_data)
if self.debug:
print("\nreturning a", type(problem), "\n")
print("problem_data:", problem_data)
return problem

View file

@ -5,7 +5,14 @@ from typing import List, Tuple, Sequence, Optional, Union
from . import grammar, JobShopVisitor, JobShopProblem from . import grammar, JobShopVisitor, JobShopProblem
class JobShop2Visitor(JobShopVisitor): class JobShop2Visitor(JobShopVisitor):
pass
def visit_job_shop2(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> JobShopProblem:
if self.debug:
print("job_shop2:\nchildren:", children)
problem = children[0]
if self.debug:
print("returning a", type(problem))
return problem
def parse_jobshop2_file(filename: Union[str, bytes]) -> JobShopProblem: def parse_jobshop2_file(filename: Union[str, bytes]) -> JobShopProblem:
"""Open file with jobshop2-formatted data (single problem instance, no name & description), """Open file with jobshop2-formatted data (single problem instance, no name & description),
@ -17,7 +24,7 @@ def parse_jobshop2_file(filename: Union[str, bytes]) -> JobShopProblem:
return arpeggio.visit_parse_tree(parse_tree, JobShop2Visitor()) return arpeggio.visit_parse_tree(parse_tree, JobShop2Visitor())
def main(): def main():
print(type(parse_jobshop2_file('../inputdata/jobshop2/ta13').problem_data)) print(type(parse_jobshop2_file('../inputdata/jobshop2/ta13')))
parser = ParserPEG(grammar, "job_shop2", skipws=False) parser = ParserPEG(grammar, "job_shop2", skipws=False)