public ITruthTableRow Simplify(ITruthTableRow otherRow) { // Instead of creating a memory intense copy, we create a very simple row object // that will only hold the truth values in each cell and a result. int numberOfVariables = Cells.Length; TruthTableRow simplifiedRow = new TruthTableRow(uniqueVariables, numberOfVariables); simplifiedRow.Result = Result; int numberOfDeferringValues = 0; for (int i = 0; i < Cells.Length; i++) { if (Cells[i] != otherRow.Cells[i]) { // Then we deal with a don't care variable, which we indicate by null. simplifiedRow.Cells[i] = '*'; numberOfDeferringValues++; if (numberOfDeferringValues > 1) { return(null); } } else { simplifiedRow.Cells[i] = Cells[i]; } } return(simplifiedRow); }
public string TableHeader() { string result = ""; for (int i = 0; i < propositionVariablesSet.Count; i++) { result += propositionVariablesSet[i] + TruthTableRow.GetPadding(); } result += "v\n"; return(result); }
public TruthTableRow Copy() { TruthTableRow rowCopy = new TruthTableRow(propositionRoot, uniqueVariables, Cells.Length); for (int i = 0; i < Cells.Length; i++) { rowCopy.Cells[i] = Cells[i]; } rowCopy.Result = Result; return(rowCopy); }
/// <summary> /// Wrapper method that makes the initial call to CreateRowsRecursively with some basic /// input parameters that do not change. /// </summary> private void CreateTruthTableRows() { TruthTableRow initialRow = new TruthTableRow(propositionRoot, propositionVariablesSet, propositionVariablesSet.Count); if (initialRow.Cells.Length == 0) { // We had no variables and are dealing with one of the constants. // return an empty row with only a result value. initialRow.Calculate(); Rows.Add(initialRow); } else { CreateRowsRecursively(0, new char[] { '0', '1' }, initialRow); } }
/// <summary> /// Recursive method that evaluates a true and false value for every unique variable, /// exhausting all possible permutations and creating a row copy for every such row. /// This row copy is then added to the collection of rows of this truth table object. /// </summary> /// <param name="index">The current index, starting at 0, and evaluating until the index is equal to the number of unique variables - 1</param> /// <param name="truthSet">A constant set of the two boolean values false and true</param> /// <param name="row">A truth table row object that holds the combination of truth values for the abstract proposition variables.</param> private void CreateRowsRecursively(int index, char[] truthSet, TruthTableRow row) { // This is something we only have to execute if we have abstract proposition variables. foreach (char truthValue in truthSet) { if (index == propositionVariablesSet.Count - 1) { // add row to the table's list of rows and return back to the calling environment row.Cells[index] = truthValue; TruthTableRow copy = row.Copy(); copy.Calculate(); // Required call to Calculate, otherwise the Result truth value can not be set :-( // With the current recursive approach, only one row is constructed inside the wrapper method. Rows.Add(copy); } else { // Not at the final variable so we add the cell into the row and call the method again. row.Cells[index] = truthValue; CreateRowsRecursively(index + 1, truthSet, row); } } }