Example usage for java.util ArrayList clone

List of usage examples for java.util ArrayList clone

Introduction

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

Prototype

public Object clone() 

Source Link

Document

Returns a shallow copy of this ArrayList instance.

Usage

From source file:com.xpn.xwiki.plugin.chronopolys.FolderManager.java

public ArrayList<Object> getProjectContainers(XWikiContext context) throws XWikiException {
    ArrayList list = null;
    String key = context.getDatabase() + ":" + context.getLocalUser() + ":folders";

    initFoldersCache(context);/*from w ww  . j av a 2  s  .c om*/
    synchronized (key) {
        try {
            // retreive folders list from cache
            list = (ArrayList) foldersCache.getFromCache(key);
        } catch (XWikiCacheNeedsRefreshException e) {
            foldersCache.cancelUpdate(key);
            list = new ArrayList<Object>();

            if (plugin.getUserManager().isAdmin(context) || plugin.getUserManager().isManager(context)) {
                // admin & managers (get all folders)
                String hql = ", BaseObject as obj where obj.name=doc.fullName" + " and obj.className='"
                        + CLASS_FOLDER + "' and doc.web='" + FOLDERS_SPACE + "'";
                List containerPages = context.getWiki().getStore().searchDocumentsNames(hql, context);
                Iterator it = containerPages.iterator();
                BaseObject bobj;
                while (it.hasNext()) {
                    String docName = (String) it.next();
                    bobj = context.getWiki().getDocument(docName, context).getObject(CLASS_FOLDER);
                    Object obj = new Object(bobj, context);
                    list.add(obj);
                }
            } else {
                // basic users (builds the list from the user's projects)
                List allProjects = plugin.getProjectManager().getProjects(context);
                Iterator it = allProjects.iterator();
                while (it.hasNext()) {
                    Object project = (Object) it.next();
                    String folderName = FOLDERS_SPACE + "." + (String) project.display("container", "view");
                    XWikiDocument currentFolder = context.getWiki().getDocument(folderName, context);
                    if (!currentFolder.isNew()) {
                        // Let's climb the tree to its root (sic)
                        while (currentFolder != null) {
                            // get the folder object and put it in the list if it's not already in it
                            BaseObject bobj = currentFolder.getObject(CLASS_FOLDER);
                            if (bobj != null) {
                                Object obj = new Object(bobj, context);
                                if (!list.contains(obj)) {
                                    list.add(obj);
                                }
                                String parent = currentFolder.getStringValue("parent");
                                if (parent != null && !parent.equals("")) {
                                    currentFolder = context.getWiki().getDocument(FOLDERS_SPACE + "." + parent,
                                            context);
                                } else {
                                    currentFolder = null;
                                }
                            } else {
                                currentFolder = null;
                            }
                        }
                    }
                }
            }
            foldersCache.putInCache(key, list);
        }
    }

    return (ArrayList<Object>) list.clone();
}

From source file:it.cnr.icar.eric.client.ui.thin.RegistryObjectCollectionBean.java

/**
 * This method is used to get RegistryObjectBeans that are selected in the
 * results table plus any bookmarked beans
 *
 * @return java.util.Collection//w  w w  .j a v  a 2s  . c  om
 * A Collection of selected RegistryObjectBeans
 */
public Collection<?> getAllSelectedRegistryObjectBeans() {
    @SuppressWarnings("unchecked")
    Collection<Object> allSelected = (Collection<Object>) getSelectedRegistryObjectBeans();
    UIData data = getPinnedScrollerBean().getData();
    int n = (data != null) ? data.getRowCount() : 0;
    if (n > 0) {
        ArrayList<?> rob = (ArrayList<?>) data.getValue();
        for (int i = 0; i < n; i++) {
            data.setRowIndex(i);
            if (((RegistryObjectBean) rob.get(i)).isPinned()) {
                // save selected ROB in a Collection                      
                allSelected.add(rob.clone());
            }
        }
    }
    return allSelected;
}

From source file:org.opencyc.constraintsolver.ForwardCheckingSearcher.java

/**
 * Recurses to instantiate the constraint rule in the constraint problem KB microtheory with
 * all remaining variables as bindings, marking the domain values as permitted with
 * <tt>Boolean</tt> <tt>true</tt>.
 *
 * @param instantiatedRule the constraint rule
 * @param remainingVariables the variables left to instantiate in the constraint rule
 * @param bindings the instantiated values for variables already instantiated in the
 * constraint rule/*  w ww  .j  a  v a  2 s  .  c  o m*/
 */
