Ejemplo n.º 1
0
void IntegrationTest::getIntegrationTest(const char* keyword){
    if(stringlist_contains(keysShort,keyword) && stringlist_contains(keysLong, keyword)){
        std::vector<double> timeVec1, timeVec2;
        setTimeVecs(timeVec1, timeVec2);  // Sets the time vectors, they are equal for all keywords (WPOR:PROD01 etc)
        setDataSets(timeVec1, timeVec2);

        if(printSpecificKeyword){
            printDataOfSpecificKeyword(timeVec1, timeVec2, keyword);
        }
        if(findVolumeError){
            WellProductionVolume volume = getSpecificWellVolume(timeVec1, timeVec2, keyword);
            if(volume.error == 0){
                std::cout << "For keyword " << keyword << " the total production volume is 0" << std::endl;
            }
            else{
                std::cout << "For keyword " << keyword << " the total production volume is "<< volume.total;
                std::cout << ", the error volume is " << volume.error << " the error ratio is " << volume.error/volume.total << std::endl;
            }
        }
        checkForKeyword(timeVec1, timeVec2, keyword);
        return;
    }
    OPM_THROW(std::invalid_argument, "The keyword used is not common for the two files.");
}
Ejemplo n.º 2
0
tokenInfo getNextToken(FILE *fp, buffer B)
{
	// Read the input from the buffer.
	// Check if the buffer has enough space. Else change the buffer.
	if(B->fwdPointer >= BUFFERSIZE)
	{
		// Change buffers.
		getStream(fp, B->nextBuffer, BUFFERSIZE);
		// Set current buffer to next buffer
		if(B->nextBuffer == NULL)
		{
			return NULL;
		}
		B = B->nextBuffer;
		// Return next token from the next buffer.
		return getNextToken(fp, B);
	}
	char a = B->buff[B->fwdPointer];
	// Check first for the one character tokens.
	if(a == '(')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = OP;
		token->token_value = "(";
		return token;
	}
	else if(a == ')')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = CL;
		token->token_value = ")";
		return token;
	}
	else if(a == '[')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = SQO;
		token->token_value = "[";
		return token;
	}
	else if(a == ']')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = SQC;
		token->token_value = "]";
		return token;
	}
	else if(a == ';')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = SEMICOLON;
		token->token_value = ";";
		return token;
	}
	else if(a == ',')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = COMMA;
		token->token_value = ",";
		return token;
	}
	else if(a == '+')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = PLUS;
		token->token_value = "+";
		return token;
	}
	else if(a == '-')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = MINUS;
		token->token_value = "-";
		return token;
	}
	else if(a == '*')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = MUL;
		token->token_value = "*";
		return token;
	}
	else if(a == '/')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = DIV;
		token->token_value = "/";
		return token;
	}
	else if(a == '@')
	{
		tokenInfo token = makeSingleToken(B);
		token->token_name = SIZE;
		token->token_value = "@";
		return token;
	}
	// Check for comments token and safely ignore
	else if(a == '#')
	{
		++B->fwdPointer;
		while(B->fwdPointer < BUFFERSIZE && (B->buff[B->fwdPointer]!= '\n' && B->buff[B->fwdPointer]!= EOF))
		{
			++B->fwdPointer;
		}
		// Any of the above cases means we have reached the end of the current buffer. Check if exceeded buffer length

		if(B->buff[B->fwdPointer] == '\n' || B->buff[B->fwdPointer] == EOF)
		{
			// The comment has ended. Get the next token and return it.
			++B->lineNumber;
			getStream(fp, B->nextBuffer, BUFFERSIZE);
			// Set current buffer to next buffer
			if(B->nextBuffer == NULL)
			{
				return NULL;
			}
			B = B->nextBuffer;
			// Keep checking for how long comments go and return next token.
			return getNextToken(fp, B);
		}

		if(B->fwdPointer >= BUFFERSIZE)
		{
			// The comment has overflowed to next buffer. Finish reading comment and return the next token.
			getStream(fp, B->nextBuffer, BUFFERSIZE);
			// Set current buffer to next buffer
			if(B->nextBuffer == NULL)
			{
				return NULL;
			}
			B = B->nextBuffer;
			// Keep checking for how long comments go and return next token.
			return getEndCommentAndNextToken(fp, B);
		}
	}
	else if(a == '>' || a == '<')
	{
		// Check next token.
		++B->fwdPointer;
		tokenInfo token = malloc(sizeof(tokenStruct));
		token->charNumber = B->charNumber;
		token->lineNumber = B->lineNumber;
		int curPointer = B->curPointer; // Store the current value temporarily.
		tokenInfo nextToken = getNextToken(fp, B);
		if(nextToken->token_name == ASSIGNOP)
		{
			// Assign the correct value to this token.
			if(a == '>')
			{
				token->token_name = GE;
				B->curPointer = ++B->fwdPointer;
				B->charNumber = B->fwdPointer;
				token->token_value = ">=";
				free(nextToken);
				return token;
			}
			else
			{
				token->token_name = LE;
				B->curPointer = ++B->fwdPointer;
				B->charNumber = B->fwdPointer;
				token->token_value = "<=";
				free(nextToken);
				return token;
			}
		}
		else
		{
			// Something else. Return LT or GT.
			// TODO: Is an error possible in this case?
			if(a == '>')
			{
				token->token_name = GT;
				// Lost the next token. Need to generate that one again.
				B->curPointer = curPointer + 1;
				B->fwdPointer = B->curPointer;
				B->charNumber = B->fwdPointer;
				token->token_value = ">";
				free(nextToken);
				return token;
			}
			else if(a == '<')
			{
				token->token_name = LT;
				// Lost the next token. Need to generate that one again.
				B->curPointer = curPointer + 1;
				B->fwdPointer = B->curPointer;
				B->charNumber = B->fwdPointer;
				token->token_value = "<";
				free(nextToken);
				return token;
			}
		}
	}
	else if(a == '=')
	{
		++B->fwdPointer;
		tokenInfo token = malloc(sizeof(tokenStruct));
		token->charNumber = B->charNumber;
		token->lineNumber = B->lineNumber;
		int curPointer = B->curPointer; // Temporary store.
		tokenInfo nextToken = getNextToken(fp, B);
		if(nextToken->token_name == ASSIGNOP)
		{
			// EQ token.
			token->token_name = EQ;
			token->token_value = "==";
			B->charNumber = ++B->fwdPointer;
			free(nextToken);
			return token;
		}
		else if(nextToken->token_name == DIV)
		{
			// This should become a NE token
			if(B->buff[B->fwdPointer] == '=')
			{
				// It is the NE token.
				token->token_name = NE;
				token->token_value = "=/=";
				++B->fwdPointer;
				++B->curPointer;
				++B->charNumber;
				free(nextToken);
				return token;
			}
			else
			{
				// Not NE token. An error
				// TODO: Decide error functions
				char *err = "Bad token. Possible Token: NE .";
				showError(err, B->lineNumber);
				++B->fwdPointer;
				++B->curPointer;
				++B->charNumber;
				free(nextToken);
				free(token);
				return NULL;
			}
		}
		else
		{
			// Something else. This is the ASSIGNOP token
			token->token_name = ASSIGNOP;
			// Lost the next token. Need to generate again.
			B->curPointer = ++curPointer;
			token->token_value = "=";
			B->fwdPointer = B->curPointer;
			B->charNumber = B->fwdPointer;
			free(nextToken);
			return token;
		}
	}
	else if(a == '.')
	{
		// One of the logical operators, possibly
		++B->fwdPointer;
		tokenInfo token = malloc(sizeof(tokenStruct));
		token->charNumber = B->charNumber;
		token->lineNumber = B->lineNumber;
		int curPointer = B->curPointer;
		tokenInfo nextToken = getNextToken(fp, B);
		if(nextToken->token_name == ID)
		{
			// Check which of the logical operators
			if(!strcmp(nextToken->token_value,"and"))
			{
				// Maybe the and operator.
				if(B->buff[B->fwdPointer] == '.')
				{
					// AND token.
					token->token_name = AND;
					token->token_value = ".and.";
					// Increment current and fwd pointer.
					++B->fwdPointer;
					++B->curPointer;
					++B->charNumber;
					free(nextToken);
					return token;
				}
				else
				{
					// Something else. A Lexical error;
					char *err = "Lexical error. Possible Token: AND .";
					showError(err, B->lineNumber);
					++B->fwdPointer;
					++B->curPointer;
					++B->charNumber;
					free(nextToken);
					free(token);
					return NULL;
				}
			}
			else if(!strcmp(nextToken->token_value,"or"))
			{
				// Maybe the or operator
				if(B->buff[B->fwdPointer] == '.')
				{
					// OR token.
					token->token_name = OR;
					token->token_value = ".or.";
					++B->fwdPointer;
					++B->curPointer;
					++B->charNumber;
					free(nextToken);
					return token;
				}
				else{
					// Lexical error.
					char *err = "Lexical error. Possible token: OR .";
					showError(err, B->lineNumber);
					++B->fwdPointer;
					++B->curPointer;
					++B->charNumber;
					free(nextToken);
					free(token);
					return NULL;
				}
			}
			else if(!strcmp(nextToken->token_value,"not"))
			{
				// Maybe the not operator
				if(B->buff[B->fwdPointer] == '.')
				{
					// NOT token.
					token->token_name = NOT;
					token->token_value = ".not.";
					++B->fwdPointer;
					++B->curPointer;
					++B->charNumber;
					free(nextToken);
					return token;
				}
				else{
					// Lexical error.
					char *err = "Lexical error. Possible Token: NOT .";
					showError(err, B->lineNumber);
					++B->fwdPointer;
					++B->curPointer;
					++B->charNumber;
					free(nextToken);
					free(token);
					return NULL;
				}
			}
			else
			{
				// Some other identifier. A Lexical error;
				char *err = "Lexical error after .";
				showError(err, B->lineNumber);
				B->curPointer = ++curPointer;
				B->fwdPointer = B->curPointer;
				B->charNumber = B->fwdPointer;
				free(nextToken);
				free(token);
				return NULL;
			}
		}
		else
		{
			// A Lexical error;
			char *err = "Lexical Error after .";
			showError(err,B->lineNumber);
			B->curPointer = ++curPointer;
			B->fwdPointer = B->curPointer;
			B->charNumber = B->fwdPointer;
			free(nextToken);
			free(token);
			return NULL;
		}
	}
	else if(a == '_')
	{
		// A function identifier
		++B->fwdPointer;
		tokenInfo token = malloc(sizeof(tokenStruct));
		token->charNumber = B->charNumber;
		token->lineNumber = B->lineNumber;
		token->token_name = FUNID;
		char* funvalue = (char*)malloc((1+FUNSIZE)*sizeof(char));
		funvalue[0] = '_';
		if((B->buff[B->fwdPointer] >= 'a' && B->buff[B->fwdPointer] <= 'z') || (B->buff[B->fwdPointer] >= 'A' && B->buff[B->fwdPointer] <= 'Z'))
		{
			// Check for the function token.
			tokenInfo funToken = getFunctionToken(fp, B, FUNSIZE);
			strcpy(funvalue,funToken->token_value);
			// No need to store the underscore of the function name.
			tokenInfo keywordToken = checkForKeyword(funToken->token_value);
			if(keywordToken != NULL)
			{
				// It is a keyword token. Return as keyword.
				free(token);
				token = keywordToken;
				token->charNumber = B->charNumber;
				token->lineNumber = B->lineNumber;
				B->curPointer = B->fwdPointer;
				B->charNumber = B->fwdPointer;
				free(funvalue);
				//free(keywordToken);
				free(funToken);
				return token;
			}
			else
			{
				// It is an identifier. Return the identifier itself.
				token->token_value = funvalue;
				B->curPointer = B->fwdPointer;
				B->charNumber = B->fwdPointer;
				free(funToken);
				return token;
			}
		}
		else
		{
			// Incorrect. Show error.
			char *err = "Lexical Error. Function names may start only with alphabets.";
			showError(err, B->lineNumber);
			B->curPointer = B->fwdPointer;
			B->charNumber = B->fwdPointer;
			free(funvalue);
			free(token);
			return NULL;
		}
	}
	else if((a >= 'a' && a<= 'z') || (a >= 'A' && a<= 'Z'))
	{
		// An alphabet identifier
		++B->fwdPointer;
		tokenInfo token = malloc(sizeof(tokenStruct));
		token->charNumber = B->charNumber;
		token->lineNumber = B->lineNumber;
		token->token_name = ID;
		char* idvalue = (char*)malloc((1+IDSIZE)*sizeof(char));
		tokenInfo idToken = getIDToken(fp, B, IDSIZE);
		// Check if it is a token
		idvalue[0] = a;
		if(idToken == NULL)
		{
			// Check for one alphabet identifier only.
			token->token_value = idvalue;
			B->curPointer = B->fwdPointer;
			B->charNumber = B->fwdPointer;
			free(idToken);
			return token;
		}
		idvalue = joinStrings(idvalue,idToken->token_value);	
		idToken->token_value = idvalue;
		tokenInfo keywordToken = checkForKeyword(idToken->token_value);
		if(keywordToken != NULL)
		{
			// It is a keyword token. Return as keyword.
			free(token);
			token = keywordToken;
			token->charNumber = B->charNumber;
			token->lineNumber = B->lineNumber;
			B->curPointer = B->fwdPointer;
			B->charNumber = B->fwdPointer;
			//free(keywordToken);
			free(idToken);
			return token;
		}
		else
		{
			// It is an identifier. Return the identifier itself.
			token->token_value = idvalue;
			//printf("%s",token->token_value);
			B->curPointer = B->fwdPointer;
			B->charNumber = B->fwdPointer;
			free(keywordToken);
			free(idToken);
			return token;
		}	
	}
	else if(a >= '0' && a<= '9')
	{
		// A number identifier
		++B->fwdPointer;
		tokenInfo token = malloc(sizeof(tokenStruct));
		token->charNumber = B->charNumber;
		token->lineNumber = B->lineNumber;
		char* numvalue = (char*)malloc((1+NUMSIZE)*sizeof(char));
		tokenInfo numToken = getNumberToken(fp, B);
		token->token_name = numToken->token_name; //whether RealNum or IntNum.
		numvalue[0] = a;
		token->token_value = joinStrings(numvalue,numToken->token_value);
		B->curPointer = B->fwdPointer;
		B->charNumber = B->fwdPointer;
		free(numToken);
		return token;
	}
	else if(a == '\"')
	{
		// String type.
		++B->fwdPointer;
		tokenInfo token = malloc(sizeof(tokenStruct));
		token->charNumber = B->charNumber;
		token->lineNumber = B->lineNumber;
		token->token_name = STR;
		char* stringValue = (char*)malloc((IDSIZE)*sizeof(char));
		tokenInfo stringToken = getStringToken(fp, B, IDSIZE);
		// CONSIDER: check if end of string has a '"'
		if(B->buff[B->fwdPointer] == '"')
		{
			// It is a string ending with the '"'
			strcpy(stringValue,stringToken->token_value);
			token->token_value = stringValue;
			++B->fwdPointer;
			//printf("%s +++ %s",token->token_value,stringToken->token_value);
			B->curPointer = B->fwdPointer;
			B->charNumber = B->fwdPointer;
			free(stringToken);
			return token;
		}
		else
		{
			// It is not a token. Lexical error
			char *err = "Lexical error after \\";
			showError(err,B->lineNumber);
			++B->fwdPointer;
			B->curPointer = B->fwdPointer;
			B->charNumber = B->fwdPointer;
			free(stringToken);
			free(token);
			free(stringValue);
			return NULL;
		}
	}
	else
	{
		// All other cases.
		if(a == '\n')
		{
			// New line. Change values of line number and char number
			++B->lineNumber;
			B->charNumber = 0;
			// Change the buffer.
			FILE* check = getStream(fp, B->nextBuffer, BUFFERSIZE);
			// Set current buffer to next buffer
			if(check == NULL)
			{
				return NULL;
			}
			B = B->nextBuffer;
			return getNextToken(fp, B);
		}
		else if(a == ' ')
		{
			++B->fwdPointer;
			B->curPointer = B->fwdPointer;
			++B->charNumber;
			return getNextToken(fp, B);
		}
		else if(a == EOF)
		{
			// End of the input file. Stop here.
			return NULL;
		}
		else if(a == '\t')
		{
			++B->fwdPointer;
			B->curPointer = B->fwdPointer;
			++B->charNumber;
			return getNextToken(fp, B);
		}
		else
		{
			// Don't know what type of cases come here. CONSIDER
			return NULL;
		}
	}
	return NULL;
}
Ejemplo n.º 3
0
void IntegrationTest::getIntegrationTest(){
    std::vector<double> timeVec1, timeVec2;
    setTimeVecs(timeVec1, timeVec2);  // Sets the time vectors, they are equal for all keywords (WPOR:PROD01 etc)
    setDataSets(timeVec1, timeVec2);

    int ivar = 0;
    if(!allowDifferentAmountOfKeywords){
        if(stringlist_get_size(keysShort) != stringlist_get_size(keysLong)){
            OPM_THROW(std::invalid_argument, "Different ammont of keywords in the two summary files.");
        }
    }
    if(printKeyword){
        printKeywords();
        return;
    }

    std::string keywordWithGreatestErrorRatio;
    double greatestRatio = 0;

    //Iterates over all keywords from the restricted file, use iterator "ivar". Searches for a  match in the file with more keywords, use the itarator "jvar".
    while(ivar < stringlist_get_size(keysShort)){
        const char* keyword = stringlist_iget(keysShort, ivar);

        if(oneOfTheMainVariables){
            std::string keywordString(keyword);
            std::string substr = keywordString.substr(0,4);
            if(substr!= mainVariable){
                ivar++;
                continue;
            }
        }
        for (int jvar = 0; jvar < stringlist_get_size(keysLong); jvar++){

            if (strcmp(keyword, stringlist_iget(keysLong, jvar)) == 0){ //When the keywords are equal, proceed in comparing summary files.
                /*	if(!checkUnits(keyword)){
                    OPM_THROW(std::runtime_error, "For keyword " << keyword << " the unit of the two files is not equal. Not possible to compare.");
                    } //Comparing the unit of the two vectors.*/
                checkForKeyword(timeVec1, timeVec2, keyword);
                if(findVectorWithGreatestErrorRatio){
                    WellProductionVolume volume = getSpecificWellVolume(timeVec1,timeVec2, keyword);
                    findGreatestErrorRatio(volume,greatestRatio, keyword, keywordWithGreatestErrorRatio);
                }
                break;
            }
            //will only enter here if no keyword match
            if(jvar == stringlist_get_size(keysLong)-1){
                if(!allowDifferentAmountOfKeywords){
                    OPM_THROW(std::invalid_argument, "No match on keyword");
                }
            }
        }
        ivar++;
    }
    if(findVectorWithGreatestErrorRatio){
        std::cout << "The keyword " << keywordWithGreatestErrorRatio << " had the greatest error ratio, which was " << greatestRatio << std::endl;
    }
    if((findVolumeError || oneOfTheMainVariables) && !findVectorWithGreatestErrorRatio){
        evaluateWellProductionVolume();
    }
    if(allowSpikes){
        std::cout << "checkWithSpikes succeeded." << std::endl;
    }
}