Example #1
0
        public static LogicDerivation ManipulateLogic(String paramString, LogicExpression paramLogicExpression)
        {
            var localLogicDerivation = new LogicDerivation(paramString, paramLogicExpression);

            if (localLogicDerivation.CNFAndDNF)
            {
                return localLogicDerivation;
            }

            CarryOutNonPrimaryOperatorReplacement(localLogicDerivation);
            CarryOutBoolValues(localLogicDerivation);

            if (localLogicDerivation.CNFAndDNF)
            {
                return localLogicDerivation;
            }
            CarryOutAssociativity(localLogicDerivation);
            CarryOutDeMorgans(localLogicDerivation);
            CarryOutAssociativity(localLogicDerivation);
            CarryOutIdempotency(localLogicDerivation);
            CarryOutBoolValues(localLogicDerivation);
            CarryOutAbsorbtion(localLogicDerivation);

            do
            {
                CarryOutDistributivity(localLogicDerivation);
                CarryOutAssociativity(localLogicDerivation);
                CarryOutIdempotency(localLogicDerivation);
                CarryOutBoolValues(localLogicDerivation);
                CarryOutAbsorbtion(localLogicDerivation);
            } while (!localLogicDerivation.CNFAndDNF);
            return localLogicDerivation;
        }
Example #2
0
 public ParsedExpression(String paramString, LogicExpression paramLogicExpression, LogicSyntax paramInt1, NegationSyntax paramInt2)
 {
     InitialText = paramString;
     Expression = paramLogicExpression;
     LogicSyntax = paramInt1;
     NegationSyntax = paramInt2;
 }
Example #3
0
        public LogicDerivation(String paramString, LogicExpression paramLogicExpression)
        {
            _initialText = paramString;

            _steps = new List<LogicStep>();

            AddStep(paramLogicExpression, "Initial parsed expression");
        }
Example #4
0
        public override LogicExpression CloneLogic()
        {
            var localLogicBranch = new LogicBranch(Operator, Negated);

            var arrayOfLogicExpression = new LogicExpression[_logicBranches.Length];

            for (int i = 0; i < _logicBranches.Length; i++)
            {
                arrayOfLogicExpression[i] = _logicBranches[i].CloneLogic();
            }
            localLogicBranch.Branches = arrayOfLogicExpression;
            return localLogicBranch;
        }
Example #5
0
        public void AddStep(LogicExpression paramLogicExpression, String paramString)
        {
            var i = paramLogicExpression.GetLogicalForm();

            Next = paramLogicExpression.CloneLogic();

            var localLogicStep = new LogicStep
                                     {
                                         Expression = paramLogicExpression,
                                         Comment = paramString,
                                         Form = i
                                     };

            _steps.Add(localLogicStep);

            int j = paramLogicExpression.GetNumberOfElements();

            switch (i)
            {
                case Form.CNF:
                    if ((CNF == null) || (j < _cnfNumberOfElements))
                    {
                        CNF = paramLogicExpression;
                        _cnfNumberOfElements = j;
                    }
                    break;
                case Form.DNF:
                    if ((DNF == null) || (j < _dnfNumberOfElements))
                    {
                        DNF = paramLogicExpression;
                        _dnfNumberOfElements = j;
                    }
                    break;
                case Form.CNF_DNF:
                    if ((CNF == null) || (j < _cnfNumberOfElements))
                    {
                        CNF = paramLogicExpression;
                        _cnfNumberOfElements = j;
                    }
                    if ((DNF == null) || (j < _dnfNumberOfElements))
                    {
                        DNF = paramLogicExpression;
                        _dnfNumberOfElements = j;
                    }
                    break;
            }
        }
Example #6
0
        private static int IsAbsorbtion(LogicExpression left, LogicExpression right)
        {
            LogicExpression[] leftArray = left is LogicBranch ? ((LogicBranch) left).Branches : new[] {left};

            LogicExpression[] rightArray = right is LogicBranch ? ((LogicBranch) right).Branches : new[] {right};

            int i = leftArray.Length < rightArray.Length ? 1 : 0;

            LogicExpression[] minArray;
            LogicExpression[] maxArray;

            if (leftArray.Length < rightArray.Length)
            {
                minArray = leftArray;
                maxArray = rightArray;
            }
            else
            {
                minArray = rightArray;
                maxArray = leftArray;
            }

            if (minArray.Any(t1 => !maxArray.Any(t1.Equals)))
            {
                return 20;
            }
            return i != 0 ? 21 : 22;
        }
