示例#1
0
  /**
   * Set the local variable to the given value. If the value is null, then the corresponding
   * variable will be removed. If there is a global variable of the same name, then an
   * EvaluationException will be thrown.
   *
   * @param name name of the local variable
   * @param terms terms used to dereference the variable, or null if the variable is to be used
   *     directly
   * @param value value to use or null to remove the variable
   * @throws EvaluationException if there is a global variable with the same name as the local
   *     variable
   */
  public void setLocalVariable(String name, Term[] terms, Element value)
      throws EvaluationException {

    assert (name != null);

    if (terms == null || terms.length == 0) {

      // Revert back to the simple case that does not require
      // dereferencing.
      setLocalVariable(name, value);

    } else {

      // The more complicated case where we need to dereference the
      // variable. (And also possibly create the parents.)

      // Retrieve the value of the local variable.
      Element var = getLocalVariable(name);

      // If the value is a protected resource, then make a shallow copy
      // and replace the value of the local variable.
      if (var != null && var.isProtected()) {
        var = var.writableCopy();
        setLocalVariable(name, var);
      }

      // If the value does not exist, create a resource of the correct
      // type and insert into variable table.
      if (var == null || var instanceof Undef) {

        Term term = terms[0];
        if (term.isKey()) {
          var = new HashResource();
        } else {
          var = new ListResource();
        }
        setLocalVariable(name, var);
      }

      // Recursively descend to set the value.
      assert (var != null);
      try {
        var.rput(terms, 0, value);
      } catch (InvalidTermException ite) {
        throw new EvaluationException(ite.formatVariableMessage(name, terms));
      }
    }
  }