Example usage for java.util ArrayList subList

List of usage examples for java.util ArrayList subList

Introduction

In this page you can find the example usage for java.util ArrayList subList.

Prototype

public List<E> subList(int fromIndex, int toIndex) 

Source Link

Document

Returns a view of the portion of this list between the specified fromIndex , inclusive, and toIndex , exclusive.

Usage

From source file:de.unijena.bioinf.FragmentationTreeConstruction.computation.FragmentationPatternAnalysis.java

/**
 * Step 6: Decomposition// ww w  .  j  a  v a  2  s .com
 * Decompose each peak as well as the parent peak
 */
public ProcessedInput performDecomposition(ProcessedInput input) {
    final FormulaConstraints constraints = input.getMeasurementProfile().getFormulaConstraints();
    final Ms2Experiment experiment = input.getExperimentInformation();
    final Deviation parentDeviation = input.getMeasurementProfile().getAllowedMassDeviation();
    // sort again...
    final ArrayList<ProcessedPeak> processedPeaks = new ArrayList<ProcessedPeak>(input.getMergedPeaks());
    Collections.sort(processedPeaks, new ProcessedPeak.MassComparator());
    final ProcessedPeak parentPeak = processedPeaks.get(processedPeaks.size() - 1);
    // decompose peaks
    final PeakAnnotation<DecompositionList> decompositionList = input
            .getOrCreatePeakAnnotation(DecompositionList.class);
    final MassToFormulaDecomposer decomposer = decomposers.getDecomposer(constraints.getChemicalAlphabet());
    final Ionization ion = experiment.getPrecursorIonType().getIonization();
    final Deviation fragmentDeviation = input.getMeasurementProfile().getAllowedMassDeviation();
    final List<MolecularFormula> pmds = decomposer.decomposeToFormulas(
            experiment.getPrecursorIonType().subtractIonAndAdduct(parentPeak.getOriginalMz()), parentDeviation,
            constraints);
    // add adduct to molecular formula of the ion - because the adduct might get lost during fragmentation
    {
        final MolecularFormula adduct = experiment.getPrecursorIonType().getAdduct();
        final ListIterator<MolecularFormula> iter = pmds.listIterator();
        while (iter.hasNext()) {
            final MolecularFormula f = iter.next();
            iter.set(f.add(adduct));
        }
    }
    decompositionList.set(parentPeak, DecompositionList.fromFormulas(pmds));
    int j = 0;
    for (ProcessedPeak peak : processedPeaks.subList(0, processedPeaks.size() - 1)) {
        peak.setIndex(j++);
        decompositionList.set(peak, DecompositionList.fromFormulas(
                decomposer.decomposeToFormulas(peak.getUnmodifiedMass(), fragmentDeviation, constraints)));
    }
    parentPeak.setIndex(processedPeaks.size() - 1);
    assert parentPeak == processedPeaks.get(processedPeaks.size() - 1);
    // important: for each two peaks which are within 2*massrange:
    //  => make decomposition list disjoint
    final Deviation window = fragmentDeviation.multiply(2);
    for (int i = 1; i < processedPeaks.size() - 1; ++i) {
        if (window.inErrorWindow(processedPeaks.get(i).getMz(), processedPeaks.get(i - 1).getMz())) {
            final HashSet<MolecularFormula> right = new HashSet<MolecularFormula>(
                    decompositionList.get(processedPeaks.get(i)).getFormulas());
            final ArrayList<MolecularFormula> left = new ArrayList<MolecularFormula>(
                    decompositionList.get(processedPeaks.get(i - 1)).getFormulas());
            final double leftMass = ion.subtractFromMass(processedPeaks.get(i - 1).getMass());
            final double rightMass = ion.subtractFromMass(processedPeaks.get(i).getMass());
            final Iterator<MolecularFormula> leftIter = left.iterator();
            while (leftIter.hasNext()) {
                final MolecularFormula leftFormula = leftIter.next();
                if (right.contains(leftFormula)) {
                    if (Math.abs(leftFormula.getMass() - leftMass) < Math
                            .abs(leftFormula.getMass() - rightMass)) {
                        right.remove(leftFormula);
                    } else {
                        leftIter.remove();
                    }
                }
            }
            decompositionList.set(processedPeaks.get(i - 1), DecompositionList.fromFormulas(left));
            decompositionList.set(processedPeaks.get(i), DecompositionList.fromFormulas(right));
        }
    }

    return postProcess(PostProcessor.Stage.AFTER_DECOMPOSING, input);
}

From source file:com.seer.datacruncher.utils.generic.CommonUtils.java

/**
 * Splits <tt>str</tt> around matches of the newline character.
 *
 * @param   str//from   ww w .  j  av a  2 s.  co  m
 *          the string to split
 *
 * @param   delimiter
 *          the delimiting field string
 *
 * @param   chrDelim
 *          the delimiting character
 *
 * @return  the array of strings computed by splitting string
 *          around matches of the newline character.      
 *         
 **/
