/**	
 *	Simulates all Fullness Sensors 
 *	
 *	Formula: 
 */
static void SimulateFullness(bool set)
{
	if(set)
	{
		int snr = singlerandom(countTrie(FullnessTable));
		char name[9+numlen((unsigned)snr)];		
		snprintf(name, sizeof(char)*(9+numlen((unsigned)snr)), "%s%d", "Fullness", snr);
		
		Sensor*const sensor = (findinTrie(FullnessTable,name));
		bSensor*const bsensor = (bSensor*)sensor;
		bool*p=malloc(sizeof*p);

		if(!p)
		{
			Log(LOGT_SERVER, LOGL_SERIOUS_ERROR, "Out of memory!");
			return;
		}
		// Set current value in history queue
		*p = bsensor->value;
		sensor->delta = AutoQadd(sensor->delta, p);

		// Get new value and set as current value
		bsensor->value = true;
		PushS(sensor);		
	}
	else
	{
		Sensor*const sensor = checkFullnessValues(FullnessTable);
		if(sensor)
		{
			bSensor*const bsensor = (bSensor*)sensor;
			bool*p=malloc(sizeof*p);
			
			if(!p)
			{
				Log(LOGT_SERVER, LOGL_SERIOUS_ERROR, "Out of memory!");
				return;
			}
			// Set current value in history queue
			*p = bsensor->value;
			sensor->delta = AutoQadd(sensor->delta, p);

			// Get new value and set as current value
			bsensor->value = false;
			PushS(sensor);
		}
	}
}
/**	
 *	Simulates all Flow Sensors 
 *
 *	Only operator can change value 
 */
static void SimulateFlow(Trie*const sensorbox)
{
	if(!sensorbox)return;
	if(!sensorbox->e)return;
	{
		int newValue;
		Sensor*const sensor = sensorbox->e;
		iSensor*const isensor = (iSensor*)sensor;
		int*p = malloc(sizeof*p);
		
		if(!p)
		{
			Log(LOGT_SERVER, LOGL_SERIOUS_ERROR, "Out of memory!");
			return;
		}
		// Set current value in history queue
		*p = isensor->value;
		sensor->delta = AutoQadd(sensor->delta, p);
		
		//TODO change value by operator
		// Get new value and set as current value
		newValue = (isensor->value);
		isensor->value = newValue;
		PushS(sensor);
	}
}
/**	
 *	Simulates all Temperature Sensors 
 *
 *	Formula: 
 *	Oldvalue + [1,15% of average Radiation] +
 *	[-1..1] - [2% of average Flow] 
 */
static void SimulateTemperature(Trie*const sensorbox)
{
	if(!sensorbox)return;
	if(!sensorbox->e)return;
	{
		int newValue, averageRadiation, averageFlow;
		Sensor*const sensor = sensorbox->e;
		
		iSensor*const isensor = (iSensor*)sensor;
		int*p = malloc(sizeof*p);
		
		if(!p)
		{
			Log(LOGT_SERVER, LOGL_SERIOUS_ERROR, "Out of memory!");
			return;
		}
		// Set current value in history queue
		*p = isensor->value;
		sensor->delta = AutoQadd(sensor->delta, p);
		
		averageRadiation = getAverageValue(RadiationTable);
		averageFlow = getAverageValue(FlowTable);
		
		// Get new value and set as current value
		newValue = (isensor->value) + ((averageRadiation/83) + (multirandom(2)-1) - (averageFlow/49));
		
		if( (newValue < 18) && (!(singlerandom(12) == 12)) ) newValue += singlerandom(10); 
		if( (newValue > 70) && (!(singlerandom(12) == 12)) ) newValue -= singlerandom(10); 

		if( (newValue > isensor->lbound) && (newValue < isensor->ubound) && Templock && (TempSens == isensor) ) 
		{
			Templock = 0;
			TempSens = 0;
		}
		else if( (newValue < isensor->lbound) && Templock && !(TempSens == isensor) ) newValue += 10;
		else if( (newValue > isensor->ubound) && Templock && !(TempSens == isensor) ) newValue -= 10;
		else if( (newValue < isensor->lbound) && !Templock )
		{
			Templock = 1;
			TempSens = isensor;
		}
		else if((newValue > isensor->ubound) && !Radlock)
		{
			Templock = 1;
			TempSens = isensor;
		}
		
		//Log(LOGT_SERVER, LOGL_DEBUG, "%s: %d",sensor->name,newValue);
		isensor->value = newValue;
		PushS(sensor);
	}
}
/**	
 *	Simulates all Pressure Sensors
 *
 *	Formula: 
 *	Startvalue + ( [0..1200] * [average Temperature] * 
 *	(( [amount of true in FnSet1] / 2) + 1) ) -
 *	( [0..1200] * [5% of average Flow] * 
 *	(( [amount of true in FnSet2] / 2) + 1) )		 
 */
