001 package aima.search.nodestore; 002 003 import java.util.Hashtable; 004 import java.util.List; 005 006 import aima.search.framework.Node; 007 import aima.search.framework.NodeStore; 008 009 /** 010 * A simple wrapper class which delegates NodeStore interface 011 * requests to the NodeStore it was created with. However, 012 * adds additional capability to keep track of nodes and look 013 * them up based on their states. 014 */ 015 016 /** 017 * @author Ciaran O'Reilly 018 * 019 */ 020 public class CachedStateNodeStore implements NodeStore { 021 022 private NodeStore nodeStore; 023 private Hashtable<Object, Node> cachedState = new Hashtable<Object, Node>(); 024 025 public CachedStateNodeStore(NodeStore aNodeStore) { 026 nodeStore = aNodeStore; 027 } 028 029 public boolean containsNodeBasedOn(Object state) { 030 return cachedState.containsKey(state); 031 } 032 033 public Node getNodeBasedOn(Object state) { 034 return cachedState.get(state); 035 } 036 037 // 038 // START Interface - NodeStore 039 public void add(Node anItem) { 040 nodeStore.add(anItem); 041 cachedState.put(anItem.getState(), anItem); 042 } 043 044 public Node remove() { 045 Node n = nodeStore.remove(); 046 cachedState.remove(n.getState()); 047 048 return n; 049 } 050 051 public void add(List<Node> nodes) { 052 nodeStore.add(nodes); 053 for (Node n : nodes) { 054 cachedState.put(n.getState(), n); 055 } 056 } 057 058 public boolean isEmpty() { 059 return nodeStore.isEmpty(); 060 } 061 062 public int size() { 063 return nodeStore.size(); 064 } 065 // END Interface - NodeStore 066 // 067 }