From 901472e8b5a550b9b85672cd9e61dbafa7914739 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Tue, 27 Jun 2017 23:47:56 +0200 Subject: [PATCH] add function for parsing inputdata strings --- src/JobShopParser/jobshop1_parser.py | 9 +++++++-- src/JobShopParser/jobshop2_parser.py | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/JobShopParser/jobshop1_parser.py b/src/JobShopParser/jobshop1_parser.py index 31f9c3c..48047b2 100644 --- a/src/JobShopParser/jobshop1_parser.py +++ b/src/JobShopParser/jobshop1_parser.py @@ -33,14 +33,19 @@ class JobShop1Visitor(JobShopVisitor): def visit_trim_ws(self, node:arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> None: return None +def parse_jobshop1_string(inputdata: str) -> JobShopProblem: + """parse string with jobshop1-formatted data (multiple problem instances) + and return list of JobShopProblem s""" + parse_tree = parser.parse(inputdata) + return arpeggio.visit_parse_tree(parse_tree, JobShop1Visitor()) + def parse_jobshop1_file(filename: Union[str, bytes]) -> List[JobShopProblem]: """Open file with jobshop1-formatted data (multiple problem instances), parse it and return list of JobShopProblem s""" with open(filename) as datafile: inputdata: str = datafile.read() - parse_tree = parser.parse(inputdata) - return arpeggio.visit_parse_tree(parse_tree, JobShop1Visitor()) + return parse_jobshop1_string(inputdata) def main(): diff --git a/src/JobShopParser/jobshop2_parser.py b/src/JobShopParser/jobshop2_parser.py index 43b6d30..7a4c4b1 100644 --- a/src/JobShopParser/jobshop2_parser.py +++ b/src/JobShopParser/jobshop2_parser.py @@ -14,14 +14,19 @@ class JobShop2Visitor(JobShopVisitor): print("returning a", type(problem)) return problem +def parse_jobshop2_string(inputdata: str) -> JobShopProblem: + """parse string of jobshop2-formatted data (single problem instance, no name & description) + and return JobShopProblem""" + parse_tree = parser.parse(inputdata) + return arpeggio.visit_parse_tree(parse_tree, JobShop2Visitor()) + def parse_jobshop2_file(filename: Union[str, bytes]) -> JobShopProblem: """Open file with jobshop2-formatted data (single problem instance, no name & description), parse it and return JobShopProblem""" with open(filename) as datafile: inputdata: str = datafile.read() - parse_tree = parser.parse(inputdata) - return arpeggio.visit_parse_tree(parse_tree, JobShop2Visitor()) + return parse_jobshop2_string(inputdata) def main(): print(type(parse_jobshop2_file('../inputdata/jobshop2/ta13')))