/**
 * Strips2.java
 * in Strips, given a difference
 * looks for an operator to deal with it
 * calls Strips3 to apply the op
 *
 * Created: Sun Jan 14 21:58:54 2001
 *
 * @author 
 * @version
 */

import java.util.*;
import StripsFn;

public class Strips2 extends StripsFn{

  String diff;  //difference to reduce
  
  //constructor
  public Strips2 (Vector opl,MStringVector is, MStringVector gs, String d){
    oplis=opl;
    initstate=is;
    goalstate=gs;
    diff=d;
  }

  //run problem solver

  public boolean run(){
    //commentary
    System.out.println("------------------");
    System.out.println("Strips2");
    System.out.println(diff);
      
    result=false; //set to true when diff dealt with
    Iterator opit=oplis.iterator();

    while (opit.hasNext()&& !result){ //try ops till one succeeds or none left
      Strips_op op= (Strips_op)opit.next();
      boolean matchres=op.getAdd_list().match(diff); //match diff against addlist
      if (matchres){ //op can deal with diff
        HashMap con=op.getAdd_list().getContext(); //in this context
        //call strips3 to attempt to apply op
        Strips3 strips3 = new Strips3(oplis, initstate, goalstate,op,con);
        boolean strips3res=strips3.run();
        if (strips3res){ //strips3 succeeded
          plan=strips3.getPlan(); //Strips2 plan is Strips3 plan
          newstate=strips3.getNewstate(); //new state is Strips3 new state
          result=true;
        }
      }
    }
    return result;
  }
}