protected void markPermittedRemainingBindings(CycList instantiatedRule, ArrayList remainingVariables,
        ArrayList bindings, Binding currentBinding, ConstraintRule currentRule)
        throws IOException, CycApiException {
    CycVariable selectedVariable = currentBinding.getCycVariable();
    if (remainingVariables.size() == 0) {
        // This is the terminating recursion case, with no more variables left to instantiate.
        boolean instantiatedRuleResult = CycAccess.current().isQueryTrue_Cached(instantiatedRule,
                constraintProblem.mt);
        if (verbosity > 2) {
            System.out.println("Bindings " + bindings);
            System.out.println(instantiatedRule.cyclify() + " --> " + instantiatedRuleResult);
        }
        if (instantiatedRuleResult) {
            CycVariable variable;
            Object value;
            for (int i = 0; i < bindings.size(); i++) {
                Binding binding = (Binding) bindings.get(i);
                variable = binding.getCycVariable();
                if (!variable.equals(selectedVariable)) {
                    value = binding.getValue();
                    if (valueDomains.domainHasValue(variable, value)) {
                        if (verbosity > 2)
                            System.out.println(
                                    "  " + binding.cyclify() + " is permitted by " + currentBinding.cyclify());
                        valueDomains.markDomain(variable, value, Boolean.TRUE);
                    }
                }
            }
        }
        return;
    } else if (remainingVariables.size() == 1) {
        // One variable left, handle the special cases where individual value
        // instantiation is not efficient.
        CycVariable variable = (CycVariable) remainingVariables.get(0);
        if (valueDomains.getUnmarkedDomainSize(variable) > ASK_ALL_OR_INDIV_THRESHOLD) {
            // Special case it is more efficient to ask for all the bindings and mark
            // them rather than to ask for them individually.
            if (verbosity > 2)
                System.out.println("Variable exceeds ask-all threshold " + variable);
            ArrayList domainValues = this.askWithVariable(instantiatedRule, variable);
            for (int i = 0; i < domainValues.size(); i++) {
                Object value = domainValues.get(i);
                if (valueDomains.domainHasValue(variable, value)) {
                    if (verbosity > 2)
                        System.out.println("  " + (new Binding(variable, value)).cyclify() + " is permitted by "
                                + currentBinding.cyclify());
                    valueDomains.markDomain(variable, value, Boolean.TRUE);
                }

            }
        }
        return;
    } else {
        // Recurse to instantiate the remaining variables.
        CycVariable variable = (CycVariable) remainingVariables.get(0);
        ArrayList domainValues = valueDomains.getUnmarkedDomainValues(variable);
        int limit = valueDomains.getUnmarkedDomainSize(variable);
        if (verbosity > 4) {
            System.out.println("variable " + variable);
            System.out.println("  domain " + domainValues);
            System.out.println("  limit  " + limit);
        }
        Object value;
        CycList newInstantiatedRule;
        ArrayList newBindings = new ArrayList();
        for (int i = 0; i < limit; i++) {
            value = domainValues.get(i);
            Binding newCurrentBinding = new Binding(variable, value);
            newBindings.addAll(bindings);
            newBindings.add(new Binding(variable, value));
            newInstantiatedRule = instantiatedRule.subst(value, variable);
            if (verbosity > 4)
                System.out.println("  instantiated rule " + newInstantiatedRule.cyclify());
            ArrayList newRemainingVariables = (ArrayList) remainingVariables.clone();
            newRemainingVariables.remove(0);
            this.markPermittedRemainingBindings(newInstantiatedRule, newRemainingVariables, newBindings,
                    newCurrentBinding, currentRule);
        }
    }
}

From source file:msi.gama.outputs.layers.charts.ChartJFreeChartOutputScatter.java

