diff --git a/parse_magic.py b/parse_magic.py index 9ef9aaa..7cc1fe0 100644 --- a/parse_magic.py +++ b/parse_magic.py @@ -15,18 +15,7 @@ from logic import ( ) from vsp import has_vsp -class SourceFile: - def __init__(self, fileobj: TextIO): - self.fileobj = fileobj - self.current_line = 0 - - def __next__(self): - contents = next(self.fileobj) - self.current_line += 1 - return contents - - -def parse_matrices(infile: SourceFile) -> List[Tuple[Model, Dict]]: +def parse_matrices(infile: TextIO) -> List[Tuple[Model, Dict]]: next(infile) # Skip header line solutions: List[Tuple[Model, Dict]] = [] @@ -38,40 +27,31 @@ def parse_matrices(infile: SourceFile) -> List[Tuple[Model, Dict]]: carrier_set = carrier_set_from_size(size) - num_negation = 0 while True: mnegation = parse_negation(infile, size) if mnegation is None: break - num_negation += 1 - num_order = 0 while True: result = parse_order(infile, size) if result is None: break mconjunction, mdisjunction = result - num_order += 1 - num_designated = 0 while True: designated_values = parse_designated(infile, size) if designated_values is None: break - num_designated += 1 results = parse_implication(infile, size) if result is None: break - num_implication = 0 for mimplication in results: logical_operations = { mnegation, mimplication } - num_implication += 1 - model_name = f"{size}.{num_negation}.{num_order}.{num_designated}.{num_implication}" - model = Model(carrier_set, logical_operations, designated_values, name=model_name) + model = Model(carrier_set, logical_operations, designated_values, name=str(len(solutions))) interpretation = { Negation: mnegation, Implication: mimplication @@ -97,17 +77,17 @@ def carrier_set_from_size(size: int): mvalue_from_index(i) for i in range(size + 1) } -def parse_size(infile: SourceFile) -> Optional[int]: +def parse_size(infile: TextIO) -> Optional[int]: """ Parse the line representing the matrix size. """ size = int(next(infile)) if size == -1: return None - assert size > 0, f"Unexpected size at line {infile.current_line}" + assert size > 0, "Unexpected size" return size -def parse_negation(infile: SourceFile, size: int) -> Optional[ModelFunction]: +def parse_negation(infile: TextIO, size: int) -> Optional[ModelFunction]: """ Parse the line representing the negation table. """ @@ -116,7 +96,7 @@ def parse_negation(infile: SourceFile, size: int) -> Optional[ModelFunction]: return None row = line.split(" ") - assert len(row) == size + 1, f"Negation table doesn't match size at line {infile.current_line}" + assert len(row) == size + 1, "Negation table doesn't match size" mapping = {} for i, j in zip(range(size + 1), row): @@ -146,7 +126,7 @@ def determine_cresult(size: int, ordering: Dict[ModelValue, ModelValue], a: Mode """ for i in range(size + 1): c = mvalue_from_index(i) - + if not ordering[(c, a)]: continue if not ordering[(c, b)]: @@ -198,7 +178,7 @@ def parse_order(infile: TextIO, size: int) -> Optional[Tuple[ModelFunction, Mode table = line.split(" ") - assert len(table) == (size + 1)**2, f"Order table doesn't match expected size at line {infile.current_line}" + assert len(table) == (size + 1)**2 omapping = {} table_i = 0 @@ -248,7 +228,7 @@ def parse_designated(infile: TextIO, size: int) -> Optional[Set[ModelValue]]: return None row = line.split(" ") - assert len(row) == size + 1, f"Designated table doesn't match expected size at line {infile.current_line}" + assert len(row) == size + 1, "Designated table doesn't match size" designated_values = set() @@ -272,7 +252,7 @@ def parse_implication(infile: TextIO, size: int) -> Optional[List[ModelFunction] # Split and remove the last '-1' character table = line.split(" ")[:-1] - assert len(table) % (size + 1)**2 == 0, f"Implication table does not match expected size at line {infile.current_line}" + assert len(table) % (size + 1)**2 == 0 table_i = 0 mimplications: List[ModelFunction] = [] @@ -297,7 +277,7 @@ def parse_implication(infile: TextIO, size: int) -> Optional[List[ModelFunction] if __name__ == "__main__": - solutions: List[Model] = parse_matrices(SourceFile(sys.stdin)) + solutions: List[Model] = parse_matrices(sys.stdin) print(f"Parsed {len(solutions)} matrices") for i, (model, interpretation) in enumerate(solutions): print(model)