Esempio n. 1
0
        public List <List <string[]> > ComputeSimplifiedTruthTable()
        {
            var tableValues      = TruthTableHelper.GetPreComputeValues(Variables);
            var truthTableResult = ComputeTruthTable();
            var data             = TruthTableHelper.GetPreSimplifyValues(tableValues, truthTableResult, Variables);
            var result           = Simplify(data);

            return(CheckSimplifiedResult(result));
        }
Esempio n. 2
0
        private void CreateTruthTable()
        {
            if (_myTree.MainFormula == null)
            {
                MessageBox.Show("Could not provide a truth table for an empty formula!!", "Error");
                return;
            }
            try
            {
                //Compute truth value
                TruthTable   myTruthTable   = _myTree.TruthTable;
                List <int[]> preComputeData = TruthTableHelper.GetPreComputeValues(_myTree.Variables);
                List <int>   result         = myTruthTable.ComputeTruthTable();

                //Setup DataGridView
                dgvTruthTable.ColumnCount       = myTruthTable.NumberOfColumn + 1;
                dgvTruthTable.RowCount          = myTruthTable.NumberOfRow;
                dgvTruthTable.Name              = "Truth Table";
                dgvTruthTable.RowHeadersVisible = false;
                dgvTruthTable.AutoSize          = true;

                for (int i = 0; i < myTruthTable.NumberOfColumn; i++)
                {
                    dgvTruthTable.Columns[i].Name = myTruthTable.Variables[i].Proposition.ToString();
                }

                dgvTruthTable.Columns[dgvTruthTable.ColumnCount - 1].Name = _originalFormula;

                for (int i = 0; i < myTruthTable.Variables.Count + 1; i++)
                {
                    for (int j = 0; j < myTruthTable.NumberOfRow; j++)
                    {
                        dgvTruthTable.Rows[j].Cells[i].Value = i != myTruthTable.Variables.Count ? preComputeData[i][j] : result[j];
                    }
                }

                foreach (DataGridViewColumn column in dgvTruthTable.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    column.SortMode     = DataGridViewColumnSortMode.NotSortable;
                }

                dgvTruthTable.Columns[dgvTruthTable.ColumnCount - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
                throw;
            }
        }
Esempio n. 3
0
        public List <int> ComputeTruthTable()
        {
            List <int[]> tableValue  = TruthTableHelper.GetPreComputeValues(Variables);
            List <int>   finalResult = new List <int>();

            for (int j = 0; j < NumberOfRow; j++)
            {
                for (int i = 0; i < Variables.Count; i++)
                {
                    Variables[i].SetTruthValue(tableValue[i][j]);
                }

                int result = MainFormula.ComputeTruthValue();
                finalResult.Add(result);
            }

            return(finalResult);
        }
        public string FindDisjunctiveNormalForm()
        {
            var    tableValues = TruthTableHelper.GetPreComputeValues(Variables);
            var    truthResult = ComputeTruthData();
            string result      = "";

            List <string> outerPair = new List <string>();

            for (int i = 0; i < TruthTable.NumberOfRow; i++)
            {
                string outerPairTmp = "";
                if (truthResult[i] != 0)
                {
                    string        rowData   = "";
                    List <string> innerPair = new List <string>();
                    for (int j = 0; j < TruthTable.NumberOfColumn; j++)
                    {
                        string variable = GetDisjunctiveFormat(tableValues[j][i], Variables[j]);
                        string tmp      = "";
                        if (innerPair.Count < 2)
                        {
                            innerPair.Add(variable);
                        }

                        if (innerPair.Count == 2)
                        {
                            tmp = $"&({innerPair[0]},{innerPair[1]})";
                            innerPair.Clear();
                            innerPair.Add(tmp);
                        }

                        if (innerPair.Count == 1 && j == TruthTable.NumberOfColumn - 1)
                        {
                            tmp = $"{innerPair[0]}";
                            innerPair.Clear();
                        }

                        rowData = tmp;
                    }

                    if (outerPair.Count < 2)
                    {
                        outerPair.Add(rowData);
                    }

                    if (outerPair.Count == 2)
                    {
                        outerPairTmp = $"|({outerPair[0]},{outerPair[1]})";
                        outerPair.Clear();
                        outerPair.Add(outerPairTmp);
                    }

                    if (outerPair.Count == 1 && i == TruthTable.NumberOfRow - 1)
                    {
                        outerPairTmp = $"{outerPair[0]}";
                        outerPair.Clear();
                    }

                    rowData = outerPairTmp;

                    if (i != TruthTable.NumberOfRow - 1)
                    {
                        result += rowData;
                    }
                    else
                    {
                        result = rowData;
                    }
                }
            }

            return(result);
        }
        private void ShowSimplifyTruthTable()
        {
            try
            {
                //Compute truth value
                TruthTable myTruthTable     = _myTree.TruthTable;
                var        truthTableValues = TruthTableHelper.GetPreComputeValues(_myTree.Variables);
                var        truthTableResult = myTruthTable.ComputeTruthTable();
                var        result           = myTruthTable.ComputeSimplifiedTruthTable();
                var        falseRows        = TruthTableHelper.GetFalseRows(truthTableValues, truthTableResult, _myTree.Variables);

                //Setup DataGridView
                dgvSimplifiedTruthTable.Name = "Simplified Truth Table";
                dgvSimplifiedTruthTable.RowHeadersVisible = false;
                dgvSimplifiedTruthTable.AutoSize          = true;


                if (result != null)
                {
                    dgvSimplifiedTruthTable.ColumnCount = myTruthTable.NumberOfColumn + 1;
                    dgvSimplifiedTruthTable.RowCount    = falseRows.Count + TruthTableHelper.GetNrOfSimplifiedRows(result);

                    for (int i = 0; i < myTruthTable.NumberOfColumn; i++)
                    {
                        dgvSimplifiedTruthTable.Columns[i].Name = myTruthTable.Variables[i].Proposition.ToString();
                    }

                    dgvSimplifiedTruthTable.Columns[dgvSimplifiedTruthTable.ColumnCount - 1].Name = _originalFormula;
                    dgvSimplifiedTruthTable.Rows.Clear();


                    for (int j = 0; j < falseRows.Count; j++)
                    {
                        string[] rows = new string[falseRows[j].Length + 1];
                        falseRows[j].CopyTo(rows, 0);
                        rows[falseRows[j].Length] = "0";
                        dgvSimplifiedTruthTable.Rows.Add(rows);
                    }

                    foreach (var listOfSimplifiedRows in result)
                    {
                        for (int i = 0; i < listOfSimplifiedRows.Count; i++)
                        {
                            string[] rows = new string[listOfSimplifiedRows[i].Length + 1];
                            listOfSimplifiedRows[i].CopyTo(rows, 0);
                            rows[listOfSimplifiedRows[i].Length] = "1";
                            dgvSimplifiedTruthTable.Rows.Add(rows);
                        }
                    }
                }
                else
                {
                    dgvSimplifiedTruthTable.ColumnCount = 1;
                    dgvSimplifiedTruthTable.RowCount    = 1;
                    dgvSimplifiedTruthTable.Rows.Clear();
                    string[] errorMessage = { "Could not simplify the truth table of this formula!!" };
                    dgvSimplifiedTruthTable.Rows.Add(errorMessage);
                }

                foreach (DataGridViewColumn column in dgvSimplifiedTruthTable.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    column.SortMode     = DataGridViewColumnSortMode.NotSortable;
                }

                dgvSimplifiedTruthTable.Columns[dgvSimplifiedTruthTable.ColumnCount - 1].AutoSizeMode =
                    DataGridViewAutoSizeColumnMode.Fill;

                this.Width  = dgvSimplifiedTruthTable.Width + 40;
                this.Height = dgvSimplifiedTruthTable.Height + 60;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
                throw;
            }
        }