Exemplo n.º 1
0
  // @requires inout.length == 3;
  public void chooseMode(String inout) {
    assert inout.length() == 3;
    boolean isInput[] = new boolean[names_.length];
    env_ = new Envir();
    // Is names_[0] an input?
    isInput[0] = inout.charAt(0) == 'I';
    // Are names_[1..length-2] inputs?
    for (int i = 1; i <= names_.length - 2; i++) isInput[i] = inout.charAt(1) == 'I';
    // Is names_[length-1] an input?
    isInput[names_.length - 1] = inout.charAt(2) == 'I';

    // Now add the inputs into env.
    for (int i = 0; i < names_.length; i++) if (isInput[i]) env_ = env_.plus(names_[i], null);
    boolean shouldWork = validModes_.contains(inout);

    mode_ = pred_.chooseMode(env_);
    debug("chooseMode(" + env_ + ") --> " + mode_);
    if (shouldWork) Assert.assertNotNull("Valid mode expected, but got null", mode_);
    else Assert.assertNull("Null mode expected, but got " + mode_, mode_);
    if (mode_ == null) {
      state_ = State.NoMode;
      env_ = null;
    } else {
      // now check that mode is correct.
      for (int i = 0; i < names_.length; i++) {
        Assert.assertEquals("name[" + i + "] = " + names_[i], isInput[i], mode_.isInput(names_[i]));
      }
      pred_.setMode(mode_);
      // check that all names are defined in the output environment.
      Envir newenv = mode_.getEnvir();
      for (int i = 0; i < names_.length; i++)
        Assert.assertTrue(names_[i] + " should be defined", newenv.isDefined(names_[i]));
      state_ = State.GotMode;
      // NOTE that env_ is left as the input environment.
    }
  }