<commit_before>package com.poco.PoCoRuntime;

import dk.brics.automaton.Automaton;
import dk.brics.automaton.RegExp;

/**
 * Created by caoyan on 1/8/15.
 */

public class SRELib {

    public static SRE PerformOPs(String operator,SRE sre1, SRE sre2) {
        if (isEmpty(sre1) && isEmpty(sre2))
            return null;

        String positiveRE= null, negativeRE=null;
        switch (operator) {
            case "Union":
                positiveRE = operate("Union", sre1.positiveRE(),
                        sre2.positiveRE());
                negativeRE = operate("InterSection", sre1.negativeRE(),
                        sre2.negativeRE());
                // negativeRE = operate("Minus", negativeRE, positiveRE);
                return new SRE(positiveRE, negativeRE);
            case "Punion":
                positiveRE = operate("Union", sre1.positiveRE(),
                        sre2.positiveRE());
                negativeRE = operate("InterSection", sre1.negativeRE(),
                        sre2.negativeRE());
                // negativeRE = operate("Minus", positiveRE, negativeRE);
                return new SRE(positiveRE, negativeRE);
            case "Disjunction":
                positiveRE = operate("InterSection", sre1.positiveRE(),
                        sre2.positiveRE());
                negativeRE = operate("Union", sre1.negativeRE(),
                        sre2.negativeRE());
                // negativeRE = operate("Minus", positiveRE, negativeRE);
                return new SRE(positiveRE, negativeRE);
            case "Conjunction":
                positiveRE = operate("InterSection", sre1.positiveRE(),
                        sre2.positiveRE());
                negativeRE = operate("Union", sre1.negativeRE(),
                        sre2.negativeRE());
                return new SRE(positiveRE, negativeRE);

            case "Equals":
                RegExp rePos1 = new RegExp(sre1.positiveRE().replace("%", ".*"));
                RegExp rePos2 = new RegExp(sre2.positiveRE().replace("%", ".*"));
                Automaton am1pos = rePos1.toAutomaton();
                Automaton am2pos = rePos2.toAutomaton();
                if (am1pos.equals(am2pos)) { //it positive part are same, then check negative part
                    RegExp reNeg1 = new RegExp(sre1.negativeRE().replace("%", ".*"));
                    RegExp reNeg2 = new RegExp(sre2.negativeRE().replace("%", ".*"));
                    Automaton am1Neg = reNeg1.toAutomaton();
                    Automaton am2Neg = reNeg2.toAutomaton();
                    if (am1Neg.equals(am2Neg)) {// if both negative is same then should bcm positive
                        Automaton amResult = am1pos.union(am1Neg);
                        if (!amResult.isEmpty())
                            positiveRE = amResult.toString();
                    }
                }
            default:
                return null;
        }
    }

    /**
     * This function use to operate set operations
     *
     * @param op
     *            This is the first parameter to specify the set operator, which
     *            can be union, punion, conjunction and disjunction
     * @param sre1
     *            This is the first sre value
     * @param sre2
     *            This is the second sre value
     * @return
     */
    private static String operate(String op, String sre1, String sre2) {
        String returnRe = null;
        if (!sre1.trim().equals("")) { // sre1pos is not empty
            if (!sre2.trim().equals("")) { // case both positive is not empty
                RegExp re1 = new RegExp(sre1.replace("%", ".*"));
                RegExp re2 = new RegExp(sre2.replace("%", ".*"));
                Automaton am1 = re1.toAutomaton();
                Automaton am2 = re2.toAutomaton();
                switch (op) {
                    case "Union":
                        returnRe = unionOP(am1, am2);
                        break;
                    case "InterSection":
                        returnRe = interSectOP(am1, am2);
                        break;
                    case "Minus":
                        returnRe = minusOP(am1, am2);
                        break;
                    default:
                        break;
                }
            } else { // sre2pos is empty, so union is just the sre1Pos
                returnRe = sre1;
            }
        } else { // sre1pos is empty
            if (!sre2.trim().equals("")) { // case both positive is not empty
                returnRe = sre2;
            }
        }
        return returnRe;
    }

