public static bool UpdatePropertyType(this SelectionRuleNode node, [Required] string schemaName, [Required] string schemaNamespace, [Required] string oldPropertyTypeName, [Required] string newPropertyTypeName) { bool result = false; if (node is BooleanRuleNode booleanRuleNode) { if (string.CompareOrdinal(booleanRuleNode.Namespace, schemaNamespace) == 0 && string.CompareOrdinal(booleanRuleNode.Schema, schemaName) == 0 && string.CompareOrdinal(booleanRuleNode.Name, oldPropertyTypeName) == 0) { booleanRuleNode.Name = newPropertyTypeName; result = true; } } else if (node is ComparisonRuleNode comparisonRuleNode) { if (string.CompareOrdinal(comparisonRuleNode.Namespace, schemaNamespace) == 0 && string.CompareOrdinal(comparisonRuleNode.Schema, schemaName) == 0 && string.CompareOrdinal(comparisonRuleNode.Name, oldPropertyTypeName) == 0) { comparisonRuleNode.Name = newPropertyTypeName; result = true; } } else if (node is EnumValueRuleNode enumValueRuleNode) { if (string.CompareOrdinal(enumValueRuleNode.Namespace, schemaNamespace) == 0 && string.CompareOrdinal(enumValueRuleNode.Schema, schemaName) == 0 && string.CompareOrdinal(enumValueRuleNode.Name, oldPropertyTypeName) == 0) { enumValueRuleNode.Name = newPropertyTypeName; result = true; } } else if (node is NaryRuleNode naryRuleNode) { var children = naryRuleNode.Children?.ToArray(); if (children?.Any() ?? false) { foreach (var child in children) { result |= UpdatePropertyType(child, schemaName, schemaNamespace, oldPropertyTypeName, newPropertyTypeName); } } } else if (node is UnaryRuleNode unaryRuleNode) { result = UpdatePropertyType(unaryRuleNode.Child, schemaName, schemaNamespace, oldPropertyTypeName, newPropertyTypeName); } return(result); }
public void Merge([NotNull] SelectionRule toBeMerged) { if (toBeMerged?.Root != null) { var or = new OrRuleNode() { Name = "OR" }; or.Children.Add(Root); or.Children.Add(toBeMerged.Root); Root = or; } }
public static IEnumerable <IPropertySchema> Traverse(this IThreatModel model, SelectionRuleNode node) { List <IPropertySchema> list = new List <IPropertySchema>(); if (node is BooleanRuleNode booleanRuleNode) { if (!string.IsNullOrWhiteSpace(booleanRuleNode.Namespace) && !string.IsNullOrWhiteSpace(booleanRuleNode.Schema)) { var schema = model.GetSchema(booleanRuleNode.Schema, booleanRuleNode.Namespace); if (schema != null && !list.Contains(schema)) { list.Add(schema); } } } else if (node is ComparisonRuleNode comparisonRuleNode) { if (!string.IsNullOrWhiteSpace(comparisonRuleNode.Namespace) && !string.IsNullOrWhiteSpace(comparisonRuleNode.Schema)) { var schema = model.GetSchema(comparisonRuleNode.Schema, comparisonRuleNode.Namespace); if (schema != null && !list.Contains(schema)) { list.Add(schema); } } } else if (node is EnumValueRuleNode enumValueRuleNode) { if (!string.IsNullOrWhiteSpace(enumValueRuleNode.Namespace) && !string.IsNullOrWhiteSpace(enumValueRuleNode.Schema)) { var schema = model.GetSchema(enumValueRuleNode.Schema, enumValueRuleNode.Namespace); if (schema != null && !list.Contains(schema)) { list.Add(schema); } } } else if (node is NaryRuleNode naryRuleNode) { var children = naryRuleNode.Children?.ToArray(); if (children?.Any() ?? false) { foreach (var child in children) { var childItems = Traverse(model, child)?.ToArray(); if (childItems?.Any() ?? false) { foreach (var childItem in childItems) { if (childItem != null && !list.Contains(childItem)) { list.Add(childItem); } } } } } } else if (node is UnaryRuleNode unaryRuleNode) { var childItems = Traverse(model, unaryRuleNode.Child); if (childItems?.Any() ?? false) { foreach (var childItem in childItems) { if (childItem != null && !list.Contains(childItem)) { list.Add(childItem); } } } } return(list.Any() ? list : null); }