Exemple #1
0
        public static bool?[] Solve(Expression expression, int variableCount)
        {
            // Allocate space for the variables and solutions.
            var variables = new bool?[variableCount];
            var validSolutions = new List<bool?[]>();

            // Determine the number of permutations that need to happen.
            var count = 1 << variableCount;

            // We will use an integer to do the bit permutations.
            for (int current = 0; current < count; current++)
            {
                // Copy the bits of the integer `current` into the variables array.
                for (int bit = 0; bit < variableCount; bit++)
                {
                    variables[bit] = (current & (1 << bit)) > 0;
                }

                // Evaluate the expression with the current variables.
                var result = expression.Evaluate(variables);

                // If the result is `true`, this is a valid solution.
                if (result == true)
                {
                    // Copy the variables into the list of valid solutions.
                    var varCopy = new bool?[variableCount];
                    Array.Copy(variables, varCopy, variableCount);
                    validSolutions.Add(varCopy);
                }
            }

            // If there are no valid solutions, there must be a contradiction.
            if (validSolutions.Count == 0)
            {
                return null;
            }

            // Allocate space for the results.
            var results = new bool?[variableCount];

            // Copy the first valid solution into the results array.
            Array.Copy(validSolutions[0], results, variableCount);

            // Compare the current results with each of the subsequent solutions.
            foreach (var solution in validSolutions.Skip(1))
            {
                // Compare each variable.
                for (int var = 0; var < variableCount; var++)
                {
                    // If the results of the solution is different from the current result, mark it as undetermined.
                    if (results[var] != solution[var])
                    {
                        results[var] = null;
                    }
                }
            }

            return results;
        }
Exemple #2
0
        public NotExpression(Expression operand)
        {
            if (operand == null)
            {
                throw new ArgumentNullException("operand");
            }

            this.operand = operand;
        }
Exemple #3
0
        protected BinaryExpression(Expression left, Expression right)
        {
            if (left == null)
            {
                throw new ArgumentNullException("left");
            }

            this.left = left;

            if (right == null)
            {
                throw new ArgumentNullException("right");
            }

            this.right = right;
        }
Exemple #4
0
        private bool TryCompile(out Expression expression)
        {
            expression = null;

            var parser = this.game.Parser;
            var compiler = this.game.Compiler;
            var nameTable = new List<string> { "A", "B", "C", "D" };

            foreach (var premise in this.proof)
            {
                if (premise.Count == 0)
                {
                    continue;
                }

                try
                {
                    var parseTree = parser.Parse(string.Join(string.Empty, from c in premise
                                                                           select c.Symbol));
                    compiler.Compile(parseTree, nameTable);
                }
                catch (ParseException)
                {
                    return false;
                }
                catch (CompileException)
                {
                    return false;
                }
            }

            return true;
        }
Exemple #5
0
 public Expression Create(Expression left, Expression right)
 {
     return new AndExpression(left, right);
 }
Exemple #6
0
 public OrExpression(Expression left, Expression right)
     : base(left, right)
 {
 }