<commit_before>package org.intermine.template;

/*
 * Copyright (C) 2002-2011 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.commons.lang.StringEscapeUtils;
import org.intermine.pathquery.PathConstraint;
import org.intermine.pathquery.PathConstraintLoop;
import org.intermine.pathquery.PathConstraintSubclass;
import org.intermine.pathquery.PathQuery;
import org.intermine.template.xml.TemplateQueryBinding;

/**
 * A template query, which consists of a PathQuery, description, category,
 * short name.
 *
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
public class TemplateQuery extends PathQuery
{
    /** Template query name. */
    protected String name;
    /** Template query title. */
    protected String title;
    /** The private comment for this query. */
    protected String comment;
    /** Whether this is an edited version of another template. */
    protected boolean edited = false;

    /** List of those Constraints that are editable */
    protected List<PathConstraint> editableConstraints = new ArrayList<PathConstraint>();
    /** Descriptions of constraints */
    protected Map<PathConstraint, String> constraintDescriptions =
        new HashMap<PathConstraint, String>();
    /** Configuration for switch-off-ability of constraints */
    protected Map<PathConstraint, SwitchOffAbility> constraintSwitchOffAbility =
        new HashMap<PathConstraint, SwitchOffAbility>();

    /**
     * Construct a new instance of TemplateQuery.
     *
     * @param name the name of the template
     * @param title the short title of this template for showing in list
     * @param comment an optional private comment for this template
     * @param query the query itself
     */
    public TemplateQuery(String name, String title, String comment, PathQuery query) {
        super(query);
        this.name = name;
        this.title = title;
        this.comment = comment;
    }

    /**
     * Copy constructor.
     * Construct a new template that is the same in all respects as the one
     * passed to the constructor.
     * @param prototype The template to copy.
     */
    public TemplateQuery(TemplateQuery prototype) {
        super(prototype);
        this.name = prototype.name;
        this.title = prototype.title;
        this.comment = prototype.comment;
        this.edited = prototype.edited;
        this.editableConstraints
            = new ArrayList<PathConstraint>(prototype.editableConstraints);
        this.constraintDescriptions
            = new HashMap<PathConstraint, String>(prototype.constraintDescriptions);
        this.constraintSwitchOffAbility =
            new HashMap<PathConstraint, SwitchOffAbility>(prototype.constraintSwitchOffAbility);
    }

    /**
     * Clone this TemplateQuery.
     *
     * @return a TemplateQuery
     */
    @Override
    public synchronized TemplateQuery clone() {
        return new TemplateQuery(this);
    }

    /**
     * Fetch the PathQuery to execute for this template.  The returned query excludes any optional
     * constraints that have been sbitched off before the template is executed.
     * @return the PathQuery that should be executed for this query
     */
    @Override
    public PathQuery getQueryToExecute() {
        TemplateQuery queryToExecute = this.clone();
        for (PathConstraint con : queryToExecute.getEditableConstraints()) {
            if (SwitchOffAbility.OFF.equals(getSwitchOffAbility(con))) {
                queryToExecute.removeConstraint(con);
            }
        }
        return queryToExecute;
    }

    /**
     * Sets a constraint to be editable or not. If setting a constraint to be editable, and it is
     * not already editable, then the constraint will be added to the end of the editable
     * constraints list.<commit_msg>Ugly hack to satisfy checkstyle's iron boot.<commit_after>package org.intermine.template;

/*
 * Copyright (C) 2002-2011 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.commons.lang.StringEscapeUtils;
import org.intermine.pathquery.PathConstraint;
import org.intermine.pathquery.PathConstraintLoop;
import org.intermine.pathquery.PathConstraintSubclass;
import org.intermine.pathquery.PathQuery;
import org.intermine.template.xml.TemplateQueryBinding;

/**
 * A template query, which consists of a PathQuery, description, category,
 * short name.
 *
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
public class TemplateQuery extends PathQuery
{
    /** Template query name. */
    protected String name;
    /** Template query title. */
    protected String title;
    /** The private comment for this query. */
    protected String comment;
    /** Whether this is an edited version of another template. */
    protected boolean edited = false;

    /** List of those Constraints that are editable */
    protected List<PathConstraint> editableConstraints = new ArrayList<PathConstraint>();
    /** Descriptions of constraints */
    protected Map<PathConstraint, String> constraintDescriptions =
        new HashMap<PathConstraint, String>();
    /** Configuration for switch-off-ability of constraints */
    protected Map<PathConstraint, SwitchOffAbility> constraintSwitchOffAbility =
        new HashMap<PathConstraint, SwitchOffAbility>();

    /**
     * Construct a new instance of TemplateQuery.
     *
     * @param name the name of the template
     * @param title the short title of this template for showing in list
     * @param comment an optional private comment for this template
     * @param query the query itself
     */
    public TemplateQuery(String name, String title, String comment, PathQuery query) {
        super(query);
        this.name = name;
        this.title = title;
        this.comment = comment;
    }

    /**
     * Copy constructor.
     * Construct a new template that is the same in all respects as the one
     * passed to the constructor.
     * @param prototype The template to copy.
     */
    public TemplateQuery(TemplateQuery prototype) {
        super(prototype);
        this.name = prototype.name;
        this.title = prototype.title;
        this.comment = prototype.comment;
        this.edited = prototype.edited;
        this.editableConstraints
            = new ArrayList<PathConstraint>(prototype.editableConstraints);
        this.constraintDescriptions
            = new HashMap<PathConstraint, String>(prototype.constraintDescriptions);
        this.constraintSwitchOffAbility =
            new HashMap<PathConstraint, SwitchOffAbility>(prototype.constraintSwitchOffAbility);
    }

    /**
     * Clone this TemplateQuery.
     *
     * @return a TemplateQuery
     */
    @Override
    public synchronized TemplateQuery clone() {
        super.clone();
        return new TemplateQuery(this);
    }

    /**
     * Fetch the PathQuery to execute for this template.  The returned query excludes any optional
     * constraints that have been sbitched off before the template is executed.
     * @return the PathQuery that should be executed for this query
     */
    @Override
    public PathQuery getQueryToExecute() {
        TemplateQuery queryToExecute = this.clone();
        for (PathConstraint con : queryToExecute.getEditableConstraints()) {
            if (SwitchOffAbility.OFF.equals(getSwitchOffAbility(con))) {
                queryToExecute.removeConstraint(con);
            }
        }
        return queryToExecute;
    }

    /**
     * Sets a constraint to be editable or not. If setting a constraint to be editable, and it is
     * not already editable, then the constraint will be added to the end of the editable
     * constraints list.
{'fraction_non_alphanumeric': 0.08286038592508513, 'fraction_numerical': 0.0018161180476730988, 'mean_word_length': 3.0943308550185873, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, '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': '3524037', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2200, 'n_words': 744}