Example #7
0
        private static bool idempotency(LogicBranch paramLogicBranch)
        {
            var i = paramLogicBranch.Operator;

            LogicExpression[] arrayOfLogicExpression = (LogicExpression[]) paramLogicBranch.Branches;

            int j = arrayOfLogicExpression.Length;

            for (int k = 0; k < arrayOfLogicExpression.Length; k++)
            {
                if ((arrayOfLogicExpression[k] == null) || (!(arrayOfLogicExpression[k] is LogicLeaf)))
                    continue;
                var localLogicLeaf1 = (LogicLeaf) arrayOfLogicExpression[k];

                for (int n = k + 1; n < arrayOfLogicExpression.Length; n++)
                {
                    if ((arrayOfLogicExpression[n] == null) || (!(arrayOfLogicExpression[n] is LogicLeaf)))
                        continue;
                    var localLogicLeaf2 = (LogicLeaf) arrayOfLogicExpression[n];

                    if (!localLogicLeaf1.Name.Equals(localLogicLeaf2.Name))
                        continue;
                    if (arrayOfLogicExpression[k].Negated != arrayOfLogicExpression[n].Negated)
                    {
                        switch (i)
                        {
                            case Operator.And:
                                arrayOfLogicExpression[k] = new LogicValue(false);
                                break;
                            case Operator.Or:
                                arrayOfLogicExpression[k] = new LogicValue(true);
                                break;
                            default:
                                Console.Error.WriteLine("Software Error: Unimplemented operator: " + i);
                                break;
                        }
                    }

                    arrayOfLogicExpression[n] = null;
                    j--;
                }
            }

            if (j == arrayOfLogicExpression.Length)
                return false;
            if (j == 1)
            {
                var localObject1 = paramLogicBranch.Parent;

                if (localObject1 != null)
                {
                    (localObject1).SetBranch(arrayOfLogicExpression[0], paramLogicBranch.PositionInParent);
                }
                return true;
            }

            var localObject = new LogicExpression[j];
            j = 0;

            foreach (LogicExpression t in arrayOfLogicExpression.Where(t => t != null))
            {
                localObject[j++] = t;
            }
            paramLogicBranch.Branches = localObject;

            return true;
        }
Example #8
0
        private static bool Distributivity(LogicBranch paramLogicBranch)
        {
            var i = paramLogicBranch.Operator;

            LogicExpression[] arrayOfLogicExpression1 = paramLogicBranch.Branches;
            var arrayOfLogicExpression = new LogicExpression[arrayOfLogicExpression1.Length][];

            int j = 0;
            int k = 1;
            LogicExpression[] arrayOfLogicExpression2;
            foreach (LogicExpression t in arrayOfLogicExpression1)
            {
                if ((t is LogicLeaf) || (t is LogicValue))
                {
                    arrayOfLogicExpression2 = new[] {t};
                    arrayOfLogicExpression[(j++)] = arrayOfLogicExpression2;
                }
                else
                {
                    arrayOfLogicExpression[j] = ((LogicBranch) t).Branches;
                    k *= arrayOfLogicExpression[j].Length;
                    j++;
                }
            }

            if (k*arrayOfLogicExpression1.Length == j)
            {
                return false;
            }
            var arrayOfInt = new int[arrayOfLogicExpression.Length];
            arrayOfInt[(arrayOfInt.Length - 1)] = -1;
            arrayOfLogicExpression2 = new LogicExpression[k];

            int n;
            for (n = 0; n < arrayOfLogicExpression2.Length; n++)
            {
                arrayOfInt[(arrayOfInt.Length - 1)] += 1;

                for (int i1 = arrayOfInt.Length - 1; i1 > 0; i1--)
                {
                    if (arrayOfInt[i1] != arrayOfLogicExpression[i1].Length)
                        break;
                    arrayOfInt[i1] = 0;
                    arrayOfInt[(i1 - 1)] += 1;
                }

                var arrayOfLogicExpression3 = new LogicExpression[arrayOfLogicExpression.Length];

                for (int i2 = 0; i2 < arrayOfLogicExpression3.Length; i2++)
                {
                    arrayOfLogicExpression3[i2] = arrayOfLogicExpression[i2][arrayOfInt[i2]];
                }
                if (arrayOfLogicExpression3.Length == 1)
                {
                    arrayOfLogicExpression2[n] = arrayOfLogicExpression3[0];
                }
                else
                {
                    var localLogicBranch = new LogicBranch(i, false)
                                               {
                                                   Branches = arrayOfLogicExpression3
                                               };
                    arrayOfLogicExpression2[n] = localLogicBranch;
                }
            }

            var nn = i == Operator.And ? Operator.Or : Operator.And;

            paramLogicBranch.Branches = arrayOfLogicExpression2;
            paramLogicBranch.Operator = nn;

            return true;
        }
