///<summary> /// Resolves given node and then all its heir _nodes recursively using DFS. ///</summary> private static void ResolveInheritanceNodeAndHeirs(InheritanceNode node) { //Error check // if we've reached a resolved node by traversing the tree, then it means // that there's a cycle, note that we're not reporting the full cycle in // the error message here, but only the last node which created a cycle if (node.Resolved) { throw new Exception(string.Format("Cyclic inheritance detected for Tyd node:\n{0}", node.TydNode.FullTyd)); } //Resolve this node { if (node.Source == null) { // No source - Just use the original node node.Resolved = true; } else { //Source exists - We now inherit from it //We must use source's RESOLVED node here because our source can have its own source. if (!node.Source.Resolved) { throw new Exception(string.Format( "Tried to resolve Tyd inheritance node {0} whose source has not been resolved yet. This means that this method was called in incorrect order.", node)); } CheckForDuplicateNodes(node.TydNode); node.Resolved = true; //Write resolved node's class attribute //Original takes precedence over source; source takes precedence over default var attClass = node.TydNode.AttributeClass ?? node.Source.TydNode.AttributeClass; node.TydNode.SetAttribute("class", attClass); //Apply inheritance from source to node ApplyInheritance(node.Source.TydNode, node.TydNode); } } //Recur to the heirs and resolve them too for (var i = 0; i < node.HeirCount; i++) { ResolveInheritanceNodeAndHeirs(node.GetHeir(i)); } }
///<summary> /// Resolves given node and then all its heir nodes recursively using DFS. ///</summary> private static void ResolveInheritanceNodeAndHeirs(InheritanceNode node) { //Error check // if we've reached a resolved node by traversing the tree, then it means // that there's a cycle, note that we're not reporting the full cycle in // the error message here, but only the last node which created a cycle if (node.resolved) { throw new Exception("Cyclic inheritance detected for Tyd node:\n" + node.tydNode.FullTyd); } //Resolve this node { if (node.source == null) { // No source - Just use the original node node.resolved = true; } else { //Source exists - We now inherit from it //We must use source's RESOLVED node here because our source can have its own source. if (!node.source.resolved) { throw new Exception("Tried to resolve Tyd inheritance node " + node + " whose source has not been resolved yet. This means that this method was called in incorrect order."); } CheckForDuplicateNodes(node.tydNode); node.resolved = true; //Apply inheritance from source to node ApplyInheritance(node.source.tydNode, node.tydNode); } } //Recur to the heirs and resolve them too for (int i = 0; i < node.HeirCount(); i++) { ResolveInheritanceNodeAndHeirs(node.GetHeir(i)); } }