<commit_before>/*
 * Copyright (c) 2014, Absolute Performance, Inc. http://www.absolute-performance.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ublu.util;

import ublu.util.Generics.FunctorParamList;
import ublu.util.Generics.TupleNameList;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * Class for defining executable parameterized functors.
 *
 * @author jwoehr
 */
public class Functor implements Serializable {

    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
    }

    private void readObject(java.io.ObjectInputStream in)
            throws IOException, ClassNotFoundException {
        in.defaultReadObject();
    }

    private void readObjectNoData()
            throws ObjectStreamException {
    }

    private Functor() {
        super();
    }

    /**
     * Instance from an execution block and a list of parameter names
     *
     * @param block execution block
     * @param fpl list of parameter names (without decoration)
     */
    public Functor(String block, FunctorParamList fpl) {
        setBlock(block);
        setFunctorParamList(fpl);
    }
    private String block;

    /**
     * Return the execution block
     *
     * @return the execution block
     */
    public String getBlock() {
        return block;
    }

    /**
     * Set the execution block
     *
     * @param block the execution block
     */
    public final void setBlock(String block) {
        this.block = block;
    }
    FunctorParamList functorParamList;

    /**
     * Set the parameter name list
     *
     * @param functorParamList the parameter name list
     */
    public final void setFunctorParamList(FunctorParamList functorParamList) {
        this.functorParamList = functorParamList;
    }

    /**
     * Add a param name to the param list
     *
     * @param paramName param name to add
     * @return the functor (this)
     */
    public Functor addParam(String paramName) {
        functorParamList.add(paramName);
        return this;
    }

    /**
     * Get the parameter name list
     *
     * @return the parameter name list
     */
    public FunctorParamList getFunctorParamList() {
        return functorParamList;
    }

    /**
     * Resolve all decorated occurrences of decorated param names in the block
     * to tuple names from the substitution list
     *
     * @param tnl substitution list of tuple names
     * @return the bound block
     */
    public String bind(TupleNameList tnl) {
        String s = getBlock();
        String y;
        for (int i = 0; i < getFunctorParamList().size(); i++) {
            String rgx = "@@" + getFunctorParamList().get(i);
            y = s.replaceAll(rgx, tnl.get(i));
            s = y;
            // /* Debug */ System.err.println("block after replacement " + s);
        }
        return s;
    }

    /**
     * Resolve all decorated occurrences of decorated param names in the block
     * to ParamSubstitutionTuple names from the substitution list
     *
     * @param interpreter
     * @param tnl substitution list of tuple names
     * @return the bound block
     */
    public String bindWithSubstitutes(Interpreter interpreter, TupleNameList tnl) {
        tnl = subTupleNameList(interpreter, tnl);
        // /* debug */ System.err.println("Tuple name list is " + tnl);
        FunctorParamList fpl = getFunctorParamList();
        String s = getBlock();
        String y;
        for (int i = 0; i < fpl.size(); i++) {
            String rgx = "@@" + fpl.get(i);
            y = s.replaceAll(rgx, tnl.get(i));
            s = y;
            // /* Debug */ System.err.println("block after ParamSubstitutionTuple replacement " + s);
        }
        return s;
    }

    /**
     * Replace the tuple names in the tuple name list passed to function with
     * substitute temporary tuples
     *
     * @param interpreter
     * @param tnl
     * @return the list with all tuple names replaced with the names of the
     * substitute temporaries<commit_msg>Fixed bug in function argument token pasting ... a tuple in the list with a longer name than a previous element in the list where the prev element was a substring of the longer name got clobbered by the regex.<commit_after>/*
 * Copyright (c) 2014, Absolute Performance, Inc. http://www.absolute-performance.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ublu.util;

import ublu.util.Generics.FunctorParamList;
import ublu.util.Generics.TupleNameList;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * Class for defining executable parameterized functors.
 *
 * @author jwoehr
 */
public class Functor implements Serializable {

    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
    }

    private void readObject(java.io.ObjectInputStream in)
            throws IOException, ClassNotFoundException {
        in.defaultReadObject();
    }

    private void readObjectNoData()
            throws ObjectStreamException {
    }

    private Functor() {
        super();
    }

    /**
     * Instance from an execution block and a list of parameter names
     *
     * @param block execution block
     * @param fpl list of parameter names (without decoration)
     */
    public Functor(String block, FunctorParamList fpl) {
        setBlock(block);
        setFunctorParamList(fpl);
    }
    private String block;

    /**
     * Return the execution block
     *
     * @return the execution block
     */
    public String getBlock() {
        return block;
    }

    /**
     * Set the execution block
     *
     * @param block the execution block
     */
    public final void setBlock(String block) {
        this.block = block;
    }
    FunctorParamList functorParamList;

    /**
     * Set the parameter name list
     *
     * @param functorParamList the parameter name list
     */
    public final void setFunctorParamList(FunctorParamList functorParamList) {
        this.functorParamList = functorParamList;
    }

    /**
     * Add a param name to the param list
     *
     * @param paramName param name to add
     * @return the functor (this)
     */
    public Functor addParam(String paramName) {
        functorParamList.add(paramName);
        return this;
    }

    /**
     * Get the parameter name list
     *
     * @return the parameter name list
     */
    public FunctorParamList getFunctorParamList() {
        return functorParamList;
    }

    /**
     * Resolve all decorated occurrences of decorated param names in the block
     * to tuple names from the substitution list
     *
     * @param tnl substitution list of tuple names
     * @return the bound block
     */
    public String bind(TupleNameList tnl) {
        String s = getBlock();
        String y;
        for (int i = 0; i < getFunctorParamList().size(); i++) {
            String rgx = "@@" + getFunctorParamList().get(i);
            y = s.replaceAll(rgx, tnl.get(i));
            s = y;
            // /* Debug */ System.err.println("block after replacement " + s);
        }
        return s;
    }

    /**
     * Resolve all decorated occurrences of decorated param names in the block
     * to ParamSubstitutionTuple names from the substitution list
     *
     * @param interpreter
     * @param tnl substitution list of tuple names
     * @return the bound block
     */
    public String bindWithSubstitutes(Interpreter interpreter, TupleNameList tnl) {
        tnl = subTupleNameList(interpreter, tnl);
        // /* debug */ System.err.println("Tuple name list is " + tnl);
        FunctorParamList fpl = getFunctorParamList();
        String s = getBlock();
        String y;
        for (int i = 0; i < fpl.size(); i++) {
            String rgx = "@@" + fpl.get(i) + "\\s";
            y = s.replaceAll(rgx, tnl.get(i) + " ");
            s = y;
            // /* Debug */ System.err.println("block after ParamSubstitutionTuple replacement " + s);
        }
        return s;
    }

    /**
     * Replace the tuple names in the tuple name list passed to function with
     * substitute temporary tuples
     *
     * @param interpreter
     * @param tnl
     * @return the list with all tuple names replaced with the names of the
     * substitute temporaries
{'fraction_non_alphanumeric': 0.07962827988338192, 'fraction_numerical': 0.0010932944606413995, 'mean_word_length': 2.6025598949786675, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6244417', 'n_tokens_mistral': 3003, 'n_tokens_neox': 2818, 'n_words': 1110}