Example #9
0
        private static void CarryOutBoolValues(LogicDerivation paramLogicDerivation)
        {
            var logicExpression = paramLogicDerivation.Next;

            int i = logicExpression.GetDepth();

            for (int m = 2; m <= i; m++)
            {
                int k = 0;
                int j = 0;
                LogicExpression localLogicExpression;
                while ((localLogicExpression = logicExpression.GetSubExpression(m, j)) != null)
                {
                    var localLogicBranch1 = (LogicBranch) localLogicExpression;
                    int n = GetBoolResolution(localLogicBranch1);
                    if (n == 31)
                    {
                        LogicExpression[] arrayOfLogicExpression = localLogicBranch1.Branches;
                        int i1 = arrayOfLogicExpression.Length;

                        i1 -= arrayOfLogicExpression.Count(t => (t is LogicValue));

                        if (i1 == 1)
                        {
                            LogicExpression localObject2 = arrayOfLogicExpression.FirstOrDefault(t => (!(t is LogicValue)));

                            LogicBranch localLogicBranch2 = localLogicExpression.Parent;

                            if (localLogicBranch2 == null)
                            {
                                logicExpression = localObject2;
                                localObject2.SetParent(null, -1);
                            }
                            else
                            {
                                localLogicBranch2.SetBranch(localObject2, localLogicExpression.PositionInParent);
                            }
                        }
                        else
                        {
                            var localObject22 = new LogicExpression[i1];

                            int i4 = 0;

                            foreach (LogicExpression t in arrayOfLogicExpression)
                            {
                                if (!(t is LogicValue))
                                {
                                    localObject22[(i4++)] = t;
                                }
                            }
                            localLogicBranch1.Branches = localObject22;

                            j++;
                        }

                        k = 1;
                    }
                    else if ((n == 32) || (n == 33))
                    {
                        bool @bool = n == 32;
                        var localLogicValue = new LogicValue(@bool);

                        var localObject2 = localLogicBranch1.Parent;

                        if (localObject2 == null)
                        {
                            localLogicValue.SetParent(null, -1);
                            paramLogicDerivation.AddStep(localLogicValue, "Resolved bool values");
                            break;
                        }

                        (localObject2).SetBranch(localLogicValue, localLogicBranch1.PositionInParent);

                        k = 1;
                    }
                    else
                    {
                        j++;
                    }
                }

                if (k == 0)
                    continue;
                paramLogicDerivation.AddStep(logicExpression, "Removed redundant bool values");
                logicExpression = paramLogicDerivation.Next;
            }
        }
Example #10
0
 public override bool Equals(LogicExpression paramLogicExpression)
 {
     return ((paramLogicExpression is LogicValue)) &&
            (((LogicValue) paramLogicExpression).EqualsTrue() == EqualsTrue());
 }
Example #11
0
        private static int Absorbtion(LogicBranch paramLogicBranch)
        {
            LogicExpression[] arrayOfLogicExpression1 = paramLogicBranch.Branches;

            int j = 0;
            int m;
            int n;
            for (int k = 0; k < arrayOfLogicExpression1.Length - 1; k++)
            {
                for (m = k + 1; (m < arrayOfLogicExpression1.Length) && (arrayOfLogicExpression1[k] != null); m++)
                {
                    if (arrayOfLogicExpression1[m] == null)
                        continue;
                    n = IsAbsorbtion(arrayOfLogicExpression1[k], arrayOfLogicExpression1[m]);
                    switch (n)
                    {
                        case 21:
                            arrayOfLogicExpression1[m] = null;
                            j++;
                            break;
                        case 22:
                            arrayOfLogicExpression1[k] = null;
                            j++;
                            break;
                    }
                }
            }

            if (j > 0)
            {
                var arrayOfLogicExpression2 = new LogicExpression[arrayOfLogicExpression1.Length - j];

                m = 0;

                for (n = 0; n < arrayOfLogicExpression1.Length; n++)
                {
                    if (arrayOfLogicExpression1[n] != null)
                    {
                        arrayOfLogicExpression2[(m++)] = arrayOfLogicExpression1[n];
                    }
                }
                paramLogicBranch.Branches = arrayOfLogicExpression2;
            }

            return j;
        }
Example #12
0
 public void SetBranch(LogicExpression paramLogicExpression, int position)
 {
     _logicBranches[position] = paramLogicExpression;
     paramLogicExpression.SetParent(this, position);
 }
Example #13
0
        private static LogicExpression SplitWordsByOperator(String[] paramArrayOfString, int position, Operator paramInt2,
            ParserSyntax paramParserSyntax)
        {
            int m;
            if (position == 0)
                throw new UnexpectedSymbolException("Operator at beginning of list of arguments");
            if (position == paramArrayOfString.Length - 1)
            {
                throw new UnexpectedSymbolException("Operator at end of list of arguments");
            }
            if ((paramInt2 == Operator.Implies) || (paramInt2 == Operator.Biimplies) || (paramInt2 == Operator.Xor))
            {
                var arrayOfLogicExpression1 = new LogicExpression[2];

                var arrayOfString1 = new String[position];
                var arrayOfString2 = new String[paramArrayOfString.Length - position - 1];
                for (m = 0; m < arrayOfString1.Length; m++)
                {
                    arrayOfString1[m] = paramArrayOfString[m];
                }
                m = position + 1;

                for (int n = 0; n < arrayOfString2.Length; n++)
                {
                    arrayOfString2[n] = paramArrayOfString[(m++)];
                }
                arrayOfLogicExpression1[0] = ParseWords(arrayOfString1, paramParserSyntax);
                arrayOfLogicExpression1[1] = ParseWords(arrayOfString2, paramParserSyntax);

                var localLogicBranch1 = new LogicBranch(paramInt2, false) {Branches = arrayOfLogicExpression1};
                return localLogicBranch1;
            }

            int i = 2;
            for (int k = position - 1; k >= 0; k--)
            {
                Operator j = GetOperatorValue(paramArrayOfString[k]);

                if (j == Operator.None)
                    continue;
                if (j != paramInt2)
                {
                    break;
                }
                paramParserSyntax.UpdateLogicSyntax(GetLogicSyntax(paramArrayOfString[k]));

                i++;
            }

            var arrayOfLogicExpression2 = new LogicExpression[i];

            m = paramArrayOfString.Length - 1;
            for (int i2 = arrayOfLogicExpression2.Length - 1; i2 >= 0; i2--)
            {
                int i1 = 0;

                int i3;
                for (i3 = m; i3 >= 0; i3--)
                {
                    var j = GetOperatorValue(paramArrayOfString[i3]);

                    if (j != paramInt2)
                        i1++;
                    else if (j == paramInt2)
                    {
                        break;
                    }
                }
                var arrayOfString3 = new String[i1];

                i3 = m - i1;
                int i4 = i1 - 1;

                for (int i5 = m; i5 > i3; i5--)
                {
                    arrayOfString3[(i4--)] = paramArrayOfString[i5];
                }

                m = m - i1 - 1;

                arrayOfLogicExpression2[i2] = ParseWords(arrayOfString3, paramParserSyntax);
            }

            var localLogicBranch2 = new LogicBranch(paramInt2, false) {Branches = arrayOfLogicExpression2};
            return localLogicBranch2;
        }
Example #14
0
 public ParserBlock(LogicExpression paramLogicExpression, LogicSyntax paramInt1, NegationSyntax paramInt2)
 {
     Expression = paramLogicExpression;
     LogicSyntax = paramInt1;
     NegationSyntax = paramInt2;
 }
Example #15
0
        private static bool associativity(LogicBranch paramLogicBranch)
        {
            LogicExpression[] arrayOfLogicExpression1 = paramLogicBranch.Branches;

            int i = 0;

            for (int j = 0; j < arrayOfLogicExpression1.Length; j++)
            {
                if (!(arrayOfLogicExpression1[j] is LogicBranch))
                    continue;
                var localLogicBranch1 = (LogicBranch) arrayOfLogicExpression1[j];

                if ((localLogicBranch1.Operator != paramLogicBranch.Operator) ||
                    localLogicBranch1.Negated)
                {
                    continue;
                }
                i += localLogicBranch1.Branches.Length - 1;
            }

            if (i == 0)
            {
                return false;
            }

            var arrayOfLogicExpression2 = new LogicExpression[arrayOfLogicExpression1.Length + i];

            int k = 0;

            for (int m = 0; m < arrayOfLogicExpression1.Length; m++)
            {
                if ((arrayOfLogicExpression1[m] is LogicBranch))
                {
                    var localLogicBranch2 = (LogicBranch) arrayOfLogicExpression1[m];

                    if ((localLogicBranch2.Operator == paramLogicBranch.Operator) &&
                        (!localLogicBranch2.Negated))
                    {
                        LogicExpression[] arrayOfLogicExpression3 = localLogicBranch2.Branches;

                        for (int n = 0; n < arrayOfLogicExpression3.Length; n++)
                            arrayOfLogicExpression2[(k++)] = arrayOfLogicExpression3[n];
                    }
                    else
                    {
                        arrayOfLogicExpression2[(k++)] = arrayOfLogicExpression1[m];
                    }
                }
                else
                {
                    arrayOfLogicExpression2[(k++)] = arrayOfLogicExpression1[m];
                }
            }
            paramLogicBranch.Branches = arrayOfLogicExpression2;

            return true;
        }
Example #16
0
 public override bool Equals(LogicExpression paramLogicExpression)
 {
     return ((paramLogicExpression is LogicLeaf)) && ((LogicLeaf) paramLogicExpression).Name.Equals(Name) &&
            (paramLogicExpression.Negated == Negated);
 }
Example #17
0
 public virtual bool Equals(LogicExpression paramLogicExpression)
 {
     return false;
 }