static void SimulatePressure(Trie*const sensorbox)
{
	if(!sensorbox)return;
	if(!sensorbox->e)return;
	{
		int newValue, averageTemperature, averageFlow, halfFnSet, amountFnSet1, amountFnSet2;
		Sensor*const sensor = sensorbox->e;
		iSensor*const isensor = (iSensor*)sensor;
		int*p = malloc(sizeof*p);
		
		if(!p)
		{
			Log(LOGT_SERVER, LOGL_SERIOUS_ERROR, "Out of memory!");
			return;
		}
		// Set current value in history queue
		*p = isensor->value;
		sensor->delta = AutoQadd(sensor->delta, p);
		
		averageTemperature = getAverageValue(TemperatureTable);
		averageFlow = getAverageValue(FlowTable);
		halfFnSet = (countTrie(FullnessTable)/2);
		amountFnSet1 = countTrueInSet(FullnessTable, 0, &halfFnSet);
		amountFnSet2 = countTrueInSet(FullnessTable, &halfFnSet, &halfFnSet);
		
		// Get new value and set as current value
		newValue = 	( (isensor->startvalue) + 
					( singlerandom(1200) * averageTemperature * ((amountFnSet1 / 2) + 1) ) - 
					( singlerandom(1200) * (averageFlow / 20) * ((amountFnSet2 / 2) + 1) ) );	
		
		if( (newValue > isensor->lbound) && (newValue < isensor->ubound) && Presslock && (PressSens == isensor) ) 
		{
			Presslock = 0;
			PressSens = 0;
		}
		else if( (newValue < isensor->lbound) && Presslock && !(PressSens == isensor) ) newValue += 10000;
		else if( (newValue > isensor->ubound) && Presslock && !(PressSens == isensor) ) newValue -= 10000;
		else if( (newValue < isensor->lbound) && !Presslock )
		{
			Presslock = 1;
			PressSens = isensor;
		}
		else if((newValue > isensor->ubound) && !Presslock)
		{
			Presslock = 1;
		}

		isensor->value = newValue;
		PushS(sensor);
	}
}
/**	
 *	Simulates all Radiation Sensors 
 *
 *	Formula: 
 *	Oldvalue + [-19..20]
 */
