/// <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> /// Builds an XML document from a decision tree, starting with the root node. /// </summary> /// <param name="writer">file writer</param> /// <param name="rootNode">The root of the tree</param> /// <param name="decisionTreeName">The name of the decision tree</param> /// <param name="myName">Character name</param> /// <returns>An xmldocument with the complete decision tree</returns> public bool BuildAndSaveXmlDocumentFromTree(IXmlFileController writer, IDecisionNode rootNode, string decisionTreeName, string myName) { XmlDocument document = new XmlDocument(); var declaration = document.CreateXmlDeclaration("1.0", "UTF-8", null); document.InsertBefore(declaration, document.DocumentElement); XmlNode decisionTree = document.CreateElement(decisionTreeName); document.AppendChild(decisionTree); XmlNode root = document.CreateElement("Root"); var name = document.CreateAttribute("text"); name.Value = rootNode.ToString().Replace("RNPC.DecisionNodes.", "").Replace("RNPC.DecisionLeaves.", ""); root.Attributes?.Append(name); //If it's not a tree with only a leaf var branchNode = rootNode as AbstractDecisionNode; if (branchNode != null) { if (branchNode.ConfiguredPassFailValue != 0) { var testValue = document.CreateAttribute("test"); testValue.Value = branchNode.ConfiguredPassFailValue.ToString(); root.Attributes?.Append(testValue); } if (!string.IsNullOrEmpty(branchNode.DefaultTreeReaction)) { var defaultReaction = document.CreateAttribute("defaultreaction"); defaultReaction.Value = branchNode.DefaultTreeReaction; decisionTree.Attributes?.Append(defaultReaction); } if (branchNode.LeftNode != null) { root.AppendChild(CreateElementFromNode(branchNode.LeftNode, document, "left")); } if (branchNode.RightNode != null) { root.AppendChild(CreateElementFromNode(branchNode.RightNode, document, "right")); } } decisionTree.AppendChild(root); writer.WriteDecisionTreeToXmlFile(myName, document); return(true); }
/// <summary> /// Takes a node and creates an XML element /// </summary> /// <param name="node"></param> /// <param name="document"></param> /// <param name="decision"></param> /// <returns></returns> private static XmlNode CreateElementFromNode(IDecisionNode node, XmlDocument document, string decision) { string nodeName; var abstractNode = node as AbstractDecisionNode; if (abstractNode != null) { nodeName = "Node"; } else { nodeName = "Leaf"; } XmlNode newNode = document.CreateElement(nodeName); var name = document.CreateAttribute("text"); name.Value = node.ToString().Replace("RNPC.DecisionNodes.", "").Replace("RNPC.DecisionLeaves.", ""); newNode.Attributes?.Append(name); AddDecisionAttribute(document, decision, newNode); AddTestAttribute(node, document, newNode); if (abstractNode == null) { return(newNode); } if (abstractNode.LeftNode != null) { newNode.AppendChild(CreateElementFromNode(abstractNode.LeftNode, document, "left")); } if (abstractNode.RightNode != null) { newNode.AppendChild(CreateElementFromNode(abstractNode.RightNode, document, "right")); } return(newNode); }