Skip to content
Snippets Groups Projects
AlarmHandler.cpp 223 KiB
Newer Older
					errors[0].origin = CORBA::string_dup(ex_origin.c_str());
					push_change_event(attr_name, &except);
					push_archive_event(attr_name, &except);
				WARN_STREAM << "AlarmHandler::"<<__func__<<": " << attr_name << " - EXCEPTION PUSHING EVENTS: " << ex.errors[0].desc << endl;
			changed = !prev_error;
			it->second.to_be_evaluated = true;
			o << tmpname << ": in formula array index out of range!";
			WARN_STREAM << "AlarmHandler::"<<__func__<<": " << o.str() << endl;
			set_internal_alarm(INTERNAL_ERROR, gettime(), o.str());
			try
			{	//DevFailed for push events
				Tango::DevErrorList errors(1);
				errors.length(1);
				it->second.ex_reason = string("OUT_OF_RANGE");
				it->second.ex_desc = ev_name + ": " + o.str();
				it->second.ex_origin = ev_name;
				errors[0].desc = CORBA::string_dup(it->second.ex_desc.c_str());
				errors[0].severity = Tango::ERR;
				errors[0].reason = CORBA::string_dup(it->second.ex_reason.c_str());
				errors[0].origin = CORBA::string_dup(it->second.ex_origin.c_str());
				Tango::DevFailed except(errors);
				bool enabled=it->second.enabled;
				bool shelved=it->second.shelved;
				int silenced=it->second.silenced;
				alarms.vlock->readerOut();	//Don't hold alarms lock while pushing events to prevent deadlocks
				if(enabled && !(shelved && silenced >0))
				{
					push_change_event(attr_name, &except);
					push_archive_event(attr_name, &except);
				}
				WARN_STREAM << "AlarmHandler::"<<__func__<<": " << attr_name << " - EXCEPTION PUSHING EVENTS: " << ex.errors[0].desc << endl;
			changed = !prev_error;
			it->second.to_be_evaluated = true;
			WARN_STREAM << "AlarmHandler::"<<__func__<<": " << o.str() << endl;
			set_internal_alarm(INTERNAL_ERROR, gettime(), o.str());
			try
			{	//DevFailed for push events
				Tango::DevErrorList errors(1);
				errors.length(1);
				it->second.ex_reason = string("FORMULA_ERROR");
				it->second.ex_desc = o.str();
				it->second.ex_origin = ev_name;
				errors[0].desc = CORBA::string_dup(it->second.ex_desc.c_str());
				errors[0].severity = Tango::ERR;
				errors[0].reason = CORBA::string_dup(it->second.ex_reason.c_str());
				errors[0].origin = CORBA::string_dup(it->second.ex_origin.c_str());
				Tango::DevFailed except(errors);
				bool enabled=it->second.enabled;
				bool shelved=it->second.shelved;
				int silenced=it->second.silenced;
				alarms.vlock->readerOut();	//Don't hold alarms lock while pushing events to prevent deadlocks
				if(enabled && !(shelved && silenced >0))
				{
					push_change_event(attr_name, &except);
					push_archive_event(attr_name, &except);
				}
				WARN_STREAM << "AlarmHandler::"<<__func__<<": " << attr_name << " - EXCEPTION PUSHING EVENTS: " << ex.errors[0].desc << endl;
	}
	else
	{
		ostringstream o;
		//o << j->first << ": not found formula in alarm table" << ends;
		o << (alm_name) << ": not found formula in alarm table";
		WARN_STREAM << "AlarmHandler::"<<__func__<<": " << o.str() << endl;
		set_internal_alarm(INTERNAL_ERROR, gettime(), o.str());
#if 0	//TODO: attribute not existing -> cannot notify error pushing exception
		try
		{	//DevFailed for push events
			Tango::DevErrorList errors(1);
			errors.length(1);
			string ex_reason = string("NOT_FOUND");
			string ex_desc = ev_name + ": " + o.str();
			string ex_origin = ev_name;
			errors[0].desc = CORBA::string_dup(ex_desc.c_str());
			errors[0].reason = CORBA::string_dup(ex_reason.c_str());
			errors[0].origin = CORBA::string_dup(ex_origin.c_str());
			//push_change_event(it->second.attr_name, &except);
			//push_archive_event(it->second.attr_name, &except);
			WARN_STREAM << "AlarmHandler::"<<__func__<<": EXCEPTION PUSHING EVENTS: " << ex.errors[0].desc << endl;
	//alarms.vlock->readerOut();
void AlarmHandler::timer_update()
	//DEBUG_STREAM << "AlarmHandler::timer_update(): entering..." << endl;
	try {
		changed=alarms.timer_update();
	} catch(string & e)
	{
		ostringstream o;
		o << "Error checking time thresholds and updating alarm status=" << e;
		WARN_STREAM << "AlarmHandler::timer_update(): " << o.str() << endl;
		set_internal_alarm(INTERNAL_ERROR, gettime(), o.str());
/*		Tango::DevErrorList errors(1);
		errors.length(1);
		it->second.ex_reason = string("INTERNAL_ERROR");
		it->second.ex_desc = o.str();
		it->second.ex_origin = string("AlarmHandler::timer_update");
		errors[0].desc = CORBA::string_dup(it->second.ex_desc.c_str());
		errors[0].severity = Tango::ERR;
		errors[0].reason = CORBA::string_dup(it->second.ex_reason.c_str());
		errors[0].origin = CORBA::string_dup(it->second.ex_origin.c_str());
		Tango::DevFailed except(errors);
		push_change_event(it->second.attr_name, &except);
		push_archive_event(it->second.attr_name, &except);*/
	prepare_alarm_attr();//TODO: frequencyAlarm should be updated anyway
		if(ds_num == 0)
		{
			//attr.set_value_date_quality(ds,0/*gettime()*/,Tango::ATTR_WARNING, ds_num, 0, false);
			struct timeval now;
			gettimeofday(&now,NULL);
			push_change_event("alarm",(char**)ds,now,Tango::ATTR_WARNING, ds_num, 0, false);
		}
		else
			//attr.set_value(ds, ds_num, 0, false);
			push_change_event("alarm",ds, ds_num, 0, false);
		push_change_event("alarmNormal",&attr_alarmNormal_read[0], normalAlarms_sz);
		push_change_event("alarmUnacknowledged",&attr_alarmUnacknowledged_read[0], unacknowledgedAlarms_sz);
		push_change_event("alarmAcknowledged",&attr_alarmAcknowledged_read[0], acknowledgedAlarms_sz);
		push_change_event("alarmUnacknowledgedNormal",&attr_alarmUnacknowledgedNormal_read[0], unacknowledgedNormalAlarms_sz);
		push_change_event("alarmShelved",&attr_alarmShelved_read[0], shelvedAlarms_sz);
		push_change_event("alarmOutOfService",&attr_alarmOutOfService_read[0], outOfServiceAlarms_sz);
		push_change_event("alarmSilenced",&attr_alarmSilenced_read[0], silencedAlarms_sz);
		push_change_event("alarmList",&attr_alarmList_read[0], listAlarms_sz);
		push_change_event("alarmFrequency",&attr_alarmFrequency_read[0], listAlarms_sz);
		push_change_event("alarmAudible",attr_alarmAudible_read);
		push_change_event("alarmSummary",attr_alarmSummary_read, alarmSummary_sz);
		push_change_event("alarmDisabled",attr_alarmDisabled_read, alarmDisabled_sz);
		push_archive_event("alarmNormal",&attr_alarmNormal_read[0], normalAlarms_sz);
		push_archive_event("alarmUnacknowledged",&attr_alarmUnacknowledged_read[0], unacknowledgedAlarms_sz);
		push_archive_event("alarmAcknowledged",&attr_alarmAcknowledged_read[0], acknowledgedAlarms_sz);
		push_archive_event("alarmUnacknowledgedNormal",&attr_alarmUnacknowledgedNormal_read[0], unacknowledgedNormalAlarms_sz);
		push_archive_event("alarmShelved",&attr_alarmShelved_read[0], shelvedAlarms_sz);
		push_archive_event("alarmOutOfService",&attr_alarmOutOfService_read[0], outOfServiceAlarms_sz);
		push_archive_event("alarmSilenced",&attr_alarmSilenced_read[0], silencedAlarms_sz);
		push_archive_event("alarmList",&attr_alarmList_read[0], listAlarms_sz);
		push_archive_event("alarmFrequency",&attr_alarmFrequency_read[0], listAlarms_sz);
		push_archive_event("alarmAudible",attr_alarmAudible_read);
		push_archive_event("alarmSummary",attr_alarmSummary_read, alarmSummary_sz);
		push_archive_event("alarmDisabled",attr_alarmDisabled_read, alarmDisabled_sz);
	} catch(Tango::DevFailed& e)
	{
		ostringstream err;
		err << "error pushing alarm change event err=" << e.errors[0].desc;
		INFO_STREAM << __func__<<": " << err.str() << endl;
	}
}

bool AlarmHandler::remove_alarm(string& s) throw(string&)
	DEBUG_STREAM << "AlarmHandler::"<<__func__<<": entering alm name=" << s << endl;
	alarms.vlock->writerIn();
	alarm_container_t::iterator i = alarms.v_alarm.find(s);
	if (i != alarms.v_alarm.end()) {
		DEBUG_STREAM << "AlarmHandler::"<<__func__<<": found in table alm name=" << s << endl;
		for (set<string>::iterator j = i->second.s_event.begin(); \
				 j != i->second.s_event.end(); j++) {
			DEBUG_STREAM << "AlarmHandler::"<<__func__<<": looping event =" << *j << endl;
			/*
		 	 * for each event into the per-alarm event list find
			 * the event table entry and remove this alarm from
			 * per-event alarm list
		 	 */	 	 
			vector<event>::iterator k = \
					find(events->v_event.begin(), events->v_event.end(), *j);
			if (k != events->v_event.end()) {
				DEBUG_STREAM << "AlarmHandler::"<<__func__<<": found event =" << *j << " in vector events, removing from its alarm list name=" << i->second.name << endl;
				k->m_alarm.pop(i->second.name);
				DEBUG_STREAM << "AlarmHandler::"<<__func__<<": after m_alarm.pop" << endl;
				if (k->m_alarm.empty()) {
					/*
					 * no more alarms associated to this event, unsubscribe
					 * and remove from event table
					 */
					DEBUG_STREAM << "AlarmHandler::remove_alarm(): removing event '" \
							 				<< k->name << "' from event table" << endl;
					try {
						events->stop(k->name);
						events->remove(k->name, false);
					} catch (...) {
						ostringstream o;
						o << "unsubscribe_event() failed for " \
						 	<< k->name;
						WARN_STREAM << "AlarmHandler::remove_alarm(): " << o.str() << endl;
						alarms.vlock->writerOut();
						throw o.str();
						//return false;
					}
					//events->v_event.erase(k);
				}
			} else {
				/*
				 * shouldn't happen!!!
				 */
				ostringstream o;
				o << "event '" << *j \
					<< "' not found in event table";
				WARN_STREAM << "AlarmHandler::remove_alarm(): " << o.str() << endl;
				alarms.vlock->writerOut();
				throw o.str();
				//return false;
			}
		}  /* for */
		events->update_property();
		//delete proxy for actions
		if(i->second.dp_a)
			delete i->second.dp_a;
		i->second.dp_a = NULL;
		if(i->second.dp_n)
			delete i->second.dp_n;
		i->second.dp_n = NULL;
		try
		{
			remove_AlarmState_dynamic_attribute(i->second.attr_name);
		}
		catch(Tango::DevFailed &e)
		{
			ostringstream o;
			o << "AlarmHandler::" << __func__<<": attname '" << i->second.attr_name << "' exception removing attribute err="<<e.errors[0].desc ;
			INFO_STREAM << o.str() << endl;
		}
Graziano Scalamera's avatar
Graziano Scalamera committed
#if _FORMULA_ATTR
		CORBA::string_free(*(i->second.attr_value_formula));
		try
		{
			remove_AlarmFormula_dynamic_attribute(i->second.attr_name_formula);
		}
		catch(Tango::DevFailed &e)
		{
			ostringstream o;
			o << "AlarmHandler::" << __func__<<": attname '" << i->second.attr_name_formula << "' exception removing attribute err="<<e.errors[0].desc ;
			INFO_STREAM << o.str() << endl;
		}
Graziano Scalamera's avatar
Graziano Scalamera committed
#endif
		alarms.erase(i); //takes vlock writer lock
		WARN_STREAM << "AlarmHandler::"<<__func__<<": NOT found in table alm name=" << s << endl;
	alarms.vlock->writerOut();
	ostringstream o;
	o << "alarm '" \
		<< s << "' not found in alarm table";
	WARN_STREAM << "AlarmHandler::remove_alarm(): " << o.str() << endl;
/*void AlarmHandler::add_to_database(alarm_t& a) throw(string&)
{
	Tango::DbDatum alarm("alarm");
	Tango::DbData db_data;
	
	alarm << (short)1;
	db_data.push_back(alarm);
	string tmpname;
	tmpname = a.name;
	while (true) {
		string::size_type j = tmpname.find_first_of("/.");
		if (j == string::npos)
			break;
		tmpname.replace(j, 1, "_");
	}
	Tango::DbDatum property(tmpname);
	ostringstream num;
	num.clear();
	num  << a.grp << ends;
//		sprintf(buf, "%02X-", buf2[j]);//change here the format of saved data
//	string pro = a.name+"\t"+"$"+a.formula+"$"+"\t"+"\""+a.msg+"\""+"\t"+num.str();
	//DEBUG_STREAM << "AlarmHandler::add_to_database(): a.name=" << a.name << " a.formula=" << a.formula << " a.lev=" << a.lev << " a.grp=" << a.grp2str() << " a.msg=" << a.msg << endl;
	string pro = a.name+"\t"+a.formula+"\t"+string(a.time_threshold)+"\t"+ a.lev+"\t"+a.grp2str()+"\t"+"\""+a.msg+"\""+"\t"+a.cmd_name+"\t";	//grp has been transformed to string
	DEBUG_STREAM << "AlarmHandler::add_to_database(): adding to database property=" << pro << endl;
	property << pro;			
	db_data.push_back(property);
	try {
		get_db_device()->put_attribute_property(db_data);
	} catch (...) {
		ostringstream o;
		o << "AlarmHandler::add_to_database(): put_device_attribute_property()" \
			<< " failed" << ends;
		ERROR_STREAM << o.str() << endl;
		throw o.str();
	}
}*/

void AlarmHandler::set_internal_alarm(string name, Tango::TimeVal t, string msg, unsigned int count)
	alarm_t alm;
	bool existing=false;
	ostringstream o;
	//o << (internal.size() + 1);
	if(internal.size() == 0)
		internal_counter = 0;
	o << internal_counter;
	internallock->writerIn();
	vector<alarm_t>::iterator it;
	for(it = internal.begin(); it != internal.end(); it++)
	{
		if(name == INTERNAL_ERROR)
		{
			if(it->msg == msg)
			{
				existing=true;
				break;
			}
		}
		else										//for tango error log only one internal error per event
		{
			if(it->msg.find(name) != string::npos)		
			{
				existing=true;
				if(it->on_counter < count)
					it->on_counter = count;
				break;
			}
		}			
	}
	if(existing) 
	{
		/*size_t index;
		int count;
		index = it->stat.find("*");
		ostringstream temp;
		if((index != std::string::npos) && (index+1 != std::string::npos))
		{
			
			size_t last = it->stat.size();
			string str_count= it->stat.substr(index+1, last - index+1);      
			count = strtol(str_count.c_str(), 0,10);
			count++;  
			temp << it->stat.substr(0,index+1) << count;
			it->stat = temp.str();
		}	
		else
			it->stat += "*2";*/
		it->msg = msg;		//update with the last message
	}
	else
	{	
		alm.name = string(INTERNAL_ERROR) + "_" + o.str();
		internal_counter++;
		alm.ts = t;
		/*ostringstream stat;
		if(count==0)
			stat << S_ALARM;
		else
			stat << S_ALARM << "*" << count;*/
		//alm.stat = stat.str();
		alm.stat = S_ALARM;
		alm.ack = NOT_ACK;
		alm.done = false;
		alm.msg = msg;
		//alm.grp = GR_DEFAULT;
		if(!alm.grp_str.empty())
			alm.grp = (alm.grp_str.begin())->second;		//set groupe 'none' to internal alarms
		else
			alm.grp = GR_DEFAULT;
		//alm.lev = LEV_DEFAULT;	
		internal.push_back(alm);
	}
	internallock->writerOut();
}

//==============================================================
//------------------- AST evaluation methods -------------------
//==============================================================
formula_res_t AlarmHandler::eval_formula(tree_parse_info_t tree, string &attr_values)
{
    return eval_expression(tree.trees.begin(), attr_values);
}

formula_res_t AlarmHandler::eval_expression(iter_t const& i, string &attr_values, int ev_ind) //throw (string &), std::out_of_range
    //iter_t it = i->children.begin();

    if (i->value.id() == formula_grammar::val_rID)
    {
        if(i->children.size() != 0)
        {
        	err <<  "in node val_rID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
        	throw err.str(); 
        }
        string val_d(i->value.begin(), i->value.end());
		formula_res_t res;
		res.value = strtod(val_d.c_str(), 0);
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node value real = " << val_d << "(value="<<res.value<<" quality="<<res.quality<<")" << endl;
    }
    else if (i->value.id() == formula_grammar::val_hID)
    {
        if(i->children.size() != 0)
        {
        	err <<  "in node val_hID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
        	throw err.str(); 
        }        		
        string val_d(i->value.begin(), i->value.end());
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node value hex = " << val_d << endl;
		formula_res_t res;
		res.value = strtod(val_d.c_str(), 0);
        return res;
    } 
    else if (i->value.id() == formula_grammar::val_stID)
    {
        if(i->children.size() != 0)
        {
        	err <<  "in node val_stID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
        	throw err.str(); 
        }     		
        string val_st(i->value.begin(), i->value.end());
        double st =  i->value.value();			//get value directly from node saved with access_node_d
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node value state : " << val_st << "=" << st << endl;
		formula_res_t res;
		res.value = st;
        return res;
    }
    else if (i->value.id() == formula_grammar::val_alarm_enum_stID)
    {
        if(i->children.size() != 0)
        {
        	err <<  "in node val_alarm_enum_stID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
        	throw err.str();
        }
        string val_st(i->value.begin(), i->value.end());
        double st =  i->value.value();			//get value directly from node saved with access_node_d
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node value alarm enum state : " << val_st << "=" << st << endl;
		formula_res_t res;
		res.value = st;
        return res;
    }
	else if (i->value.id() == formula_grammar::val_qualityID)
	{
		if(i->children.size() != 0)
		{
			err <<  "in node val_qualityID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
			throw err.str();
		}
		string val_quality(i->value.begin(), i->value.end());

		double quality =  i->value.value();			//get value directly from node saved with access_node_d
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node value quality : " << val_quality << "=" << quality << endl;
		formula_res_t res;
		res.value = quality;
        return res;
	}
    else if (i->value.id() == formula_grammar::unary_exprID)
    {
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node unary expression: " << string(i->value.begin(), i->value.end()) << endl;
        	err <<  "in node unary_exprID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
		formula_res_t res;
		res = eval_expression(i->children.begin(), attr_values);
        }
        else if (*i->value.begin() == '-')
        {
        }
        else if (*i->value.begin() == '!')
        {
        	err <<  "in node unary_exprID(" << string(i->value.begin(), i->value.end()) << ") value not allowed";
    }
    else if (i->value.id() == formula_grammar::mult_exprID)
    {
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node mult expression: " << string(i->value.begin(), i->value.end()) << endl;
        	err <<  "in node mult_exprID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
        	throw err.str(); 
        }
        if (*i->value.begin() == '*')
        {
            return eval_expression(i->children.begin(), attr_values) *
                eval_expression(i->children.begin()+1, attr_values);
        }
        else if (*i->value.begin() == '/')
        {
            return eval_expression(i->children.begin(), attr_values) /
                eval_expression(i->children.begin()+1, attr_values);
        }
        else
        {
        	err <<  "in node mult_exprID(" << string(i->value.begin(), i->value.end()) << ") value not allowed";
        	throw err.str(); 
        }
    }
    else if (i->value.id() == formula_grammar::add_exprID)
    {
#ifdef _DEBUG_FORMULA
        DEBUG_STREAM << "		node add expression: " << string(i->value.begin(), i->value.end()) << endl;
        	err <<  "in node add_exprID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
        	throw err.str(); 
        }
        if (*i->value.begin() == '+')
        {
            return eval_expression(i->children.begin(), attr_values) +
                eval_expression(i->children.begin()+1, attr_values);
        }
        else if (*i->value.begin() == '-')
        {
            return eval_expression(i->children.begin(), attr_values) -
                eval_expression(i->children.begin()+1, attr_values);
        }
        else
        {
        	err <<  "in node add_exprID(" << string(i->value.begin(), i->value.end()) << ") value not allowed";
        	throw err.str(); 
        }
    }
    else if (i->value.id() == formula_grammar::event_ID)
    {
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node event" << string(i->value.begin(), i->value.end()) << endl;
        	err <<  "in node event_ID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();;
		if((i->children.begin()+1)->value.id() == formula_grammar::indexID)
			ind = eval_expression(i->children.begin()+1, attr_values);		//array index
		else if((i->children.begin()+1)->value.id() == formula_grammar::propertyID)
			if(string((i->children.begin()+1)->value.begin(), (i->children.begin()+1)->value.end()) == ".quality")
			{
				formula_res_t res = eval_expression(i->children.begin(), attr_values, (int)ind.value);
				res.value = res.quality;
#ifdef _DEBUG_FORMULA
				DEBUG_STREAM << "		node event.quality -> " << res.value << endl;
				return res;
			}
			else if(string((i->children.begin()+1)->value.begin(), (i->children.begin()+1)->value.end()) == ".alarm")
			{
				formula_res_t res = eval_expression(i->children.begin(), attr_values, (int)ind.value);
				res.value = (res.value == _UNACK) || (res.value == _ACKED);
#ifdef _DEBUG_FORMULA
				DEBUG_STREAM << "		node event.alarm -> " << res.value << endl;
				return res;
			}
			else if(string((i->children.begin()+1)->value.begin(), (i->children.begin()+1)->value.end()) == ".normal")
			{
				formula_res_t res = eval_expression(i->children.begin(), attr_values, (int)ind.value);
				res.value = (res.value == _NORM) || (res.value == _RTNUN);
#ifdef _DEBUG_FORMULA
				DEBUG_STREAM << "		node event.normal -> " << res.value << endl;
        	err <<  "in node event_ID(" << string(i->value.begin(), i->value.end()) << ") children2 is not an index ->" << string((i->children.begin()+1)->value.begin(), (i->children.begin()+1)->value.end());
        }
		return eval_expression(i->children.begin(), attr_values, (int)ind.value);
    }    
    else if (i->value.id() == formula_grammar::nameID)
    {
		if(i->children.size() != 0)		
		{
			err <<  "in node nameID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
			throw err.str(); 
		}
		events->veclock.readerIn();
		vector<event>::iterator it = events->v_event.begin();
		string s(i->value.begin(), i->value.end());
		std::transform(s.begin(), s.end(), s.begin(), (int(*)(int))tolower);		//transform to lowercase
		while ((it != events->v_event.end()) && (it->name != s))
			it++;
				if(it->ex_desc.length() > 0)
					err << it->ex_desc << "";
					err <<  "attribute " << string(i->value.begin(), i->value.end()) << " value not valid while evaluating formula";
				events->veclock.readerOut();
			else if(it->type != Tango::DEV_STRING && it->value.empty())
				if(it->ex_desc.length() > 0)
					err << it->ex_desc << "";
					err <<  "attribute " << string(i->value.begin(), i->value.end()) << " value not initialized while evaluating formula";
				events->veclock.readerOut();
			if(it->value.size() > 1)
				temp_attr_val << "\"" <<  it->name << "[" << ev_ind << "]\":" <<it->value.at(ev_ind) << ",";//throw  std::out_of_range
			else
				temp_attr_val << "\"" <<  it->name << "\":" <<it->value.at(ev_ind) << ",";//throw  std::out_of_range
			formula_res_t res;
			res.quality = it->quality;
			res.ex_reason = it->ex_reason;
			res.ex_desc = it->ex_desc;
			res.ex_origin = it->ex_origin;
#ifdef _DEBUG_FORMULA
			DEBUG_STREAM << "		node name -> " << temp_attr_val.str() << " quality=" << res.quality << endl;
			res.value = it->value.at(ev_ind);		//throw  std::out_of_range
			events->veclock.readerOut();
			events->veclock.readerOut();
			err <<  "in event: (" << string(i->value.begin(), i->value.end()) << ") not found in event table";
        	throw err.str();		
		}  
    } 
    else if (i->value.id() == formula_grammar::indexID)
    {
		if(i->children.size() != 0)		
		{
        	err <<  "in node indexID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
        	throw err.str(); 
        }			
        string val_d(i->value.begin(), i->value.end());
#ifdef _DEBUG_FORMULA
     	DEBUG_STREAM << "		node index = " << val_d << endl;
     	formula_res_t res;
     	res.value = strtod(val_d.c_str(), 0);
        return res;
    }   
    else if (i->value.id() == formula_grammar::logical_exprID)
    {
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node logical expression: " << string(i->value.begin(), i->value.end()) << endl;
        	err <<  "in node logical_exprID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
        	throw err.str(); 
        }	        		 
		if (string(i->value.begin(), i->value.end()) == string("&&"))
        {
            return eval_expression(i->children.begin(), attr_values) &&
                eval_expression(i->children.begin()+1, attr_values);
        }
		else if (string(i->value.begin(), i->value.end()) == string("||"))
        {
            return eval_expression(i->children.begin(), attr_values) ||
                eval_expression(i->children.begin()+1, attr_values);
        }             
        else
        {
        	err <<  "in node logical_exprID(" << string(i->value.begin(), i->value.end()) << ") value not allowed";
        	throw err.str(); 
        }		  
    }   
    else if (i->value.id() == formula_grammar::bitwise_exprID)
    {
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node bitwise expression: " << string(i->value.begin(), i->value.end()) << endl;
        	err <<  "in node bitwise_exprID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
		formula_res_t res_1=eval_expression(i->children.begin(), attr_values),
           	res_2=eval_expression(i->children.begin()+1, attr_values);
        long val_l1,val_l2;
            
    	string err2("ERROR: non-int value in bitwise operation!");
    	val_l1 = (long)trunc(res_1.value);		//transform to long
    	val_l2 = (long)trunc(res_2.value);		//transform to long
	    if((val_l1 != res_1.value) || (val_l2 != res_2.value))	//if different, lost something with truncf
        {
        	formula_res_t res;
        	res.value = (double)(val_l1 & val_l2);
        	res.quality = res.combine_quality(res_1.quality, res_2.quality);
        	res.ex_reason = res.combine_exception(res_1.ex_reason, res_2.ex_reason);
        	res.ex_desc = res.combine_exception(res_1.ex_desc, res_2.ex_desc);
        	res.ex_origin = res.combine_exception(res_1.ex_origin, res_2.ex_origin);
            return res;
        	formula_res_t res;
        	res.value = (double)(val_l1 | val_l2);
        	res.quality = res.combine_quality(res_1.quality, res_2.quality);
        	res.ex_reason = res.combine_exception(res_1.ex_reason, res_2.ex_reason);
        	res.ex_desc = res.combine_exception(res_1.ex_desc, res_2.ex_desc);
        	res.ex_origin = res.combine_exception(res_1.ex_origin, res_2.ex_origin);
            return res;
        {
        	formula_res_t res;
        	res.value = (double)(val_l1 ^ val_l2);
        	res.quality = res.combine_quality(res_1.quality, res_2.quality);
        	res.ex_reason = res.combine_exception(res_1.ex_reason, res_2.ex_reason);
        	res.ex_desc = res.combine_exception(res_1.ex_desc, res_2.ex_desc);
        	res.ex_origin = res.combine_exception(res_1.ex_origin, res_2.ex_origin);
            return res;
        	err << "in node bitwise_exprID(" << string(i->value.begin(), i->value.end()) << ") value not allowed";
        	throw err.str(); 
        }			   
    }   
    else if (i->value.id() == formula_grammar::shift_exprID)
    {
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node shift expression: " << string(i->value.begin(), i->value.end()) << endl;
        	err <<  "in node shift_exprID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
		formula_res_t res_1=eval_expression(i->children.begin(), attr_values),
           	res_2=eval_expression(i->children.begin()+1, attr_values);
        long val_l1,val_l2;
            
    	string err2("ERROR: non-int value in bitwise operation!");
    	val_l1 = (long)trunc(res_1.value);		//transform to long
    	val_l2 = (long)trunc(res_2.value);		//transform to long
	    if((val_l1 != res_1.value) || (val_l2 != res_2.value))	//if different, lost something with truncf
    		throw err2;
    		  
        if (string(i->value.begin(), i->value.end()) == string("<<"))
        {
        	formula_res_t res;
        	res.value = (double)(val_l1 << val_l2);
        	res.quality = res.combine_quality(res_1.quality, res_2.quality);
        	res.ex_reason = res.combine_exception(res_1.ex_reason, res_2.ex_reason);
        	res.ex_desc = res.combine_exception(res_1.ex_desc, res_2.ex_desc);
        	res.ex_origin = res.combine_exception(res_1.ex_origin, res_2.ex_origin);
            return res;
        }
        else if (string(i->value.begin(), i->value.end()) == string(">>"))
        {
        	formula_res_t res;
        	res.value = (double)(val_l1 >> val_l2);
        	res.quality = res.combine_quality(res_1.quality, res_2.quality);
        	res.ex_reason = res.combine_exception(res_1.ex_reason, res_2.ex_reason);
        	res.ex_desc = res.combine_exception(res_1.ex_desc, res_2.ex_desc);
        	res.ex_origin = res.combine_exception(res_1.ex_origin, res_2.ex_origin);
            return res;
        	err <<  "in node shift_exprID(" << string(i->value.begin(), i->value.end()) << ") value not allowed";
        	throw err.str(); 
        }		   
    }      
    else if (i->value.id() == formula_grammar::equality_exprID)
    {
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node equality expression: " << string(i->value.begin(), i->value.end()) << endl;
        	err <<  "in node equality_exprID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();


		//string comparison here
		iter_t const& i2_1 = i->children.begin();
		iter_t const& i2_2 = i->children.begin()+1;
		//OK only attr == 'string' or attr != 'string'
		if(i2_1->value.id() == formula_grammar::nameID && i2_2->value.id() == formula_grammar::val_stringID)
		{
			if(i2_1->children.size() == 0 && i2_2->children.size() == 0)
			{
				//retrieve string from attribute:
				string attr_val = "";
				string name_id(i2_1->value.begin(), i2_1->value.end());
				std::transform(name_id.begin(), name_id.end(), name_id.begin(), (int(*)(int))tolower);		//transform to lowercase
				formula_res_t res;
				events->veclock.readerIn();
				vector<event>::iterator it = events->v_event.begin();

				while ((it != events->v_event.end()) && (it->name != name_id))
						it++;
				if (it != events->v_event.end())
				{
					if(!it->valid)
					{
						err <<  "in node equality_exprID -> nameID(" << string(i2_1->value.begin(), i2_1->value.end()) << ") value not valid!";
						if(it->ex_desc.length() > 0)
							err << " EX: '" << it->ex_desc << "'";
						events->veclock.readerOut();
						throw err.str();
					}
					else if(it->type != Tango::DEV_STRING && it->value.empty())
					{
						err <<  "in node nameID(" << string(i2_1->value.begin(), i2_1->value.end()) << ") value not initialized!!";
						if(it->ex_desc.length() > 0)
							err << " EX: '" << it->ex_desc << "'";
						events->veclock.readerOut();
						throw err.str();
					}
					ostringstream temp_attr_val;
					temp_attr_val << "\"" << it->name << "\":\"" <<it->value_string << "\",";
					attr_values += temp_attr_val.str();
					res.quality = it->quality;
					res.ex_reason = it->ex_reason;
					res.ex_desc = it->ex_desc;
					res.ex_origin = it->ex_origin;
#ifdef _DEBUG_FORMULA
					DEBUG_STREAM << "		node name -> " << temp_attr_val.str() << " quality=" << res.quality << endl;
					attr_val =  string("'") + it->value_string + string("'");
					events->veclock.readerOut();
					events->veclock.readerOut();
					err <<  "in event: (" << string(i->value.begin(), i->value.end()) << ") not found in event table";
					throw err.str();
				}

				//retrieve string from formula
				string val_string(i2_2->value.begin(), i2_2->value.end());

				if (string(i->value.begin(), i->value.end()) == string("!="))
				{
					res.value = attr_val != val_string;
					return res;
				}
				else if (string(i->value.begin(), i->value.end()) == string("=="))
				{
					res.value = attr_val == val_string;
					return res;
				}
				else
				{
					err <<  "in node equality_exprID(" << string(i->value.begin(), i->value.end()) << ") value not allowed (val_stringID)";
					throw err.str();
				}
			}
		}
		else
		{
			if (string(i->value.begin(), i->value.end()) == string("!="))
			{
				return eval_expression(i->children.begin(), attr_values) !=
					eval_expression(i->children.begin()+1, attr_values);
			}
			else if (string(i->value.begin(), i->value.end()) == string("=="))
			{
				return eval_expression(i->children.begin(), attr_values) ==
					eval_expression(i->children.begin()+1, attr_values);
			}
			else
			{
				err <<  "in node equality_exprID(" << string(i->value.begin(), i->value.end()) << ") value not allowed";
    }   
    else if (i->value.id() == formula_grammar::compare_exprID)
    {
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node compare expression: " << string(i->value.begin(), i->value.end()) << endl; 
        	err <<  "in node compare_exprID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
        	throw err.str(); 
        }	
		if (string(i->value.begin(), i->value.end()) == string("<="))
        {
            return eval_expression(i->children.begin(), attr_values) <=
                eval_expression(i->children.begin()+1, attr_values);
        }
		else if (string(i->value.begin(), i->value.end()) == string(">="))
        {
            return eval_expression(i->children.begin(), attr_values) >=
                eval_expression(i->children.begin()+1, attr_values);
        }        
        else if (*i->value.begin() == '<')
        {
            return eval_expression(i->children.begin(), attr_values) <
                eval_expression(i->children.begin()+1, attr_values);
        }
        else if (*i->value.begin() == '>')
        {
            return eval_expression(i->children.begin(), attr_values) >
                eval_expression(i->children.begin()+1, attr_values);
        }        
        else
        {
        	err <<  "in node equality_exprID(" << string(i->value.begin(), i->value.end()) << ") value not allowed";
        	throw err.str(); 
        }	
    }   
    else if (i->value.id() == formula_grammar::funcID)
    {
#ifdef _DEBUG_FORMULA
		DEBUG_STREAM << "		node function: " << string(i->value.begin(), i->value.end()) << endl;
			err <<  "in node funcID(" << string(i->value.begin(), i->value.end()) << ") children=" << i->children.size();
			throw err.str();