static void SimulateRadiation(Trie*const sensorbox)
{
	if(!sensorbox)return;
	if(!sensorbox->e)return;
	{
		int newValue, fx;
		Sensor*const sensor = sensorbox->e;
		iSensor*const isensor = (iSensor*)sensor;
		int*p = malloc(sizeof*p);
		
		if(!p)
		{
			Log(LOGT_SERVER, LOGL_SERIOUS_ERROR, "Out of memory!");
			return;
		}
		// Set current value in history queue
		*p = isensor->value;
		sensor->delta = AutoQadd(sensor->delta, p);
		
		//srand(_seed=(unsigned)rand());
		//fx = ((rand()%(39))-20);
		fx = multirandom(19)+1;
		
		// Get new value and set as current value
		newValue = (isensor->value) + fx;

		if( (newValue < 80) && (!(singlerandom(10) == 10)) ) newValue += singlerandom(25); 
		if( (newValue > 950) && (!(singlerandom(10) == 10)) ) newValue -= singlerandom(25); 
		
		if( (newValue > isensor->lbound) && (newValue < isensor->ubound) && Radlock && (RadSens == isensor) ) 
		{
			Radlock = 0;
			RadSens = 0;
		}
		else if( (newValue < isensor->lbound) && Radlock && !(RadSens == isensor) ) newValue += 30;
		else if( (newValue > isensor->ubound) && Radlock && !(RadSens == isensor) ) newValue -= 30;
		else if( (newValue < isensor->lbound) && !Radlock )
		{
			Radlock = 1;
			RadSens = isensor;
		}
		else if((newValue > isensor->ubound) && !Radlock)
		{
			Radlock = 1;
		}

		isensor->value = newValue;
		PushS(sensor);
	}
}
示例#6
0
/*
** Hlavná funkcia syntaktickej analýzy, simuluje vytváranie derivačného stromu
*/
int analyza(FILE *fp, char *vstup, tState *tokenType){
	char pravidla[16][6] = { // čísla sú z enum štruktúry v scanner.h
										{E, 4, E, -1, '\0'}, // *
										{E, 5, E, -1, '\0'}, // /
										{E, 6, E, -1, '\0'}, // +
										{E, 7, E, -1, '\0'}, // -
										{E, 8, E, -1, '\0'}, // <
										{E, 9, E, -1, '\0'}, // >
										{E, 10, E, -1, '\0'}, // <=
										{E, 11, E, -1, '\0'}, // >=
										{E, 13, E, -1, '\0'}, // ==
										{E, 14, E, -1, '\0'}, // !=
										{17, E, 16, -1, '\0'}, // zátvorky ()
										{E, 7, -1, '\0'}, // záp**né číslo
										{37, -1, '\0'}, // cislo alebo identifikátor
										{E, 24, E, -1, '\0'}, // argumenty funkcie
										{17, E, 16, 37, -1, '\0'} // funkcia
									}; // 60 je $
	char vyraz[5]; // výraz sa bude porovnávať s tabuľkou
	STACK zasobnik;
	char znak; // znak zo vstupu a zo zásobníku
	int i;

printf("SAV **************\n");
	InitS(&zasobnik); // inicializácia zásobniku

	PushS(&zasobnik, DOLAR); //na vrcholu zásobniku musí byť $

	do{
		znak = TopS(&zasobnik);
		if(znak == E){ //nemôže porovnávať s neterminálom, preto vyberie zo zásobníku druhý znak
			znak = TopSecS(&zasobnik);
		}

		//if(token T)

		if(*tokenType == 36 || *tokenType == 42){ // zmeni id na cislo
			*tokenType = 37;
		}

		/*pomocné ladiace funkcie*/
		printf("SAV **************\n");
		PrintS(&zasobnik);
		printf("SAV znak = %d\n", znak);
		printf("SAV vstup = %d\n******************\n", *tokenType);
		/** *********************** */
		printf("PT: %c\n", precedencna_tabulka(znak, *tokenType));
		switch(precedencna_tabulka(znak, *tokenType)){

			case '=':
				PushS(&zasobnik, *tokenType);
				//FreeToken(vstup);
				vstup = Token(tokenType, fp);
				continue;

			case '<':
				if(TopS(&zasobnik) == E){
					PopS(&zasobnik);
					PushS(&zasobnik, -1);
					PushS(&zasobnik, E);
					PushS(&zasobnik, *tokenType);
				}
				else{
					PushS(&zasobnik, -1); // -1 je <
					PushS(&zasobnik, *tokenType);
				}
				//FreeToken(vstup);
				vstup = Token(tokenType, fp);
				continue;

			case '>':

				/* Vyberie zo zásobníku znaky a vloží do premennej výraz */
				i = 0;
				do{
					vyraz[i] = TopPopS(&zasobnik);
					i++;
				}while(vyraz[i - 1] != -1);
				vyraz[i] = '\0';

				/* Porovnáva výraz s pravidlami */
				for(i = 0; i<=16; i++){
					if(i == 16){ // nenašla sa zhoda
						FreeS(&zasobnik);
						SAError();
					}
					if(strcmp(pravidla[i], vyraz) == 0){ // našla sa zhoda
						break;
					}
				}

					PushS(&zasobnik, E); // Pushne neterminál na zásobník
					continue;

			case 'U': // výraz je správny
				FreeS(&zasobnik);
				return 0;
			case 'X': // chyba
				FreeS(&zasobnik);
				SAError();
			}
		}while(1);

	FreeS(&zasobnik);
	return 0;
}