<commit_before>/*
 * Copyright (c) 2005, 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.world;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

import blog.ObjectIdentifier;
import blog.bn.BasicVar;
import blog.bn.BayesNetVar;
import blog.bn.CBN;
import blog.bn.DefaultCBN;
import blog.bn.DerivedVar;
import blog.bn.NumberVar;
import blog.bn.OriginVar;
import blog.bn.RandFuncAppVar;
import blog.bn.VarWithDistrib;
import blog.common.HashMapWithPreimages;
import blog.common.HashMultiMap;
import blog.common.IndexedHashMultiMap;
import blog.common.IndexedMultiMap;
import blog.common.IndexedSet;
import blog.common.MapWithPreimages;
import blog.common.MultiMap;
import blog.common.Util;
import blog.model.DependencyModel;
import blog.model.Model;
import blog.model.NonGuaranteedObject;
import blog.model.POP;
import blog.model.RandomFunction;
import blog.objgen.AbstractObjectSet;
import blog.objgen.ObjectIterator;
import blog.objgen.ObjectSet;
import blog.sample.ParentRecEvalContext;

/**
 * An implementation of the PartialWorld interface that just requires concrete
 * subclasses to initialize some protected variables.
 */
public abstract class AbstractPartialWorld implements PartialWorld {
	/**
	 * Creates a new partial world. Identifiers will be used to represent the
	 * user-defined types in the set idTypes.
	 * 
	 * @param idTypes
	 *          Set of Type objects
	 */
	public AbstractPartialWorld(Set idTypes) {
		this.idTypes = new HashSet(idTypes);
		this.cbn = new DefaultCBN();
	}

	public Set getInstantiatedVars() {
		return Collections.unmodifiableSet(basicVarToValue.keySet());
	}

	public boolean isInstantiated(BayesNetVar var) {
		return basicVarToValue.containsKey(var)
				|| derivedVarToValue.containsKey(var);
	}

	public Object getValue(BayesNetVar var) {
		if (var instanceof BasicVar) {
			return basicVarToValue.get(var);
		}
		if (var instanceof OriginVar) {
			return commIdToPOPApp.get(((OriginVar) var).getIdentifier());
		}
		if (var instanceof DerivedVar) {
			if (cbn.nodes().contains(var)) {
				updateParentsAndProbs();
				return derivedVarToValue.get(var);
			}

			// need to compute value
			return ((DerivedVar) var).getValue(this);
		}

		throw new IllegalArgumentException("Unrecognized BayesNetVar: " + var);
	}

	public void setValue(BasicVar var, Object value) {

		// if (var.toString().contains("ApparentPos") && value == Model.NULL) {
		// System.out.println("AbstractPartialWorld: " + var + " set to NULL!");
		// System.out.println();
		// }

		Object oldValue = basicVarToValue.get(var);
		if (value == null ? (oldValue == null) : value.equals(oldValue)) {
			Util.debug("Setting var: " + var + " to " + value);
			return;
		}
		Util.debug("Setting var: " + var + " to " + value + ", replacing "
				+ oldValue);
		var.ensureStable();

		if ((var instanceof NumberVar) && (oldValue != null)) {
			prepareForNumberVarChange((NumberVar) var, oldValue, value);
		}

		if (value == null) {
			basicVarToValue.remove(var);
		} else {
			// checkIdentifiers(var, value); // allow any identifiers
			basicVarToValue.put(var, value);
		}

		dirtyVars.add(var);
		updateUsageForChange(var, oldValue, value);<commit_msg>remove var from varToUninstParent when uninstantiating the var<commit_after>/*
 * Copyright (c) 2005, 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.world;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

import blog.ObjectIdentifier;
import blog.bn.BasicVar;
import blog.bn.BayesNetVar;
import blog.bn.CBN;
import blog.bn.DefaultCBN;
import blog.bn.DerivedVar;
import blog.bn.NumberVar;
import blog.bn.OriginVar;
import blog.bn.RandFuncAppVar;
import blog.bn.VarWithDistrib;
import blog.common.HashMapWithPreimages;
import blog.common.HashMultiMap;
import blog.common.IndexedHashMultiMap;
import blog.common.IndexedMultiMap;
import blog.common.IndexedSet;
import blog.common.MapWithPreimages;
import blog.common.MultiMap;
import blog.common.Util;
import blog.model.DependencyModel;
import blog.model.Model;
import blog.model.NonGuaranteedObject;
import blog.model.POP;
import blog.model.RandomFunction;
import blog.objgen.AbstractObjectSet;
import blog.objgen.ObjectIterator;
import blog.objgen.ObjectSet;
import blog.sample.ParentRecEvalContext;

/**
 * An implementation of the PartialWorld interface that just requires concrete
 * subclasses to initialize some protected variables.
 */
public abstract class AbstractPartialWorld implements PartialWorld {
	/**
	 * Creates a new partial world. Identifiers will be used to represent the
	 * user-defined types in the set idTypes.
	 * 
	 * @param idTypes
	 *          Set of Type objects
	 */
	public AbstractPartialWorld(Set idTypes) {
		this.idTypes = new HashSet(idTypes);
		this.cbn = new DefaultCBN();
	}

	public Set getInstantiatedVars() {
		return Collections.unmodifiableSet(basicVarToValue.keySet());
	}

	public boolean isInstantiated(BayesNetVar var) {
		return basicVarToValue.containsKey(var)
				|| derivedVarToValue.containsKey(var);
	}

	public Object getValue(BayesNetVar var) {
		if (var instanceof BasicVar) {
			return basicVarToValue.get(var);
		}
		if (var instanceof OriginVar) {
			return commIdToPOPApp.get(((OriginVar) var).getIdentifier());
		}
		if (var instanceof DerivedVar) {
			if (cbn.nodes().contains(var)) {
				updateParentsAndProbs();
				return derivedVarToValue.get(var);
			}

			// need to compute value
			return ((DerivedVar) var).getValue(this);
		}

		throw new IllegalArgumentException("Unrecognized BayesNetVar: " + var);
	}

	public void setValue(BasicVar var, Object value) {

		// if (var.toString().contains("ApparentPos") && value == Model.NULL) {
		// System.out.println("AbstractPartialWorld: " + var + " set to NULL!");
		// System.out.println();
		// }

		Object oldValue = basicVarToValue.get(var);
		if (value == null ? (oldValue == null) : value.equals(oldValue)) {
			Util.debug("Setting var: " + var + " to " + value);
			return;
		}
		Util.debug("Setting var: " + var + " to " + value + ", replacing "
				+ oldValue);
		var.ensureStable();

		if ((var instanceof NumberVar) && (oldValue != null)) {
			prepareForNumberVarChange((NumberVar) var, oldValue, value);
		}

		if (value == null) {
			basicVarToValue.remove(var);
      varToUninstParent.remove(var);
		} else {
			// checkIdentifiers(var, value); // allow any identifiers
			basicVarToValue.put(var, value);
		}

		dirtyVars.add(var);
		updateUsageForChange(var, oldValue, value);
{'fraction_non_alphanumeric': 0.09495665878644602, 'fraction_numerical': 0.0007880220646178094, 'mean_word_length': 4.366279069767442, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5818453', 'n_tokens_mistral': 3123, 'n_tokens_neox': 2849, 'n_words': 963}