Example usage for java.util.concurrent ArrayBlockingQueue toArray

List of usage examples for java.util.concurrent ArrayBlockingQueue toArray

Introduction

In this page you can find the example usage for java.util.concurrent ArrayBlockingQueue toArray.

Prototype

public Object[] toArray() 

Source Link

Document

Returns an array containing all of the elements in this queue, in proper sequence.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int capacity = 10;
    ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);

    for (int i = 0; i < 10; i++) {
        queue.add(i);/*  w ww .j  a  v  a2 s .  c  o m*/
    }

    System.out.println(Arrays.toString(queue.toArray()));
}

From source file:gsn.vsensor.RVirtualSensor.java

public void dataAvailable(String inputStreamName, StreamElement streamElement) {
    ArrayBlockingQueue<StreamElement> circularBuffer = circularBuffers.get(inputStreamName);

    // Get the circular buffer that matches the input stream. Create a new one
    // if none exists
    if (circularBuffer == null) {
        circularBuffer = new ArrayBlockingQueue<StreamElement>(windowSize);
        circularBuffers.put(inputStreamName, circularBuffer);
    }//from w  ww .j  av  a2s .c  o  m
    try {
        circularBuffer.put(streamElement);

        logger.debug(
                "Window for " + inputStreamName + " contains: " + circularBuffer.size() + " of " + windowSize);

        if (circularBuffer.size() == windowSize) {
            logger.info("Window for " + inputStreamName + " contains: " + circularBuffer.size() + " of "
                    + windowSize);

            // Connect to Rserve and assign global variables
            try {
                rc = new RConnection(params.get(SERVER), Integer.parseInt(params.get(PORT)));

                logger.info("Connected to R server " + params.get(SERVER) + ":" + params.get(PORT));

                String[] fieldname = streamElement.getFieldNames();

                logger.info("Sending " + fieldname.length + " data attributes to R server.");

                // Assign R vector variables prior the script
                for (int n = 0; n < fieldname.length; n++) {
                    // Build the window
                    double[] values = new double[windowSize];
                    StreamElement elt = null;

                    // convert the circular buffer to an array
                    Object[] elts = circularBuffer.toArray();
                    for (int i = 0; i < elts.length; i++) {
                        elt = (StreamElement) elts[i];
                        values[i] = ((Number) elt.getData()[n]).doubleValue(); //
                    }

                    // assign vectors as R variables
                    rc.assign("gsn_" + fieldname[n].toLowerCase(), values);
                }

                logger.info("Done.");

                // read the R script
                // open the script file every time we do some processing (this can be
                // improved).
                File file = new File(params.get(SCRIPT).toString());
                script = FileUtils.readFileToString(file, "UTF-8");

                logger.info("Sending R script.");

                // evaluate the R script
                rc.voidEval(script);
                logger.info("Done.");

                // get the output timestamp
                logger.info("Performing computation in R server (please wait).");

                // collect vector values after computation
                DataField[] outStructure = null;

                outStructure = getVirtualSensorConfiguration().getOutputStructure();

                String[] plotFieldName = new String[outStructure.length];
                Byte[] plotFieldType = new Byte[outStructure.length];

                for (int w = 0; w < outStructure.length; w++) {
                    plotFieldName[w] = outStructure[w].getName();
                    plotFieldType[w] = outStructure[w].getDataTypeID();
                }

                Serializable[] outputData = null;
                StreamElement se = null;

                Byte[] fieldType = streamElement.getFieldTypes();

                // check if we have defined more attributes in the output structure
                if (outStructure.length > fieldname.length) {
                    outputData = new Serializable[outStructure.length];
                } else {
                    outputData = new Serializable[fieldname.length];
                }

                for (int n = 0; n < fieldname.length; n++) {
                    // evaluate/get attribute data from R server
                    xp = rc.parseAndEval(fieldname[n].toLowerCase());

                    if (fieldType[n] == DataTypes.DOUBLE) {
                        double[] b1 = xp.asDoubles();
                        outputData[n] = b1[b1.length - 1];
                    }

                    if (fieldType[n] == DataTypes.INTEGER) {
                        int[] b1 = xp.asIntegers();
                        outputData[n] = b1[b1.length - 1];
                    }

                    if (fieldType[n] == DataTypes.BIGINT) {
                        int[] b1 = xp.asIntegers();
                        outputData[n] = (long) b1[b1.length - 1];
                    }
                }

                int len1 = outStructure.length;
                int len2 = fieldname.length;

                // check if we have defined more attributes in the output structure
                if (len1 > len2) {
                    if (stype.equals("plot")) {
                        xp = rc.parseAndEval("gsn_plot");
                        outputData[len2] = xp.asBytes();

                        se = new StreamElement(plotFieldName, plotFieldType, outputData);
                    }
                } else {
                    se = new StreamElement(fieldname, fieldType, outputData);
                }

                logger.info("Computation finished.");

                dataProduced(se);
                logger.debug("Stream published: " + se.toString().toLowerCase());

                // Close connection to R server
                rc.close();
                logger.info("Connection to R server closed.");

            } catch (Exception e) {
                logger.warn(e.getMessage());
                // Close connection to R server
                logger.info("Connection to R server closed.");
                rc.close();
            }

            // Remove step size elements from the beginning of the buffer
            for (int i = 0; i < stepSize; i++) {
                try {
                    circularBuffer.take();
                } catch (InterruptedException e) {
                    logger.warn(e.getMessage(), e);
                }
            }

        }

        // end if if for window
    } catch (InterruptedException e) {
        logger.warn(e.getMessage(), e);
    }

}

