package aminePlatform.test.util; /******************************************************************* Amine - Artificial Intelligence platform for the development of Intelligent Systems. Copyright (C) 2004 AmineGroup. GNU Lesser General Public License This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, version 2.1 of the License. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *********************************************************************/ import aminePlatform.util.*; import aminePlatform.kernel.lexicons.*; import java.util.*; /** *

Title : util.test.SimpleBindingContext Class

*

Description : SimpleBindingContext is a very simple context: binding * (association of a value to a variable) occurs in general due to an explicit * assignment or due to an unification of two structures. During unification, * variables of both structures can be binded. And since both structures can * contain variables with the same identifier, our solution here is to use two * HashMaps (varsOfObj1 and varsOfObj2), one for each structure and each hashMap * records the binding of variables of the corresponding structure * @author Adil KABBAJ * @version 2 */ public class SimpleBindingContext implements BindingContext { /** This hashMap is used to store variables of the first object */ private HashMap varsOfObj1 = new HashMap(); /** This hashMap is used to store variables of the second object */ private HashMap varsOfObj2 = new HashMap(); /***************** Implement BindingContext Interface *********************/ /** * This method returns the value of the variable obj, according to the current * binding context. If obj is not a variable, it is returned. * @param obj An object that corresponds in general to a variable * @param bindInf The binding information for the parameter obj. In this implementation, * it corresponds to a Boolean: true if obj belongs to the first structure and * false if it belongs to the second structure. * @return an ObjectWithBindInf object which is composed of an object (the value of the variable) * and its binding information. */ public ObjectWithBindInf getValue(Object obj, Object bindInf) { Object value = null; if (!Variable.isVariable(obj)) value = obj; else if (((Boolean) bindInf).booleanValue()) value = varsOfObj1.get(obj); else value = varsOfObj2.get(obj); if (value == null) return null; else return new ObjectWithBindInf(value, bindInf); } /** * Associate the value val to the variable var in this binding context * @param variable A Variable * @param value A value * @param bindInf The binding information for variable. In this implementation, * it corresponds to a Boolean: true if obj belongs to the first structure and * false if it belongs to the second structure. */ public void setValue(Variable variable, Object value, Object bindInf) { if (((Boolean) bindInf).booleanValue()) varsOfObj1.put(variable, value); else varsOfObj2.put(variable, value); } /**************************************************************************/ public HashMap getResultSet(Object bindInf) { if (((Boolean) bindInf).booleanValue()) return varsOfObj1; else return varsOfObj2; } public void printResultSet(HashMap resultSet, Lexicon lexicon, StringBuffer output) { Variable var = null; Object val = null; try { for (Iterator i = resultSet.keySet().iterator(); i.hasNext();) { var = (Variable) i.next(); val = resultSet.get(var); output.append("\n").append(var.toString() + " = " + AmineObjects.toString(val, lexicon)); } } catch (ToStringException tex) { output.append("\nError during the textual formulation of an object"); } } public void printResultSet(HashMap resultSet, StringBuffer output) { Variable var = null; Object val = null; try { for (Iterator i = resultSet.keySet().iterator(); i.hasNext();) { var = (Variable) i.next(); val = resultSet.get(var); output.append("\n").append(var.toString() + " = " + AmineObjects.toString(val)); } } catch (ToStringException tex) { output.append("\nError during the textual formulation of an object"); } } public void clear() { varsOfObj1.clear(); varsOfObj2.clear(); } }