<commit_before>/*
 * Copyright (c) 2005, 2006, Regents of the University of California
 * 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.  
 *
 * * Neither the name of the University of California, Berkeley nor
 *   the names of its contributors may be used to endorse or promote
 *   products derived from this software without specific prior 
 *   written permission.
 *
 * 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 OWNER 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 blog.model;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import blog.EqualsCPD;
import blog.bn.BasicVar;
import blog.bn.RandFuncAppVar;
import blog.distrib.CondProbDistrib;
import blog.sample.EvalContext;

/**
 * Represents a clause in dependency statements and number statements.
 * 
 * Each clause has a predicate, a conditional probability distribution (CPD),
 * and a list of arguments to this conditional probability distribution. Each
 * argument is assumed to be of class ArgSpec. If the condition is checked and
 * turns out to be true, then the arguments are evaluated and the CPD is used to
 * sample a value for these arguments.
 */
public class Clause {

	/**
	 * Creates a new clause.
	 * 
	 * @param cond
	 *          the condition under which this clause applies
	 * @param cpdClass
	 *          the class of the conditional probability distribution used in this
	 *          clause
	 * @param cpdParams
	 *          List of ArgSpec objects representing CPD parameters. These must be
	 *          non-random and must contain no free variables.
	 * @param cpdArgs
	 *          List of ArgSpec objects whose denotations will be passed to the
	 *          CPD each time it is invoked
	 */
	public Clause(Formula cond, Class cpdClass, List<ArgSpec> cpdParams,
			List<ArgSpec> cpdArgs) {
		this.cond = cond;
		this.cpdClass = cpdClass;
		this.cpdParams = cpdParams;
		this.cpd = null;
		this.cpdArgs = cpdArgs;
		this.cpdArgsAndParams = null;
	}

	/**
	 * Create a new clause where it is not yet known which fields are
	 * parameters and which are arguments.
	 * 
	 * @param cond
	 *          the condition under which this clause applies
	 * @param cpdCLass
	 *          the class of the conditional probability distribution used.
	 * @param cpdArgsAndParams
	 *          A mixed list of arguments and parameters.
	 */
	public Clause(Formula cond, Class cpdClass, List<ArgSpec> cpdArgsAndParams) {
		this.cond = cond;
		this.cpdClass = cpdClass;
		this.cpdParams = null;
		this.cpdArgs = null;
		this.cpdArgsAndParams = cpdArgsAndParams;
	}

	/**
	 * Creates a new clause using the given CondProbDistrib object.
	 * 
	 * @param cond
	 *          the condition under which this clause applies
	 * @param cpd
	 *          conditional probability distribution for this clause
	 * @param cpdArgs
	 *          List of ArgSpec objects whose denotations will be passed to the
	 *          CPD each time it is invoked
	 */
	public Clause(Formula cond, CondProbDistrib cpd, List<ArgSpec> cpdArgs) {
		this.cond = cond;
		this.cpdClass = cpd.getClass();
		this.cpdParams = Collections.emptyList();
		this.cpd = cpd;
		this.cpdArgs = cpdArgs;
		this.cpdArgsAndParams = null;
	}

	public void setCond(Formula cond) {
		this.cond = cond;
	}

	public Formula getCond() {
		return cond;
	}

	public Class getCPDClass() {
		if (cpd != null) {
			return cpd.getClass();
		}
		return cpdClass;
	}

	public CondProbDistrib getCPD() {

		return cpd;

	}

	/**
	 * @return List of ArgSpec objects
	 */
	public List getArgs() {

		return cpdArgs;

	}