@SuppressWarnings("unchecked")
@Override/*from   ww w .  j  a  v a2  s . c  om*/
protected void resetSerie(final IScope scope, final String serieid) {
    // TODO Auto-generated method stub

    final ChartDataSeries dataserie = chartdataset.getDataSeries(scope, serieid);
    final XYIntervalSeries serie = ((XYIntervalSeriesCollection) jfreedataset
            .get(IdPosition.get(dataserie.getSerieId(scope)))).getSeries(0);
    serie.clear();
    final ArrayList<Double> XValues = dataserie.getXValues(scope);
    final ArrayList<Double> YValues = dataserie.getYValues(scope);
    final ArrayList<Double> SValues = dataserie.getSValues(scope);
    boolean secondaxis = false;
    if (dataserie.getMysource().getUseSecondYAxis(scope)) {
        secondaxis = true;
        this.setUseSecondYAxis(scope, true);

    }

    if (XValues.size() > 0) {
        final NumberAxis domainAxis = (NumberAxis) ((XYPlot) this.chart.getPlot()).getDomainAxis();
        NumberAxis rangeAxis = (NumberAxis) ((XYPlot) this.chart.getPlot()).getRangeAxis(0);
        int ids = IdPosition.get(dataserie.getSerieId(scope));
        if (secondaxis) {
            //         rangeAxis = (NumberAxis) ((XYPlot) this.chart.getPlot()).getRangeAxis(1);
            //            ((XYPlot) this.chart.getPlot()).setRangeAxis(IdPosition.get(dataserie.getSerieId(scope)),rangeAxis);
            //         ((XYPlot) this.chart.getPlot()).setRangeAxis(IdPosition.get(dataserie.getSerieId(scope)),rangeAxis);
            ((XYPlot) this.chart.getPlot()).mapDatasetToRangeAxis(ids, 1);
        } else {
            //            ((XYPlot) this.chart.getPlot()).setRangeAxis(IdPosition.get(dataserie.getSerieId(scope)),rangeAxis);
            ((XYPlot) this.chart.getPlot()).mapDatasetToRangeAxis(ids, 0);

        }
        domainAxis.setAutoRange(false);
        rangeAxis.setAutoRange(false);
        // domainAxis.setRange(Math.min((double)(Collections.min(XValues)),0),
        // Math.max(Collections.max(XValues),Collections.min(XValues)+1));
        // rangeAxis.setRange(Math.min((double)(Collections.min(YValues)),0),
        // Math.max(Collections.max(YValues),Collections.min(YValues)+1));
        XYIntervalDataItem newval;
        for (int i = 0; i < XValues.size(); i++) {
            if (dataserie.isUseYErrValues()) {
                if (dataserie.isUseXErrValues()) {
                    newval = new XYIntervalDataItem(XValues.get(i), dataserie.xerrvaluesmin.get(i),
                            dataserie.xerrvaluesmax.get(i), YValues.get(i), dataserie.yerrvaluesmin.get(i),
                            dataserie.yerrvaluesmax.get(i));
                    // serie.add(XValues.get(i),dataserie.xerrvaluesmin.get(i),dataserie.xerrvaluesmax.get(i),YValues.get(i),dataserie.yerrvaluesmin.get(i),dataserie.yerrvaluesmax.get(i));
                } else {
                    newval = new XYIntervalDataItem(XValues.get(i), XValues.get(i), XValues.get(i),
                            YValues.get(i), dataserie.yerrvaluesmin.get(i), dataserie.yerrvaluesmax.get(i));
                    // serie.add(XValues.get(i),XValues.get(i),XValues.get(i),YValues.get(i),dataserie.yerrvaluesmin.get(i),dataserie.yerrvaluesmax.get(i));
                }

            } else {
                if (dataserie.isUseXErrValues()) {
                    newval = new XYIntervalDataItem(XValues.get(i), dataserie.xerrvaluesmin.get(i),
                            dataserie.xerrvaluesmax.get(i), YValues.get(i), YValues.get(i), YValues.get(i));
                    // serie.add(XValues.get(i),dataserie.xerrvaluesmin.get(i),dataserie.xerrvaluesmax.get(i),YValues.get(i),YValues.get(i),YValues.get(i));
                } else {
                    newval = new XYIntervalDataItem(XValues.get(i), XValues.get(i), XValues.get(i),
                            YValues.get(i), YValues.get(i), YValues.get(i));
                    // serie.add(XValues.get(i),XValues.get(i),XValues.get(i),YValues.get(i),YValues.get(i),YValues.get(i));
                }

            }
            serie.add(newval, false);
        }
        // domainAxis.setAutoRange(true);
        // rangeAxis.setAutoRange(true);
    }
    // resetAutorange(scope);
    if (SValues.size() > 0) {
        MarkerScale.remove(serieid);
        final ArrayList<Double> nscale = (ArrayList<Double>) SValues.clone();
        MarkerScale.put(serieid, nscale);

    }

    this.resetRenderer(scope, serieid);

}

From source file:org.dspace.app.xmlui.aspect.administrative.item.EditItemBitstreamsForm.java

