<commit_before>package lemming.context.inbound;

import lemming.context.Context;
import lemming.context.ContextDao;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import java.util.List;
import java.util.SortedMap;

/**
 * A form for the verification of inbound contexts.
 */
class InboundContextVerificationForm extends Form<InboundContextPackage> {
    /**
     * Creates a context verify form.
     *
     * @param model model of a group of inbound contexts
     */
    public InboundContextVerificationForm(IModel<InboundContextPackage> model) {
        super("InboundContextVerificationForm", model);
        add(new SubmitButton(this));
    }

    private class SubmitButton extends AjaxButton {

        public SubmitButton(Form<InboundContextPackage> form) {
            super("submitButton", form);
        }

        /**
         * Match contexts by hash.
         */
        private void matchContextsByHash() {
            InboundContextPackage contextPackage = (InboundContextPackage) getForm().getModelObject();
            new InboundContextPackageDao().matchContextsByHash(contextPackage);
        }

        private void alignContextsStep2() {
            ContextDao contextDao = new ContextDao();
            InboundContextDao inboundContextDao = new InboundContextDao();
            InboundContextPackageDao inboundContextPackageDao = new InboundContextPackageDao();
            InboundContextPackage contextPackage = (InboundContextPackage) getForm().getModelObject();
            List<InboundContext> allContexts = inboundContextPackageDao.getContexts(contextPackage);
            List<InboundContext> unmatchedContexts = inboundContextPackageDao.findUnmatchedContexts(contextPackage);

            System.err.println(allContexts.size());
            System.err.println(unmatchedContexts.size());
        }

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            super.onSubmit(target, form);

            matchContextsByHash();
            alignContextsStep2();
        }
    }
}<commit_msg>Simplify code in InboundContextVerificationForm. Compute matching triples.<commit_after>package lemming.context.inbound;

import lemming.context.Context;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import javax.ws.rs.core.MultivaluedMap;
import java.util.ArrayList;
import java.util.List;

/**
 * A form for the verification of inbound contexts.
 */
class InboundContextVerificationForm extends Form<InboundContextPackage> {
    /**
     * Creates a context verify form.
     *
     * @param model model of a group of inbound contexts
     */
    public InboundContextVerificationForm(IModel<InboundContextPackage> model) {
        super("InboundContextVerificationForm", model);
        add(new SubmitButton(this));
    }

    private class SubmitButton extends AjaxButton {

        public SubmitButton(Form<InboundContextPackage> form) {
            super("submitButton", form);
        }

        /**
         * Match contexts by hash.
         */
        private void matchContextsByHash() {
            InboundContextPackage contextPackage = (InboundContextPackage) getForm().getModelObject();
            new InboundContextPackageDao().matchContextsByHash(contextPackage);
        }

        /**
         * Finds and sets locations with unmatched contexts.
         */
        private List<String> findUnmatchedLocations(InboundContextPackage contextPackage) {
            return new InboundContextPackageDao().findUnmatchedContextLocations(contextPackage);
        }

        private List<MatchHelper.Triple> computeMatchingTriplets(InboundContextPackage contextPackage, String location) {
            InboundContextPackageDao inboundContextPackageDao = new InboundContextPackageDao();
            MultivaluedMap<Integer, InboundContext> groupedContexts = inboundContextPackageDao
                    .groupUnmatchedContextsByLocation(contextPackage, location);
            List<MatchHelper.Triple> allMatchingTriples = new ArrayList<>();

            for (Integer key : groupedContexts.keySet()) {
                List<InboundContext> contexts = groupedContexts.get(key);
                List<Context> complements = new InboundContextDao().findComplements(contexts);

                if (complements != null) {
                    MultivaluedMap<Integer, MatchHelper.Triple> tripleMap = MatchHelper
                            .getTriples(contexts, complements);
                    List<MatchHelper.Triple> matchingTriples = MatchHelper.computeMatchingTriples(tripleMap);
                    allMatchingTriples.addAll(matchingTriples);
                }
            }

            return allMatchingTriples;
        }

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            super.onSubmit(target, form);
            InboundContextPackage contextPackage = (InboundContextPackage) getForm().getModelObject();
            List<String> unmatchedLocations;

            matchContextsByHash();
            unmatchedLocations = findUnmatchedLocations(contextPackage);

            for (String location : unmatchedLocations) {
                List<MatchHelper.Triple> matchingTriples = computeMatchingTriplets(contextPackage, location);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08403511915427343, 'fraction_numerical': 0.00035835871707579287, 'mean_word_length': 2.950460014154282, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7251769', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1427, 'n_words': 278}