001/*
002 * $Id: GroebnerBaseSeqPairDistTest.java 4010 2012-07-21 20:39:56Z kredel $
003 */
004
005package edu.jas.gb;
006
007//import edu.jas.poly.GroebnerBase;
008
009import java.util.List;
010import java.util.ArrayList;
011import java.io.IOException;
012import java.io.Reader;
013import java.io.StringReader;
014
015import junit.framework.Test;
016import junit.framework.TestCase;
017import junit.framework.TestSuite;
018
019import org.apache.log4j.BasicConfigurator;
020//import org.apache.log4j.Logger;
021
022import edu.jas.structure.RingElem;
023
024import edu.jas.arith.BigRational;
025import edu.jas.gb.GroebnerBase;
026import edu.jas.gb.GroebnerBaseSeq;
027
028import edu.jas.poly.GenPolynomial;
029import edu.jas.poly.GenPolynomialRing;
030import edu.jas.poly.GenPolynomialTokenizer;
031import edu.jas.poly.PolynomialList;
032
033
034/**
035 * Groebner base distributed, sequential pair list, tests with JUnit.
036 * @author Heinz Kredel
037 */
038
039public class GroebnerBaseSeqPairDistTest extends TestCase {
040
041    //private static final Logger logger = Logger.getLogger(GroebnerBaseSeqPairDistTest.class);
042
043/**
044 * main
045 */
046   public static void main (String[] args) {
047          BasicConfigurator.configure();
048          junit.textui.TestRunner.run( suite() );
049   }
050
051/**
052 * Constructs a <CODE>GroebnerBaseSeqPairDistTest</CODE> object.
053 * @param name String.
054 */
055public GroebnerBaseSeqPairDistTest(String name) {
056          super(name);
057   }
058
059/**
060 * suite.
061 */ 
062 public static Test suite() {
063     TestSuite suite= new TestSuite(GroebnerBaseSeqPairDistTest.class);
064     return suite;
065   }
066
067   int port = 4711;
068   String host = "localhost";
069
070   GenPolynomialRing<BigRational> fac;
071
072   List<GenPolynomial<BigRational>> L;
073   PolynomialList<BigRational> F;
074   List<GenPolynomial<BigRational>> G;
075
076   GroebnerBase<BigRational> bbseq;
077   GroebnerBaseSeqPairDistributed<BigRational> bbdist;
078
079   GenPolynomial<BigRational> a;
080   GenPolynomial<BigRational> b;
081   GenPolynomial<BigRational> c;
082   GenPolynomial<BigRational> d;
083   GenPolynomial<BigRational> e;
084
085   int rl = 3; //4; //3; 
086   int kl = 3;
087   int ll = 7;
088   int el = 3;
089   float q = 0.2f; //0.4f
090
091   int threads = 2;
092
093   protected void setUp() {
094       BigRational coeff = new BigRational(9);
095       fac = new GenPolynomialRing<BigRational>(coeff,rl);
096       a = b = c = d = e = null;
097       bbseq = new GroebnerBaseSeq<BigRational>();
098       bbdist = new GroebnerBaseSeqPairDistributed<BigRational>(threads,port);
099   }
100
101   protected void tearDown() {
102       a = b = c = d = e = null;
103       fac = null;
104       bbseq = null;
105       bbdist.terminate();
106       bbdist = null;
107   }
108
109
110/**
111 * Helper method to start threads with distributed clients.
112 * 
113 */
114
115  Thread[] startThreads() {
116     Thread[] clients = new Thread[threads];
117     for (int t = 0; t < threads; t++) {
118         clients[t] = new Thread( new JunitSeqPairClient(host,port) );
119         clients[t].start();
120     }
121     return clients;
122 }
123
124/**
125 * Helper method to stop threads with distributed clients.
126 * 
127 */
128
129    void stopThreads(Thread[] clients) {
130        for (int t = 0; t < threads; t++) {
131            try {
132                clients[t].join();
133            } catch (InterruptedException e) {
134            }
135        }
136    }
137
138
139/**
140 * Test distributed GBase.
141 * 
142 */
143 public void testSeqPairDistributedGBase() {
144
145     Thread[] clients;
146
147     L = new ArrayList<GenPolynomial<BigRational>>();
148
149     a = fac.random(kl, ll, el, q );
150     b = fac.random(kl, ll, el, q );
151     c = fac.random(kl, ll, el, q );
152     d = fac.random(kl, ll, el, q );
153     e = d; //fac.random(kl, ll, el, q );
154
155     if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
156        return;
157     }
158
159     assertTrue("not isZERO( a )", !a.isZERO() );
160     L.add(a);
161
162     clients = startThreads();
163     L = bbdist.GB( L );
164     stopThreads( clients );
165     assertTrue("isGB( { a } )", bbseq.isGB(L) );
166
167     assertTrue("not isZERO( b )", !b.isZERO() );
168     L.add(b);
169     //System.out.println("L = " + L.size() );
170
171     clients = startThreads();
172     L = bbdist.GB( L );
173     stopThreads( clients );
174     assertTrue("isGB( { a, b } )", bbseq.isGB(L) );
175
176     assertTrue("not isZERO( c )", !c.isZERO() );
177     L.add(c);
178
179     clients = startThreads();
180     L = bbdist.GB( L );
181     stopThreads( clients );
182     assertTrue("isGB( { a, b, c } )", bbseq.isGB(L) );
183
184     assertTrue("not isZERO( d )", !d.isZERO() );
185     L.add(d);
186
187     clients = startThreads();
188     L = bbdist.GB( L );
189     stopThreads( clients );
190     assertTrue("isGB( { a, b, c, d } )", bbseq.isGB(L) );
191
192     assertTrue("not isZERO( e )", !e.isZERO() );
193     L.add(e);
194
195     clients = startThreads();
196     L = bbdist.GB( L );
197     stopThreads( clients );
198     assertTrue("isGB( { a, b, c, d, e } )", bbseq.isGB(L) );
199 }
200
201
202/**
203 * Test compare sequential with distributed GBase.
204 * 
205 */
206 public void testSequentialSeqPairDistributedGBase() {
207
208     Thread[] clients;
209
210     List<GenPolynomial<BigRational>> Gs, Gp = null;
211
212     L = new ArrayList<GenPolynomial<BigRational>>();
213
214     a = fac.random(kl, ll, el, q );
215     b = fac.random(kl, ll, el, q );
216     c = fac.random(kl, ll, el, q );
217     d = fac.random(kl, ll, el, q );
218     e = d; //fac.random(kl, ll, el, q );
219
220     if ( a.isZERO() || b.isZERO() || c.isZERO() || d.isZERO() ) {
221        return;
222     }
223
224     L.add(a);
225     Gs = bbseq.GB( L );
226     clients = startThreads();
227     Gp = bbdist.GB( L );
228     stopThreads( clients );
229
230     assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
231     assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
232
233     L = Gs;
234     L.add(b);
235     Gs = bbseq.GB( L );
236     clients = startThreads();
237     Gp = bbdist.GB( L );
238     stopThreads( clients );
239     assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
240     assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
241
242     L = Gs;
243     L.add(c);
244     Gs = bbseq.GB( L );
245     clients = startThreads();
246     Gp = bbdist.GB( L );
247     stopThreads( clients );
248
249     assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
250     assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
251
252     L = Gs;
253     L.add(d);
254     Gs = bbseq.GB( L );
255     clients = startThreads();
256     Gp = bbdist.GB( L );
257     stopThreads( clients );
258
259     assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
260     assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
261
262     L = Gs;
263     L.add(e);
264     Gs = bbseq.GB( L );
265     clients = startThreads();
266     Gp = bbdist.GB( L );
267     stopThreads( clients );
268
269     assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp, Gs.containsAll(Gp) );
270     assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp, Gp.containsAll(Gs) );
271 }
272
273
274/**
275 * Test Trinks7 GBase.
276 * 
277 */
278 public void testTrinks7GBase() {
279     Thread[] clients;
280     String exam = "(B,S,T,Z,P,W) L "
281                 + "( "  
282                 + "( 45 P + 35 S - 165 B - 36 ), " 
283                 + "( 35 P + 40 Z + 25 T - 27 S ), "
284                 + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
285                 + "( - 9 W + 15 T P + 20 S Z ), "
286                 + "( P W + 2 T Z - 11 B**3 ), "
287                 + "( 99 W - 11 B S + 3 B**2 ), "
288                 + "( B**2 + 33/50 B + 2673/10000 ) "
289                 + ") ";
290     Reader source = new StringReader( exam );
291     GenPolynomialTokenizer parser
292                  = new GenPolynomialTokenizer( source );
293     try {
294         F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
295     } catch(IOException e) {
296         fail(""+e);
297     }
298     //System.out.println("F = " + F);
299
300     clients = startThreads();
301     G = bbdist.GB( F.list );
302     stopThreads( clients );
303
304     assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G) );
305     assertEquals("#GB(Trinks7) == 6", 6, G.size() );
306     //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring,G);
307     //System.out.println("G = " + trinks);
308
309 }
310
311}
312
313
314/**
315 * Unit Test client to be executed by test threads.
316 */
317
318class JunitSeqPairClient<C extends RingElem<C>> implements Runnable {
319    private final String host;
320    private final int port;
321
322    JunitSeqPairClient(String host, int port) {
323        this.host = host;
324        this.port = port;
325    }
326
327    public void run() {
328        GroebnerBaseSeqPairDistributed<C> bbd;
329        bbd = new GroebnerBaseSeqPairDistributed<C>(1,null,port);
330        try {
331            bbd.clientPart(host);
332        } catch (IOException ignored) {
333        }
334        bbd.terminate();
335    }
336}
337