/[pdpsoft]/trunk/nl.nikhef.pdp.dot2term/treedefs.py
ViewVC logotype

Annotation of /trunk/nl.nikhef.pdp.dot2term/treedefs.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1455 - (hide annotations) (download) (as text)
Thu Feb 4 09:56:48 2010 UTC (12 years, 5 months ago) by templon
File MIME type: application/x-python
File size: 4216 byte(s)
First checkin of dot2term, a program to convert 'dot' files to 'udg' files.


1 templon 1455 # definition of tree structures for fctree
2     # J. Templon NIKHEF, written May 2001 while at University of Georgia.
3    
4     TINDENT = 5 # indentation (number of spaces) for nesting of text tree print
5    
6     # Node: one node per subroutine, main routine, function, or block data
7     # name: string, name of routine
8     # children: list of Nodes, routines called by this routine.
9    
10     class Node:
11     def __init__(self, nodename,shape=None):
12     self.name = nodename
13     self.children = None
14     self.shape = shape
15    
16     # apply function to node and all children.
17     # exception: when during a traversal, it is found that a node is its own
18     # parent, the traversal does not follow to children. In this way
19     # infinite recursion is avoided.
20    
21     def init_traverse(): # prepare for new tree traversal
22     for i in Lprocessed:
23     Lprocessed.remove(i)
24     for j in Lparent:
25     Lparent.remove(j)
26    
27     def applyall(node, function):
28     function(node)
29    
30     if node.name in Lparent : return # avoid infinite recursion
31     if not node.children : return # no children to process
32    
33     Lparent.append(node.name) # push current node onto parent stack
34     for child in node.children: # process children
35     applyall(child, function)
36     junk = Lparent.pop() # pop current node off parent stack
37    
38     def applyunique(node, function):
39     function(node)
40    
41     if node.name in Lprocessed : return # never print subtree more than once
42     if not node.children : return # no children to process
43    
44     Lprocessed.append(node.name)
45     Lparent.append(node.name) # push current node onto parent stack
46     for child in node.children: # process children
47     applyunique(child, function)
48     junk = Lparent.pop() # pop current node off parent stack
49    
50     def tprint(node): # test: text print
51     spaces = 5*len(Lparent)*' ' # indentation
52     print spaces+node.name
53    
54     class Formathandler: # holds functions for formatted
55     # traversal of tree
56     def __init__(self,
57     Fnode, # for node not yet encountered
58     Fref, # for already-encountered node
59     Fb4child, # for b4 application to each child
60     Fb4nextchild, # called between application to children
61     Fafterchild, # called after application to each child
62     Fafterchildren, # called after traversing all children
63     filename=None) :
64    
65     self.node = Fnode
66     self.ref = Fref
67     self.b4child = Fb4child
68     self.b4nextchild = Fb4nextchild
69     self.afterchild = Fafterchild
70     self.afterchildren = Fafterchildren
71     if not filename :
72     self.of = sys.stdout
73     else:
74     self.of = open(filename,'w')
75    
76     def fmt_applyunique(node, fmt):
77     if node.name in Lprocessed : # never print subtree more than once
78     fmt.ref(node,fmt)
79     return
80     fmt.node(node,fmt)
81     rfirst = 1
82     Lprocessed.append(node.name)
83     Lparent.append(node.name) # push current node onto parent stack
84     if node.children:
85     for child in node.children:
86     if rfirst == 0:
87     fmt.b4nextchild(fmt)
88     else:
89     rfirst = 0
90     fmt.b4child(child,fmt)
91     fmt_applyunique(child, fmt)
92     fmt.afterchild(fmt)
93     fmt.afterchildren(fmt)
94     junk = Lparent.pop() # pop current node off parent stack
95    
96    
97     Ltreenode = [ ] # list of Nodes at root of top-level trees
98    
99     Dnode = { } # dict mapping names of Node onto Node
100    
101     Dsubtree = { } # dict mapping keys cID onto subtree objects (list of Node)
102     # only contains top-level 'continuation trees'
103     # Node.children = Dsubtree[cID] works.
104    
105     Lparent = [ ] # stack (implemented as list) keeping track of parents during
106     # list traversal, to avoid infinite recursion in case of
107     # cyclical references
108    
109     Lprocessed = [ ] # list of nodes already processed in applyunique
110     # if applyunique traversal finds node in this list, its
111     # children will not be processed.

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28