001 /* 002 * Created on Sep 21, 2004 003 * 004 */ 005 package aima.search.csp; 006 007 import java.util.ArrayList; 008 import java.util.Hashtable; 009 import java.util.Iterator; 010 import java.util.List; 011 012 /** 013 * @author Ravi Mohan 014 * 015 */ 016 017 public class Domain { 018 private Hashtable<String, List<Object>> variablesToValues; 019 020 // a hash Of Lists { variable: ListOfDomainValues} 021 public Domain(List<String> variables) { 022 this.variablesToValues = new Hashtable<String, List<Object>>(); 023 Iterator<String> varIter = variables.iterator(); 024 while (varIter.hasNext()) { 025 variablesToValues.put(varIter.next(), new ArrayList<Object>()); 026 } 027 } 028 029 public List<Object> getDomainOf(String variable) { 030 return variablesToValues.get(variable); 031 } 032 033 public void add(String variable, Object value) { 034 List<Object> varDomains = variablesToValues.get(variable); 035 036 if (!(varDomains.contains(value))) { 037 varDomains.add(value); 038 } 039 } 040 041 public void addToDomain(String variable, List values) { 042 for (int i = 0; i < values.size(); i++) { 043 add(variable, values.get(i)); 044 } 045 046 } 047 048 public void remove(String variable, Object value) { 049 List varDomains = variablesToValues.get(variable); 050 varDomains.remove(value); 051 } 052 053 @Override 054 public String toString() { 055 return variablesToValues.toString(); 056 } 057 058 }