/**
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is a part of The iWear Framework.
 * In particular is this file a part of the Framework context Library
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework 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 General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include <iwear-context/logiccontextterm.h>

using namespace std;

namespace iwear{ 

namespace context{

    LogicContextTerm::LogicContextTerm(){
	this->context_part_list = new list<ContextPart*>;
    }
    
    LogicContextTerm::~LogicContextTerm(void){
	delete context_part_list;
    }

    void LogicContextTerm::set_logic_relation(LogicRelation new_logic_relation){
	this->logic_relation = new_logic_relation;
    }
   
    bool LogicContextTerm::get_value(void) const{
	
	list<ContextPart*>::iterator it = context_part_list->begin();

	switch(logic_relation){

	case AND:
	    while(it != context_part_list->end()){
		if (!(*it)->get_value()){
		    return false;
		}
		it++;
	    }
	    return true;
	    
	case OR:
	    while(it != context_part_list->end()){
		if ((*it)->get_value()){
		    return true;
		}
		it ++;
	    }
	    return false;

 	default:
		stringstream error_msg; 
		error_msg << "Invalid argument for logic relation.";
		throw invalidargument_error(error_msg.str(), 
					    logic_relation);
	}
    }
    
    void LogicContextTerm::add_context_part(ContextPart* context_part){
	this->context_part_list->push_back(context_part);
    }

}  // context
}  // iwear