From source file:org.openiot.gsn.vsensor.RVirtualSensor.java

public void dataAvailable(String inputStreamName, StreamElement streamElement) {
    ArrayBlockingQueue<StreamElement> circularBuffer = circularBuffers.get(inputStreamName);

    // Get the circular buffer that matches the input stream. Create a new one
    // if none exists
    if (circularBuffer == null) {
        circularBuffer = new ArrayBlockingQueue<StreamElement>(windowSize);
        circularBuffers.put(inputStreamName, circularBuffer);
    }//from   ww w . j  a  va2 s . c  o m
    try {
        circularBuffer.put(streamElement);

        logger.debug(
                "Window for " + inputStreamName + " contains: " + circularBuffer.size() + " of " + windowSize);

        if (circularBuffer.size() == windowSize) {
            logger.info("Window for " + inputStreamName + " contains: " + circularBuffer.size() + " of "
                    + windowSize);

            // Connect to Rserve and assign global variables
            try {
                rc = new RConnection(params.get(SERVER), Integer.parseInt(params.get(PORT)));

                logger.info("Connected to R server " + params.get(SERVER) + ":" + params.get(PORT));

                String[] fieldname = streamElement.getFieldNames();

                logger.info("Sending " + fieldname.length + " data attributes to R server.");

                // Assign R vector variables prior the script
                for (int n = 0; n < fieldname.length; n++) {
                    // Build the window
                    double[] values = new double[windowSize];
                    StreamElement elt = null;

                    // convert the circular buffer to an array
                    Object[] elts = circularBuffer.toArray();
                    for (int i = 0; i < elts.length; i++) {
                        elt = (StreamElement) elts[i];
                        values[i] = ((Number) elt.getData()[n]).doubleValue(); //
                    }

                    // assign vectors as R variables
                    rc.assign("gsn_" + fieldname[n].toLowerCase(), values);
                }

                logger.info("Done.");

                // read the R script
                // open the script file every time we do some processing (this can be
                // improved).
                File file = new File(params.get(SCRIPT).toString());
                script = FileUtils.readFileToString(file, "UTF-8");

                logger.info("Sending R script.");

                // evaluate the R script
                rc.voidEval(script);
                logger.info("Done.");

                // get the output timestamp
                logger.info("Performing computation in R server (please wait).");

                // collect vector values after computation
                DataField[] outStructure = null;

                outStructure = getVirtualSensorConfiguration().getOutputStructure();

                String[] plotFieldName = new String[outStructure.length];
                Byte[] plotFieldType = new Byte[outStructure.length];

                for (int w = 0; w < outStructure.length; w++) {
                    plotFieldName[w] = outStructure[w].getName();
                    plotFieldType[w] = outStructure[w].getDataTypeID();
                }

                Serializable[] outputData = null;
                StreamElement se = null;

                Byte[] fieldType = streamElement.getFieldTypes();

                // check if we have defined more attributes in the output structure
                if (outStructure.length > fieldname.length) {
                    outputData = new Serializable[outStructure.length];
                } else {
                    outputData = new Serializable[fieldname.length];
                }

                for (int n = 0; n < fieldname.length; n++) {
                    // evaluate/get attribute data from R server
                    xp = rc.parseAndEval(fieldname[n].toLowerCase());

                    if (fieldType[n] == DataTypes.DOUBLE) {
                        double[] b1 = xp.asDoubles();
                        outputData[n] = b1[b1.length - 1];
                    }

                    if (fieldType[n] == DataTypes.INTEGER) {
                        int[] b1 = xp.asIntegers();
                        outputData[n] = b1[b1.length - 1];
                    }

                    if (fieldType[n] == DataTypes.BIGINT) {
                        int[] b1 = xp.asIntegers();
                        outputData[n] = (long) b1[b1.length - 1];
                    }
                }

                int len1 = outStructure.length;
                int len2 = fieldname.length;

                // check if we have defined more attributes in the output structure
                if (len1 > len2) {
                    if (stype.equals("plot")) {
                        xp = rc.parseAndEval("gsn_plot");
                        outputData[len2] = xp.asBytes();

                        se = new StreamElement(plotFieldName, plotFieldType, outputData);
                    }
                } else {
                    se = new StreamElement(fieldname, fieldType, outputData);
                }

                logger.info("Computation finished.");

                dataProduced(se);
                logger.debug("Stream published: " + se.toString().toLowerCase());

                // Close connection to R server
                rc.close();
                logger.info("Connection to R server closed.");

            } catch (Exception e) {
                logger.warn(e);
                // Close connection to R server
                logger.info("Connection to R server closed.");
                rc.close();
            }

            // Remove step size elements from the beginning of the buffer
            for (int i = 0; i < stepSize; i++) {
                try {
                    circularBuffer.take();
                } catch (InterruptedException e) {
                    logger.warn(e.getMessage(), e);
                }
            }

        }

        // end if if for window
    } catch (InterruptedException e) {
        logger.warn(e.getMessage(), e);
    }

}