mirror of
https://github.com/Brandon-Rozek/matmod.git
synced 2024-11-08 21:20:33 -05:00
Compare commits
2 commits
d14ad52798
...
c9a4f6ce36
Author | SHA1 | Date | |
---|---|---|---|
c9a4f6ce36 | |||
a6ca7ff7a3 |
1 changed files with 31 additions and 11 deletions
|
@ -15,7 +15,18 @@ from logic import (
|
|||
)
|
||||
from vsp import has_vsp
|
||||
|
||||
def parse_matrices(infile: TextIO) -> List[Tuple[Model, Dict]]:
|
||||
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]]:
|
||||
next(infile) # Skip header line
|
||||
|
||||
solutions: List[Tuple[Model, Dict]] = []
|
||||
|
@ -27,31 +38,40 @@ def parse_matrices(infile: TextIO) -> 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
|
||||
}
|
||||
model = Model(carrier_set, logical_operations, designated_values, name=str(len(solutions)))
|
||||
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)
|
||||
interpretation = {
|
||||
Negation: mnegation,
|
||||
Implication: mimplication
|
||||
|
@ -77,17 +97,17 @@ def carrier_set_from_size(size: int):
|
|||
mvalue_from_index(i) for i in range(size + 1)
|
||||
}
|
||||
|
||||
def parse_size(infile: TextIO) -> Optional[int]:
|
||||
def parse_size(infile: SourceFile) -> Optional[int]:
|
||||
"""
|
||||
Parse the line representing the matrix size.
|
||||
"""
|
||||
size = int(next(infile))
|
||||
if size == -1:
|
||||
return None
|
||||
assert size > 0, "Unexpected size"
|
||||
assert size > 0, f"Unexpected size at line {infile.current_line}"
|
||||
return size
|
||||
|
||||
def parse_negation(infile: TextIO, size: int) -> Optional[ModelFunction]:
|
||||
def parse_negation(infile: SourceFile, size: int) -> Optional[ModelFunction]:
|
||||
"""
|
||||
Parse the line representing the negation table.
|
||||
"""
|
||||
|
@ -96,7 +116,7 @@ def parse_negation(infile: TextIO, size: int) -> Optional[ModelFunction]:
|
|||
return None
|
||||
|
||||
row = line.split(" ")
|
||||
assert len(row) == size + 1, "Negation table doesn't match size"
|
||||
assert len(row) == size + 1, f"Negation table doesn't match size at line {infile.current_line}"
|
||||
mapping = {}
|
||||
|
||||
for i, j in zip(range(size + 1), row):
|
||||
|
@ -178,7 +198,7 @@ def parse_order(infile: TextIO, size: int) -> Optional[Tuple[ModelFunction, Mode
|
|||
|
||||
table = line.split(" ")
|
||||
|
||||
assert len(table) == (size + 1)**2
|
||||
assert len(table) == (size + 1)**2, f"Order table doesn't match expected size at line {infile.current_line}"
|
||||
|
||||
omapping = {}
|
||||
table_i = 0
|
||||
|
@ -228,7 +248,7 @@ def parse_designated(infile: TextIO, size: int) -> Optional[Set[ModelValue]]:
|
|||
return None
|
||||
|
||||
row = line.split(" ")
|
||||
assert len(row) == size + 1, "Designated table doesn't match size"
|
||||
assert len(row) == size + 1, f"Designated table doesn't match expected size at line {infile.current_line}"
|
||||
|
||||
designated_values = set()
|
||||
|
||||
|
@ -252,7 +272,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
|
||||
assert len(table) % (size + 1)**2 == 0, f"Implication table does not match expected size at line {infile.current_line}"
|
||||
|
||||
table_i = 0
|
||||
mimplications: List[ModelFunction] = []
|
||||
|
@ -277,7 +297,7 @@ def parse_implication(infile: TextIO, size: int) -> Optional[List[ModelFunction]
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
solutions: List[Model] = parse_matrices(sys.stdin)
|
||||
solutions: List[Model] = parse_matrices(SourceFile(sys.stdin))
|
||||
print(f"Parsed {len(solutions)} matrices")
|
||||
for i, (model, interpretation) in enumerate(solutions):
|
||||
print(model)
|
||||
|
|
Loading…
Reference in a new issue