001    package aima.search.framework;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    /**
007     * @author Ravi Mohan
008     * 
009     */
010    
011    public class NodeExpander {
012            protected Metrics metrics;
013    
014            protected static String NODES_EXPANDED = "nodesExpanded";
015    
016            public NodeExpander() {
017                    metrics = new Metrics();
018            }
019    
020            public void clearInstrumentation() {
021                    metrics.set(NODES_EXPANDED, 0);
022            }
023    
024            public List<Node> expandNode(Node node, Problem problem) {
025    
026                    List<Node> nodes = new ArrayList<Node>();
027                    List successors = problem.getSuccessorFunction().getSuccessors(
028                                    node.getState());
029                    for (int i = 0; i < successors.size(); i++) {
030                            Successor successor = (Successor) successors.get(i);
031                            Node aNode = new Node(node, successor.getState());
032                            aNode.setAction(successor.getAction());
033                            Double stepCost = problem.getStepCostFunction().calculateStepCost(
034                                            node.getState(), successor.getState(),
035                                            successor.getAction());
036                            aNode.setStepCost(stepCost);
037                            aNode.addToPathCost(stepCost);
038                            nodes.add(aNode);
039    
040                    }
041                    metrics.set(NODES_EXPANDED, metrics.getInt(NODES_EXPANDED) + 1);
042                    // System.out.println("Nodes expanded = " +
043                    // metrics.getInt(NODES_EXPANDED));
044                    return nodes;
045            }
046    
047            public int getNodesExpanded() {
048                    return metrics.getInt(NODES_EXPANDED);
049            }
050    
051            public void setNodesExpanded(int nodesExpanded) {
052                    metrics.set(NODES_EXPANDED, nodesExpanded);
053            }
054    
055            public Object getSearchMetric(String name) {
056                    return metrics.get(name);
057            }
058    
059            public Metrics getMetrics() {
060                    return metrics;
061            }
062    }