diff --git a/src/JobShopParser/common.py b/src/JobShopParser/common.py index 1a7112b..51df1c2 100644 --- a/src/JobShopParser/common.py +++ b/src/JobShopParser/common.py @@ -46,8 +46,8 @@ class JobShopProblem: self.machines = machines self.jobs = jobs - def __str__() -> str: - return name + def __str__(self) -> str: + return self.name class JobShopVisitor(arpeggio.PTNodeVisitor): """contains visitor functions needed for both jobshop1 (list of instances) @@ -83,7 +83,8 @@ class JobShopVisitor(arpeggio.PTNodeVisitor): def visit_problem_data(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> JobShopProblem: if self.debug: print("problem_data\nchildren:", children) - problem_data: List[List[Tuple(int, int)]] = [ children[i] for i in range(2, len(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))] return JobShopProblem(children[0], children[1], problem_data) diff --git a/src/JobShopParser/jobshop2_parser.py b/src/JobShopParser/jobshop2_parser.py index 78d3790..de170c2 100644 --- a/src/JobShopParser/jobshop2_parser.py +++ b/src/JobShopParser/jobshop2_parser.py @@ -1,11 +1,25 @@ +import arpeggio from arpeggio.cleanpeg import ParserPEG +from typing import List, Tuple, Sequence, Optional, Union -from common import grammar +from common import grammar, JobShopVisitor, JobShopProblem -parser = ParserPEG(grammar, "problem_data", skipws=False,debug=True) +class JobShop2Visitor(JobShopVisitor): + pass -with open('./inputdata/jobshop2/ta13') as jobdatafile: - jobdata : str = jobdatafile.read() - print("zu parsender String:\n\v\v", jobdata) - parse_tree = parser.parse(jobdata) - print(parse_tree) +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(debug=True)) + +def main(): + print(parse_jobshop2_file('./inputdata/jobshop2/ta13').problem_data) + +parser = ParserPEG(grammar, "problem_data", skipws=False) + +if __name__ == "__main__": + main()