/// <summary> /// Verifies that the substitution configured is applicable here /// </summary> /// <param name="nodeToEvaluate">the node to evaluate</param> /// <param name="substitution">the substitution case</param> private static bool EvaluateSubstitution(IDecisionNode nodeToEvaluate, Substition substitution) { if (nodeToEvaluate.ToString().Replace("RNPC.DecisionLeaves.", "").ToLower() != substitution.LeafName.ToLower()) { return(false); } switch (substitution.Condition) { case SubstitionCondition.Default: return(true); case SubstitionCondition.ParentNot: string parentName = nodeToEvaluate.GetParentNode()?.ToString().Replace("RNPC.DecisionNodes.", "").ToLower(); if (!substitution.ConditionName.ToLower().Split(',').Contains(parentName)) { return(true); } return(false); default: return(false); } }
/// <summary> /// Recursive method that iterates through the tree and finds nodes that are subject to substitution /// </summary> /// <param name="currentNode">node to start with</param> /// <param name="substitution">substitution to do</param> /// <param name="nodeToReplace">The node will be substituted</param> private static bool FindMatchingNodes(IDecisionNode currentNode, Substition substitution, IDecisionNode nodeToReplace) { var abstractNode = (AbstractDecisionNode)currentNode; if (abstractNode.LeftNode != null) { // ReSharper disable once UsePatternMatching var leftNode = abstractNode.LeftNode as AbstractDecisionNode; if (leftNode != null) { if (FindMatchingNodes(leftNode, substitution, nodeToReplace)) { return(true); } } else { if (EvaluateSubstitution(abstractNode.LeftNode, substitution)) { abstractNode.LeftNode = nodeToReplace; return(true); } } } if (abstractNode.RightNode == null) { return(false); } // ReSharper disable once UsePatternMatching var rightNode = abstractNode.RightNode as AbstractDecisionNode; if (rightNode != null) { if (FindMatchingNodes(rightNode, substitution, nodeToReplace)) { return(true); } } if (!EvaluateSubstitution(abstractNode.RightNode, substitution)) { return(false); } abstractNode.RightNode = nodeToReplace; return(true); }
public List <Substition> ConvertDocumentToList(XmlDocument document) { var substitions = new List <Substition>(); if (document?.DocumentElement?.ChildNodes == null) { return(substitions); } foreach (XmlNode node in document.DocumentElement.ChildNodes) { if (node == null) { continue; } // ReSharper disable once PossibleNullReferenceException string leaf = node.Attributes["leaf"].Value; string subtree = node.Attributes["subtree"].Value; string conditionName = node.Attributes["conditionname"].Value; SubstitionCondition condition; switch (node.Attributes["condition"].Value.ToLower()) { case "parentnot": condition = SubstitionCondition.ParentNot; break; default: condition = SubstitionCondition.Default; break; } var newSubstitutionRule = new Substition { LeafName = leaf, SubTreeName = subtree, Condition = condition, ConditionName = conditionName }; substitions.Add(newSubstitutionRule); } return(substitions); }
/// <summary> /// Method that calls the recursive method that finds nodes that are subject to substitution /// </summary> /// <param name="firstNode">node to start with</param> /// <param name="substitution">substitution to do</param> /// <param name="replacementNode"></param> private static bool FindAndReplaceMatchingNode(IDecisionNode firstNode, Substition substitution, IDecisionNode replacementNode) { //A decision tree could conceivably be only a leaf //That would be an extremely simple decision process //an subject to evolving var abstractNode = firstNode as AbstractDecisionNode; if (abstractNode == null) { if (!EvaluateSubstitution(firstNode, substitution)) { return(false); } firstNode = replacementNode; return(true); } return(FindMatchingNodes(firstNode, substitution, replacementNode)); }
private void TranslateHandler(bool cr) { try { ICrypt crypt; switch (ComboBox1.SelectedIndex.ToString()) { case "0": crypt = new Substition(Text_Box1.Text, _alphabet, Text_Box4.Text); break; case "1": if (_alphabet == null) { throw new NullReferenceException(); } crypt = new Caesar(Text_Box1.Text, Convert.ToInt32(Text_box3.Text), _alphabet); break; case "2": CheckXOR(Text_Box1.Text, Text_box3.Text); crypt = new XOR(Text_Box1.Text, Text_box3.Text); break; case "3": CheckReshuff(Convert.ToInt32(Text_box3.Text), Text_Box4.Text.Split('-').ToList()); crypt = new Permutation(Text_Box1.Text, Convert.ToInt32(Text_box3.Text), Text_Box4.Text); break; case "4": CheckReshuff(Convert.ToInt32(Text_box3.Text), Text_Box4.Text.Split('-').ToList()); crypt = new PermutationHard(Text_Box1.Text, Convert.ToInt32(Text_box3.Text), Text_Box4.Text); break; default: throw new InvalidDataException("Не выбран алгоритм шифрования!"); } if (cr) { Text_Box2.Text = crypt.Encryption(); } else { Text_Box2.Text = crypt.Decryption(); } } catch (ArgumentException ex) { MessageBox.Show("Неверный алфавит шифрования", "Error"); File.AppendAllText("../../../Error/StackTrace.txt", ex.StackTrace); } catch (NullReferenceException ex) { MessageBox.Show("Не выбран алфавит шифрования", "Error"); File.AppendAllText("../../../Error/StackTrace.txt", ex.StackTrace); } catch (InvalidDataException ex) { MessageBox.Show(ex.Message, "Error"); File.AppendAllText("../../../Error/StackTrace.txt", ex.StackTrace); } catch (FormatException ex) { MessageBox.Show("Не указан сдвиг", "Error"); File.AppendAllText("../../../Error/StackTrace.txt", ex.StackTrace); } catch (KeyNotFoundException ex) { MessageBox.Show("Исходный алфавит не содержит символов шифруемого текста", "Error"); File.AppendAllText("../../../Error/StackTrace.txt", ex.StackTrace); } }