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