deltablue.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,011 行 · 第 1/3 页

JAVA
1,011
字号
// possible output variable.//abstract class UnaryConstraint extends Constraint {  protected Variable myOutput; // possible output variable  protected boolean satisfied; // true if I am currently satisfied  protected UnaryConstraint(Variable v, Strength strength)  {    super(strength);    myOutput= v;    satisfied= false;    addConstraint();  }  // Answer true if this constraint is satisfied in the current solution.  public boolean isSatisfied() { return satisfied; }  // Record the fact that I am unsatisfied.  public void markUnsatisfied() { satisfied= false; }  // Answer my current output variable.  public Variable output() { return myOutput; }  // Add myself to the constraint graph.  public void addToGraph()  {    myOutput.addConstraint(this);    satisfied= false;  }  // Remove myself from the constraint graph.  public void removeFromGraph()  {    if (myOutput != null) myOutput.removeConstraint(this);    satisfied= false;  }  // Decide if I can be satisfied and record that decision.  protected void chooseMethod(int mark)  {    satisfied=    myOutput.mark != mark               && Strength.stronger(strength, myOutput.walkStrength);  }  protected void markInputs(int mark) {}   // I have no inputs  public boolean inputsKnown(int mark) { return true; }  // Calculate the walkabout strength, the stay flag, and, if it is  // 'stay', the value for the current output of this  // constraint. Assume this constraint is satisfied."  public void recalculate()  {    myOutput.walkStrength= strength;    myOutput.stay= !isInput();    if (myOutput.stay) execute(); // stay optimization  }  protected void printInputs() {} // I have no inputs}// I am a unary input constraint used to mark a variable that the// client wishes to change.//class EditConstraint extends UnaryConstraint {  public EditConstraint(Variable v, Strength str) { super(v, str); }  // I indicate that a variable is to be changed by imperative code.  public boolean isInput() { return true; }  public void execute() {} // Edit constraints do nothing.}// I mark variables that should, with some level of preference, stay// the same. I have one method with zero inputs and one output, which// does nothing. Planners may exploit the fact that, if I am// satisfied, my output will not change during plan execution. This is// called "stay optimization".//class StayConstraint extends UnaryConstraint {  // Install a stay constraint with the given strength on the given variable.  public StayConstraint(Variable v, Strength str) { super(v, str); }  public void execute() {} // Stay constraints do nothing.}//-------------binary constraints-------------------------------------------// I am an abstract superclass for constraints having two possible// output variables.//abstract class BinaryConstraint extends Constraint {  protected Variable v1, v2; // possible output variables  protected byte direction; // one of the following...  protected static byte backward= -1;    // v1 is output  protected static byte nodirection= 0;  // not satisfied  protected static byte forward= 1;      // v2 is output  protected BinaryConstraint() {} // this has to be here because of                                  // Java's constructor idiocy.  protected BinaryConstraint(Variable var1, Variable var2, Strength strength) {    super(strength);    v1= var1;    v2= var2;    direction= nodirection;    addConstraint();  }  // Answer true if this constraint is satisfied in the current solution.  public boolean isSatisfied() { return direction != nodirection; }  // Add myself to the constraint graph.  public void addToGraph()  {    v1.addConstraint(this);    v2.addConstraint(this);    direction= nodirection;  }  // Remove myself from the constraint graph.  public void removeFromGraph()  {    if (v1 != null) v1.removeConstraint(this);    if (v2 != null) v2.removeConstraint(this);    direction= nodirection;  }  // Decide if I can be satisfied and which way I should flow based on  // the relative strength of the variables I relate, and record that  // decision.  //  protected void chooseMethod(int mark)  {    if (v1.mark == mark)       direction=	v2.mark != mark && Strength.stronger(strength, v2.walkStrength)	  ? forward : nodirection;    if (v2.mark == mark)       direction=	v1.mark != mark && Strength.stronger(strength, v1.walkStrength)	  ? backward : nodirection;    // If we get here, neither variable is marked, so we have a choice.    if (Strength.weaker(v1.walkStrength, v2.walkStrength))      direction=	Strength.stronger(strength, v1.walkStrength) ? backward : nodirection;    else      direction=	Strength.stronger(strength, v2.walkStrength) ? forward : nodirection;  }  // Record the fact that I am unsatisfied.  public void markUnsatisfied() { direction= nodirection; }  // Mark the input variable with the given mark.  protected void markInputs(int mark)  {    input().mark= mark;  }    public boolean inputsKnown(int mark)  {    Variable i= input();    return i.mark == mark || i.stay || i.determinedBy == null;  }    // Answer my current output variable.  public Variable output() { return direction==forward ? v2 : v1; }  // Answer my current input variable  public Variable input() { return direction==forward ? v1 : v2; }  // Calculate the walkabout strength, the stay flag, and, if it is  // 'stay', the value for the current output of this  // constraint. Assume this constraint is satisfied.  //  public void recalculate()  {    Variable in= input(), out= output();    out.walkStrength= Strength.weakestOf(strength, in.walkStrength);    out.stay= in.stay;    if (out.stay) execute();  }  protected void printInputs()  {    input().print();  }}// I constrain two variables to have the same value: "v1 = v2".//class EqualityConstraint extends BinaryConstraint {  // Install a constraint with the given strength equating the given variables.  public EqualityConstraint(Variable var1, Variable var2, Strength strength)  {    super(var1, var2, strength);  }  // Enforce this constraint. Assume that it is satisfied.  public void execute() {    output().value= input().value;  }}// I relate two variables by the linear scaling relationship: "v2 =// (v1 * scale) + offset". Either v1 or v2 may be changed to maintain// this relationship but the scale factor and offset are considered// read-only.//class ScaleConstraint extends BinaryConstraint {  protected Variable scale; // scale factor input variable  protected Variable offset; // offset input variable  // Install a scale constraint with the given strength on the given variables.  public ScaleConstraint(Variable src, Variable scale, Variable offset,		         Variable dest, Strength strength)  {    // Curse this wretched language for insisting that constructor invocation    // must be the first thing in a method...    // ..because of that, we must copy the code from the inherited    // constructors.    this.strength= strength;    v1= src;    v2= dest;    direction= nodirection;    this.scale= scale;    this.offset= offset;    addConstraint();  }  // Add myself to the constraint graph.  public void addToGraph()  {    super.addToGraph();    scale.addConstraint(this);    offset.addConstraint(this);  }  // Remove myself from the constraint graph.  public void removeFromGraph()  {    super.removeFromGraph();    if (scale != null) scale.removeConstraint(this);    if (offset != null) offset.removeConstraint(this);  }  // Mark the inputs from the given mark.  protected void markInputs(int mark)  {    super.markInputs(mark);    scale.mark= offset.mark= mark;  }  // Enforce this constraint. Assume that it is satisfied.  public void execute()  {    if (direction == forward)       v2.value= v1.value * scale.value + offset.value;    else      v1.value= (v2.value - offset.value) / scale.value;  }  // Calculate the walkabout strength, the stay flag, and, if it is  // 'stay', the value for the current output of this  // constraint. Assume this constraint is satisfied.  public void recalculate()  {    Variable in= input(), out= output();    out.walkStrength= Strength.weakestOf(strength, in.walkStrength);    out.stay= in.stay && scale.stay && offset.stay;    if (out.stay) execute(); // stay optimization  }}    // ------------------------------------------------------------// A Plan is an ordered list of constraints to be executed in sequence// to resatisfy all currently satisfiable constraints in the face of// one or more changing inputs.class Plan {  private Vector v;  public Plan() { v= new Vector(); }  public void addConstraint(Constraint c) { v.addElement(c); }  public int size() { return v.size(); }  public Constraint constraintAt(int index) {    return (Constraint) v.elementAt(index); }  public void execute()  {    for (int i= 0; i < size(); ++i) {      Constraint c= (Constraint) constraintAt(i);      c.execute();    }  }}// ------------------------------------------------------------// The DeltaBlue plannerclass Planner {  int currentMark= 0;  // Select a previously unused mark value.  private int newMark() { return ++currentMark; }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?