public void setLocalVariable ( String name , Term [ ] terms , Element value ) throws EvaluationException { assert ( name != null ) ; // Only truly local variables can be set via this method. Throw an // exception if a global variable is found which matches the name. if ( globalVariables . containsKey ( name ) ) { throw new EvaluationException ( MessageUtils . format ( MSG_CANNOT_MODIFY_GLOBAL_VARIABLE_FROM_DML , name ) ) ; } 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 ) ) ; } } }
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 .