Browse Source
			
			
			Parser for labelling files including new data structure managing
			
				
		Parser for labelling files including new data structure managing
	
		
	
			
				different atomic propositions. (Works now as node_array attribute of class Atomic_proposition is now always instantiated with 0)main
							committed by
							
								 Lanchid
								Lanchid
							
						
					
				
				 8 changed files with 357 additions and 16 deletions
			
			
		- 
					31src/dtmc/atomic_proposition.h
- 
					85src/dtmc/labelling.h
- 
					15src/parser/parser.h
- 
					116src/parser/read_lab_file.cpp
- 
					23src/parser/read_lab_file.h
- 
					18src/parser/read_tra_file.cpp
- 
					77test/parser/read_lab_file_test.cpp
- 
					8test/parser/read_tra_file_test.cpp
| @ -0,0 +1,85 @@ | |||||
|  | /* | ||||
|  |  * labelling.h | ||||
|  |  * | ||||
|  |  *  Created on: 10.09.2012 | ||||
|  |  *      Author: Thomas Heinemann | ||||
|  |  */ | ||||
|  | 
 | ||||
|  | #ifndef MRMC_DTMC_LABELLING_H_ | ||||
|  | #define MRMC_DTMC_LABELLING_H_ | ||||
|  | 
 | ||||
|  | #include "atomic_proposition.h" | ||||
|  | 
 | ||||
|  | #include "boost/unordered_map.hpp" | ||||
|  | 
 | ||||
|  | #include <stdexcept> | ||||
|  | 
 | ||||
|  | namespace mrmc { | ||||
|  | 
 | ||||
|  | namespace dtmc { | ||||
|  | 
 | ||||
|  | 
 | ||||
|  | class labelling { | ||||
|  |    public: | ||||
|  | 
 | ||||
|  | 
 | ||||
|  |       labelling(const uint_fast32_t p_nodes) { | ||||
|  |          nodes = p_nodes; | ||||
|  |       } | ||||
|  | 
 | ||||
|  |       virtual ~labelling() { | ||||
|  |          //deleting all the labelling vectors in the map. | ||||
|  |          boost::unordered_map<std::string, AtomicProposition*>::iterator it; | ||||
|  |          for (it = proposition_map.begin(); it != proposition_map.end(); ++it) { | ||||
|  |             if (it->second != NULL) { | ||||
|  |                delete (it->second); | ||||
|  |             } | ||||
|  |          } | ||||
|  |       } | ||||
|  | 
 | ||||
|  |       void addProposition(std::string proposition) { | ||||
|  |          if (proposition_map.count(proposition) != 0) { | ||||
|  |             throw std::out_of_range("Proposition does already exist."); | ||||
|  |          } | ||||
|  |          proposition_map[proposition] = new AtomicProposition(nodes); | ||||
|  |       } | ||||
|  | 
 | ||||
|  |       bool containsProposition(std::string proposition) { | ||||
|  |          return (proposition_map.count(proposition) != 0); | ||||
|  |       } | ||||
|  | 
 | ||||
|  | 
 | ||||
|  |       void addLabelToNode(std::string proposition, const uint_fast32_t node) { | ||||
|  |          //TODO (Thomas Heinemann): Differentiate exceptions? | ||||
|  |          if (proposition_map.count(proposition) == 0) { | ||||
|  |             throw std::out_of_range("Proposition does not exist."); | ||||
|  |          } | ||||
|  |          if (node >= nodes) { | ||||
|  |             throw std::out_of_range("Node number out of range"); | ||||
|  |          } | ||||
|  |          AtomicProposition* prop = (proposition_map[proposition]); | ||||
|  |          prop->addLabelToNode(node); | ||||
|  |       } | ||||
|  | 
 | ||||
|  |       bool nodeHasProposition(std::string proposition, const uint_fast32_t node) { | ||||
|  |          return proposition_map[proposition]->hasNodeLabel(node); | ||||
|  |       } | ||||
|  | 
 | ||||
|  |       uint_fast32_t getNumberOfPropositions() { | ||||
|  |          return proposition_map.size(); | ||||
|  |       } | ||||
|  | 
 | ||||
|  |       AtomicProposition* getProposition(std::string proposition) { | ||||
|  |          return (proposition_map[proposition]); | ||||
|  |       } | ||||
|  | 
 | ||||
|  |    private: | ||||
|  |       uint_fast32_t nodes; | ||||
|  |       boost::unordered_map<std::string, AtomicProposition*> proposition_map; | ||||
|  | }; | ||||
|  | 
 | ||||
|  | } //namespace dtmc | ||||
|  | 
 | ||||
|  | } //namespace mrmc | ||||
|  | 
 | ||||
|  | #endif /* MRMC_DTMC_LABELLING_H_ */ | ||||
| @ -0,0 +1,15 @@ | |||||
|  | /* | ||||
|  |  * parser.h | ||||
|  |  * | ||||
|  |  *  Created on: 12.09.2012 | ||||
|  |  *      Author: Thomas Heinemann | ||||
|  |  */ | ||||
|  | 
 | ||||
