001    package aima.test.search.map;
002    
003    import junit.framework.TestCase;
004    import aima.basic.BasicEnvironmentView;
005    import aima.search.framework.GraphSearch;
006    import aima.search.map.ExtendableMap;
007    import aima.search.map.MapAgent;
008    import aima.search.map.MapEnvironment;
009    import aima.search.uninformed.UniformCostSearch;
010    
011    /**
012     * @author Ciaran O'Reilly
013     * 
014     */
015    
016    public class MapAgentTest extends TestCase {
017    
018            ExtendableMap aMap;
019    
020            StringBuffer envChanges;
021    
022            @Override
023            public void setUp() {
024                    aMap = new ExtendableMap();
025                    aMap.addBidirectionalLink("A", "B", 5.0);
026                    aMap.addBidirectionalLink("A", "C", 6.0);
027                    aMap.addBidirectionalLink("B", "C", 4.0);
028                    aMap.addBidirectionalLink("C", "D", 7.0);
029                    aMap.addUnidirectionalLink("B", "E", 14.0);
030    
031                    envChanges = new StringBuffer();
032            }
033    
034            public void testAlreadyAtGoal() {
035                    MapEnvironment me = new MapEnvironment(aMap);
036                    MapAgent ma = new MapAgent(me,
037                                    new UniformCostSearch(new GraphSearch()), new String[] { "A" });
038                    me.addAgent(ma, "A");
039                    me.registerView(new BasicEnvironmentView() {
040                            @Override
041                            public void envChanged(String command) {
042                                    envChanges.append(command).append(":");
043                            }
044                    });
045                    me.stepUntilDone();
046    
047                    assertEquals(
048                                    "CurrentLocation=In(A), Goal=In(A):NoOP:METRIC[pathCost]=0.0:METRIC[maxQueueSize]=1:METRIC[queueSize]=0:METRIC[nodesExpanded]=0:NoOP:",
049                                    envChanges.toString());
050            }
051    
052            public void testNormalSearch() {
053                    MapEnvironment me = new MapEnvironment(aMap);
054                    MapAgent ma = new MapAgent(me,
055                                    new UniformCostSearch(new GraphSearch()), new String[] { "D" });
056                    me.addAgent(ma, "A");
057                    me.registerView(new BasicEnvironmentView() {
058                            @Override
059                            public void envChanged(String command) {
060                                    envChanges.append(command).append(":");
061                            }
062                    });
063                    me.stepUntilDone();
064    
065                    assertEquals(
066                                    "CurrentLocation=In(A), Goal=In(D):C:D:METRIC[pathCost]=13.0:METRIC[maxQueueSize]=6:METRIC[queueSize]=1:METRIC[nodesExpanded]=3:NoOP:",
067                                    envChanges.toString());
068            }
069    
070            public void testNoPath() {
071                    MapEnvironment me = new MapEnvironment(aMap);
072                    MapAgent ma = new MapAgent(me,
073                                    new UniformCostSearch(new GraphSearch()), new String[] { "A" });
074                    me.addAgent(ma, "E");
075                    me.registerView(new BasicEnvironmentView() {
076                            @Override
077                            public void envChanged(String command) {
078                                    envChanges.append(command).append(":");
079                            }
080                    });
081                    me.stepUntilDone();
082    
083                    assertEquals(
084                                    "CurrentLocation=In(E), Goal=In(A):NoOP:METRIC[pathCost]=0:METRIC[maxQueueSize]=1:METRIC[queueSize]=0:METRIC[nodesExpanded]=1:NoOP:",
085                                    envChanges.toString());
086            }
087    }