public static Rule LoadXml(XElement X) { List <Expression> t; var IfNode = X.Descendants("If").First(); if (IfNode.Elements() != null && IfNode.Elements().Count() > 0) { t = (from x in X.Descendants("If").First().Elements() select Expression.LoadXml(x)).ToList(); } else { t = new List <Expression>(); } // var t = (t1 == null || t1.Count() == 0) ? new List<Expression>() : t1.ToList(); if (IfNode.Attribute("Text") != null) { t.Insert(0, Expression.ParseString(IfNode.Attribute("Text").Value)); } Expression _if; if (t.Count == 1) { _if = t[0]; } else { _if = new ExpressionAnd(t); } var s = (from x in X.Descendants("Then").First().Elements() select Action.LoadXml(x)).ToList(); Action _then; if (s.Count == 1) { _then = s[0]; } else { _then = new CombinedAction(s); } var R = new Rule(_if, _then); if (X.Attribute("Priority") != null) { R.Priority = int.Parse(X.Attribute("Priority").Value); } if (X.Attribute("RuleSet") != null) { R.RuleSet = X.Attribute("RuleSet").Value; } return(R); }
public static Action ParseActionSequence(string actionsSequence) { // ReSharper disable once RedundantAssignment Action action = null; action = ParseAtomicAction(actionsSequence); if (action == null) { int firstOpeningBracePosition = actionsSequence.IndexOf("("); int[] closingBracesPositions = BracketedConfigProcessor.AllIndexesOf(actionsSequence, ")"); if (firstOpeningBracePosition == -1 || closingBracesPositions.Length != 0) { if (closingBracesPositions.Length > 0) { for (int tryingClosingBraceIndex = 0; tryingClosingBraceIndex != closingBracesPositions.Length; tryingClosingBraceIndex++) { int possibleExpression1Start = firstOpeningBracePosition + 1; int possibleAction1End = closingBracesPositions[tryingClosingBraceIndex]; int possibleExpression1Length = possibleAction1End - possibleExpression1Start; Action action1 = null; if (possibleExpression1Length > 0) { string possibleAction1Substring = actionsSequence.Substring(possibleExpression1Start, possibleExpression1Length); action1 = ParseActionSequence(possibleAction1Substring); } if (action1 != null) { string restOfString = actionsSequence.Substring(possibleAction1End); if (!Regex.IsMatch(restOfString, @"^\s*\)\s*$")) { if (Regex.IsMatch(restOfString, @"^\s*\)\s*(and|AND).+$")) { string possibleAction2Substring = restOfString.Substring(restOfString.IndexOf("and") + "and".Length); Action action2 = ParseActionSequence(possibleAction2Substring); if (action2 != null) { action = new CombinedAction(new List <Action> { action1 }); if (action2 is OneOf) { ((CombinedAction)action).Actions.Add(action2); } else if (action2 is CombinedAction) { ((CombinedAction)action).Actions.AddRange(((CombinedAction)action2).Actions); } else { ((CombinedAction)action).Actions.Add(action2); } } } else if (Regex.IsMatch(restOfString, @"^\s*\)\s*(or|OR).+$")) { string possibleAction2Substring = restOfString.Substring(restOfString.IndexOf("or") + "or".Length); Action action2 = ParseActionSequence(possibleAction2Substring); if (action2 != null) { action = new OneOf(new List <Action> { action1 }); if (action2 is OneOf action2AsOneOf) { ((OneOf)action).Actions.AddRange(action2AsOneOf.Actions); } else { ((OneOf)action).Actions.Add(action2); } } } } else { action = action1; } if (action != null) { break; } } } } } } return(action); }
public static Action ParseActionSequence(string actionsSequence) { Action action = null; try { action = ParseAtomicAction(actionsSequence); return(action); } catch { int firstOpeningBracePosition = actionsSequence.IndexOf("("); int[] closingBracesPositions = BracketedConfigProcessor.AllIndexesOf(actionsSequence, ")"); if (firstOpeningBracePosition != -1 && closingBracesPositions.Length == 0) { throw new ActionParseException(); } if (closingBracesPositions.Length > 0) { for (int tryingClosingBraceIndex = 0; tryingClosingBraceIndex != closingBracesPositions.Length; tryingClosingBraceIndex++) { try { int possibleExpression1Start = firstOpeningBracePosition + 1; int possibleAction1End = closingBracesPositions[tryingClosingBraceIndex]; int possibleExpression1Length = possibleAction1End - possibleExpression1Start; string possibleAction1Substring = actionsSequence.Substring(possibleExpression1Start, possibleExpression1Length); Action action1 = ParseActionSequence(possibleAction1Substring); string restOfString = actionsSequence.Substring(possibleAction1End); if (!Regex.IsMatch(restOfString, @"^\s*\)\s*$")) { if (Regex.IsMatch(restOfString, @"^\s*\)\s*(and|AND).+$")) { string possibleAction2Substring = restOfString.Substring( restOfString.IndexOf("and") + "and".Length); Action action2 = ParseActionSequence(possibleAction2Substring); if (action1 == null || action2 == null) { throw new ActionParseException(); } action = new CombinedAction( new List <Action> { action1, action2 }); } else if (Regex.IsMatch(restOfString, @"^\s*\)\s*(or|OR).+$")) { string possibleAction2Substring = restOfString.Substring( restOfString.IndexOf("or") + "or".Length); Action action2 = ParseActionSequence(possibleAction2Substring); if (action1 == null || action2 == null) { throw new ActionParseException(); } action = new OneOf(new List <Action> { action1, action2 }); } else { throw new ActionParseException(); } break; } else { action = action1; } break; } catch { } } if (action == null) { throw new ActionParseException(); } } return(action); } }