]> SALOME platform Git repositories - modules/yacs.git/blob - src/yacsloader/Test/genPascal.py
Salome HOME
copy tag mergefrom_BR_V0_1_CC_Salome_04oct07
[modules/yacs.git] / src / yacsloader / Test / genPascal.py
1 #!/usr/bin/env python
2
3 def triangle(n):
4     """generate a YACS graph for computation of the Pascal triangle
5
6     parameter: rank of the triangle.
7     Use module decimal for an exact calculation with big numbers.
8     The last node gives the sum of rank n (=2**n) and also a direct calculation of 2**n.
9     """
10        
11     print """
12 <proc>
13     <!-- types -->
14     <!-- inline -->
15
16 <inline name="node_0_0" >
17 <script><code>
18 import time
19 from decimal import *"""
20     print "getcontext().prec = " + str(1+n/3)
21     print """
22 aa=Decimal(a)
23 bb=Decimal(b)
24 cc=aa+bb
25 c=str(cc)
26 print "cc=",cc
27 time.sleep(1)
28 </code></script>
29 <inport name="a" type="string"/>
30 <inport name="b" type="string"/>
31 <outport name="c" type="string"/>
32 </inline>"""
33
34     print """
35 <inline name="collect" >
36 <script><code>"""
37     print "import time"
38     print "from decimal import *"
39     print "getcontext().prec = " + str(1+n/3)
40     print "tot = Decimal(0)"
41     print "for i in range (" + str(n+1) + "):"
42     print "    v='a' + str(i)"
43     print "    tot+=Decimal(eval(v))"
44     print "print tot"
45     print "result=str(tot)"
46     print "ref=Decimal(2)**" + str(n)
47     print "reference=str(ref)"
48     print "time.sleep(1)"
49     print "</code></script>"
50     for i in range (n+1):
51         inport='<inport name="a' + str(i) + '" type="string"/>'
52         print inport
53         pass
54     print '<outport name="result" type="string"/>'
55     print '<outport name="reference" type="string"/>'
56     print "</inline>"
57     print
58     
59     for i in range (1,n+1):
60         for j in range (i+1):
61             node="node_" + str(i)   +"_" + str(j)
62             nodetxt='<node name="'+node+'" type="node_0_0"></node>'
63             print nodetxt
64             pass
65         pass
66
67     print """
68
69     <!-- service -->
70     <!-- control -->
71
72     """
73     
74     for i in range (n):
75         for j in range (i+1):
76             fromnode="node_" + str(i)   +"_" + str(j)
77             tonode1="node_" + str(i+1)   +"_" + str(j)
78             tonode2="node_" + str(i+1)   +"_" + str(j+1)
79             control1='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode1+'</tonode> </control>'
80             control2='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode2+'</tonode> </control>'
81             print control1
82             print control2
83             pass
84         pass
85     for i in range (n+1):
86         fromnode="node_" + str(n)   +"_" + str(i)
87         control='<control> <fromnode>'+fromnode+'</fromnode> <tonode>collect</tonode> </control>'
88         print control
89         pass
90
91     print """
92
93     <!-- datalinks -->
94
95     """
96     
97     for i in range (n):
98         for j in range (i+1):
99             fromnode="node_" + str(i)   +"_" + str(j)
100             tonode1="node_" + str(i+1)   +"_" + str(j)
101             tonode2="node_" + str(i+1)   +"_" + str(j+1)
102             datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
103             datato1 ='<tonode>'   + tonode1  + '</tonode> <toport>b</toport>'
104             datato2 ='<tonode>'   + tonode2  + '</tonode> <toport>a</toport>'
105             print '<datalink>'
106             print '   ' + datafrom
107             print '   ' + datato1
108             print '</datalink>'
109             print '<datalink>'
110             print '   ' + datafrom
111             print '   ' + datato2
112             print '</datalink>'
113             pass
114         pass
115     for i in range (n+1):
116         fromnode="node_" + str(n)   +"_" + str(i)
117         datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
118         toport='a' + str(i)
119         datato  ='<tonode>collect</tonode> <toport>' + toport + '</toport>'
120         print '<datalink>'
121         print '   ' + datafrom
122         print '   ' + datato
123         print '</datalink>'
124         
125         
126     print """
127
128     <!--parameters -->
129
130     """
131
132     print """
133     <parameter>
134         <tonode>node_0_0</tonode> <toport>a</toport>
135         <value><string>0</string></value>
136     </parameter>
137     <parameter>
138         <tonode>node_0_0</tonode> <toport>b</toport>
139         <value><string>1</string></value>
140     </parameter>
141     """
142
143     for i in range (1,n+1):
144         node1="node_" + str(i)   +"_" + str(0)
145         node2="node_" + str(i)   +"_" + str(i)
146         tonode1 ='   <tonode>' + node1 + '</tonode> <toport>a</toport>'
147         tonode2 ='   <tonode>' + node2 + '</tonode> <toport>b</toport>'
148         print '<parameter>'
149         print tonode1
150         print '   <value><string>0</string></value>'
151         print '</parameter>'
152         
153         print '<parameter>'
154         print tonode2
155         print '   <value><string>0</string></value>'
156         print '</parameter>'
157
158     print """
159
160 </proc>
161     """
162      
163 if __name__ == "__main__":
164     import sys
165     usage ="""Usage: %s rank > file.xml
166     where rank is positive integer > 2
167     """
168     try:
169         rank = int(sys.argv[1])
170         if rank <2:
171             raise ValueError("rank must be >1")
172     except (IndexError, ValueError):
173         print usage%(sys.argv[0])
174         sys.exit(1)
175         pass
176     triangle(rank)
177     pass