public static String[] lineSplit(String str, String delimiter, char chrDelim) {
    String linedelimiter = "\n";
    String spltdelim = "\\";

    if ((str.indexOf(chrDelim, 0)) < 0) {
        return str.split(spltdelim + linedelimiter);
    } else {
        ArrayList<String> list = new ArrayList<String>();
        String record;
        List<String> streamsList = Arrays.asList(str.split(spltdelim + linedelimiter));
        int fStart;
        int fEnd;
        boolean checkfields = true;
        for (int i = 0; i < streamsList.size(); i++) {
            record = streamsList.get(i);
            if ((record.indexOf(chrDelim, 0)) < 0) {
                list.add(record);
            } else {
                if (record.startsWith(chrDelim + "")) { // check in first field
                    fEnd = record.indexOf(chrDelim + delimiter, 1); // find end
                    if (fEnd < 0) { //not found
                        if ((i + 1) < streamsList.size()) {
                            streamsList.set(i + 1, record + linedelimiter + streamsList.get(i + 1));
                        } else {
                            list.add(record);
                        }
                        checkfields = false;
                    } else {
                        checkfields = true;
                    }
                }
                if (checkfields) {
                    fStart = record.indexOf(delimiter + chrDelim, 1);
                    while (fStart < record.length()) {
                        if (fStart > 0) {
                            fEnd = record.indexOf(chrDelim + delimiter, fStart);
                            if (fEnd > 0) {
                                fStart = record.indexOf(delimiter + chrDelim, fEnd);
                                if (fStart < 0) {
                                    list.add(record);
                                    fStart = record.length();
                                }
                            } else {
                                fEnd = record.indexOf(chrDelim, fStart + 2);
                                if (fEnd < 0) {
                                    if ((i + 1) < streamsList.size()) {
                                        streamsList.set(i + 1, record + linedelimiter + streamsList.get(i + 1));
                                    } else {
                                        list.add(record);
                                    }
                                } else {
                                    if (record.charAt(record.length() - 1) == chrDelim) { //last field
                                        list.add(record);
                                    } else {
                                        if ((i + 1) < streamsList.size()) {
                                            streamsList.set(i + 1,
                                                    record + linedelimiter + streamsList.get(i + 1));
                                        } else {
                                            list.add(record);
                                        }
                                    }
                                }
                                fStart = record.length();
                            }
                        } else {
                            fStart = record.length();
                        }
                    }
                }
            }
        }
        int resultSize = list.size();
        String[] result = new String[resultSize];
        return list.subList(0, resultSize).toArray(result);
    }
}

From source file:org.hyperic.hq.measurement.server.session.DataManagerImpl.java

private Map<Integer, MetricValue> getLastDataPts(Collection<Integer> mids, long timestamp) {
    final int BATCH_SIZE = 500;
    final Map<Integer, MetricValue> rtn = new HashMap<Integer, MetricValue>(mids.size());
    if ((mids == null) || mids.isEmpty()) {
        return rtn;
    }//from ww w  .  j a va2  s .  c om
    // all cached values are inserted into rtn
    // nodata represents values that are not cached
    final Collection<Integer> nodata = getCachedDataPoints(mids, rtn, timestamp);
    ArrayList<Integer> ids = null;
    final boolean debug = log.isDebugEnabled();
    if (nodata.isEmpty()) {
        if (debug) {
            log.debug("got data from cache");
        }
        // since we have all the data from cache (nodata is empty), just return it
        return rtn;
    } else {
        ids = new ArrayList<Integer>(nodata);
    }
    Connection conn = null;
    Statement stmt = null;
    final StopWatch watch = new StopWatch();
    try {
        conn = dbUtil.getConnection();
        stmt = conn.createStatement();
        for (int i = 0; i < ids.size(); i += BATCH_SIZE) {
            final int max = Math.min(ids.size(), i + BATCH_SIZE);
            // Create sub array
            Collection<Integer> subids = ids.subList(i, max);
            if (debug) {
                watch.markTimeBegin("setDataPoints");
            }
            setDataPoints(rtn, timestamp, subids, stmt);
            if (debug) {
                watch.markTimeEnd("setDataPoints");
            }
        }
    } catch (SQLException e) {
        throw new SystemException("Cannot get last values", e);
    } finally {
        DBUtil.closeJDBCObjects(LOG_CTX, conn, stmt, null);
        if (debug) {
            log.debug(watch);
        }
    }
    List<DataPoint> dataPoints = convertMetricId2MetricValueMapToDataPoints(rtn);
    updateMetricDataCache(dataPoints);
    return rtn;
}

From source file:com.virtusa.isq.rft.runtime.RFTCommandBase.java

private void validateCellValue(ObjectLocator locator, TestObject object, Object objExpectedValue,
        boolean stopOnFaliure) {

    ArrayList<String> inputStringArray;
    boolean failedOnce = false;
    int row = -1;
    int col = -1;
    String cellText = "";
    String result = "";
    ArrayList<String> htmlTable = new ArrayList<String>();
    final int inputStringItems = 3;

    inputStringArray = new ArrayList<String>(
            Arrays.asList(objExpectedValue.toString().split("(?<!\\\\),", Integer.MAX_VALUE)));

    ArrayList<String> tempInputTable = new ArrayList<String>();
    for (String inputVal : inputStringArray) {
        String formattedValue = inputVal.replaceAll("\\\\,", ",");
        tempInputTable.add(formattedValue);
    }//w w w. j  av a  2s  . c om
    inputStringArray = tempInputTable;

    if (inputStringArray.size() < inputStringItems) {
        failedOnce = true;
        reportResults(stopOnFaliure, ReportLogger.ReportLevel.FAILURE, "Check Table", "Error",
                "Check table command TABLECELL failed. ::: " + "Object : "
                        + Arrays.asList(locator.getPropertyArray()) + " ::: "
                        + "Actual Error : Verification data not provided correctly. " + objExpectedValue);
        return;
    }
    row = Integer.parseInt(inputStringArray.get(0));
    col = Integer.parseInt(inputStringArray.get(1));

    cellText = StringUtils.join(inputStringArray.subList(2, inputStringArray.size()).toArray(), ",");

    try {

        htmlTable = getAppTableRow(object, row);
    } catch (Exception ex) {
        failedOnce = true;
        result = result + " | Expected Row : " + row + " cannot be found in the actual table \n";
    }

    int verifyIndex = col; // get the sequential index of the value to be
                           // verified

    String verifyValue = "";

    try {
        verifyValue = htmlTable.get(verifyIndex).trim();

        if (!cellText.equals(verifyValue)) {
            failedOnce = true;
            result = result + " | Expected : " + cellText + " Actual :" + htmlTable.get(verifyIndex) + "\n";

        }

    } catch (IndexOutOfBoundsException ex) {
        failedOnce = true;
        result = result + " | Expected Column : " + verifyIndex + " cannot be found in the actual table \n";
    }

    if (failedOnce) {

        reportResults(stopOnFaliure, ReportLogger.ReportLevel.FAILURE, "Check Table", "Error",
                "Check Table TABLECELL command failed. ::: " + "Object : "
                        + Arrays.asList(locator.getPropertyArray()) + " ::: "
                        + "Actual Error : Verification errors " + result);

    } else {
        reportResults(ReportLogger.ReportLevel.SUCCESS, "Check Table", "Success",
                "Check table TABLECELL : " + objExpectedValue);
    }

}

