
/**
 * Strips1.java
 * find difference, call strips2
 * if that succeeds, call strips1 to deal with any remaining diffs
 * Created: Sun Jan 14 21:06:02 2001
 *
 * @author 
 * @version
 */

import java.util.*;
import StripsFn;

public class Strips1 extends StripsFn{

  //constructor called from outside at startup, reads ops from file

  //ops is vector of operator names
  
  public Strips1 (MStringVector ops, MStringVector is, MStringVector gs){
    initstate=is;
    goalstate=gs;
    oplis=new Vector();
    
    //get the operators from file

    for (Iterator i=ops.iterator();i.hasNext();){
      String str=(String) i.next();
      Strips_op op=new Strips_op(str);
      oplis.add(op);
    }
  }

  //constructor called internally, passed oplis directly

  public Strips1 (Vector opl,MStringVector is, MStringVector gs){
    oplis=opl;
    initstate=is;
    goalstate=gs;
  }

  //run problem solver

  public boolean run(){
    //commentary
    System.out.println("-----------------");
    System.out.println("Strips1");
    System.out.println(initstate);
    System.out.println(goalstate);

    //look for a difference
    boolean diffound=false; //set to true when diff found
    String g=new String(); //where the diff found will go
    Iterator git=goalstate.iterator();
    while(git.hasNext()&& !diffound){ //iterate over goals
      g=(String)git.next();
      //try to match goal g within state - initstate is an MStringVector
      diffound=!initstate.match(g); 
    }
    
    if (!diffound){ 
      result=true; //no difference found - trivial success
      newstate=initstate; //end state same as initstate
      plan=new Vector(); //empty plan
      return result;
    }
    else{ //found a diff, use Strips2
      Strips2 strips2=new Strips2(oplis,initstate, goalstate,g); //make instance
      boolean s2res=strips2.run(); //run it
      if (!s2res){  //did Strips2 succeed?
        result=false; //no, failure
        return result;
      }
      else { //strips2 succeeded - call Strips1 again with state returned
        //must be a new instance - state will have changed
        Strips1 nstrips1=new Strips1(oplis, strips2.getNewstate(),goalstate);
        boolean nstrips1res=nstrips1.run();
        if (!nstrips1res){ //did strips1 succeed?
          result=false; //no, failure
          return result;
        }
        else {  //success
          result=true;
          //plan is strips2 plan + strips1 plan
          plan=strips2.getPlan();
          plan.addAll(nstrips1.getPlan());
          newstate=nstrips1.getNewstate();//final state is that returned by strips1
          return result;
        }
      }
    }
  }
}
      
  
  
