public Generator(BinaryGraph tree) { if (tree == null) { return; } _tree = tree; _countTreeNodes(_tree); //BinaryNode currentNode = _tree.Root; Stack <string> conditionsStack = new Stack <string>(); Stack <BinaryNode> traverseStack = new Stack <BinaryNode>(); traverseStack.Push(_tree.Root); //int counter = 1; while (traverseStack.Any()) { BinaryNode currentNode = traverseStack.Pop(); _addChildrenAvoidEndpoinDoubleAdding(ref traverseStack, currentNode); if (currentNode.Type == NodeType.Expression) { //String.Join(" & ", conditionsStack); char[] trimChars = { ' ', '&' }; //TODO: use Smv.OrTrimChars & Smv.AndTrimChars //_outputLines.Add(new OutputLine(currentNode.Counter, currentNode.)); string[] varval = currentNode.Value.Split(new string[] { ":=" }, StringSplitOptions.RemoveEmptyEntries); string condition = conditionsStack.Aggregate("", (current, s) => current + ("(" + s + ") & ")).TrimEnd(trimChars); if (conditionsStack.Count > 1) { condition = String.Format("({0})", condition); } _outputLines.Add(new OutputLine(currentNode.Counter, varval[0].Trim(), condition, varval[1].Trim())); if (_conditionEndPointLeftBranchChild(currentNode)) { string lastCondition = conditionsStack.Pop(); conditionsStack.Push(String.Format("!({0})", lastCondition)); } } else if (currentNode.Type == NodeType.Condition || currentNode.Type == NodeType.ConditionAlternativeElsif) { conditionsStack.Push(currentNode.Value); } else if (currentNode.Type == NodeType.ConditionEndPoint) { conditionsStack.Pop(); } else { throw new Exception("Unsupported node type during instruction enumeration"); } } //_outputLines.Sort((a, b) => String.CompareOrdinal(a.Variable, b.Variable)); //Sort by variable name }
private void _countTreeNodes(BinaryGraph tree) { Stack <BinaryNode> traverseStack = new Stack <BinaryNode>(); List <BinaryNode> allGraphNodes = new List <BinaryNode>(); traverseStack.Push(_tree.Root); while (traverseStack.Any()) { BinaryNode currentNode = traverseStack.Pop(); allGraphNodes.Add(currentNode); _addChildrenAvoidEndpoinDoubleAdding(ref traverseStack, currentNode); } //_tree.Root.Counter = 1; if (_tree.Root.Type == NodeType.Condition) { _tree.Root.Counter = 0; } else if (_tree.Root.Type == NodeType.Expression) { _tree.Root.Counter = 1; } else { throw new Exception("Invalid first operator"); } foreach (BinaryNode node in allGraphNodes) { if (node.LeftAncestor != null) { if (node.Type == NodeType.Expression) { node.Counter = node.LeftAncestor.Counter + 1; } else if (node.Type == NodeType.Condition || node.Type == NodeType.ConditionAlternativeElsif) { node.Counter = node.LeftAncestor.Counter; } else if (node.Type == NodeType.ConditionEndPoint) { node.Counter = Math.Max(node.LeftAncestor.Counter, node.RightAncestor.Counter); } else { throw new Exception("Unsupported node type encountered during instruction enumeration"); } } } }
private void _countTreeNodes(BinaryGraph tree) { Stack<BinaryNode> traverseStack = new Stack<BinaryNode>(); List<BinaryNode> allGraphNodes = new List<BinaryNode>(); traverseStack.Push(_tree.Root); while (traverseStack.Any()) { BinaryNode currentNode = traverseStack.Pop(); allGraphNodes.Add(currentNode); _addChildrenAvoidEndpoinDoubleAdding(ref traverseStack, currentNode); } //_tree.Root.Counter = 1; if (_tree.Root.Type == NodeType.Condition) _tree.Root.Counter = 0; else if(_tree.Root.Type == NodeType.Expression) _tree.Root.Counter = 1; else throw new Exception("Invalid first operator"); foreach (BinaryNode node in allGraphNodes) { if (node.LeftAncestor != null) { if (node.Type == NodeType.Expression) { node.Counter = node.LeftAncestor.Counter + 1; } else if (node.Type == NodeType.Condition) { node.Counter = node.LeftAncestor.Counter; } else if (node.Type == NodeType.ConditionEndPoint) { node.Counter = Math.Max(node.LeftAncestor.Counter, node.RightAncestor.Counter); } else throw new Exception("Unsupported node type throw instruction enumeration"); } } }
public Generator(BinaryGraph tree) { if (tree == null) return; _tree = tree; _countTreeNodes(_tree); //BinaryNode currentNode = _tree.Root; Stack<string> conditionsStack = new Stack<string>(); Stack<BinaryNode> traverseStack = new Stack<BinaryNode>(); traverseStack.Push(_tree.Root); //int counter = 1; while (traverseStack.Any()) { BinaryNode currentNode = traverseStack.Pop(); _addChildrenAvoidEndpoinDoubleAdding(ref traverseStack, currentNode); if (currentNode.Type == NodeType.Expression) { //String.Join(" & ", conditionsStack); char[] trimChars = { ' ', '&' }; //TODO: use Smv.OrTrimChars & Smv.AndTrimChars //_outputLines.Add(new OutputLine(currentNode.Counter, currentNode.)); string[] varval = currentNode.Value.Split(new string[] { ":=" }, StringSplitOptions.RemoveEmptyEntries); string condition = conditionsStack.Aggregate("", (current, s) => current + ("(" + s + ") & ")).TrimEnd(trimChars); if (conditionsStack.Count > 1) { condition = String.Format("({0})", condition); } _outputLines.Add(new OutputLine(currentNode.Counter, varval[0].Trim(), condition, varval[1].Trim())); if (_conditionEndPointLeftBranchChild(currentNode)) { string lastCondition = conditionsStack.Pop(); conditionsStack.Push(String.Format("!({0})", lastCondition)); } } else if (currentNode.Type == NodeType.Condition) { conditionsStack.Push(currentNode.Value); } else if (currentNode.Type == NodeType.ConditionEndPoint) { conditionsStack.Pop(); } else throw new Exception("Unsupported node type throw instruction enumeration"); } //_outputLines.Sort((a, b) => String.CompareOrdinal(a.Variable, b.Variable)); //Sort by variable name }