From source file:com.hygenics.parser.GetImages.java

private void addFromFile() {
    File f = new File(fpath);
    ForkJoinPool fjp = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
    ArrayList<String> imageData = new ArrayList<String>();
    int size = (int) Math.ceil(commitsize / numqueries);

    if (f.exists()) {
        // get the image data
        File[] list = f.listFiles();
        int curr = 0;
        if (list != null) {
            for (File img : list) {
                curr += 1;/*from   w w  w.j  a  v  a  2  s.c o  m*/
                if (img.isDirectory() == false
                        && (img.getName().contains(".bmp") || img.getName().toLowerCase().contains(".jpg")
                                || img.getName().toLowerCase().contains(".png")
                                || img.getName().toLowerCase().contains("jpeg"))) {
                    try {
                        if (dbcondition == null
                                || template
                                        .getJsonData(dbcondition.replace("$IMAGE$",
                                                img.getName().replaceAll("(?mis)" + imagepostfix, "")))
                                        .size() > 0) {
                            BufferedImage bi = ImageIO.read(img);// only
                            // used
                            // to
                            // ensure
                            // that
                            // this
                            // is an
                            // image
                            JsonObject jobj = new JsonObject();
                            jobj.add("offenderhash", img.getName().replaceAll("(?mis)" + imagepostfix, ""));// assumes
                            // hash
                            // is
                            // file
                            // name+postfix
                            jobj.add("image", img.getName().replaceAll("(?mis)" + imagepostfix, ""));
                            jobj.add("image_path", img.getName());
                            jobj.add("table", table);
                            jobj.add("date", Calendar.getInstance().getTime().toString());
                            imageData.add(jobj.toString());
                        }
                    } catch (IOException e) {
                        log.info(img.getName() + " is not an Image!");
                        e.printStackTrace();
                    } catch (Exception e) {
                        log.info("Error in Posting to Database.");
                        e.printStackTrace();
                    }
                }

                // post if > commitsize
                if (imageData.size() >= commitsize || curr == list.length) {
                    log.info("Posting to DB @ " + Calendar.getInstance().getTime().toString());
                    for (int i = 0; i < numqueries; i++) {
                        if (((i + 1) * size) < imageData.size()) {
                            fjp.execute(new ImagePost(imageData.subList((i * size), ((i + 1) * size))));
                        } else {
                            fjp.execute(new ImagePost(imageData.subList((i * size), imageData.size())));
                        }
                    }

                    int w = 0;
                    while (fjp.isQuiescent() == false && fjp.getActiveThreadCount() > 0) {
                        w++;
                    }
                    log.info("Waited for " + w + " cycles");
                    imageData.clear();
                    log.info("Finished Posting to DB @ " + Calendar.getInstance().getTime().toString());
                }
            }
        }

    } else {
        log.error("File Path does Not Exist.Please Check Image Pull!");
    }
    fjp.shutdown();
    fjp = null;
}

From source file:org.eclipse.ice.item.utilities.moose.MOOSEFileHandler.java

/**
 * This operations loads a MOOSE GetPot file at the specified path and
 * returns a fully-configured set of ICE TreeComposites.
 * //ww  w .j  a v  a  2 s . co  m
 * @param filePath
 *            The file path from which the MOOSE blocks written in GetPot
 *            should be read. If the path is null or empty, the operation
 *            returns without doing any work.
 * @return The MOOSE input file specification as read from the GetPot input
 *         and stored in TreeComposites. Each TreeComposite contains both
 *         parameters and exemplar children. Any parameters in a
 *         TreeComposite are contained in a DataComponent. The id of the
 *         data component is 1.
 */
public ArrayList<TreeComposite> loadFromGetPot(String filePath) {

    // Local Declarations
    ArrayList<TreeComposite> trees = new ArrayList<TreeComposite>();
    byte[] fileByteArray = null;
    String mooseFileString = null, potLine = null;

    // Quit if the path is boned
    if (filePath == null || filePath.isEmpty()) {
        return null;
    }

    // Post some debug info
    if (debugFlag) {
        logger.info("MOOSEFileHandler Message: " + "Attempting to loading GetPot file " + filePath);
    }

    // Load the GetPot file
    try {
        RandomAccessFile mooseFile = new RandomAccessFile(filePath, "r");
        // Put it into a byte array
        fileByteArray = new byte[(int) mooseFile.length()];
        mooseFile.read(fileByteArray);
        // And then a string
        mooseFileString = new String(fileByteArray);
        // Close the file
        mooseFile.close();
        // Post some more debug info
        if (debugFlag) {
            logger.info("MOOSEFileHandler Message: File loaded.");
        }
    } catch (IOException e) {
        // Complain if the file is not found
        System.err.println("MOOSEFileHandler Message: " + "Unable to load GetPot file!");
        logger.error(getClass().getName() + " Exception!", e);
    }

    // Check the string before proceeding
    if (mooseFileString != null && !mooseFileString.isEmpty()) {
        // Create an array list from the string
        ArrayList<String> potLines = new ArrayList<String>(Arrays.asList(mooseFileString.split("\n")));

        // Remove (non-parameter) commented lines and white space
        String trimmedPotLine = "";
        for (int i = 0; i < potLines.size(); i++) {

            trimmedPotLine = potLines.get(i).trim();

            if (trimmedPotLine.startsWith("#") && !trimmedPotLine.contains("=") && !trimmedPotLine.contains("[")
                    && !trimmedPotLine.contains("]")) {
                // Lines that start with "#" but have no "=" are comments
                // that aren't parameters and should be removed
                potLines.remove(i);
                // Update "i" so that we read correctly
                --i;
            } else if (potLines.get(i).isEmpty()) {
                // Remove empty lines
                potLines.remove(i);
                // Update "i" so that we read correctly
                --i;
            } else {
                // This is a rare scenario to check for, but it's possible
                // (and has happened at least once) where a line is just a
                // comment (starts with "#") AND includes a "=" in the text
                // of the comment
                if (trimmedPotLine.startsWith("#") && trimmedPotLine.contains("=")) {
                    String[] splitTrimmedPotLine = trimmedPotLine.split("\\s+");
                    if (splitTrimmedPotLine.length > 4) {
                        // Skip this line, it's a comment that's been
                        // mistaken as a parameter
                        potLines.remove(i);
                        --i;
                        continue;
                    }
                }

                // Otherwise, the normal behavior is that the line should be
                // trimmed and be considered a real parameter
                potLines.set(i, potLines.get(i).trim());
            }
        }

        // Read all of the lines again, create blocks and load them.
        int counter = 0, endCounter = 1;
        while (counter < potLines.size()) {
            // Get the line and shift the counters
            potLine = potLines.get(counter);
            ++counter;

            // The start of a full block is a line with the name in brackets
            // and without the "./" sequence.
            if (potLine.contains("[") && potLine.contains("]")) {
                // Go to the next line
                potLine = potLines.get(endCounter);

                // Loop over the block and find the end
                while (!potLine.contains("[]")) {
                    // Update the line and the counter
                    potLine = potLines.get(endCounter);
                    ++endCounter;
                }
                // Create a new block
                Block block = new Block();
                ArrayList<String> blockLines = null;
                if (endCounter >= counter - 1) {
                    blockLines = new ArrayList<String>(potLines.subList(counter - 1, endCounter));
                }
                if (blockLines != null && !blockLines.isEmpty()) {
                    StringBuilder stringBuilder = new StringBuilder(blockLines.get(0));
                    blockLines.set(0, stringBuilder.toString());
                    block.fromGetPot(blockLines);
                    // Add the block to the list
                    trees.add(block.toTreeComposite());
                    // Update the counter to point to the last read line
                    counter = endCounter;
                }

                // Print some debug information
                if (debugFlag) {
                    logger.info("\nMOOSEFileHandler Message: " + "Block output read from GetPot file "
                            + filePath + " follows.");
                    // Dump each line of the newly created block
                    for (String line : blockLines) {
                        logger.info(line);
                    }
                }

            }
        }
    } else if (debugFlag) {
        System.err.println(
                "MOOSEFileHandler Message: " + "String loaded from " + filePath + " is null or empty.");
    }

    return trees;
}

From source file:com.krawler.spring.crm.dashboard.CrmDashboardController.java

