mirror of
https://github.com/Brandon-Rozek/matmod.git
synced 2024-12-04 13:55:32 -05:00
Refactored
This commit is contained in:
parent
45ee3d546c
commit
b4b5a7d4e6
1 changed files with 60 additions and 39 deletions
|
@ -31,91 +31,112 @@ class UglyHeader:
|
||||||
self.negation = negation
|
self.negation = negation
|
||||||
self.necessitation = necessitation
|
self.necessitation = necessitation
|
||||||
|
|
||||||
# NOTE: Global variable used to keep track of solution models
|
class ModelBuilder:
|
||||||
solutions: List[Tuple[Model, Dict]] = []
|
def __init__(self):
|
||||||
|
self.size : int = 0
|
||||||
|
self.carrier_set : Set[ModelValue] = set()
|
||||||
|
self.num_negation: int = 0
|
||||||
|
self.mnegation: Optional[ModelFunction] = None
|
||||||
|
self.num_order: int = 0
|
||||||
|
self.mconjunction: Optional[ModelFunction] = None
|
||||||
|
self.mdisjunction: Optional[ModelFunction] = None
|
||||||
|
self.num_designated: int = 0
|
||||||
|
self.designated_values: Set[ModelValue] = set()
|
||||||
|
self.num_implication: int = 0
|
||||||
|
self.mimplication: Optional[ModelFunction] = None
|
||||||
|
|
||||||
def parse_matrices(infile: SourceFile) -> List[Tuple[Model, Dict]]:
|
def parse_matrices(infile: SourceFile) -> List[Tuple[Model, Dict]]:
|
||||||
global solutions
|
|
||||||
solutions = [] # Reset
|
solutions = [] # Reset
|
||||||
header = parse_header(infile)
|
header = parse_header(infile)
|
||||||
process_sizes(infile, header)
|
current_model_parts = ModelBuilder()
|
||||||
|
process_sizes(infile, header, current_model_parts, solutions)
|
||||||
|
return solutions
|
||||||
|
|
||||||
def process_sizes(infile: SourceFile, header: UglyHeader):
|
def process_sizes(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
|
||||||
"""Stage 1"""
|
"""Stage 1"""
|
||||||
while True:
|
while True:
|
||||||
size = parse_size(infile)
|
size = parse_size(infile)
|
||||||
if size is None:
|
if size is None:
|
||||||
break
|
break
|
||||||
carrier_set = carrier_set_from_size(size)
|
carrier_set = carrier_set_from_size(size)
|
||||||
process_negations(infile, header, size, carrier_set)
|
current_model_parts.size = size
|
||||||
|
current_model_parts.carrier_set = carrier_set
|
||||||
|
process_negations(infile, header, current_model_parts, solutions)
|
||||||
|
|
||||||
def process_negations(infile: SourceFile, header: UglyHeader, size: int, carrier_set: Set[ModelValue]):
|
def process_negations(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
|
||||||
"""Stage 2 (Optional)"""
|
"""Stage 2 (Optional)"""
|
||||||
num_negation = 0
|
num_negation = 0
|
||||||
while True:
|
while True:
|
||||||
mnegation = None
|
mnegation = None
|
||||||
if header.negation:
|
if header.negation:
|
||||||
mnegation = parse_single_negation(infile, size)
|
mnegation = parse_single_negation(infile, current_model_parts.size)
|
||||||
if mnegation is None:
|
if mnegation is None:
|
||||||
break
|
break
|
||||||
num_negation += 1
|
num_negation += 1
|
||||||
|
|
||||||
process_orders(infile, header, size, carrier_set, num_negation, mnegation)
|
current_model_parts.num_negation = num_negation
|
||||||
|
current_model_parts.mnegation = mnegation
|
||||||
|
|
||||||
|
process_orders(infile, header, current_model_parts, solutions)
|
||||||
|
|
||||||
if not header.negation:
|
if not header.negation:
|
||||||
break
|
break
|
||||||
|
|
||||||
def process_orders(infile: SourceFile, header: UglyHeader, size: int, carrier_set: Set[ModelValue], num_negation: int, mnegation: Optional[ModelFunction]):
|
def process_orders(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
|
||||||
"""Stage 3"""
|
"""Stage 3"""
|
||||||
num_order = 0
|
num_order = 0
|
||||||
while True:
|
while True:
|
||||||
result = parse_single_order(infile, size)
|
result = parse_single_order(infile, current_model_parts.size)
|
||||||
if result is None:
|
if result is None:
|
||||||
break
|
break
|
||||||
mconjunction, mdisjunction = result
|
|
||||||
num_order += 1
|
num_order += 1
|
||||||
process_designateds(infile, header, size, carrier_set, num_negation, mnegation, num_order, mconjunction, mdisjunction)
|
mconjunction, mdisjunction = result
|
||||||
|
current_model_parts.num_order = num_order
|
||||||
|
current_model_parts.mconjunction = mconjunction
|
||||||
|
current_model_parts.mdisjunction = mdisjunction
|
||||||
|
process_designateds(infile, header, current_model_parts, solutions)
|
||||||
|
|
||||||
def process_designateds(infile: SourceFile, header: UglyHeader, size: int, carrier_set: Set[ModelValue], num_negation: int, mnegation: Optional[ModelFunction], num_order: int, mconjunction: Optional[ModelFunction], mdisjunction: Optional[ModelFunction]):
|
def process_designateds(infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
|
||||||
"""Stage 4"""
|
"""Stage 4"""
|
||||||
num_designated = 0
|
num_designated = 0
|
||||||
while True:
|
while True:
|
||||||
designated_values = parse_single_designated(infile, size)
|
designated_values = parse_single_designated(infile, current_model_parts.size)
|
||||||
if designated_values is None:
|
if designated_values is None:
|
||||||
break
|
break
|
||||||
num_designated += 1
|
num_designated += 1
|
||||||
process_implications(infile, header, size, carrier_set, num_negation, mnegation, num_order, mconjunction, mdisjunction, num_designated, designated_values)
|
current_model_parts.num_designated = num_designated
|
||||||
|
current_model_parts.designated_values = designated_values
|
||||||
|
process_implications(infile, header, current_model_parts, solutions)
|
||||||
|
|
||||||
def process_implications(
|
def process_implications(
|
||||||
infile: SourceFile, header: UglyHeader, size: int, carrier_set: Set[ModelValue], num_negation: int, mnegation: Optional[ModelFunction],
|
infile: SourceFile, header: UglyHeader, current_model_parts: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
|
||||||
num_order: int, mconjunction: Optional[ModelFunction], mdisjunction: Optional[ModelFunction], num_designated: int, designated_values: Set[ModelValue]):
|
|
||||||
"""Stage 5"""
|
"""Stage 5"""
|
||||||
results = parse_implications(infile, size)
|
results = parse_implications(infile, current_model_parts.size)
|
||||||
for num_implication, mimplication in enumerate(results, 1):
|
for num_implication, mimplication in enumerate(results, 1):
|
||||||
process_model(size, carrier_set, num_negation, mnegation, num_order, mconjunction, mdisjunction, num_designated, designated_values, num_implication, mimplication)
|
current_model_parts.num_implication = num_implication
|
||||||
|
current_model_parts.mimplication = mimplication
|
||||||
|
process_model(current_model_parts, solutions)
|
||||||
|
|
||||||
def process_model(
|
def process_model(mp: ModelBuilder, solutions: List[Tuple[Model, Dict]]):
|
||||||
size: int, carrier_set: Set[ModelValue], num_negation: int, mnegation: Optional[ModelFunction],
|
|
||||||
num_order: int, mconjunction: Optional[ModelFunction], mdisjunction: Optional[ModelFunction], num_designated: int,
|
|
||||||
designated_values: Set[ModelValue], num_implication: int, mimplication: ModelFunction):
|
|
||||||
"""Create Model"""
|
"""Create Model"""
|
||||||
global solutions
|
assert mp.mimplication is not None
|
||||||
|
assert len(mp.carrier_set) > 0
|
||||||
|
|
||||||
logical_operations = { mimplication }
|
logical_operations = { mp.mimplication }
|
||||||
model_name = f"{size}{'.' + str(num_negation) if num_negation != 0 else ''}.{num_order}.{num_designated}.{num_implication}"
|
model_name = f"{mp.size}{'.' + str(mp.num_negation) if mp.num_negation != 0 else ''}.{mp.num_order}.{mp.num_designated}.{mp.num_implication}"
|
||||||
model = Model(carrier_set, logical_operations, designated_values, name=model_name)
|
model = Model(mp.carrier_set, logical_operations, mp.designated_values, name=model_name)
|
||||||
interpretation = {
|
interpretation = {
|
||||||
Implication: mimplication
|
Implication: mp.mimplication
|
||||||
}
|
}
|
||||||
if mnegation is not None:
|
if mp.mnegation is not None:
|
||||||
logical_operations.add(mnegation)
|
logical_operations.add(mp.mnegation)
|
||||||
interpretation[Negation] = mnegation
|
interpretation[Negation] = mp.mnegation
|
||||||
if mconjunction is not None:
|
if mp.mconjunction is not None:
|
||||||
logical_operations.add(mconjunction)
|
logical_operations.add(mp.mconjunction)
|
||||||
interpretation[Conjunction] = mconjunction
|
interpretation[Conjunction] = mp.mconjunction
|
||||||
if mdisjunction is not None:
|
if mp.mdisjunction is not None:
|
||||||
logical_operations.add(mdisjunction)
|
logical_operations.add(mp.mdisjunction)
|
||||||
interpretation[Disjunction] = mdisjunction
|
interpretation[Disjunction] = mp.mdisjunction
|
||||||
|
|
||||||
solutions.append((model, interpretation))
|
solutions.append((model, interpretation))
|
||||||
print(f"Parsed Matrix {model.name}")
|
print(f"Parsed Matrix {model.name}")
|
||||||
|
@ -343,7 +364,7 @@ if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description="VSP Checker")
|
parser = argparse.ArgumentParser(description="VSP Checker")
|
||||||
parser.add_argument("--verbose", action='store_true', help="Print out all parsed matrices")
|
parser.add_argument("--verbose", action='store_true', help="Print out all parsed matrices")
|
||||||
args = vars(parser.parse_args())
|
args = vars(parser.parse_args())
|
||||||
parse_matrices(SourceFile(sys.stdin))
|
solutions = parse_matrices(SourceFile(sys.stdin))
|
||||||
print(f"Parsed {len(solutions)} matrices")
|
print(f"Parsed {len(solutions)} matrices")
|
||||||
num_has_vsp = 0
|
num_has_vsp = 0
|
||||||
for i, (model, interpretation) in enumerate(solutions):
|
for i, (model, interpretation) in enumerate(solutions):
|
||||||
|
|
Loading…
Reference in a new issue