|  | #ifndef PARSER_H_ | ||||
|  | #define PARSER_H_ | ||||
|  | 
 | ||||
|  | #include "boost/integer/integer_mask.hpp" | ||||
|  | 
 | ||||
|  | const uint_fast32_t BUFFER_SIZE=1024; | ||||
|  | 
 | ||||
|  | #endif /* PARSER_H_ */ | ||||
| @ -0,0 +1,116 @@ | |||||
|  | /*
 | ||||
|  |  * read_lab_file.cpp | ||||
|  |  * | ||||
|  |  *  Created on: 10.09.2012 | ||||
|  |  *      Author: Thomas Heinemann | ||||
|  |  */ | ||||
|  | 
 | ||||
|  | #include "parser.h"
 | ||||
|  | 
 | ||||
|  | #include "read_lab_file.h"
 | ||||
|  | 
 | ||||
|  | #include "src/dtmc/labelling.h"
 | ||||
|  | 
 | ||||
|  | #include "src/exceptions/wrong_file_format.h"
 | ||||
|  | #include "src/exceptions/file_IO_exception.h"
 | ||||
|  | 
 | ||||
|  | #include <cstring>
 | ||||
|  | #include <cstdlib>
 | ||||
|  | #include <cstdio>
 | ||||
|  | 
 | ||||
|  | 
 | ||||
|  | namespace mrmc { | ||||
|  | 
 | ||||
|  | namespace parser { | ||||
|  | 
 | ||||
|  | 
 | ||||
|  | /*!
 | ||||
|  |  * Reads a .lab file and puts the result in a labelling structure. | ||||
|  |  * @param node_count the number of states. | ||||
|  |  * @param filename   input .lab file's name. | ||||
|  |  * @return returns a pointer to a labelling object. | ||||
|  |  */ | ||||
|  | mrmc::dtmc::labelling * read_lab_file(int node_count, const char * filename) | ||||
|  | { | ||||
|  |    /* Note that this function uses strtok_r on char-array s.
 | ||||
|  |     * This function will modify this string. | ||||
|  |     * However, as only the result of its tokenization is used, this is not a problem. | ||||
|  |     * | ||||
|  |     * Thread-safety is ensured by using strtok_r instead of strtok. | ||||
|  |     */ | ||||
|  |    FILE *P; | ||||
|  |    char s[BUFFER_SIZE];              //String buffer
 | ||||
|  |    char *saveptr = NULL;             //Buffer for strtok_r
 | ||||
|  |    char sep[] = " \n\t";             //Separators for the tokens
 | ||||
|  | 
 | ||||
|  |    P = fopen(filename, "r"); | ||||
|  | 
 | ||||
|  |    if (P == NULL) { | ||||
|  |       throw mrmc::exceptions::file_IO_exception(); | ||||
|  |    } | ||||
|  | 
 | ||||
|  |    if (fgets(s, BUFFER_SIZE, P)) { | ||||
|  |       if (strcmp(s, "#DECLARATION\n")) { | ||||
|  |          fclose(P); | ||||
|  |          throw mrmc::exceptions::wrong_file_format(); | ||||
|  |       } | ||||
|  |    } | ||||
|  | 
 | ||||
|  | 
 | ||||
|  |    mrmc::dtmc::labelling* result = new mrmc::dtmc::labelling(node_count); | ||||
|  | 
 | ||||
|  |    //Here, all propositions are to be declared...
 | ||||
|  |    if (fgets(s, BUFFER_SIZE, P)) { | ||||
|  |       char * proposition; | ||||
|  |       for (proposition = strtok_r(s, sep, &saveptr); | ||||
|  |            proposition != NULL; | ||||
|  |            proposition = strtok_r(NULL, sep, &saveptr)) { | ||||
|  |          result -> addProposition(proposition); | ||||
|  |       } | ||||
|  |    } else { | ||||
|  |       fclose(P); | ||||
|  |       delete result; | ||||
|  |       throw mrmc::exceptions::wrong_file_format(); | ||||
|  |    } | ||||
|  | 
 | ||||
|  |    saveptr = NULL;                        //resetting save pointer for strtok_r
 | ||||
|  | 
 | ||||
|  |    if (fgets(s, BUFFER_SIZE, P)) { | ||||
|  |       if (strcmp(s, "#END\n")) { | ||||
|  |          fclose(P); | ||||
|  |          delete result; | ||||
|  |          throw mrmc::exceptions::wrong_file_format(); | ||||
|  |       } | ||||
|  |    } | ||||
|  | 
 | ||||
|  |    while (fgets(s, BUFFER_SIZE, P)) { | ||||
|  |       char * token = NULL; | ||||
|  |       uint_fast32_t node = 0; | ||||
|  |       /* First token has to be a number identifying the node,
 | ||||
|  |        * hence it is treated differently from the other tokens. | ||||
|  |        */ | ||||
|  |       token = strtok_r(s, sep, &saveptr); | ||||
|  |       if (sscanf(token, "%u", &node) == 0) { | ||||
|  |          fclose(P); | ||||
|  |          delete result; | ||||
|  |          throw mrmc::exceptions::wrong_file_format(); | ||||
|  |       } | ||||
|  |       do { | ||||
|  |          token = strtok_r(NULL, sep, &saveptr); | ||||
|  |          if (token == NULL) { | ||||
|  |             break; | ||||
|  |          } | ||||
|  |          result->addLabelToNode(token, node); | ||||
|  |       } while (token != NULL); | ||||
|  | 
 | ||||
|  |    } | ||||
|  | 
 | ||||
|  |    fclose(P); | ||||
|  | 
 | ||||
|  |    return result; | ||||
|  | 
 | ||||
|  | } | ||||
|  | 
 | ||||
|  | } //namespace parser
 | ||||
|  | 
 | ||||
|  | } //namespace mrmc
 | ||||