public void addBody(Body body) throws SQLException, WingException {
    // Get our parameters and state
    int itemID = parameters.getParameterAsInteger("itemID", -1);
    Item item = Item.find(context, itemID);
    String baseURL = contextPath + "/admin/item?administrative-continue=" + knot.getId();

    // DIVISION: main div
    Division main = body.addInteractiveDivision("edit-item-status", contextPath + "/admin/item",
            Division.METHOD_POST, "primary administrative item");
    main.setHead(T_option_head);/* w w w  .  j a  va2 s  . c  om*/

    // LIST: options
    List options = main.addList("options", List.TYPE_SIMPLE, "horizontal");
    options.addItem().addXref(baseURL + "&submit_status", T_option_status);
    options.addItem().addHighlight("bold").addXref(baseURL + "&submit_bitstreams", T_option_bitstreams);
    options.addItem().addXref(baseURL + "&submit_metadata", T_option_metadata);
    options.addItem().addXref(baseURL + "&view_item", T_option_view);
    options.addItem().addXref(baseURL + "&submit_curate", T_option_curate);

    // TABLE: Bitstream summary
    Table files = main.addTable("editItemBitstreams", 1, 1);

    files.setHead(T_head1);

    Row header = files.addRow(Row.ROLE_HEADER);
    header.addCellContent(T_column1);
    header.addCellContent(T_column2);
    header.addCellContent(T_column3);
    header.addCellContent(T_column4);
    header.addCellContent(T_column5);
    header.addCellContent(T_column6);
    header.addCellContent(T_column7);

    Bundle[] bundles = item.getBundles();

    boolean showBitstreamUpdateOrderButton = false;
    for (Bundle bundle : bundles) {

        Cell bundleCell = files.addRow("bundle_head_" + bundle.getID(), Row.ROLE_DATA, "").addCell(1, 5);
        bundleCell.addContent(T_bundle_label.parameterize(bundle.getName()));

        Bitstream[] bitstreams = bundle.getBitstreams();
        ArrayList<Integer> bitstreamIdOrder = new ArrayList<Integer>();
        for (Bitstream bitstream : bitstreams) {
            bitstreamIdOrder.add(bitstream.getID());
        }

        for (int bitstreamIndex = 0; bitstreamIndex < bitstreams.length; bitstreamIndex++) {
            Bitstream bitstream = bitstreams[bitstreamIndex];
            boolean primary = (bundle.getPrimaryBitstreamID() == bitstream.getID());
            String name = bitstream.getName();

            if (name != null && name.length() > 50) {
                // If the fiel name is too long the shorten it so that it will display nicely.
                String shortName = name.substring(0, 15);
                shortName += " ... ";
                shortName += name.substring(name.length() - 25, name.length());
                name = shortName;
            }

            String description = bitstream.getDescription();
            String format = null;
            BitstreamFormat bitstreamFormat = bitstream.getFormat();
            if (bitstreamFormat != null) {
                format = bitstreamFormat.getShortDescription();
            }
            String editURL = contextPath + "/admin/item?administrative-continue=" + knot.getId()
                    + "&bitstreamID=" + bitstream.getID() + "&submit_edit";
            String viewURL = contextPath + "/bitstream/id/" + bitstream.getID() + "/" + bitstream.getName();

            Row row = files.addRow("bitstream_row_" + bitstream.getID(), Row.ROLE_DATA, "");
            CheckBox remove = row.addCell().addCheckBox("remove");
            remove.setLabel("remove");
            remove.addOption(bundle.getID() + "/" + bitstream.getID());
            if (!AuthorizeManager.authorizeActionBoolean(context, item, Constants.REMOVE)) {
                remove.setDisabled();
            }

            if (AuthorizeManager.authorizeActionBoolean(context, bitstream, Constants.WRITE)) {
                // The user can edit the bitstream give them a link.
                Cell cell = row.addCell();
                cell.addXref(editURL, name);
                if (primary) {
                    cell.addXref(editURL, T_primary_label);
                }

                row.addCell().addXref(editURL, description);
                row.addCell().addXref(editURL, format);
            } else {
                // The user can't edit the bitstream just show them it.
                Cell cell = row.addCell();
                cell.addContent(name);
                if (primary) {
                    cell.addContent(T_primary_label);
                }

                row.addCell().addContent(description);
                row.addCell().addContent(format);
            }

            Highlight highlight = row.addCell().addHighlight("fade");
            highlight.addContent("[");
            highlight.addXref(viewURL, T_view_link);
            highlight.addContent("]");

            if (AuthorizeManager.authorizeActionBoolean(context, bundle, Constants.WRITE)) {
                Cell cell = row.addCell("bitstream_order_" + bitstream.getID(), Cell.ROLE_DATA, "");
                //Add the +1 to make it more human readable
                cell.addHidden("order_" + bitstream.getID()).setValue(String.valueOf(bitstreamIndex + 1));
                showBitstreamUpdateOrderButton = true;
                Button upButton = cell.addButton(
                        "submit_order_" + bundle.getID() + "_" + bitstream.getID() + "_up",
                        ((bitstreamIndex == 0) ? "disabled" : "") + " icon-button arrowUp ");
                if ((bitstreamIndex == 0)) {
                    upButton.setDisabled();
                }
                upButton.setValue(T_order_up);
                upButton.setHelp(T_order_up);
                Button downButton = cell.addButton(
                        "submit_order_" + bundle.getID() + "_" + bitstream.getID() + "_down",
                        (bitstreamIndex == (bitstreams.length - 1) ? "disabled" : "")
                                + " icon-button arrowDown ");
                if (bitstreamIndex == (bitstreams.length - 1)) {
                    downButton.setDisabled();
                }
                downButton.setValue(T_order_down);
                downButton.setHelp(T_order_down);

                //These values will only be used IF javascript is disabled or isn't working
                cell.addHidden(bundle.getID() + "_" + bitstream.getID() + "_up_value")
                        .setValue(retrieveOrderUpButtonValue((java.util.List<Integer>) bitstreamIdOrder.clone(),
                                bitstreamIndex));
                cell.addHidden(bundle.getID() + "_" + bitstream.getID() + "_down_value").setValue(
                        retrieveOrderDownButtonValue((java.util.List<Integer>) bitstreamIdOrder.clone(),
                                bitstreamIndex));
            } else {
                row.addCell().addContent(String.valueOf(bitstreamIndex));
            }
        }
    }

    if (AuthorizeManager.authorizeActionBoolean(context, item, Constants.ADD)) {
        Cell cell = files.addRow().addCell(1, 5);
        cell.addXref(contextPath + "/admin/item?administrative-continue=" + knot.getId() + "&submit_add",
                T_submit_add);
    } else {
        Cell cell = files.addRow().addCell(1, 5);
        cell.addHighlight("fade").addContent(T_no_upload);
    }

    // PARA: actions
    Para actions = main.addPara("editItemActionsP", "editItemActionsP");
    if (showBitstreamUpdateOrderButton) {
        //Add a button to submit the new order (this button is hidden & will be displayed by the javascript)
        //Should javascript be disabled for some reason this button isn't used.
        actions.addButton("submit_update_order", "hidden").setValue(T_submit_reorder);
    }

    // Only System Administrators can delete bitstreams
    if (AuthorizeManager.authorizeActionBoolean(context, item, Constants.REMOVE)) {
        actions.addButton("submit_delete").setValue(T_submit_delete);
    } else {
        Button button = actions.addButton("submit_delete");
        button.setValue(T_submit_delete);
        button.setDisabled();

        main.addPara().addHighlight("fade").addContent(T_no_remove);
    }
    actions.addButton("submit_return").setValue(T_submit_return);

    main.addHidden("administrative-continue").setValue(knot.getId());

}

From source file:imitationNLG.SFX.java

public Double evaluateGeneration(HashMap<String, JAROW> classifierAttrs,
        HashMap<String, HashMap<String, JAROW>> classifierWords, ArrayList<DatasetInstance> trainingData,
        ArrayList<DatasetInstance> testingData, HashMap<String, HashSet<String>> availableAttributeActions,
        HashMap<String, HashMap<String, HashSet<Action>>> availableWordActions,
        HashMap<Integer, HashSet<String>> nGrams, boolean printResults, int epoch) {
    System.out.println("Evaluate argument generation ");

    int totalArgDistance = 0;
    ArrayList<ScoredFeaturizedTranslation<IString, String>> generations = new ArrayList<>();
    ArrayList<ArrayList<Action>> generationActions = new ArrayList<>();
    HashMap<ArrayList<Action>, DatasetInstance> generationActionsMap = new HashMap<>();
    ArrayList<ArrayList<Sequence<IString>>> finalReferences = new ArrayList<>();
    ArrayList<String> predictedStrings = new ArrayList<>();
    ArrayList<String> predictedStringMRs = new ArrayList<>();
    ArrayList<Double> attrCoverage = new ArrayList<>();
    ArrayList<ArrayList<String>> predictedAttrLists = new ArrayList<>();
    HashSet<HashMap<String, HashSet<String>>> mentionedAttrs = new HashSet<HashMap<String, HashSet<String>>>();
    for (DatasetInstance di : testingData) {
        String predicate = di.getMeaningRepresentation().getPredicate();
        ArrayList<Action> predictedActionList = new ArrayList<>();
        ArrayList<Action> predictedWordList = new ArrayList<>();

        //PHRASE GENERATION EVALUATION
        String predictedAttr = "";
        ArrayList<String> predictedAttrValues = new ArrayList<>();
        ArrayList<String> predictedAttributes = new ArrayList<>();

        HashSet<String> attrValuesToBeMentioned = new HashSet<>();
        HashSet<String> attrValuesAlreadyMentioned = new HashSet<>();
        HashMap<String, ArrayList<String>> valuesToBeMentioned = new HashMap<>();
        for (String attribute : di.getMeaningRepresentation().getAttributes().keySet()) {
            for (String value : di.getMeaningRepresentation().getAttributes().get(attribute)) {
                attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase());
            }/*from ww  w  . ja va2  s.  c o  m*/
            valuesToBeMentioned.put(attribute,
                    new ArrayList<>(di.getMeaningRepresentation().getAttributes().get(attribute)));
        }
        if (attrValuesToBeMentioned.isEmpty()) {
            attrValuesToBeMentioned.add("empty=empty");
        }
        HashSet<String> attrValuesToBeMentionedCopy = new HashSet<>(attrValuesToBeMentioned);
        while (!predictedAttr.equals(SFX.TOKEN_END) && predictedAttrValues.size() < maxAttrRealizationSize) {
            if (!predictedAttr.isEmpty()) {
                attrValuesToBeMentioned.remove(predictedAttr);
            }
            Instance attrTrainingVector = SFX.this.createAttrInstance(predicate, "@TOK@", predictedAttrValues,
                    predictedActionList, attrValuesAlreadyMentioned, attrValuesToBeMentioned,
                    di.getMeaningRepresentation(), availableAttributeActions);

            if (attrTrainingVector != null) {
                Prediction predictAttr = classifierAttrs.get(predicate).predict(attrTrainingVector);
                if (predictAttr.getLabel() != null) {
                    predictedAttr = predictAttr.getLabel().trim();
                    String predictedValue = "";
                    if (!predictedAttr.equals(SFX.TOKEN_END)) {
                        predictedValue = chooseNextValue(predictedAttr, attrValuesToBeMentioned, trainingData);

                        HashSet<String> rejectedAttrs = new HashSet<String>();
                        while (predictedValue.isEmpty() && !predictedAttr.equals(SFX.TOKEN_END)) {
                            rejectedAttrs.add(predictedAttr);

                            predictedAttr = SFX.TOKEN_END;
                            double maxScore = -Double.MAX_VALUE;
                            for (String attr : predictAttr.getLabel2Score().keySet()) {
                                if (!rejectedAttrs.contains(attr) && (Double
                                        .compare(predictAttr.getLabel2Score().get(attr), maxScore) > 0)) {
                                    maxScore = predictAttr.getLabel2Score().get(attr);
                                    predictedAttr = attr;
                                }
                            }
                            if (!predictedAttr.equals(SFX.TOKEN_END)) {
                                predictedValue = chooseNextValue(predictedAttr, attrValuesToBeMentioned,
                                        trainingData);
                            }
                        }
                    }
                    if (!predictedAttr.equals(SFX.TOKEN_END)) {
                        predictedAttr += "=" + predictedValue;
                    }
                    predictedAttrValues.add(predictedAttr);

                    String attribute = predictedAttrValues.get(predictedAttrValues.size() - 1).split("=")[0];
                    String attrValue = predictedAttrValues.get(predictedAttrValues.size() - 1);
                    predictedAttributes.add(attrValue);

                    //GENERATE PHRASES
                    if (!attribute.equals(SFX.TOKEN_END)) {
                        if (classifierWords.get(predicate).containsKey(attribute)) {
                            String predictedWord = "";

                            boolean isValueMentioned = false;
                            String valueTBM = "";
                            if (attrValue.contains("=")) {
                                valueTBM = attrValue.substring(attrValue.indexOf('=') + 1);
                            }
                            if (valueTBM.isEmpty()) {
                                isValueMentioned = true;
                            }
                            ArrayList<String> subPhrase = new ArrayList<>();
                            while (!predictedWord.equals(RoboCup.TOKEN_END)
                                    && predictedWordList.size() < maxWordRealizationSize) {
                                ArrayList<String> predictedAttributesForInstance = new ArrayList<>();
                                for (int i = 0; i < predictedAttributes.size() - 1; i++) {
                                    predictedAttributesForInstance.add(predictedAttributes.get(i));
                                }
                                if (!predictedAttributes.get(predictedAttributes.size() - 1)
                                        .equals(attrValue)) {
                                    predictedAttributesForInstance
                                            .add(predictedAttributes.get(predictedAttributes.size() - 1));
                                }
                                Instance wordTrainingVector = createWordInstance(predicate,
                                        new Action("@TOK@", attrValue), predictedAttributesForInstance,
                                        predictedActionList, isValueMentioned, attrValuesAlreadyMentioned,
                                        attrValuesToBeMentioned, di.getMeaningRepresentation(),
                                        availableWordActions.get(predicate), nGrams, false);

                                if (wordTrainingVector != null) {
                                    if (classifierWords.get(predicate) != null) {
                                        if (classifierWords.get(predicate).get(attribute) != null) {
                                            Prediction predictWord = classifierWords.get(predicate)
                                                    .get(attribute).predict(wordTrainingVector);
                                            if (predictWord.getLabel() != null) {
                                                predictedWord = predictWord.getLabel().trim();
                                                predictedActionList.add(new Action(predictedWord, attrValue));
                                                if (!predictedWord.equals(SFX.TOKEN_END)) {
                                                    subPhrase.add(predictedWord);
                                                    predictedWordList.add(new Action(predictedWord, attrValue));
                                                }
                                            } else {
                                                predictedWord = SFX.TOKEN_END;
                                                predictedActionList.add(new Action(predictedWord, attrValue));
                                            }
                                        } else {
                                            predictedWord = SFX.TOKEN_END;
                                            predictedActionList.add(new Action(predictedWord, attrValue));
                                        }
                                    }
                                }
                                if (!isValueMentioned) {
                                    if (!predictedWord.equals(SFX.TOKEN_END)) {
                                        if (predictedWord.startsWith(SFX.TOKEN_X)
                                                && (valueTBM.matches("\"[xX][0-9]+\"")
                                                        || valueTBM.matches("[xX][0-9]+")
                                                        || valueTBM.startsWith(SFX.TOKEN_X))) {
                                            isValueMentioned = true;
                                        } else if (!predictedWord.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")) {
                                                if (attribute.contains("=")) {
                                                    valueToCheck = attribute.replace("=", ":");
                                                } else {
                                                    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 (!predictedWord.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(attrValue);
                                    attrValuesToBeMentioned.remove(mentionedAttrValue);
                                }
                            }
                            if (predictedWordList.size() >= maxWordRealizationSize && !predictedActionList
                                    .get(predictedActionList.size() - 1).getWord().equals(SFX.TOKEN_END)) {
                                predictedWord = SFX.TOKEN_END;
                                predictedActionList.add(new Action(predictedWord,
                                        predictedAttrValues.get(predictedAttrValues.size() - 1)));
                            }
                        } else {
                            String predictedWord = SFX.TOKEN_END;
                            predictedActionList.add(new Action(predictedWord, attrValue));
                        }
                    }
                } else {
                    predictedAttr = SFX.TOKEN_END;
                }
            }
        }
        ArrayList<String> predictedAttrs = new ArrayList<>();
        for (String attributeValuePair : predictedAttrValues) {
            predictedAttrs.add(attributeValuePair.split("=")[0]);
        }

        ArrayList<Action> cleanActionList = new ArrayList<Action>();
        for (Action action : predictedActionList) {
            if (!action.getWord().equals(SFX.TOKEN_END) && !action.getWord().equals(SFX.TOKEN_START)) {
                cleanActionList.add(action);
            }
        }
        for (int i = 0; i < cleanActionList.size(); i++) {
            for (ArrayList<Action> surrounds : punctPatterns.keySet()) {
                boolean matches = true;
                int m = 0;
                for (int s = 0; s < surrounds.size(); s++) {
                    if (surrounds.get(s) != null) {
                        if (i + s < cleanActionList.size()) {
                            if (!cleanActionList.get(i + s).getWord().equals(surrounds.get(s)
                                    .getWord()) /*|| !cleanActionList.get(i).getAttribute().equals(surrounds.get(s).getAttribute())*/) {
                                matches = false;
                                s = surrounds.size();
                            } else {
                                m++;
                            }
                        } else {
                            matches = false;
                            s = surrounds.size();
                        }
                    }
                }
                if (matches && m > 0) {
                    cleanActionList.add(i + 2, punctPatterns.get(surrounds));
                }
            }
        }

        String predictedString = "";
        ArrayList<String> predictedAttrList = new ArrayList<String>();
        HashSet<String> redundants = new HashSet<String>();
        for (Action action : cleanActionList) {
            if (action.getWord().startsWith(SFX.TOKEN_X)) {
                predictedString += di.getMeaningRepresentation().getDelexMap().get(action.getWord()) + " ";
                //predictedString += "x ";
                if (di.getMeaningRepresentation().getDelexMap().get(action.getWord()) == null
                        || di.getMeaningRepresentation().getDelexMap().get(action.getWord()).equals("null")) {
                    redundants.add(action.getWord());
                }
            } else {
                predictedString += action.getWord() + " ";
            }
            if (predictedAttrList.isEmpty()) {
                predictedAttrList.add(action.getAttribute());
            } else if (!predictedAttrList.get(predictedAttrList.size() - 1).equals(action.getAttribute())) {
                predictedAttrList.add(action.getAttribute());
            }
        }
        predictedAttrLists.add(predictedAttrList);
        if (attrValuesToBeMentionedCopy.size() != 0.0) {
            double redundAttrs = 0.0;
            double missingAttrs = 0.0;
            for (String attr : predictedAttrList) {
                if (!attrValuesToBeMentionedCopy.contains(attr)) {
                    redundAttrs += 1.0;
                }
            }
            for (String attr : attrValuesToBeMentionedCopy) {
                if (!predictedAttrList.contains(attr)) {
                    missingAttrs += 1.0;
                }
            }
            double attrSize = (double) attrValuesToBeMentionedCopy.size();
            attrCoverage.add((redundAttrs + missingAttrs) / attrSize);
        }

        if (predicate.startsWith("?")) {
            predictedString = predictedString.trim() + "?";
        } else {
            predictedString = predictedString.trim() + ".";
        }
        predictedString = predictedString.replaceAll("\\?", " \\? ").replaceAll(":", " : ")
                .replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ").trim();

        if (!mentionedAttrs.contains(di.getMeaningRepresentation().getAttributes())) {
            predictedStrings.add(predictedString);
            predictedStringMRs.add(di.getMeaningRepresentation().getMRstr());
            mentionedAttrs.add(di.getMeaningRepresentation().getAttributes());
        }

        Sequence<IString> translation = IStrings
                .tokenize(NISTTokenizer.tokenize(predictedString.toLowerCase()));
        ScoredFeaturizedTranslation<IString, String> tran = new ScoredFeaturizedTranslation<>(translation, null,
                0);
        generations.add(tran);
        generationActions.add(predictedActionList);
        generationActionsMap.put(predictedActionList, di);

        ArrayList<Sequence<IString>> references = new ArrayList<>();
        for (ArrayList<Action> realization : di.getEvalRealizations()) {
            String cleanedWords = "";
            for (Action nlWord : realization) {
                if (!nlWord.equals(new Action(SFX.TOKEN_START, ""))
                        && !nlWord.equals(new Action(SFX.TOKEN_END, ""))) {
                    if (nlWord.getWord().startsWith(SFX.TOKEN_X)) {
                        cleanedWords += di.getMeaningRepresentation().getDelexMap().get(nlWord.getWord()) + " ";
                    } else {
                        cleanedWords += nlWord.getWord() + " ";
                    }
                }
            }
            cleanedWords = cleanedWords.trim();
            if (!cleanedWords.endsWith(".")) {
                cleanedWords += ".";
            }
            cleanedWords = cleanedWords.replaceAll("\\?", " \\? ").replaceAll(":", " : ")
                    .replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ").trim();
            references.add(IStrings.tokenize(NISTTokenizer.tokenize(cleanedWords)));
        }
        finalReferences.add(references);

        //EVALUATE ATTRIBUTE SEQUENCE
        HashSet<ArrayList<String>> goldAttributeSequences = new HashSet<>();
        for (DatasetInstance di2 : testingData) {
            if (di2.getMeaningRepresentation().getAttributes()
                    .equals(di.getMeaningRepresentation().getAttributes())) {
                goldAttributeSequences.addAll(di2.getEvalMentionedAttributeSequences().values());
            }
        }

        int minTotArgDistance = Integer.MAX_VALUE;
        for (ArrayList<String> goldArgs : goldAttributeSequences) {
            int totArgDistance = 0;
            HashSet<Integer> matchedPositions = new HashSet<>();
            for (int i = 0; i < predictedAttrs.size(); i++) {
                if (!predictedAttrs.get(i).equals(SFX.TOKEN_START)
                        && !predictedAttrs.get(i).equals(SFX.TOKEN_END)) {
                    int minArgDistance = Integer.MAX_VALUE;
                    int minArgPos = -1;
                    for (int j = 0; j < goldArgs.size(); j++) {
                        if (!matchedPositions.contains(j)) {
                            if (goldArgs.get(j).equals(predictedAttrs.get(i))) {
                                int argDistance = Math.abs(j - i);

                                if (argDistance < minArgDistance) {
                                    minArgDistance = argDistance;
                                    minArgPos = j;
                                }
                            }
                        }
                    }

                    if (minArgPos == -1) {
                        totArgDistance += 100;
                    } else {
                        matchedPositions.add(minArgPos);
                        totArgDistance += minArgDistance;
                    }
                }
            }
            ArrayList<String> predictedCopy = (ArrayList<String>) predictedAttrs.clone();
            for (String goldArg : goldArgs) {
                if (!goldArg.equals(SFX.TOKEN_END)) {
                    boolean contained = predictedCopy.remove(goldArg);
                    if (!contained) {
                        totArgDistance += 1000;
                    }
                }
            }
            if (totArgDistance < minTotArgDistance) {
                minTotArgDistance = totArgDistance;
            }
        }
        totalArgDistance += minTotArgDistance;
    }

    previousResults = generationActions;

    crossAvgArgDistances.add(totalArgDistance / (double) testingData.size());

    NISTMetric NIST = new NISTMetric(finalReferences);
    BLEUMetric BLEU = new BLEUMetric(finalReferences, 4, false);
    BLEUMetric BLEUsmooth = new BLEUMetric(finalReferences, 4, true);
    Double nistScore = NIST.score(generations);
    Double bleuScore = BLEU.score(generations);
    Double bleuSmoothScore = BLEUsmooth.score(generations);

    double finalCoverage = 0.0;
    for (double c : attrCoverage) {
        finalCoverage += c;
    }
    finalCoverage /= (double) attrCoverage.size();
    crossNIST.add(nistScore);
    crossBLEU.add(bleuScore);
    crossBLEUSmooth.add(bleuSmoothScore);
    System.out.println("Avg arg distance: \t" + totalArgDistance / (double) testingData.size());
    System.out.println("NIST: \t" + nistScore);
    System.out.println("BLEU: \t" + bleuScore);
    System.out.println("COVERAGE: \t" + finalCoverage);
    System.out.println("g: " + generations);
    System.out.println("attr: " + predictedAttrLists);
    System.out.println("BLEU smooth: \t" + bleuSmoothScore);
    previousBLEU = bleuScore;

    if (printResults) {
        BufferedWriter bw = null;
        File f = null;
        try {
            f = new File("random_SFX" + dataset + "TextsAfter" + (epoch) + "_"
                    + JDAggerForSFX.earlyStopMaxFurtherSteps + "_" + JDAggerForSFX.p + "epochsTESTINGDATA.txt");
        } catch (NullPointerException e) {
            System.err.println("File not found." + e);
        }

        try {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
        } catch (FileNotFoundException e) {
            System.err.println("Error opening file for writing! " + e);
        }

        try {
            bw.write("BLEU:" + bleuScore);
            bw.write("\n");
        } catch (IOException e) {
            System.err.println("Write error!");
        }
        for (int i = 0; i < predictedStrings.size(); i++) {
            try {
                //Grafoume to String sto arxeio
                //SFX HOTEL TEXTS WITH LOLS -> 3
                //SFX RESTAURANT TEXTS WITH LOLS -> 5
                bw.write("MR;" + predictedStringMRs.get(i).replaceAll(";", ",") + ";");
                if (dataset.equals("hotel")) {
                    bw.write("LOLS_SFHOT;");
                } else {
                    bw.write("LOLS_SFRES;");
                }
                //bw.write("@@srcdoc@@" + (i + 1));
                /*String out = predictedStrings.get(i).replaceAll(" i ", " I ").replaceAll(" -ly ", "ly ").replaceAll(" s ", "s ").replaceAll("\\?", " \\? ").replaceAll(":", " : ").replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ");
                out = out.substring(0, 1).toUpperCase() + out.substring(1);
                bw.write(out + ";");
                if (dataset.equals("hotel")) {
                bw.write("WEN_SFHOT;");
                } else {
                bw.write("WEN_SFRES;");
                }
                if (!wenDaToGen.containsKey(predictedStringMRs.get(i).trim().toLowerCase())) {
                System.out.println(wenDaToGen.keySet());
                System.out.println(predictedStringMRs.get(i).trim().toLowerCase());
                System.exit(0);
                }
                out = wenDaToGen.get(predictedStringMRs.get(i).trim().toLowerCase()).replaceAll(" i ", " I ").replaceAll(" -ly ", "ly ").replaceAll(" s ", "s ").replaceAll("\\?", " \\? ").replaceAll(":", " : ").replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ");
                out = out.substring(0, 1).toUpperCase() + out.substring(1);
                bw.write(out + ";");*/
                //bw.write("@@judgeFluency@@-1");
                //bw.write("@@judgeInform@@-1");
                //bw.write("@@judgeQuality@@-1");

                bw.write("\n");
            } catch (IOException e) {
                System.err.println("Write error!");
            }
        }

        try {
            bw.close();
        } catch (IOException e) {
            System.err.println("Error closing file.");
        } catch (Exception e) {
        }
    }
    return bleuScore;
}