    private static String unionOP(Automaton am1, Automaton am2) {
        Automaton amResult = am1.union(am2);
        if (!amResult.isEmpty())
            return amResult.toString();
        else
            return null;
    }

    private static String interSectOP(Automaton am1, Automaton am2) {
        Automaton amResult = am1.intersection(am2);
        if (!amResult.isEmpty())
            return amResult.toString();
        else
            return null;
    }

    private static String minusOP(Automaton am1, Automaton am2) {
        Automaton amResult = am1.minus(am2);
        if (!amResult.isEmpty())
            return amResult.toString();
        else
            return null;
    }

    private static boolean isEmpty(SRE sre) {
        String srePos = sre.positiveRE();
        String sreNeg = sre.negativeRE();
        if (srePos.trim().equals("") && sreNeg.trim().equals(""))
            return true;
        else
            return false;
    }
}
<commit_msg>add functions to SRELibs<commit_after>package com.poco.PoCoRuntime;

import dk.brics.automaton.Automaton;
import dk.brics.automaton.RegExp;

/**
 * Created by caoyan on 1/8/15.
 */

public class SRELib {
    /**
     * This function is used to perform Binary Set Operations on SRES
     *
     * @param operator
     *            It defines the set operator, which can be Union, Punion,
     *            Disjunction, Conjunction and Equals
     * @param sre1
     *            It is the first SRE value
     * @param sre2
     *            It is the second SRE value
     * @return the result SRE
     */
    public static SRE PerformBOPs(String operator, SRE sre1, SRE sre2) {
        if (isEmpty(sre1) && isEmpty(sre2))
            return null;

        String positiveRE = null, negativeRE = null;
        switch (operator) {
            // Positive favoring union of SREs
            case "Union":
                positiveRE = operate("Union", sre1.positiveRE(), sre2.positiveRE());
                negativeRE = operate("InterSection", sre1.negativeRE(),
                        sre2.negativeRE());
                if(negativeRE != null)
                    negativeRE = operate("Minus", negativeRE, positiveRE);
                break;

            // Negative favoring union of SREs
            case "Punion":
                positiveRE = operate("Union", sre1.positiveRE(), sre2.positiveRE());
                negativeRE = operate("InterSection", sre1.negativeRE(),
                        sre2.negativeRE());
                if(positiveRE != null)
                    positiveRE = operate("Minus", positiveRE, negativeRE);
                break;

            // Disjunction of SREs
            case "Disjunction":
                positiveRE = operate("Union", sre1.positiveRE(), sre2.positiveRE());
                negativeRE = operate("InterSection", sre1.negativeRE(),
                        sre2.negativeRE());
                break;

            // Conjunction of SREs
            case "Conjunction":
                positiveRE = operate("InterSection", sre1.positiveRE(),
                        sre2.positiveRE());
                negativeRE = operate("Union", sre1.negativeRE(), sre2.negativeRE());
                break;

            // Positive where SREs match in sign
            case "Equals":
                if (isEqual(sre1.positiveRE(), sre2.positiveRE())
                        && isEqual(sre1.negativeRE(), sre2.negativeRE()))
                    positiveRE = operate("Union", sre1.positiveRE(),
                            sre1.negativeRE());
                break;
            default:
                break;
        }
        return new SRE(positiveRE, negativeRE);
    }

    /**
     * This function is used to perform Binary Set Operations on SRES
     *
     * @param operator
     *            It defines the set operator, which can be Complement, action,
     *            result, positive and negative
     * @param sre1
     *            It is the first SRE value
     * @return the result SRE
     */
    public static SRE PerformUOPs(String operator, SRE sre) {
        if (isEmpty(sre))
            return null;

        switch (operator) {
            case "Complement": // Switches sign of SRE
                return new SRE(sre.getNegativeRE(), sre.getPositiveRE());
			/*
			 * case "Action": //Includes only the actions in SRE
			 *
			 * case "Result": //Includes only the results in SRE
			 */
            case "Positive": // Includes only positive portion of SRE
                return new SRE(sre.getPositiveRE(), null);
            case "Negative": // Includes only negative portion of SRE
                return new SRE(null, sre.getNegativeRE());
            default:
                return sre;
        }
    }

