-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsepddlfiles.cpp
119 lines (104 loc) · 3.16 KB
/
parsepddlfiles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "parsepddlfiles.h"
/*
* splitting a strign by spaces to separate words
*/
std::vector<std::string> splitLine(std::string s)
{
std::vector<std::string> words;
std::istringstream iss(s);
for(std::string s; iss >> s;)
{
words.push_back(s);
}
return words;
}
int brackets(std::string line, int opened_bracket)
{
//important: this doesnt really handle the case ".....)(...."
size_t index = line.find("(");
while (index != std::string::npos)
{
opened_bracket++;
index = line.find("(", index+1);
}
index = line.find(")");
while (index != std::string::npos)
{
opened_bracket--;
index = line.find(")", index+1);
}
return opened_bracket;
}
void parseType(std::vector<std::shared_ptr<Type>> * types, std::vector<std::string> words)
{
//take the last word - that is the type
if((words.end()-1)->find("object")!=std::string::npos) //it is main general type
{
unsigned int index = types->size();
types->push_back(std::shared_ptr<Type>(new Type("object",nullptr)));
for(unsigned int i=0; i< words.size(); i++)
{
if(words.at(i).find("types")!=std::string::npos)
continue;
if(words.at(i).find("-")!=std::string::npos)
break;
types->push_back(std::shared_ptr<Type>(new Type(words.at(i),types->at(index))));
}
}
else
{
//the type is already saved, find the index
for(int i=types->size()-1;i>0;i--)
{
if((words.end()-1)->find(types->at(i)->getName())!=std::string::npos)
{
for(unsigned int j=0; j< words.size(); j++)
{
if(words.at(j).find("types")!=std::string::npos)
continue;
if(words.at(j).find("-")!=std::string::npos)
break;
types->push_back(std::shared_ptr<Type>(new Type(words.at(j),types->at(i))));
}
break; //we found it
}
}
}
}
void ParsePDDLfiles::parseDomain(std::string domain_file)
{
std::string line;
std::ifstream file_in;;
file_in.open(domain_file.c_str());
std::vector<std::shared_ptr<Type>> types;
if (file_in.is_open())
{
while ( getline (file_in,line) )
{
//splitting line by spaces
std::vector<std::string> words = splitLine(line);
if(words.size()==0)
{
continue;
}
//get types
if(words.at(0) == "(:types")
{
while(line.find(")")==std::string::npos)
{
parseType(&types, words);
do
{
getline(file_in,line);
words = splitLine(line);
} while(words.size()==0);
}
parseType(&types,words);
}
}//end while ( getline (file_in,line) )
}//(file_in.is_open())
for(unsigned int i=0; i< types.size();i++)
{
std::cout << types.at(i)->getName() << "\n";
}
}