Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / yacsloader / Test / genPascal.py
1 #!/usr/bin/env python
2 #  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
3 #
4 #  This library is free software; you can redistribute it and/or
5 #  modify it under the terms of the GNU Lesser General Public
6 #  License as published by the Free Software Foundation; either
7 #  version 2.1 of the License.
8 #
9 #  This library is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 #  Lesser General Public License for more details.
13 #
14 #  You should have received a copy of the GNU Lesser General Public
15 #  License along with this library; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 def triangle(n):
21     """generate a YACS graph for computation of the Pascal triangle
22
23     parameter: rank of the triangle.
24     Use module decimal for an exact calculation with big numbers.
25     The last node gives the sum of rank n (=2**n) and also a direct calculation of 2**n.
26     """
27        
28     print """
29 <proc>
30     <!-- types -->
31     <!-- inline -->
32
33 <inline name="node_0_0" >
34 <script><code>
35 import time
36 from decimal import *"""
37     print "getcontext().prec = " + str(1+n/3)
38     print """
39 aa=Decimal(a)
40 bb=Decimal(b)
41 cc=aa+bb
42 c=str(cc)
43 print "cc=",cc
44 time.sleep(1)
45 </code></script>
46 <inport name="a" type="string"/>
47 <inport name="b" type="string"/>
48 <outport name="c" type="string"/>
49 </inline>"""
50
51     print """
52 <inline name="collect" >
53 <script><code>"""
54     print "import time"
55     print "from decimal import *"
56     print "getcontext().prec = " + str(1+n/3)
57     print "tot = Decimal(0)"
58     print "for i in range (" + str(n+1) + "):"
59     print "    v='a' + str(i)"
60     print "    tot+=Decimal(eval(v))"
61     print "print tot"
62     print "result=str(tot)"
63     print "ref=Decimal(2)**" + str(n)
64     print "reference=str(ref)"
65     print "time.sleep(1)"
66     print "</code></script>"
67     for i in range (n+1):
68         inport='<inport name="a' + str(i) + '" type="string"/>'
69         print inport
70         pass
71     print '<outport name="result" type="string"/>'
72     print '<outport name="reference" type="string"/>'
73     print "</inline>"
74     print
75     
76     for i in range (1,n+1):
77         for j in range (i+1):
78             node="node_" + str(i)   +"_" + str(j)
79             nodetxt='<node name="'+node+'" type="node_0_0"></node>'
80             print nodetxt
81             pass
82         pass
83
84     print """
85
86     <!-- service -->
87     <!-- control -->
88
89     """
90     
91     for i in range (n):
92         for j in range (i+1):
93             fromnode="node_" + str(i)   +"_" + str(j)
94             tonode1="node_" + str(i+1)   +"_" + str(j)
95             tonode2="node_" + str(i+1)   +"_" + str(j+1)
96             control1='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode1+'</tonode> </control>'
97             control2='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode2+'</tonode> </control>'
98             print control1
99             print control2
100             pass
101         pass
102     for i in range (n+1):
103         fromnode="node_" + str(n)   +"_" + str(i)
104         control='<control> <fromnode>'+fromnode+'</fromnode> <tonode>collect</tonode> </control>'
105         print control
106         pass
107
108     print """
109
110     <!-- datalinks -->
111
112     """
113     
114     for i in range (n):
115         for j in range (i+1):
116             fromnode="node_" + str(i)   +"_" + str(j)
117             tonode1="node_" + str(i+1)   +"_" + str(j)
118             tonode2="node_" + str(i+1)   +"_" + str(j+1)
119             datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
120             datato1 ='<tonode>'   + tonode1  + '</tonode> <toport>b</toport>'
121             datato2 ='<tonode>'   + tonode2  + '</tonode> <toport>a</toport>'
122             print '<datalink>'
123             print '   ' + datafrom
124             print '   ' + datato1
125             print '</datalink>'
126             print '<datalink>'
127             print '   ' + datafrom
128             print '   ' + datato2
129             print '</datalink>'
130             pass
131         pass
132     for i in range (n+1):
133         fromnode="node_" + str(n)   +"_" + str(i)
134         datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
135         toport='a' + str(i)
136         datato  ='<tonode>collect</tonode> <toport>' + toport + '</toport>'
137         print '<datalink>'
138         print '   ' + datafrom
139         print '   ' + datato
140         print '</datalink>'
141         
142         
143     print """
144
145     <!--parameters -->
146
147     """
148
149     print """
150     <parameter>
151         <tonode>node_0_0</tonode> <toport>a</toport>
152         <value><string>0</string></value>
153     </parameter>
154     <parameter>
155         <tonode>node_0_0</tonode> <toport>b</toport>
156         <value><string>1</string></value>
157     </parameter>
158     """
159
160     for i in range (1,n+1):
161         node1="node_" + str(i)   +"_" + str(0)
162         node2="node_" + str(i)   +"_" + str(i)
163         tonode1 ='   <tonode>' + node1 + '</tonode> <toport>a</toport>'
164         tonode2 ='   <tonode>' + node2 + '</tonode> <toport>b</toport>'
165         print '<parameter>'
166         print tonode1
167         print '   <value><string>0</string></value>'
168         print '</parameter>'
169         
170         print '<parameter>'
171         print tonode2
172         print '   <value><string>0</string></value>'
173         print '</parameter>'
174
175     print """
176
177 </proc>
178     """
179      
180 if __name__ == "__main__":
181     import sys
182     usage ="""Usage: %s rank > file.xml
183     where rank is positive integer > 2
184     """
185     try:
186         rank = int(sys.argv[1])
187         if rank <2:
188             raise ValueError("rank must be >1")
189     except (IndexError, ValueError):
190         print usage%(sys.argv[0])
191         sys.exit(1)
192         pass
193     triangle(rank)
194     pass