Package groovyx.gpars.dataflow.operator

Examples of groovyx.gpars.dataflow.operator.DataflowProcessor


        ch2.bind(BigInteger.ZERO);


        final List<DataflowQueue<BigInteger>> calculationInputList = asList(ch1, ch2);
        final List<DataflowQueue<BigInteger>> calculationOutputList = asList(ch3, ch1, ch2);
        final DataflowProcessor op = Dataflow.operator(calculationInputList, calculationOutputList, new DataflowMessagingRunnable(2) {
            long counter = 0L;

            @Override
            protected void doRun(final Object... arguments) {
                final DataflowProcessor owner = getOwningProcessor();
                final BigInteger a = (BigInteger) arguments[0];
                final BigInteger b = (BigInteger) arguments[1];
                counter++;
                final BigInteger sum = a.add(b);
                owner.bindOutput(1, sum);
                owner.bindOutput(2, sum);
                if (counter == 1000000L) {
                    owner.bindOutput(0, sum);
                    owner.terminateAfterNextRun();
                }
            }
        });

View Full Code Here


        final DataflowQueue stream4 = new DataflowQueue();

        // processor1 waits for value in stream 1 and writes 2*value to stream2
        final List<DataflowQueue> calculationInputList = asList(stream1);
        final List<DataflowQueue> calculationOutputList = asList(stream2);
        final DataflowProcessor processor1 = Dataflow.operator(calculationInputList, calculationOutputList, new DataflowMessagingRunnable(1) {
            @Override
            protected void doRun(final Object... arguments) {
                // Passing calculated value to output stream
                getOwningProcessor().bindOutput(2 * (Integer) arguments[0]);
            }
        });

        assertFalse("Stream2 should not be bound as no value was passed to stream1", stream2.isBound());
        // send values to streams
        stream1.bind(1);


        waitForValue(stream2);
        assertTrue("Stream2 should be bound as value has been calculated by processor1", stream2.isBound());

        // processor2 reads value from stream2 and stream 2 and writes sum to stream 4
        final DataflowProcessor processor2 = Dataflow.operator(asList(stream2, stream3), asList(stream4), new DataflowMessagingRunnable(2) {
            @Override
            protected void doRun(final Object... arguments) {
                getOwningProcessor().bindOutput((Integer) arguments[0] + (Integer) arguments[1]);
            }
        });

        // Multiple values can be send to one stream
        stream1.bind(2);
        stream1.bind(3);

        assertFalse("Stream3 should not be bound as no value was set and it is input for calculation, not output", stream3.isBound());
        assertFalse("Stream4 should not be bound as no value was passed yet to its input streams", stream4.isBound());
        // processor processor2 waits for stream3 values, lets send them
        stream3.bind(100);
        stream3.bind(100);
        stream3.bind(100);
        waitForValue(stream4);
        assertTrue("Stream4 should be bound as values has been passed to its input streams", stream4.isBound());

        //fetch values
        assertEquals(102, stream4.getVal());
        assertEquals(104, stream4.getVal());
        assertEquals(106, stream4.getVal());

        assertFalse("All values fetched, no output expected", stream4.isBound());

        // would wait for another input and hang
        //  stream4.getVal();

        processor1.terminate();
        processor2.terminate();
    }
View Full Code Here

TOP

Related Classes of groovyx.gpars.dataflow.operator.DataflowProcessor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.