planParser.cpp
Go to the documentation of this file.
00001 #include "planParser.h"
00002 #include <stdio.h>
00003 #include <QString>
00004 #include <QStringList>
00005 #include <QRegExp>
00006 #include <sstream>
00007 #include <ros/ros.h>
00008 
00009 PlanParser::PlanParser()
00010 {
00011 }
00012 
00013 PlanParser::~PlanParser()
00014 {
00015 }
00016 
00017 bool PlanParser::parsePlan(std::istream & is, Plan & plan_)
00018 {
00019     vector<DurativeAction> & plan = plan_.actions;
00020 
00021     bool ret = true;
00022     plan.clear();
00023 
00024     // what is failure vs. empty is
00025     if(!is.good())
00026         return false;
00027 
00028     char buf[4096];
00029     memset(buf, 0, 4096);
00030     while(!is.eof()) {
00031         is.getline(buf, 4095);
00032         //printf("%s\n", buf);
00033         if(strlen(buf) == 0)    //skip empty line
00034             continue;
00035 
00036         DurativeAction action;
00037 
00038         QString line = buf;
00039         if(line.startsWith(";"))    // comment line
00040             continue;
00041 
00042         // should read like:
00043         // 7.00070000: (explore robot7 t15 t16) [1.00000000]
00044         QStringList sl1 = line.trimmed().split(":");
00045         if(sl1.size() != 2) {
00046             ROS_WARN("unparsable plan line: \"%s\"", buf);
00047             ret = false;
00048             continue;
00049         }
00050         bool ok;
00051         action.startTime = -1;
00052         action.startTime = sl1[0].toDouble(&ok);
00053         if(!ok) {
00054             ROS_WARN("unparsable plan line: \"%s\"", buf);
00055             ret = false;
00056             continue;
00057         }
00058 
00059         QString rest = sl1[1].trimmed();
00060         // (explore robot7 t15 t16) [1.00000000]
00061         QRegExp re("\\((.*)\\) *\\[(.*)\\]");
00062         if(rest.indexOf(re) < 0) {
00063             ROS_WARN("unparsable plan line: \"%s\"", buf);
00064             ret = false;
00065             continue;
00066         }
00067         if(re.captureCount() != 2) {
00068             ROS_WARN("unparsable plan line: \"%s\"", buf);
00069             ret = false;
00070             continue;
00071         }
00072         QString actionDesc = re.cap(1);
00073         QString durationDesc = re.cap(2);
00074         action.duration = -1;
00075         action.duration = durationDesc.toDouble(&ok);
00076         if(!ok) {
00077             ROS_WARN("unparsable plan line: \"%s\"", buf);
00078             ret = false;
00079             continue;
00080         }
00081 
00082         QStringList actionS = actionDesc.split(" ", QString::SkipEmptyParts);
00083         if(actionS.size() < 1) {
00084             ROS_WARN("unparsable plan line: \"%s\"", buf);
00085             ret = false;
00086             continue;
00087         }
00088 
00089         action.name = qPrintable(actionS[0]);
00090         for(int i = 1; i < actionS.size(); i++) {
00091             action.parameters.push_back(qPrintable(actionS[i]));
00092         }
00093         plan.push_back(action);
00094     }
00095 
00096     return ret;
00097 }
00098 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Tue Jan 22 2013 12:25:03