    public static SRE GetBaseSRE(SRE sre) {
        Class<BopSRE> classBS = BopSRE.class;
        Class<UopSRE> classUS = UopSRE.class;
        Class<? extends SRE> classChild = sre.getClass();
        if (classBS.isAssignableFrom(classChild)) { // BopSRE
            BopSRE bopSre = (BopSRE) sre;
            SRE result = SRELib.PerformBOPs(bopSre.getSrebop(),
                    GetBaseSRE(bopSre.sre1), GetBaseSRE(bopSre.sre2));
            // System.out.println(result);
            return result;
        } else if (classUS.isAssignableFrom(classChild)) { // UopSRE
            UopSRE uopSre = (UopSRE) sre;
            SRE result = SRELib.PerformUOPs(uopSre.getSreuop(),
                    GetBaseSRE(uopSre.sre));
            // System.out.println(result);
            return result;
        } else
            return sre;
    }

    /**
     * This function use to operate set operations
     *
     * @param op
     *            This is the first parameter to specify the set operator, which
     *            can be union, punion, conjunction and disjunction
     * @param sre1
     *            This is the first sre value
     * @param sre2
     *            This is the second sre value
     * @return
     */
    private static String operate(String op, String sre1, String sre2) {
        if(sre1 == null || sre2== null)
            return null;

        String returnRe = null;
        if (!sre1.trim().equals("")) { // sre1pos is not empty
            if (!sre2.trim().equals("")) { // case both positive is not empty
                RegExp re1 = new RegExp(sre1.replace("%", ".*"));
                RegExp re2 = new RegExp(sre2.replace("%", ".*"));
                Automaton am1 = re1.toAutomaton();
                Automaton am2 = re2.toAutomaton();
                switch (op) {
                    case "Union":
                        returnRe = unionOP(am1, am2);
                        break;
                    case "InterSection":
                        returnRe = interSectOP(am1, am2);
                        break;
                    case "Minus":
                        returnRe = minusOP(am1, am2);
                        break;
                    default:
                        break;
                }
            } else { // sre2pos is empty, so union is just the sre1Pos
                returnRe = sre1;
            }
        } else { // sre1pos is empty
            if (!sre2.trim().equals("")) { // case both positive is not empty
                returnRe = sre2;
            }
        }
        return returnRe;
    }

    private static String unionOP(Automaton am1, Automaton am2) {
        Automaton amResult = am1.union(am2);
        if (!amResult.isEmpty())
            return amResult.toString();
        else
            return null;
    }

    private static String interSectOP(Automaton am1, Automaton am2) {
        Automaton amResult = am1.intersection(am2);
        if (!amResult.isEmpty())
            return amResult.toString();
        else
            return null;
    }

    private static String minusOP(Automaton am1, Automaton am2) {
        Automaton amResult = am1.minus(am2);
        if (!amResult.isEmpty())
            return amResult.toString();
        else
            return null;
    }

    private static boolean isEmpty(SRE sre) {
        String srePos = sre.positiveRE();
        String sreNeg = sre.negativeRE();
        if (srePos.trim().equals("") && sreNeg.trim().equals(""))
            return true;
        else
            return false;
    }

    private static boolean isEqual(String sre1, String sre2) {
        RegExp re1 = new RegExp(sre1.replace("%", ".*"));
        RegExp re2 = new RegExp(sre2.replace("%", ".*"));
        Automaton am1 = re1.toAutomaton();
        Automaton am2 = re2.toAutomaton();
        if (am1.equals(am2))
            return true;
        else
            return false;
    }
}
{'fraction_non_alphanumeric': 0.1053782907143396, 'fraction_numerical': 0.013049709587387794, 'mean_word_length': 1.5048176837332325, 'pattern_counts': {'":': 21, '<': 6, '<?xml version=': 0, '>': 6, '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': '1359259', 'n_tokens_mistral': 3773, 'n_tokens_neox': 3565, 'n_words': 940}