All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aima.core.probability.domain.ArbitraryTokenDomain Maven / Gradle / Ivy

Go to download

AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.

The newest version!
package aima.core.probability.domain;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Artificial Intelligence A Modern Approach (3rd Edition): page 486.
 * 
 * As in CSPs, domains can be sets of arbitrary tokens; we might choose the
 * domain of Age to be {juvenile,teen,adult} and the domain of
 * Weather might be {sunny,rain,cloudy,snow}.
 * 
 * @author Ciaran O'Reilly
 */
public class ArbitraryTokenDomain extends AbstractFiniteDomain {

	private Set possibleValues = null;
	private boolean ordered = false;

	public ArbitraryTokenDomain(Object... pValues) {
		this(false, pValues);
	}

	public ArbitraryTokenDomain(boolean ordered, Object... pValues) {
		this.ordered = ordered;
		// Keep consistent order
		possibleValues = new LinkedHashSet();
		for (Object v : pValues) {
			possibleValues.add(v);
		}
		// Ensure cannot be modified
		possibleValues = Collections.unmodifiableSet(possibleValues);

		indexPossibleValues(possibleValues);
	}

	//
	// START-Domain

	@Override
	public int size() {
		return possibleValues.size();
	}

	@Override
	public boolean isOrdered() {
		return ordered;
	}

	// END-Domain
	//

	//
	// START-FiniteDomain
	@Override
	public Set getPossibleValues() {
		return possibleValues;
	}

	// END-finiteDomain
	//

	@Override
	public boolean equals(Object o) {

		if (this == o) {
			return true;
		}
		if (!(o instanceof ArbitraryTokenDomain)) {
			return false;
		}

		ArbitraryTokenDomain other = (ArbitraryTokenDomain) o;

		return this.possibleValues.equals(other.possibleValues);
	}

	@Override
	public int hashCode() {
		return possibleValues.hashCode();
	}
}