Salome HOME
Copyright update 2022
[modules/yacs.git] / src / yacsloader / Test / genTriangle.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2006-2022  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, or (at your option) any later version.
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
21 debut="""
22 <proc>
23     <!-- types -->
24     <!-- inline -->
25
26 <inline name="node_0_0" >
27 <script><code>
28 c=a+b
29 print(c)
30 </code></script>
31 <inport name="a" type="int"/>
32 <inport name="b" type="int"/>
33 <outport name="c" type="int"/>
34 </inline>
35
36
37 """
38         
39
40 def triangle(n):
41     """generate a YACS graph for computation of the Pascal triangle
42
43     parameter: rank of the triangle.
44     Use integers, so rank is limited to 31 (2**31)
45     The last node gives the sum of rank n (=2**n) and also a direct calculation of 2**n.
46     """
47     
48     print(debut)
49     
50     print("""
51 <inline name="collect" >
52 <script><code>""")
53     print("tot = 0")
54     print("for i in range (" + str(n+1) + "):")
55     print("    v='a' + str(i)")
56     print("    tot+=eval(v)")
57     print("result=tot")
58     print("print result")
59     print("reference=2**" + str(n))
60     print("print reference")
61     print("</code></script>")
62     for i in range (n+1):
63         inport='<inport name="a' + str(i) + '" type="int"/>'
64         print(inport)
65         pass
66     print('<outport name="result" type="int"/>')
67     print('<outport name="reference" type="int"/>')
68     print("</inline>")
69     print()
70     
71     for i in range (1,n+1):
72         for j in range (i+1):
73             node="node_" + str(i)   +"_" + str(j)
74             nodetxt='<node name="'+node+'" type="node_0_0"></node>'
75             print(nodetxt)
76             pass
77         pass
78
79     print("""
80
81     <!-- service -->
82     <!-- control -->
83
84     """)
85     
86     for i in range (n):
87         for j in range (i+1):
88             fromnode="node_" + str(i)   +"_" + str(j)
89             tonode1="node_" + str(i+1)   +"_" + str(j)
90             tonode2="node_" + str(i+1)   +"_" + str(j+1)
91             control1='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode1+'</tonode> </control>'
92             control2='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode2+'</tonode> </control>'
93             print(control1)
94             print(control2)
95             pass
96         pass
97
98     print("""
99
100     <!-- datalinks -->
101
102     """)
103     
104     for i in range (n):
105         for j in range (i+1):
106             fromnode="node_" + str(i)   +"_" + str(j)
107             tonode1="node_" + str(i+1)   +"_" + str(j)
108             tonode2="node_" + str(i+1)   +"_" + str(j+1)
109             datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
110             datato1 ='<tonode>'   + tonode1  + '</tonode> <toport>b</toport>'
111             datato2 ='<tonode>'   + tonode2  + '</tonode> <toport>a</toport>'
112             print('<datalink>')
113             print('   ' + datafrom)
114             print('   ' + datato1)
115             print('</datalink>')
116             print('<datalink>')
117             print('   ' + datafrom)
118             print('   ' + datato2)
119             print('</datalink>')
120             pass
121         pass
122
123     for i in range (n+1):
124         fromnode="node_" + str(n)   +"_" + str(i)
125         datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
126         toport='a' + str(i)
127         datato  ='<tonode>collect</tonode> <toport>' + toport + '</toport>'
128         print('<datalink>')
129         print('   ' + datafrom)
130         print('   ' + datato)
131         print('</datalink>')
132     
133     print("""
134
135     <!--parameters -->
136
137     """)
138
139     print("""
140     <parameter>
141         <tonode>node_0_0</tonode> <toport>a</toport>
142         <value><int>0</int></value>
143     </parameter>
144     <parameter>
145         <tonode>node_0_0</tonode> <toport>b</toport>
146         <value><int>1</int></value>
147     </parameter>
148     """)
149
150     for i in range (1,n+1):
151         node1="node_" + str(i)   +"_" + str(0)
152         node2="node_" + str(i)   +"_" + str(i)
153         tonode1 ='   <tonode>' + node1 + '</tonode> <toport>a</toport>'
154         tonode2 ='   <tonode>' + node2 + '</tonode> <toport>b</toport>'
155         print('<parameter>')
156         print(tonode1)
157         print('   <value><int>0</int></value>')
158         print('</parameter>')
159         
160         print('<parameter>')
161         print(tonode2)
162         print('   <value><int>0</int></value>')
163         print('</parameter>')
164
165     print("""
166
167 </proc>
168     """)
169      
170 if __name__ == "__main__":
171     import sys
172     usage ="""Usage: %s rank > file.xml
173     where rank is positive integer >2 and <32
174     """
175     try:
176         rank = int(sys.argv[1])
177         if rank <2:
178             raise ValueError("rank must be >1")
179         if rank >31:
180             raise ValueError("rank must be <32")
181     except (IndexError, ValueError):
182         print(usage%(sys.argv[0]))
183         sys.exit(1)
184         pass
185     triangle(rank)
186     pass