package run;
import java.text.ParseException;

import jp.lang.RMICompatibility;

import jp.lang.RemoteObject;

import uka.karmi.rmi.RemoteException;

import uka.karmi.rmi.server.ReplicatedObject;

public class Controlset extends ReplicatedObject implements uka.patch.Patchable, uka.transport.Transportable {
   public void createPatch(Object _copy, uka.patch.PatchOutput po) throws java.io.IOException {
      Controlset copy = (Controlset)_copy;
      super.createPatch(copy, po);
      if (po.writeDiff(this.Track_Print_is_set, copy.Track_Print_is_set)) copy.Track_Print_is_set = this.Track_Print_is_set;
      if (po.writeDiff(this.Print_is_set, copy.Print_is_set)) copy.Print_is_set = this.Print_is_set;
      if (po.writeDiff(this.Run_is_set, copy.Run_is_set)) copy.Run_is_set = this.Run_is_set;
      if (po.writeDiff(this.counter_tracker, copy.counter_tracker)) copy.counter_tracker = this.counter_tracker;
      if (po.writeDiff(this.counter, copy.counter)) copy.counter = this.counter;
      if (po.writeDiff(this.printstep_tracker, copy.printstep_tracker)) copy.printstep_tracker = this.printstep_tracker;
      if (po.writeDiff(this.printstep, copy.printstep)) copy.printstep = this.printstep;
      if (po.writeDiff(this.endtime, copy.endtime)) copy.endtime = this.endtime;
      if (po.writeDiff(this.starttime, copy.starttime)) copy.starttime = this.starttime;
      copy.trackwriter = this.trackwriter = (java.lang.String)po.writeDiff(this.trackwriter, copy.trackwriter);
      copy.writer = this.writer = (java.lang.String)po.writeDiff(this.writer, copy.writer);
      copy.timestep = this.timestep = (run.Variable)po.writeDiff(this.timestep, copy.timestep);
   }
   public void applyPatch(Object _copy, uka.patch.PatchInput pi) throws java.io.IOException, ClassNotFoundException {
      Controlset copy = (Controlset)_copy;
      super.applyPatch(copy, pi);
      if (pi.hasDiff()) copy.Track_Print_is_set = this.Track_Print_is_set = pi.getDiffAsBoolean();
      if (pi.hasDiff()) copy.Print_is_set = this.Print_is_set = pi.getDiffAsBoolean();
      if (pi.hasDiff()) copy.Run_is_set = this.Run_is_set = pi.getDiffAsBoolean();
      if (pi.hasDiff()) copy.counter_tracker = this.counter_tracker = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.counter = this.counter = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.printstep_tracker = this.printstep_tracker = pi.getDiffAsDouble();
      if (pi.hasDiff()) copy.printstep = this.printstep = pi.getDiffAsDouble();
      if (pi.hasDiff()) copy.endtime = this.endtime = pi.getDiffAsDouble();
      if (pi.hasDiff()) copy.starttime = this.starttime = pi.getDiffAsDouble();
      if (pi.hasDiff()) copy.trackwriter = this.trackwriter = (java.lang.String)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.writer = this.writer = (java.lang.String)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.timestep = this.timestep = (run.Variable)pi.getDiffAsObject();
   }
   public void descendReferences(uka.patch.ReferenceConsumer c) throws java.io.IOException {
      super.descendReferences(c);
      c.descend(this.trackwriter);
      c.descend(this.writer);
      c.descend(this.timestep);
   }
   public void filterReferences(uka.patch.ReferenceFilter f) {
      super.filterReferences(f);
      this.trackwriter = (java.lang.String)f.filter(this.trackwriter);
      this.writer = (java.lang.String)f.filter(this.writer);
      this.timestep = (run.Variable)f.filter(this.timestep);
   }
   public static final uka.transport.TransportDescriptor TRANSPORT_DESCRIPTOR = new uka.transport.TransportDescriptor(){
      public Object unmarshalReference(uka.transport.UnmarshalStream s, int id) throws java.io.IOException, ClassNotFoundException {
         return new Controlset(s, id);
      }
      public boolean unmarshal(Object obj, uka.transport.UnmarshalStream s) throws ClassNotFoundException, java.io.IOException {
         ((Controlset)obj).unmarshal(s);
         return false;
      }
      public void marshalReference(Object obj, uka.transport.MarshalStream s) throws java.io.IOException {
         ((Controlset)obj).marshalReference(s);
      }
      public void marshal(Object obj, uka.transport.MarshalStream s) throws java.io.IOException {
         ((Controlset)obj).marshal(s);
      }
      public Object deepClone(Object orig, int id, uka.transport.DeepClone _helper) throws CloneNotSupportedException {
         try {
            return new Controlset((Controlset)orig, id, _helper);
         }  catch (java.io.IOException ex) {
            throw (CloneNotSupportedException)new CloneNotSupportedException().initCause(ex);
         }
      }
      public boolean deepCloneReferences(Object orig, Object copy, uka.transport.DeepClone _helper) throws CloneNotSupportedException {
         ((Controlset)copy).deepCloneReferences((Controlset)orig, _helper);
         return false;
      }
      public Class getType() {
         return Controlset.class;
      }
   };
   public uka.transport.TransportDescriptor getTransportDescriptor() {
      return TRANSPORT_DESCRIPTOR;
   }
   private static final int _SIZE = uka.transport.BasicIO.SIZEOF_boolean + uka.transport.BasicIO.SIZEOF_boolean + uka.transport.BasicIO.SIZEOF_boolean + uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_double + uka.transport.BasicIO.SIZEOF_double + uka.transport.BasicIO.SIZEOF_double + uka.transport.BasicIO.SIZEOF_double;
   public Controlset(uka.transport.UnmarshalStream _stream, int _id) throws java.io.IOException, ClassNotFoundException {
      super(_stream, _id);
   }
   public void unmarshal(uka.transport.UnmarshalStream _stream) throws java.io.IOException, ClassNotFoundException {
      super.unmarshal(_stream);
      _stream.request(Controlset._SIZE);
      byte[] _buffer = _stream.getBuffer();
      int _pos = _stream.getPosition();
      this.Track_Print_is_set = uka.transport.BasicIO.extractBoolean(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_boolean;
      this.Print_is_set = uka.transport.BasicIO.extractBoolean(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_boolean;
      this.Run_is_set = uka.transport.BasicIO.extractBoolean(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_boolean;
      this.counter_tracker = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.counter = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.printstep_tracker = uka.transport.BasicIO.extractDouble(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_double;
      this.printstep = uka.transport.BasicIO.extractDouble(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_double;
      this.endtime = uka.transport.BasicIO.extractDouble(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_double;
      this.starttime = uka.transport.BasicIO.extractDouble(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_double;
      _stream.accept(Controlset._SIZE);
      this.trackwriter = (java.lang.String)_stream.readReference();
      this.writer = (java.lang.String)_stream.readReference();
      this.timestep = (run.Variable)_stream.readReference();
   }
   public void marshal(uka.transport.MarshalStream _stream) throws java.io.IOException {
      super.marshal(_stream);
      _stream.reserve(Controlset._SIZE);
      byte[] _buffer = _stream.getBuffer();
      int _pos = _stream.getPosition();
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.Track_Print_is_set);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.Print_is_set);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.Run_is_set);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.counter_tracker);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.counter);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.printstep_tracker);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.printstep);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.endtime);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.starttime);
      _stream.deliver(_SIZE);
      _stream.writeReference(this.trackwriter);
      _stream.writeReference(this.writer);
      _stream.writeReference(this.timestep);
   }
   public Controlset(Controlset _orig, int _id, uka.transport.DeepClone _helper) throws CloneNotSupportedException, java.io.IOException {
      super(_orig, _id, _helper);
      this.Track_Print_is_set = _orig.Track_Print_is_set;
      this.Print_is_set = _orig.Print_is_set;
      this.Run_is_set = _orig.Run_is_set;
      this.counter_tracker = _orig.counter_tracker;
      this.counter = _orig.counter;
      this.printstep_tracker = _orig.printstep_tracker;
      this.printstep = _orig.printstep;
      this.endtime = _orig.endtime;
      this.starttime = _orig.starttime;
   }
   public void deepCloneReferences(Controlset _orig, uka.transport.DeepClone _helper) throws CloneNotSupportedException {
      super.deepCloneReferences(_orig, _helper);
      this.trackwriter = (java.lang.String)_helper.internalDeepClone(_orig.trackwriter);
      this.writer = (java.lang.String)_helper.internalDeepClone(_orig.writer);
      this.timestep = (run.Variable)_helper.internalDeepClone(_orig.timestep);
   }
   private double starttime;
   private double endtime;
   private double printstep;
   private double printstep_tracker;
   private Variable timestep;
   private int counter;
   private int counter_tracker;
   private String writer;
   private String trackwriter;
   private boolean Run_is_set;
   private boolean Print_is_set;
   private boolean Track_Print_is_set;
   public Controlset(RemoteObject[] cluster_nodes) throws RemoteException {
      super(RMICompatibility.getStubs(cluster_nodes));
      run.Controlset.this.counter = 0;
      run.Controlset.this.counter_tracker = 0;
      run.Controlset.this.timestep = new Variable(0);
      run.Controlset.this.writer = new String("GIDWRITER");
      run.Controlset.this.trackwriter = new String("GIDTRACKWRITER");
   }
   public Controlset() throws RemoteException {
      super(null);
      run.Controlset.this.counter = 0;
      run.Controlset.this.counter_tracker = 0;
      run.Controlset.this.timestep = new Variable(0);
      run.Controlset.this.writer = new String("GIDWRITER");
      run.Controlset.this.trackwriter = new String("GIDTRACKWRITER");
   }
   public void parse_Fembic(Token[] param, int lineno) throws java.text.ParseException {
      int i = 0;
      while (i < param.length) {
         if (param[i].getw().toUpperCase().equals("RUN")) {
            if (param[i + 1].getw().toUpperCase().equals("FROM")) {
               if (param[i + 2].is_a_number()) {
                  run.Controlset.this.starttime = param[i + 2].getn();
                  if (param[i + 3].getw().toUpperCase().equals("TO")) {
                     if (param[i + 4].is_a_number()) {
                        run.Controlset.this.endtime = param[i + 4].getn();
                        i += 5;
                        run.Controlset.this.Run_is_set = true;
                        if (i + 1 < param.length) {
                           if (param[i].getw().toUpperCase().equals("STEP")) {
                              if (param[i + 1].is_a_word()) {
                                 if (param[i + 1].getw().startsWith("[") && param[i + 1].getw().endsWith("]")) {
                                    run.Controlset.this.timestep = new Variable(param[i + 1].getw().substring(1, param[i + 1].getw().length() - 1));
                                    i += 2;
                                 } else {
                                    throw new ParseException("Illegal parameter. Syntax: STEP [t1,ts1,t2,ts2,...] ", lineno);
                                 }
                              } else if (param[i + 1].is_a_number()) {
                                 run.Controlset.this.timestep = new Variable(param[i + 1].getn());
                                 i += 2;
                              } else {
                                 throw new ParseException("No number xx defined in RUN FROM .. TO .. STEP xx", lineno);
                              }
                           }
                        }
                     } else {
                        throw new ParseException("No number xx defined in RUN FROM .. TO xx", lineno);
                     }
                  }
               } else {
                  throw new ParseException("No number xx defined in RUN FROM xx TO ..", lineno);
               }
            }
         } else if (param[i].getw().toUpperCase().equals("PRINT")) {
            if (param[i + 1].getw().toUpperCase().equals("EVERY")) {
               if (param[i + 2].is_a_number()) {
                  run.Controlset.this.printstep = param[i + 2].getn();
                  if (param[i + 3].getw().toUpperCase().equals("STEP")) {
                     i += 4;
                  }
                  run.Controlset.this.Print_is_set = true;
               } else {
                  throw new ParseException("No number xx defined in PRINT EVERY xx STEP", lineno);
               }
            } else if (param[i + 1].getw().toUpperCase().equals("TRACKER")) {
               if (param[i + 2].getw().toUpperCase().equals("EVERY")) {
                  if (param[i + 3].is_a_number()) {
                     run.Controlset.this.printstep_tracker = param[i + 3].getn();
                     if (param[i + 4].getw().toUpperCase().equals("STEP")) {
                        i += 5;
                     }
                     run.Controlset.this.Track_Print_is_set = true;
                  } else {
                     throw new ParseException("No number xx defined in PRINT TRACKER EVERY xx STEP", lineno);
                  }
               }
            }
         } else if (param[i].getw().toUpperCase().equals("FOR")) {
            if (param[i + 1].getw().toUpperCase().equals("WRITER")) {
               if (param[i + 2].getw().toUpperCase().equals("USE")) {
                  if (param[i + 3].is_a_word()) {
                     run.Controlset.this.writer = new String(param[i + 3].getw());
                     i += 4;
                  }
               }
            } else if (param[i + 1].getw().toUpperCase().equals("TRACKWRITER")) {
               if (param[i + 2].getw().toUpperCase().equals("USE")) {
                  if (param[i + 3].is_a_word()) {
                     run.Controlset.this.trackwriter = new String(param[i + 3].getw());
                     i += 4;
                  }
               }
            }
         } else {
            throw new java.text.ParseException("No recognized control command found or missing parameter", lineno);
         }
      }
   }
   public void parse_Nastran(Token[] param, int lineno) throws java.text.ParseException {
   }
   public String print_Fembic(int ctrl) {
      String out;
      switch (ctrl) {
      case Element.MESH: 
         out = new String("");
         if (run.Controlset.this.Run_is_set) out += "Run from " + run.Controlset.this.starttime + " to " + run.Controlset.this.endtime;
         if (run.Controlset.this.timestep.value(0) != 0) out += " step " + run.Controlset.this.timestep.printFembic();
         out += "\n";
         if (run.Controlset.this.Print_is_set) out += "Print every " + run.Controlset.this.printstep + " step \n";
         if (run.Controlset.this.Track_Print_is_set) out += "Print tracker every " + run.Controlset.this.printstep_tracker + " step \n";
         out += "For writer use " + run.Controlset.this.writer + "\n";
         out += "For trackwriter use " + run.Controlset.this.trackwriter + "\n";
         out += "\n";
         return out;
      
      default: 
         return new String("");
      
      }
   }
   public void checkIndata() throws Error {
      if (!run.Controlset.this.Run_is_set) {
         throw new Error("No RUN FROM .. TO ..  defined in the controlset");
      }
      if (!run.Controlset.this.Print_is_set) {
         throw new Error("No PRINT EVERY .. STEP defined in the controlset");
      }
   }
   public void setInitialConditions() throws Error {
      if (!run.Controlset.this.Track_Print_is_set) {
         run.Controlset.this.printstep_tracker = run.Controlset.this.printstep;
      }
   }
   public double getEndtime() {
      return run.Controlset.this.endtime;
   }
   public double getPrintstep() {
      return run.Controlset.this.printstep;
   }
   public double getStarttime() {
      return run.Controlset.this.starttime;
   }
   public double getTimestep(double time) {
      if (run.Controlset.this.timestep.on(time)) return run.Controlset.this.timestep.value(time); else return 0;
   }
   public String getWriter() {
      return run.Controlset.this.writer;
   }
   public String getTrackWriter() {
      return run.Controlset.this.trackwriter;
   }
   public void setEndtime(double newEndtime) {
      run.Controlset.this.endtime = newEndtime;
   }
   public void setPrintstep(double newPrintstep) {
      run.Controlset.this.printstep = newPrintstep;
   }
   public void setStarttime(double newStarttime) {
      run.Controlset.this.starttime = newStarttime;
   }
   public boolean timeToPrint(double current_time) {
      if (current_time >= run.Controlset.this.printstep * run.Controlset.this.counter) {
         run.Controlset.this.counter++;
         return true;
      } else {
         return false;
      }
   }
   public boolean timeToPrintTracker(double current_time) {
      if (current_time >= run.Controlset.this.printstep_tracker * run.Controlset.this.counter_tracker) {
         run.Controlset.this.counter_tracker++;
         return true;
      } else {
         return false;
      }
   }
}