	/**
	 * Returns this clause's CPD, and the values of this clause's arguments
	 * evaluated in the given context. Returns null if the partial world in the
	 * given context is not complete enough to evaluate the arguments.
	 */
	public DependencyModel.Distrib getDistrib(EvalContext context) {
		context.pushEvaluee(this);
		List argValues = new ArrayList();

		for (Iterator iter = cpdArgs.iterator(); iter.hasNext();) {
			ArgSpec argSpec = (ArgSpec) iter.next();
			if (!argSpec.isDetermined(context.getPartialWorld())) {
				break;
			}
			Object argValue = argSpec.evaluate(context);
			if (argValue == null) {
				break; // CPD arg not determined
			}
			argValues.add(argValue);
		}

		context.popEvaluee();
		if (argValues.size() == cpdArgs.size()) {
			// all CPD args were determined<commit_msg>Reverted change from Clause; needed to make nearly all models run<commit_after>/*
 * Copyright (c) 2005, 2006, Regents of the University of California
 * 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.  
 *
 * * Neither the name of the University of California, Berkeley nor
 *   the names of its contributors may be used to endorse or promote
 *   products derived from this software without specific prior 
 *   written permission.
 *
 * 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 OWNER 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 blog.model;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import blog.EqualsCPD;
import blog.bn.BasicVar;
import blog.bn.RandFuncAppVar;
import blog.distrib.CondProbDistrib;
import blog.sample.EvalContext;

/**
 * Represents a clause in dependency statements and number statements.
 * 
 * Each clause has a predicate, a conditional probability distribution (CPD),
 * and a list of arguments to this conditional probability distribution. Each
 * argument is assumed to be of class ArgSpec. If the condition is checked and
 * turns out to be true, then the arguments are evaluated and the CPD is used to
 * sample a value for these arguments.
 */
public class Clause {

	/**
	 * Creates a new clause.
	 * 
	 * @param cond
	 *          the condition under which this clause applies
	 * @param cpdClass
	 *          the class of the conditional probability distribution used in this
	 *          clause
	 * @param cpdParams
	 *          List of ArgSpec objects representing CPD parameters. These must be
	 *          non-random and must contain no free variables.
	 * @param cpdArgs
	 *          List of ArgSpec objects whose denotations will be passed to the
	 *          CPD each time it is invoked
	 */
	public Clause(Formula cond, Class cpdClass, List<ArgSpec> cpdParams,
			List<ArgSpec> cpdArgs) {
		this.cond = cond;
		this.cpdClass = cpdClass;
		this.cpdParams = cpdParams;
		this.cpd = null;
		this.cpdArgs = cpdArgs;
		this.cpdArgsAndParams = null;
	}

	/**
	 * Create a new clause where it is not yet known which fields are
	 * parameters and which are arguments.
	 * 
	 * @param cond
	 *          the condition under which this clause applies
	 * @param cpdCLass
	 *          the class of the conditional probability distribution used.
	 * @param cpdArgsAndParams
	 *          A mixed list of arguments and parameters.
	 */
	public Clause(Formula cond, Class cpdClass, List<ArgSpec> cpdArgsAndParams) {
		this.cond = cond;
		this.cpdClass = cpdClass;
		this.cpdParams = null;
		this.cpdArgs = null;
		this.cpdArgsAndParams = cpdArgsAndParams;
	}

	/**
	 * Creates a new clause using the given CondProbDistrib object.
	 * 
	 * @param cond
	 *          the condition under which this clause applies
	 * @param cpd
	 *          conditional probability distribution for this clause
	 * @param cpdArgs
	 *          List of ArgSpec objects whose denotations will be passed to the
	 *          CPD each time it is invoked
	 */
	public Clause(Formula cond, CondProbDistrib cpd, List<ArgSpec> cpdArgs) {
		this.cond = cond;
		this.cpdClass = cpd.getClass();
		this.cpdParams = Collections.emptyList();
		this.cpd = cpd;
		this.cpdArgs = cpdArgs;
		this.cpdArgsAndParams = null;
	}

	public void setCond(Formula cond) {
		this.cond = cond;
	}

	public Formula getCond() {
		return cond;
	}

	public Class getCPDClass() {
		if (cpd != null) {
			return cpd.getClass();
		}
		return cpdClass;
	}

	public CondProbDistrib getCPD() {

		return cpd;

	}

	/**
	 * @return List of ArgSpec objects
	 */
	public List getArgs() {

		return cpdArgs;

	}

	/**
	 * Returns this clause's CPD, and the values of this clause's arguments
	 * evaluated in the given context. Returns null if the partial world in the
	 * given context is not complete enough to evaluate the arguments.
	 */
	public DependencyModel.Distrib getDistrib(EvalContext context) {
		context.pushEvaluee(this);
		List argValues = new ArrayList();

		for (Iterator iter = cpdArgs.iterator(); iter.hasNext();) {
			ArgSpec argSpec = (ArgSpec) iter.next();
			Object argValue = argSpec.evaluate(context);
			if (argValue == null) {
				break; // CPD arg not determined
			}
			argValues.add(argValue);
		}

		context.popEvaluee();
		if (argValues.size() == cpdArgs.size()) {
			// all CPD args were determined
{'fraction_non_alphanumeric': 0.07445970776095896, 'fraction_numerical': 0.0013999475019686762, 'mean_word_length': 3.4029275808936825, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5739527', 'n_tokens_mistral': 3468, 'n_tokens_neox': 3092, 'n_words': 1306}