private String getReportWidgetLinks(HttpServletRequest request) throws ServiceException {
    String jdata = "";
    try {/*from ww  w .j a v  a2s .c  o  m*/
        ArrayList li = new ArrayList();
        crmdashboardHandler.getLeadsReportsLink(sessionHandlerImpl.getPerms(request, "Lead Report"), li,
                RequestContextUtils.getLocale(request));
        crmdashboardHandler.getAccountReportsLink(sessionHandlerImpl.getPerms(request, "AccountReport"), li,
                RequestContextUtils.getLocale(request));
        crmdashboardHandler.getContactReportsLink(sessionHandlerImpl.getPerms(request, "ContactReport"), li,
                RequestContextUtils.getLocale(request));
        crmdashboardHandler.getOpportunityReportsLink(sessionHandlerImpl.getPerms(request, "OpportunityReport"),
                li, RequestContextUtils.getLocale(request));
        crmdashboardHandler.getActivityReportsLink(sessionHandlerImpl.getPerms(request, "ActivityReport"), li,
                RequestContextUtils.getLocale(request));
        crmdashboardHandler.getCaseReportsLink(sessionHandlerImpl.getPerms(request, "CaseReport"), li,
                RequestContextUtils.getLocale(request));
        crmdashboardHandler.getProductReportsLink(sessionHandlerImpl.getPerms(request, "ProductReport"), li,
                RequestContextUtils.getLocale(request));
        crmdashboardHandler.getOpportunityProductReportsLink(request, li,
                RequestContextUtils.getLocale(request));
        crmdashboardHandler.getSalesReportsLink(sessionHandlerImpl.getPerms(request, "SalesReport"), li,
                RequestContextUtils.getLocale(request));
        crmdashboardHandler.getCampaignReportsLink(sessionHandlerImpl.getPerms(request, "CampaignReport"), li,
                RequestContextUtils.getLocale(request));
        crmdashboardHandler.getTargetReportsLink(request, li, RequestContextUtils.getLocale(request));
        crmdashboardHandler.getGoalReportsLink(request, li, RequestContextUtils.getLocale(request));
        li = getBubbleSortList(li);
        int start = Integer.parseInt(request.getParameter("start"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String limitReport = request.getParameter("limitReport");
        if (!StringUtil.isNullOrEmpty(limitReport)) {
            limit = Integer.parseInt(limitReport);
        }
        limit = (start + limit) > li.size() ? li.size() : (start + limit);
        List currli = (List) li.subList(start, limit);
        Iterator it = currli.iterator();
        ArrayList newArr = new ArrayList();
        while (it.hasNext()) {
            newArr.add(it.next());
        }
        JSONObject jobj = new JSONObject("{\"count\":" + li.size() + ",\"data\":" + newArr.toString() + "}");
        jdata = jobj.toString();
    } catch (JSONException ex) {
        logger.warn(ex.getMessage(), ex);
        throw ServiceException.FAILURE(ex.getMessage(), ex);
    } catch (Exception ex) {
        logger.warn(ex.getMessage(), ex);
        throw ServiceException.FAILURE(ex.getMessage(), ex);
    }
    return jdata;
}

From source file:imitationNLG.SFX.java

public Object[] createTrainingDatasets(ArrayList<DatasetInstance> trainingData,
        HashMap<String, HashSet<String>> availableAttributeActions,
        HashMap<String, HashMap<String, HashSet<Action>>> availableWordActions,
        HashMap<Integer, HashSet<String>> nGrams) {
    HashMap<String, ArrayList<Instance>> predicateAttrTrainingData = new HashMap<>();
    HashMap<String, HashMap<String, ArrayList<Instance>>> predicateWordTrainingData = new HashMap<>();

    if (!availableWordActions.isEmpty() && !predicates.isEmpty()/* && !arguments.isEmpty()*/) {
        for (String predicate : predicates) {
            predicateAttrTrainingData.put(predicate, new ArrayList<Instance>());
            predicateWordTrainingData.put(predicate, new HashMap<String, ArrayList<Instance>>());
        }/*  ww  w .ja  v  a2s .  co m*/

        for (DatasetInstance di : trainingData) {
            //System.out.println("BEGIN");
            String predicate = di.getMeaningRepresentation().getPredicate();
            //for (ArrayList<Action> realization : di.getEvalRealizations()) {
            ArrayList<Action> realization = di.getTrainRealization();
            //System.out.println(realization);
            HashSet<String> attrValuesAlreadyMentioned = new HashSet<>();
            HashSet<String> attrValuesToBeMentioned = new HashSet<>();
            for (String attribute : di.getMeaningRepresentation().getAttributes().keySet()) {
                //int a = 0;
                for (String value : di.getMeaningRepresentation().getAttributes().get(attribute)) {
                    /*if (value.startsWith("\"x")) {
                     value = "x" + a;
                     a++;
                     } else if (value.startsWith("\"")) {
                     value = value.substring(1, value.length() - 1).replaceAll(" ", "_");
                     }*/
                    attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase());
                }
            }
            if (attrValuesToBeMentioned.isEmpty()) {
                attrValuesToBeMentioned.add("empty=empty");
            }

            ArrayList<String> attrs = new ArrayList<>();
            boolean isValueMentioned = false;
            String valueTBM = "";
            String attrValue = "";
            ArrayList<String> subPhrase = new ArrayList<>();
            for (int w = 0; w < realization.size(); w++) {
                if (!realization.get(w).getAttribute().equals(SFX.TOKEN_PUNCT)) {
                    if (!realization.get(w).getAttribute().equals(attrValue)) {
                        if (!attrValue.isEmpty()) {
                            attrValuesToBeMentioned.remove(attrValue);
                        }
                        Instance attrTrainingVector = SFX.this.createAttrInstance(predicate,
                                realization.get(w).getAttribute(), attrs,
                                new ArrayList<Action>(realization.subList(0, w)), attrValuesAlreadyMentioned,
                                attrValuesToBeMentioned, di.getMeaningRepresentation(),
                                availableAttributeActions);
                        if (attrTrainingVector != null) {
                            /*System.out.println(realization.get(w).getAttribute() + " >>>> " + attrTrainingVector.getCorrectLabels());
                             for (String f : attrTrainingVector.getGeneralFeatureVector().keySet()) {
                             if (f.startsWith("feature_attrValue_5gram_")
                             || f.startsWith("feature_word_5gram_")) {
                             System.out.println(">> " + f);
                             }
                             }*/
                            predicateAttrTrainingData.get(predicate).add(attrTrainingVector);
                        }
                        attrs.add(realization.get(w).getAttribute());

                        attrValue = realization.get(w).getAttribute();
                        subPhrase = new ArrayList<>();
                        isValueMentioned = false;
                        valueTBM = "";
                        if (attrValue.contains("=")) {
                            valueTBM = attrValue.substring(attrValue.indexOf('=') + 1);
                        }
                        if (valueTBM.isEmpty()) {
                            isValueMentioned = true;
                        }
                    }
                    if (!attrValue.equals(SFX.TOKEN_END)) {
                        ArrayList<String> predictedAttributesForInstance = new ArrayList<>();
                        for (int i = 0; i < attrs.size() - 1; i++) {
                            predictedAttributesForInstance.add(attrs.get(i));
                        }
                        if (!attrs.get(attrs.size() - 1).equals(attrValue)) {
                            predictedAttributesForInstance.add(attrs.get(attrs.size() - 1));
                        }
                        Instance wordTrainingVector = createWordInstance(predicate, realization.get(w),
                                predictedAttributesForInstance,
                                new ArrayList<Action>(realization.subList(0, w)), isValueMentioned,
                                attrValuesAlreadyMentioned, attrValuesToBeMentioned,
                                di.getMeaningRepresentation(), availableWordActions.get(predicate), nGrams,
                                false);

                        if (wordTrainingVector != null) {
                            String attribute = attrValue;
                            if (attribute.contains("=")) {
                                attribute = attrValue.substring(0, attrValue.indexOf('='));
                            }
                            if (!predicateWordTrainingData.get(predicate).containsKey(attribute)) {
                                predicateWordTrainingData.get(predicate).put(attribute,
                                        new ArrayList<Instance>());
                            }
                            /*System.out.println(realization.get(w) + " >>>> " + wordTrainingVector.getCorrectLabels());
                             for (String f : wordTrainingVector.getGeneralFeatureVector().keySet()) {
                             if (f.startsWith("feature_attrValue_5gram_")
                             || f.startsWith("feature_word_5gram_")) {
                             System.out.println(">> " + f);
                             }
                             }*/
                            predicateWordTrainingData.get(predicate).get(attribute).add(wordTrainingVector);
                            if (!realization.get(w).getWord().equals(SFX.TOKEN_START)
                                    && !realization.get(w).getWord().equals(SFX.TOKEN_END)) {
                                subPhrase.add(realization.get(w).getWord());
                            }
                        }
                        if (!isValueMentioned) {
                            if (realization.get(w).getWord().startsWith(SFX.TOKEN_X)
                                    && (valueTBM.matches("[xX][0-9]+") || valueTBM.matches("\"[xX][0-9]+\"")
                                            || valueTBM.startsWith(SFX.TOKEN_X))) {
                                isValueMentioned = true;
                            } else if (!realization.get(w).getWord().startsWith(SFX.TOKEN_X)
                                    && !(valueTBM.matches("[xX][0-9]+") || valueTBM.matches("\"[xX][0-9]+\"")
                                            || valueTBM.startsWith(SFX.TOKEN_X))) {
                                String valueToCheck = valueTBM;
                                if (valueToCheck.equals("no") || valueToCheck.equals("yes")
                                        || valueToCheck.equals("yes or no") || valueToCheck.equals("none")
                                        || valueToCheck.equals("dont_care") || valueToCheck.equals("empty")) {
                                    String attribute = attrValue;
                                    if (attribute.contains("=")) {
                                        attribute = attrValue.substring(0, attrValue.indexOf('='));
                                    }
                                    valueToCheck = attribute + ":" + valueTBM;
                                }
                                if (!valueToCheck.equals("empty:empty")
                                        && valueAlignments.containsKey(valueToCheck)) {
                                    for (ArrayList<String> alignedStr : valueAlignments.get(valueToCheck)
                                            .keySet()) {
                                        if (endsWith(subPhrase, alignedStr)) {
                                            isValueMentioned = true;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (isValueMentioned) {
                                attrValuesAlreadyMentioned.add(attrValue);
                                attrValuesToBeMentioned.remove(attrValue);
                            }
                        }
                        String mentionedAttrValue = "";
                        if (!realization.get(w).getWord().startsWith(SFX.TOKEN_X)) {
                            for (String attrValueTBM : attrValuesToBeMentioned) {
                                if (attrValueTBM.contains("=")) {
                                    String value = attrValueTBM.substring(attrValueTBM.indexOf('=') + 1);
                                    if (!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+")
                                            || value.startsWith(SFX.TOKEN_X))) {
                                        String valueToCheck = value;
                                        if (valueToCheck.equals("no") || valueToCheck.equals("yes")
                                                || valueToCheck.equals("yes or no")
                                                || valueToCheck.equals("none")
                                                || valueToCheck.equals("dont_care")
                                                || valueToCheck.equals("empty")) {
                                            valueToCheck = attrValueTBM.replace("=", ":");
                                        }
                                        if (!valueToCheck.equals("empty:empty")
                                                && valueAlignments.containsKey(valueToCheck)) {
                                            for (ArrayList<String> alignedStr : valueAlignments
                                                    .get(valueToCheck).keySet()) {
                                                if (endsWith(subPhrase, alignedStr)) {
                                                    mentionedAttrValue = attrValueTBM;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (!mentionedAttrValue.isEmpty()) {
                            attrValuesAlreadyMentioned.add(mentionedAttrValue);
                            attrValuesToBeMentioned.remove(mentionedAttrValue);
                        }
                    }
                }
            }
            //}
        }
    }
    Object[] results = new Object[2];
    results[0] = predicateAttrTrainingData;
    results[1] = predicateWordTrainingData;
    return results;
}

From source file:ubic.gemma.association.phenotype.PhenotypeAssociationManagerServiceImpl.java

/**
 * Giving a phenotype searchQuery, returns a selection choice to the user
 * /*w w  w.  ja v a2  s  .  c  o  m*/
 * @param searchQuery query typed by the user
 * @param geneId the id of the chosen gene
 * @return Collection<CharacteristicValueObject> list of choices returned
 */
@Override
public Collection<CharacteristicValueObject> searchOntologyForPhenotypes(String searchQuery, Long geneId) {
    StopWatch timer = new StopWatch();
    timer.start();
    ArrayList<CharacteristicValueObject> orderedPhenotypesFromOntology = new ArrayList<CharacteristicValueObject>();

    boolean geneProvided = true;

    if (geneId == null) {
        geneProvided = false;
    }

    // prepare the searchQuery to correctly query the Ontology
    String newSearchQuery = prepareOntologyQuery(searchQuery);

    // search the Ontology with the new search query
    Set<CharacteristicValueObject> allPhenotypesFoundInOntology = this.ontologyHelper
            .findPhenotypesInOntology(newSearchQuery);

    // All phenotypes present on the gene (if the gene was given)
    Set<CharacteristicValueObject> phenotypesOnCurrentGene = null;

    if (geneProvided) {
        phenotypesOnCurrentGene = findUniquePhenotypesForGeneId(geneId);
    }

    // all phenotypes currently in the database
    Set<String> allPhenotypesInDatabase = this.associationService.loadAllPhenotypesUri();

    // rules to order the Ontology results found
    Set<CharacteristicValueObject> phenotypesWithExactMatch = new TreeSet<CharacteristicValueObject>();
    Set<CharacteristicValueObject> phenotypesAlreadyPresentOnGene = new TreeSet<CharacteristicValueObject>();
    Set<CharacteristicValueObject> phenotypesStartWithQueryAndInDatabase = new TreeSet<CharacteristicValueObject>();
    Set<CharacteristicValueObject> phenotypesStartWithQuery = new TreeSet<CharacteristicValueObject>();
    Set<CharacteristicValueObject> phenotypesSubstringAndInDatabase = new TreeSet<CharacteristicValueObject>();
    Set<CharacteristicValueObject> phenotypesSubstring = new TreeSet<CharacteristicValueObject>();
    Set<CharacteristicValueObject> phenotypesNoRuleFound = new TreeSet<CharacteristicValueObject>();

    /*
     * for each CharacteristicVO found from the Ontology, filter them and add them to a specific list if they
     * satisfied the condition
     */
    for (CharacteristicValueObject cha : allPhenotypesFoundInOntology) {

        // set flag for UI, flag if the phenotype is on the Gene or if in the database
        if (phenotypesOnCurrentGene != null && phenotypesOnCurrentGene.contains(cha)) {
            cha.setAlreadyPresentOnGene(true);
        } else if (allPhenotypesInDatabase.contains(cha.getValueUri())) {
            cha.setAlreadyPresentInDatabase(true);
        }

        // order the results by specific rules

        // Case 1, exact match
        if (cha.getValue().equalsIgnoreCase(searchQuery)) {
            phenotypesWithExactMatch.add(cha);
        }
        // Case 2, phenotype already present on Gene
        else if (phenotypesOnCurrentGene != null && phenotypesOnCurrentGene.contains(cha)) {
            phenotypesAlreadyPresentOnGene.add(cha);
        }
        // Case 3, starts with a substring of the word
        else if (cha.getValue().toLowerCase().startsWith(searchQuery.toLowerCase())) {
            if (allPhenotypesInDatabase.contains(cha.getValueUri())) {
                phenotypesStartWithQueryAndInDatabase.add(cha);
            } else {
                phenotypesStartWithQuery.add(cha);
            }
        }
        // Case 4, contains a substring of the word
        else if (cha.getValue().toLowerCase().indexOf(searchQuery.toLowerCase()) != -1) {
            if (allPhenotypesInDatabase.contains(cha.getValueUri())) {
                phenotypesSubstringAndInDatabase.add(cha);
            } else {
                phenotypesSubstring.add(cha);
            }
        } else {
            phenotypesNoRuleFound.add(cha);
        }
    }

    // place them in the correct order to display
    orderedPhenotypesFromOntology.addAll(phenotypesWithExactMatch);
    orderedPhenotypesFromOntology.addAll(phenotypesAlreadyPresentOnGene);
    orderedPhenotypesFromOntology.addAll(phenotypesStartWithQueryAndInDatabase);
    orderedPhenotypesFromOntology.addAll(phenotypesSubstringAndInDatabase);
    orderedPhenotypesFromOntology.addAll(phenotypesStartWithQuery);
    orderedPhenotypesFromOntology.addAll(phenotypesSubstring);
    orderedPhenotypesFromOntology.addAll(phenotypesNoRuleFound);

    // limit the size of the returned phenotypes to 100 terms
    if (orderedPhenotypesFromOntology.size() > MAX_PHENOTYPES_FROM_ONTOLOGY) {
        if (timer.getTime() > 1000) {
            log.info("Phenotype search: " + timer.getTime() + "ms");
        }
        return orderedPhenotypesFromOntology.subList(0, MAX_PHENOTYPES_FROM_ONTOLOGY);
    }
    if (timer.getTime() > 1000) {
        log.info("Phenotype search: " + timer.getTime() + "ms");
    }
    return orderedPhenotypesFromOntology;
}

From source file:com.hygenics.parser.GetImages.java

private void getImages() {
    // controls the web process from a removed method
    log.info("Setting Up Pull");
    String[] proxyarr = (proxies == null) ? null : proxies.split(",");
    // cleanup//from  www  . j a va2  s .c  o  m
    if (cleanup) {
        cleanupDir(fpath);
    }

    // image grab
    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);
    int numimages = 0;
    InputStream is;
    byte[] bytes;
    int iter = 0;
    int found = 0;

    // set proxy if needed
    if (proxyuser != null) {
        proxy(proxyhost, proxyport, https, proxyuser, proxypass);
    }

    int i = 0;
    ArrayList<String> postImages = new ArrayList<String>();
    ForkJoinPool fjp = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
    Set<Callable<String>> pulls = new HashSet<Callable<String>>();
    Set<Callable<ArrayList<String>>> sqls = new HashSet<Callable<ArrayList<String>>>();
    List<Future<String>> imageFutures;

    ArrayList<String> images;
    int chunksize = (int) Math.ceil(commitsize / numqueries);
    log.info("Chunksize: " + chunksize);
    if (baseurl != null || baseurlcolumn != null) {
        do {
            log.info("Offset: " + offset);
            log.info("Getting Images");
            images = new ArrayList<String>(commitsize);
            log.info("Getting Columns");
            for (int n = 0; n < numqueries; n++) {
                String tempsql = sql + " WHERE " + idString + " >= " + offset + " AND " + idString + " < "
                        + (offset + chunksize);

                if (conditions != null) {
                    tempsql += conditions;
                }

                sqls.add(new QueryDatabase(
                        ((extracondition != null) ? tempsql + " " + extracondition : tempsql)));

                offset += chunksize;
            }

            List<Future<ArrayList<String>>> futures = fjp.invokeAll(sqls);

            int w = 0;
            while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) {
                w++;
            }

            for (Future<ArrayList<String>> f : futures) {
                try {
                    ArrayList<String> fjson;
                    fjson = f.get();
                    if (fjson.size() > 0) {
                        images.addAll(fjson);
                    }

                    if (f.isDone() == false) {
                        f.cancel(true);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
            log.info(Integer.toString(images.size()) + " image links found. Pulling.");

            ArrayList<String> tempproxies = new ArrayList<String>();

            if (proxyarr != null) {
                for (String proxy : proxyarr) {
                    tempproxies.add(proxy.trim());
                }
            }

            if (maxproxies > 0) {
                maxproxies -= 1;// 0 and 1 should be equivalent conditions
                // --num is not like most 0 based still due
                // to >=
            }

            // get images
            for (int num = 0; num < images.size(); num++) {
                String icols = images.get(num);
                int proxnum = (int) Math.random() * (tempproxies.size() - 1);
                String proxy = (tempproxies.size() == 0) ? null : tempproxies.get(proxnum);

                // add grab
                pulls.add(new ImageGrabber(icols, proxy));

                if (proxy != null) {
                    tempproxies.remove(proxy);
                }

                // check for execution
                if (num + 1 == images.size() || pulls.size() >= commitsize || tempproxies.size() == 0) {
                    if (tempproxies.size() == 0 && proxies != null) {
                        tempproxies = new ArrayList<String>(proxyarr.length);

                        for (String p : proxyarr) {
                            tempproxies.add(p.trim());
                        }
                    }

                    imageFutures = fjp.invokeAll(pulls);
                    w = 0;

                    while (fjp.isQuiescent() == false && fjp.getActiveThreadCount() > 0) {
                        w++;
                    }

                    for (Future<String> f : imageFutures) {
                        String add;
                        try {
                            add = f.get();

                            if (add != null) {
                                postImages.add(add);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        }
                    }
                    imageFutures = null;// garbage collect elligible
                    pulls = new HashSet<Callable<String>>(commitsize);
                }

                if (postImages.size() >= commitsize && addtoDB == true) {
                    if (addtoDB) {
                        log.info("Posting to Database");
                        log.info("Found " + postImages.size() + " images");
                        numimages += postImages.size();
                        int size = (int) Math.floor(postImages.size() / numqueries);
                        for (int n = 0; n < numqueries; n++) {
                            if (((n + 1) * size) < postImages.size() && (n + 1) < numqueries) {
                                fjp.execute(new ImagePost(postImages.subList(n * size, (n + 1) * size)));
                            } else {
                                fjp.execute(new ImagePost(postImages.subList(n * size, postImages.size() - 1)));
                            }
                        }

                        w = 0;
                        while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) {
                            w++;
                        }
                    }
                    found += postImages.size();
                    postImages.clear();
                }

            }

            if (postImages.size() > 0 && addtoDB == true) {
                log.info("Posting to Database");
                numimages += postImages.size();
                int size = (int) Math.floor(postImages.size() / numqueries);
                for (int n = 0; n < numqueries; n++) {
                    if (((n + 1) * size) < postImages.size()) {
                        fjp.execute(new ImagePost(postImages.subList(n * size, (n + 1) * size)));
                    } else {
                        fjp.execute(new ImagePost(postImages.subList(n * size, postImages.size())));
                    }
                }

                w = 0;
                while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) {
                    w++;
                }

                found += postImages.size();
                postImages.clear();
            }

            // handle iterations specs
            iter += 1;
            log.info("Iteration: " + iter);
            if ((iter < iterations && found < images.size()) || tillfound == true) {
                log.info("Not All Images Obtained Trying Iteration " + iter + " of " + iterations);
                offset -= commitsize;
            } else if ((iter < iterations && found >= images.size()) && tillfound == false) {
                log.info("Images Obtained in " + iter + " iterations. Continuing.");
                iter = 0;
            } else {
                // precautionary
                log.info("Images Obtained in " + iter + " iterations. Continuing");
                iter = 0;
            }

        } while (images.size() > 0 && iter < iterations);

        if (fjp.isShutdown()) {
            fjp.shutdownNow();
        }
    }

    log.info("Complete. Check for Errors \n " + numimages + " Images Found");
}