| @ -0,0 +1,23 @@ | |||||
|  | /* | ||||
|  |  * read_lab_file.h | ||||
|  |  * | ||||
|  |  *  Created on: 10.09.2012 | ||||
|  |  *      Author: thomas | ||||
|  |  */ | ||||
|  | 
 | ||||
|  | #ifndef READ_LAB_FILE_H_ | ||||
|  | #define READ_LAB_FILE_H_ | ||||
|  | 
 | ||||
|  | #include "src/dtmc/labelling.h" | ||||
|  | 
 | ||||
|  | 
 | ||||
|  | namespace mrmc { | ||||
|  | 
 | ||||
|  | namespace parser { | ||||
|  | 
 | ||||
|  | mrmc::dtmc::labelling * read_lab_file(int node_count, const char * filename); | ||||
|  | 
 | ||||
|  | } | ||||
|  | } | ||||
|  | 
 | ||||
|  | #endif /* READ_LAB_FILE_H_ */ | ||||
| @ -0,0 +1,77 @@ | |||||
|  | /*
 | ||||
|  |  * read_lab_file_test.cpp | ||||
|  |  * | ||||
|  |  *  Created on: 12.09.2012 | ||||
|  |  *      Author: Thomas Heinemann | ||||
|  |  */ | ||||
|  | 
 | ||||
|  | #include "gtest/gtest.h"
 | ||||
|  | #include "src/dtmc/labelling.h"
 | ||||
|  | #include "src/parser/read_lab_file.h"
 | ||||
|  | #include "src/exceptions/file_IO_exception.h"
 | ||||
|  | #include "src/exceptions/wrong_file_format.h"
 | ||||
|  | 
 | ||||
|  | TEST(ReadLabFileTest, NonExistingFileTest) { | ||||
|  |    //No matter what happens, please don't create a file with the name "nonExistingFile.not"! :-)
 | ||||
|  |    ASSERT_THROW(mrmc::parser::read_lab_file(0,"nonExistingFile.not"), mrmc::exceptions::file_IO_exception); | ||||
|  | } | ||||
|  | 
 | ||||
|  | TEST(ReadLabFileTest, ParseTest) { | ||||
|  |    //This test is based on a testcase from the original MRMC.
 | ||||
|  |    mrmc::dtmc::labelling* labelling; | ||||
|  | 
 | ||||
|  |    //Parsing the file
 | ||||
|  |    ASSERT_NO_THROW(labelling = mrmc::parser::read_lab_file(12,"test/parser/lab_files/pctl_general_input_01.lab")); | ||||
|  | 
 | ||||
|  |    //Checking whether all propositions are in the labelling
 | ||||
|  | 
 | ||||
|  |    char phi[] = "phi", psi[] = "psi", smth[] = "smth"; | ||||
|  | 
 | ||||
|  |    ASSERT_TRUE(labelling->containsProposition(phi)); | ||||
|  |    ASSERT_TRUE(labelling->containsProposition(psi)); | ||||
|  |    ASSERT_TRUE(labelling->containsProposition(smth)); | ||||
|  | 
 | ||||
|  |    //Testing whether all and only the correct nodes are labeled with "phi"
 | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(phi,1)); | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(phi,2)); | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(phi,3)); | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(phi,5)); | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(phi,7)); | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(phi,9)); | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(phi,10)); | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(phi,11)); | ||||
|  | 
 | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(phi,4)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(phi,6)); | ||||
|  | 
 | ||||
|  |    //Testing whether all and only the correct nodes are labeled with "psi"
 | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(psi,6)); | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(psi,7)); | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(psi,8)); | ||||
|  | 
 | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(psi,1)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(psi,2)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(psi,3)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(psi,4)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(psi,5)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(psi,9)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(psi,10)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(psi,11)); | ||||
|  | 
 | ||||
|  |    //Testing whether all and only the correct nodes are labeled with "smth"
 | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(smth,4)); | ||||
|  |    ASSERT_TRUE(labelling->nodeHasProposition(smth,5)); | ||||
|  | 
 | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(smth,1)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(smth,2)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(smth,3)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(smth,6)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(smth,7)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(smth,8)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(smth,9)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(smth,10)); | ||||
|  |    ASSERT_FALSE(labelling->nodeHasProposition(smth,11)); | ||||
|  | 
 | ||||
|  |    //Deleting the labelling
 | ||||
|  |    delete labelling; | ||||
|  | } | ||||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue