protected override Expression VisitBinary(BinaryExpression node) { Visit(node.Left); var left = _expression; Visit(node.Right); var right = _expression; Operator @operator; switch (node.NodeType) { case ExpressionType.And: case ExpressionType.AndAlso: @operator = Operator.And; break; case ExpressionType.Or: case ExpressionType.OrElse: @operator = Operator.Or; break; case ExpressionType.ExclusiveOr: @operator = Operator.OperatorXor; break; default: throw new NotSupportedException(node.ToString()); } _expression = new OperatorExpression(@operator) { Branches = new[]{left,right}}; return node; }
public override bool Equals(LogicExpression paramLogicExpression) { var logicValue = paramLogicExpression as LogicValue; if (logicValue == null) return false; return logicValue.EqualsTrue() == EqualsTrue(); }
public ParsedExpression(String initialText, LogicExpression expression, LogicSyntax logicSyntax, NegationSyntax negationSyntax) { InitialText = initialText; Expression = expression; LogicSyntax = logicSyntax; NegationSyntax = negationSyntax; }
public LogicDerivation(LogicExpression paramLogicExpression) { _steps = new List<LogicStep>(); _next = paramLogicExpression.Clone(); AddStep(paramLogicExpression, "Initial parsed expression"); }
protected override Expression VisitConstant(ConstantExpression node) { if (node.Value is bool) { var value = (bool) node.Value; _expression = new LogicValue(value); } else { throw new NotSupportedException(node.ToString()); } return node; }
public override LogicExpression Clone() { var localLogicBranch = new OperatorExpression(Operator, Negated); var arrayOfLogicExpression = new LogicExpression[_logicBranches.Length]; for (int i = 0; i < _logicBranches.Length; i++) { arrayOfLogicExpression[i] = _logicBranches[i].Clone(); } localLogicBranch.Branches = arrayOfLogicExpression; return localLogicBranch; }
public void CarryOutNonPrimaryOperatorReplacement() { LogicExpression localLogicExpression1 = _next; for (int k = 2; k <= localLogicExpression1.GetDepth(); k++) { int j = 0; LogicExpression localLogicExpression2; while ((localLogicExpression2 = localLogicExpression1.GetSubExpression(k, j++)) != null) { var localLogicBranch = (OperatorExpression) localLogicExpression2; switch (localLogicBranch.Operator) { case Operator.OperatorImplies: LogicHandler.ReplaceIMPLIESOperator(localLogicBranch); j += 2; _next = localLogicExpression1.Clone(); AddStep(localLogicExpression1, "Replaced IMPLIES operator"); localLogicExpression1 = _next; break; case Operator.OperatorBiimplies: LogicHandler.ReplaceBIIMPLIESOperator(localLogicBranch); j++; _next = localLogicExpression1.Clone(); AddStep(localLogicExpression1, "Replaced BIIMPLIES operator"); localLogicExpression1 = _next; break; case Operator.OperatorXor: LogicHandler.ReplaceXOROperator(localLogicBranch); j++; _next = localLogicExpression1.Clone(); AddStep(localLogicExpression1, "Replaced XOR operator"); localLogicExpression1 = _next; break; default: j++; break; } } } }
public bool Idempotency() { int j = Branches.Length; for (var k = 0; k < Branches.Length; k++) { if ((Branches[k] == null) || (!(Branches[k] is ParameterLogicExpression))) continue; var localLogicLeaf1 = (ParameterLogicExpression) Branches[k]; for (int n = k + 1; n < Branches.Length; n++) { if ((Branches[n] == null) || (!(Branches[n] is ParameterLogicExpression))) continue; var localLogicLeaf2 = (ParameterLogicExpression) Branches[n]; if (!localLogicLeaf1.Name.Equals(localLogicLeaf2.Name)) continue; if (Branches[k].Negated != Branches[n].Negated) { switch (Operator) { case Operator.And: Branches[k] = new LogicValue(false); break; case Operator.Or: Branches[k] = new LogicValue(true); break; default: Console.Error.WriteLine("Software Error: Unimplemented operator: " + Operator); break; } } Branches[n] = null; j--; } } if (j == Branches.Length) return false; if (j == 1) { var localObject1 = Parent; if (localObject1 != null) { localObject1.SetBranch(Branches[0], GetPositionInParent()); } } else { var localObject = new LogicExpression[j]; j = 0; foreach (LogicExpression t in Branches.Where(t => t != null)) { localObject[j++] = t; } Branches = localObject; } return true; }
public void SetBranch(LogicExpression paramLogicExpression, int paramInt) { _logicBranches[paramInt] = paramLogicExpression; paramLogicExpression.SetParent(this, paramInt); }
protected override Expression VisitParameter(ParameterExpression node) { if (node.Type != typeof(bool)) throw new NotSupportedException(node.ToString()); _expression = new ParameterLogicExpression(node.Name); return node; }
public virtual bool Equals(LogicExpression paramLogicExpression) { return false; }
public static int Absorbtion(OperatorExpression paramOperatorExpression) { LogicExpression[] arrayOfLogicExpression1 = paramOperatorExpression.Branches; int j = 0; int m; 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; var q = IsAbsorbtion(arrayOfLogicExpression1[k], arrayOfLogicExpression1[m]); switch (q) { case Absorption.AbsorbedRight: arrayOfLogicExpression1[m] = null; j++; break; case Absorption.AbsorbedLeft: arrayOfLogicExpression1[k] = null; j++; break; } } } if (j > 0) { var arrayOfLogicExpression2 = new LogicExpression[arrayOfLogicExpression1.Length - j]; m = 0; foreach (LogicExpression t in arrayOfLogicExpression1.Where(t => t != null)) { arrayOfLogicExpression2[m++] = t; } paramOperatorExpression.Branches = arrayOfLogicExpression2; } return j; }
public void CarryOutAssociativity() { LogicExpression localLogicExpression1 = _next; int i = localLogicExpression1.GetDepth(); for (int k = 3; k <= i; k++) { int j = 0; LogicExpression localLogicExpression2; while ((localLogicExpression2 = localLogicExpression1.GetSubExpression(k, j)) != null) { if (LogicHandler.Associativity((OperatorExpression) localLogicExpression2)) { _next = localLogicExpression1.Clone(); AddStep(localLogicExpression1, "Associativity"); localLogicExpression1 = _next; continue; } j++; } } i = localLogicExpression1.GetDepth(); if (i == 2) { var localLogicBranch = (OperatorExpression) localLogicExpression1; LogicExpression[] arrayOfLogicExpression = localLogicBranch.Branches; if (arrayOfLogicExpression.Length == 1) { localLogicExpression1 = arrayOfLogicExpression[0]; localLogicExpression1.SetParent(null, -1); _next = localLogicExpression1.Clone(); AddStep(localLogicExpression1, "Associativity"); } } }
public static bool Associativity(OperatorExpression paramOperatorExpression) { LogicExpression[] arrayOfLogicExpression1 = paramOperatorExpression.Branches; int i = arrayOfLogicExpression1.OfType<OperatorExpression>() .Where( branch => (branch.Operator == paramOperatorExpression.Operator) && (!branch.Negated)) .Sum(branch => branch.Branches.Length - 1); if (i == 0) { return false; } var arrayOfLogicExpression2 = new LogicExpression[arrayOfLogicExpression1.Length + i]; int k = 0; foreach (LogicExpression t in arrayOfLogicExpression1) { var operatorExpression = t as OperatorExpression; if (operatorExpression != null) { var localLogicBranch2 = operatorExpression; if ((localLogicBranch2.Operator == paramOperatorExpression.Operator) && (!localLogicBranch2.Negated)) { LogicExpression[] arrayOfLogicExpression3 = localLogicBranch2.Branches; foreach (LogicExpression t1 in arrayOfLogicExpression3) arrayOfLogicExpression2[k++] = t1; } else { arrayOfLogicExpression2[k++] = t; } } else { arrayOfLogicExpression2[(k++)] = t; } } paramOperatorExpression.Branches = arrayOfLogicExpression2; return true; }
public void CarryOutBoolValues() { var expression = _next; int i = expression.GetDepth(); for (int m = 2; m <= i; m++) { int k = 0; int j = 0; LogicExpression localLogicExpression; while ((localLogicExpression = expression.GetSubExpression(m, j)) != null) { var localLogicBranch1 = (OperatorExpression) localLogicExpression; BoolResolution n = LogicHandler.GetBoolResolution(localLogicBranch1); if (n == BoolResolution.BoolRemoveBoolValues) { LogicExpression[] arrayOfLogicExpression = localLogicBranch1.Branches; int i1 = arrayOfLogicExpression.Length; foreach (LogicExpression t in arrayOfLogicExpression.Where(t => (t is LogicValue))) { i1--; } if (i1 == 1) { var localObject2 = arrayOfLogicExpression.FirstOrDefault(t => (!(t is LogicValue))); OperatorExpression localLogicBranch2 = localLogicExpression.Parent; if (localLogicBranch2 == null) { expression = localObject2; localObject2.SetParent(null, -1); } else { localLogicBranch2.SetBranch(localObject2, localLogicExpression.GetPositionInParent()); } } else { var expressions = new LogicExpression[i1]; int i4 = 0; foreach (LogicExpression t in arrayOfLogicExpression.Where(t => !(t is LogicValue))) { expressions[i4++] = t; } localLogicBranch1.Branches = expressions; j++; } k = 1; } else if ((n == BoolResolution.BoolResolveTrue) || (n == BoolResolution.BoolResolveFalse)) { bool @bool = n == BoolResolution.BoolResolveTrue; var localLogicValue = new LogicValue(@bool); var localObject2 = localLogicBranch1.Parent; if (localObject2 == null) { localLogicValue.SetParent(null, -1); _next = localLogicValue.Clone(); AddStep(localLogicValue, "Resolved bool values"); break; } localObject2.SetBranch(localLogicValue, localLogicBranch1.GetPositionInParent()); k = 1; } else { j++; } } if (k == 0) continue; _next = expression.Clone(); AddStep(expression, "Removed redundant bool values"); expression = _next; } }
public void CarryOutIdempotency() { LogicExpression localLogicExpression1 = _next; { bool i = false; int j = 0; LogicExpression localLogicExpression2; while ((localLogicExpression2 = localLogicExpression1.GetSubExpression(2, j)) != null) { if (((OperatorExpression)localLogicExpression2).Idempotency()) { i = true; continue; } j++; } if (!i) return; } { if ((localLogicExpression1 is OperatorExpression)) { LogicExpression[] arrayOfLogicExpression = ((OperatorExpression) localLogicExpression1).Branches; if (arrayOfLogicExpression.Length == 1) { localLogicExpression1 = arrayOfLogicExpression[0]; localLogicExpression1.SetParent(null, -1); } } _next = localLogicExpression1.Clone(); AddStep(localLogicExpression1, "Idempotency"); } }
public void CarryOutDistributivity() { LogicExpression localLogicExpression1 = _next; int i = 0; LogicExpression localLogicExpression2; while ((localLogicExpression2 = localLogicExpression1.GetSubExpression(3, i++)) != null) { if (!LogicHandler.Distributivity((OperatorExpression) localLogicExpression2)) continue; _next = localLogicExpression1.Clone(); AddStep(localLogicExpression1, "Distributivity"); localLogicExpression1 = _next; } }
public void CarryOutDeMorgans() { LogicExpression expression = _next; int i = expression.GetDepth(); for (int k = i; k >= 2; k--) { int j = 0; LogicExpression logicExpression; while ((logicExpression = expression.GetSubExpression(k, j)) != null) { if (LogicHandler.DeMorgans((OperatorExpression) logicExpression)) { _next = expression.Clone(); AddStep(expression, "De Morgan's"); expression = _next; continue; } j++; } } }
// EmptyLogicException, DifferentSyntaxesUsedException, UnexpectedSymbolException, InvalidVariableNameException, UnclosedBracketException private static LogicExpression SplitWordsByOperator(String[] paramArrayOfString, int paramInt1, Operator paramInt2, ParserSyntax paramParserSyntax) { if (paramInt1 == 0) throw new UnexpectedSymbolException("Operator at beginning of list of arguments"); if (paramInt1 == paramArrayOfString.Length - 1) { throw new UnexpectedSymbolException("Operator at end of list of arguments"); } int m; if ((paramInt2 == Operator.OperatorXor) || (paramInt2 == Operator.OperatorImplies) || (paramInt2 == Operator.OperatorBiimplies)) { var arrayOfLogicExpression1 = new LogicExpression[2]; var arrayOfString1 = new String[paramInt1]; var arrayOfString2 = new String[paramArrayOfString.Length - paramInt1 - 1]; for (var mm = 0; mm < arrayOfString1.Length; mm++) { arrayOfString1[mm] = paramArrayOfString[mm]; } m = paramInt1 + 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 OperatorExpression(paramInt2) { Branches = arrayOfLogicExpression1 }; return localLogicBranch1; } int i = 2; for (int k = paramInt1 - 1; k >= 0; k--) { var j = GetOperatorValue(paramArrayOfString[k]); if (j == Operator.NonOperator) 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 OperatorExpression(paramInt2) { Branches = arrayOfLogicExpression2 }; return localLogicBranch2; }
private void AddStep(LogicExpression paramLogicExpression, string paramString) { LogicalForm logicalForm = paramLogicExpression.GetLogicalForm(); var localLogicStep = new LogicStep {Expression = paramLogicExpression, Comment = paramString, Form = logicalForm}; _steps.Add(localLogicStep); int j = paramLogicExpression.GetNumberOfElements(); if (logicalForm.HasFlag(LogicalForm.LogicCNF)) { if ((_cnf == null) || (j < _cnfNumberOfElements)) { _cnf = paramLogicExpression; _cnfNumberOfElements = j; } } if (logicalForm.HasFlag(LogicalForm.LogicDNF)) { if ((_dnf == null) || (j < _dnfNumberOfElements)) { _dnf = paramLogicExpression; _dnfNumberOfElements = j; } } }
public static bool Distributivity(OperatorExpression paramOperatorExpression) { Operator @operator = paramOperatorExpression.Operator; LogicExpression[] branches = paramOperatorExpression.Branches; var arrayOfLogicExpression = new LogicExpression[branches.Length][]; int j = 0; int k = 1; foreach (LogicExpression expression in branches) { if (((expression is ParameterLogicExpression)) || ((expression is LogicValue))) { arrayOfLogicExpression[(j++)] = new[] {expression}; } else { arrayOfLogicExpression[j] = ((OperatorExpression) expression).Branches; k *= arrayOfLogicExpression[j].Length; j++; } } if (k*branches.Length == j) { return false; } var arrayOfInt = new int[arrayOfLogicExpression.Length]; arrayOfInt[arrayOfInt.Length - 1] = -1; var expressions = new LogicExpression[k]; for (int n = 0; n < expressions.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) { expressions[n] = arrayOfLogicExpression3[0]; } else { expressions[n] = new OperatorExpression(@operator) { Branches = arrayOfLogicExpression3 }; } } var nn = (Operator) (@operator == 0 ? 1 : 0); paramOperatorExpression.Branches = expressions; paramOperatorExpression.Operator = nn; return true; }
public void CarryOutAbsorbtion() { var localObject = _next; int i = 0; LogicExpression localLogicExpression1; while ((localLogicExpression1 = localObject.GetSubExpression(3, i)) != null) { var localLogicBranch1 = (OperatorExpression) localLogicExpression1; int j = localLogicBranch1.Branches.Length; int k = LogicHandler.Absorbtion(localLogicBranch1); if (k > 0) { if (j - k == 1) { OperatorExpression localLogicBranch2 = localLogicExpression1.Parent; LogicExpression[] arrayOfLogicExpression = localLogicBranch1.Branches; LogicExpression localLogicExpression2 = arrayOfLogicExpression[0]; if (localLogicBranch2 == null) { localObject = localLogicExpression2; localObject.SetParent(null, -1); } else { localLogicBranch2.SetBranch(localLogicExpression2, localLogicBranch1.GetPositionInParent()); } i--; } _next = localObject.Clone(); AddStep(localObject, "Absorbtion"); localObject = _next; continue; } i++; } }
private static Absorption IsAbsorbtion(LogicExpression left, LogicExpression right) { LogicExpression[] arrayOfLogicExpression1; if ((left is OperatorExpression)) { arrayOfLogicExpression1 = ((OperatorExpression) left).Branches; } else { arrayOfLogicExpression1 = new LogicExpression[1]; arrayOfLogicExpression1[0] = left; } LogicExpression[] arrayOfLogicExpression2; if ((right is OperatorExpression)) { arrayOfLogicExpression2 = ((OperatorExpression) right).Branches; } else { arrayOfLogicExpression2 = new LogicExpression[1]; arrayOfLogicExpression2[0] = right; } bool i = arrayOfLogicExpression1.Length < arrayOfLogicExpression2.Length; LogicExpression[] arrayOfLogicExpression3; LogicExpression[] arrayOfLogicExpression4; if (i) { arrayOfLogicExpression3 = arrayOfLogicExpression1; arrayOfLogicExpression4 = arrayOfLogicExpression2; } else { arrayOfLogicExpression3 = arrayOfLogicExpression2; arrayOfLogicExpression4 = arrayOfLogicExpression1; } if (arrayOfLogicExpression3.Any(t => arrayOfLogicExpression4.Any(t.Equals) == false)) { return Absorption.AbsorbtionNotDone; } return (i ? Absorption.AbsorbedRight : Absorption.AbsorbedLeft); }
public override bool Equals(LogicExpression paramLogicExpression) { return ((paramLogicExpression is ParameterLogicExpression)) && (((ParameterLogicExpression) paramLogicExpression).Name.Equals(Name)) && (paramLogicExpression.Negated == Negated); }