Skip to content

Commit 3ae4526

Browse files
committed
completed truth table evaluation (prototype)
1 parent f538d8a commit 3ae4526

1 file changed

Lines changed: 59 additions & 9 deletions

File tree

evaluation_function/truth_table/evaluate.py

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11

2+
from lf_toolkit.evaluation import Result
3+
24
from evaluation_function.domain.formula import (
35
Formula,
46
Atom
57
)
68

79

8-
from evaluation_function.domain.evaluators import _extract_atoms
10+
from evaluation_function.domain.evaluators import _extract_atoms, Assignment, FormulaEvaluator
911
from evaluation_function.domain.formula import *
1012
from evaluation_function.parsing.parser import formula_parser
1113

1214
from evaluation_function.parsing.tree_builder_error import BuildError
1315

1416

1517
# assume table sent through is of type list[list[str]]
16-
def evaluate(input: list[list[str]]) -> bool:
18+
def evaluate_truth_table(input: list[list[str]], num_atoms) -> Result:
19+
"""
20+
Function used to evaluate truth table response
21+
---
22+
23+
- `input` the 2D array containing the formuals and the cells of the truth table
24+
- `num_atoms` the number of atoms in the truth table
25+
26+
returns True if truth table is valid
27+
"""
1728

1829
if len(input) == 0:
1930
raise Exception("no input was given")
@@ -22,7 +33,7 @@ def evaluate(input: list[list[str]]) -> bool:
2233

2334
# find the atoms of the formula
2435
formulas = input[0]
25-
existing_atoms = set()
36+
existing_atoms = {}
2637

2738
for i in range(len(formulas)):
2839
formula_string = formulas[i]
@@ -41,7 +52,7 @@ def evaluate(input: list[list[str]]) -> bool:
4152

4253
# if formula is an atom, keep track of it
4354
if isinstance(formula, Atom):
44-
existing_atoms.add(formula)
55+
existing_atoms[formula] = i
4556

4657
# otherwise check all atoms in formula is to the left on the table (i.e all atoms in formula has been defined)
4758
else:
@@ -52,18 +63,57 @@ def evaluate(input: list[list[str]]) -> bool:
5263
# if an atom is undefined, erro
5364
if atom not in existing_atoms:
5465
raise Exception(f"in column {i+1}, atom {atom} in formula {formula_string} is undefined")
55-
56-
# all formula and its order in the table is valid
57-
58-
59-
6066

67+
# replace strings with
68+
formulas[i] = formula
69+
70+
# all formula and its order in the table is valid====
6171

72+
# check all the cells are valid:
6273

74+
for i in range(1, len(input)):
75+
for j in range(len(input[i])):
76+
if input[i][j] == "tt":
77+
input[i][j] = True
78+
elif input[i][j] == "ff":
79+
input[i][j] = False
80+
else:
81+
raise Exception(f"cell in column {j+1} row {i+1} invalid")
6382

6483

84+
# check that every combination of the atoms is stated in the truth table.
6585

86+
if len(existing_atoms) != num_atoms:
87+
raise Exception(f"missing combinations in truth table")
88+
if len(input) - 1 < 2 ** num_atoms:
89+
raise Exception(f"missing combinations in truth table")
90+
if len(input) - 1 > 2 ** num_atoms:
91+
raise Exception(f"excessive combinations in truth table")
6692

6793

94+
unique_rows = set(tuple(row[cell] for cell in existing_atoms.values()) for row in input[1:])
95+
if unique_rows < 2 ** num_atoms:
96+
raise Exception("dupliated assignment to atoms")
6897

6998

99+
# evaluate truth table row by row
100+
101+
for i in range(1, len(input)):
102+
atoms_mapping = {}
103+
for j in range(len(input[i])):
104+
formula = formula[j]
105+
106+
if isinstance(formula, Atom):
107+
atoms_mapping[formula] = input[i][j]
108+
continue
109+
110+
assignment = Assignment(atoms_mapping)
111+
if FormulaEvaluator(formula, assignment).evaluate() != input[i][j]:
112+
return Result(
113+
is_correct=False,
114+
feedback_items=[(Exception, "incorrect cell value")]
115+
)
116+
117+
118+
return Result(is_correct=True)
119+

0 commit comments

Comments
 (0)