Commit 36ee7be9 authored by Tao Zhong's avatar Tao Zhong

Read traffic from a external xml

parent 5cbc2092
......@@ -2201,9 +2201,10 @@ void jpsDatamanager::parseTransitions(QXmlStreamReader &xmlReader)
qreal y2=xmlReader.attributes().value("py").toString().toFloat();
qDebug("\t x1 = %.2f, y1 = %.2f, x2 = %.2f y2 = %.2f", x1, y1, x2, y2);
jpsLineItem* lineItem = _mView->addLineItem(x1,y1,x2,y2,"Exit");
if (id!=-2)
{
jpsCrossing* exit = new jpsCrossing(lineItem);
jpsCrossing *exit = new jpsCrossing(lineItem);
exit->set_id(id);
exit->change_name(caption);
//exit->set_type(type);
......@@ -3081,9 +3082,10 @@ void jpsDatamanager::writeTrafficXML(QFile &file)
stream->writeAttribute("version", "0.8");
stream->writeStartElement("traffic_constrains");
stream->writeStartElement("doors");
writeTraffics(stream, doorlist);
doorlist.clear();
stream->writeEndElement(); //end traffic_constrains
stream->writeEndElement(); //end
stream->writeEndDocument();
......@@ -3095,8 +3097,6 @@ void jpsDatamanager::writeTraffics(QXmlStreamWriter *stream, QList<jpsCrossing *
{
for(jpsCrossing* door:doorlist)
{
stream->writeStartElement("doors");
stream->writeStartElement("door");
stream->writeAttribute("trans_id", QString::number(door->get_id()));
stream->writeAttribute("caption", "NaN");
......@@ -3106,10 +3106,62 @@ void jpsDatamanager::writeTraffics(QXmlStreamWriter *stream, QList<jpsCrossing *
stream->writeAttribute("state", "close");
stream->writeAttribute("outflow", door->getOutflow());
stream->writeAttribute("max_agents", door->getMaxAgents());
stream->writeEndElement(); //end door
}
}
bool jpsDatamanager::readTrafficXML(QFile &file)
{
QXmlStreamReader xmlReader(&file);
stream->writeEndElement(); //end doors and door
xmlReader.readNext(); // read first token
while (!xmlReader.atEnd())
{
if(xmlReader.isStartElement() && xmlReader.name() == QLatin1String("doors"))
{
readDoor(xmlReader);
} else
{
xmlReader.readNext();
}
}
return true;
}
void jpsDatamanager::readDoor(QXmlStreamReader &xmlReader)
{
bool state;
QString max_agents;
QString outflow;
int id;
while (xmlReader.readNextStartElement())
{
if(xmlReader.name() == QLatin1String("door"))
{
state = xmlReader.attributes().value("state") == "open" ? true : false;
max_agents = xmlReader.attributes().value("max_agents").toString();
outflow = xmlReader.attributes().value("outflow").toString();
id = xmlReader.attributes().value("trans_id").toInt();
for(jpsCrossing *door : crossingList)
{
if(door->get_id() == id && door->IsExit())
{
door->setState(state);
door->setMaxAgents(max_agents);
door->setOutflow(outflow);
}
}
// now token is end element, readNextStartElement() will return false. Have to use readNext
// to go to next start element.
xmlReader.readNext();
}
}
}
......@@ -117,6 +117,8 @@ public:
//Traffic
void writeTrafficXML(QFile &file);
void writeTraffics(QXmlStreamWriter *stream, QList<jpsCrossing *> const &doorlist);
bool readTrafficXML(QFile &file);
void readDoor(QXmlStreamReader &xmlReader);
void remove_all();
void remove_marked_lines();
......
......@@ -96,11 +96,11 @@ void jpsCrossing::add_rooms(jpsRoom *room1, jpsRoom *room2)
qDebug("Enter jpsCrossing::add_rooms");
qDebug("\t room1 = <%s> of type <%s>",
room1->get_name().toStdString().c_str(),
room1->get_type().toStdString().c_str()
);
room1->get_type().toStdString().c_str());
roomList.clear();
roomList.push_back(room1);
room1->AddDoor(this);
if(room1->get_type().toUpper() != "STAIR") // assuming a crossing can
// not separate two stairs
this->set_elevation(room1->get_elevation());
......@@ -109,12 +109,11 @@ void jpsCrossing::add_rooms(jpsRoom *room1, jpsRoom *room2)
{
qDebug("\t room2 = <%s> of type <%s>",
room2->get_name().toStdString().c_str(),
room2->get_type().toStdString().c_str()
);
room2->get_type().toStdString().c_str());
if(room2->get_type().toUpper() != "STAIR")
this->set_elevation(room2->get_elevation());
roomList.push_back(room2);
room2->AddDoor(this);
room2->AddDoor(this);
}
qDebug("Leave jpsCrossing::add_rooms");
}
......
......@@ -408,6 +408,8 @@ void MWindow::openFileXML()
openSource(fileName);
openTraffic(fileName);
//AutoZoom to drawing
mview->AutoZoom();
}
......@@ -1101,3 +1103,37 @@ void MWindow::goalButtionClicked()
goalWidget = nullptr;
}
}
/*
Since v0.8.8
Open traffic file
*/
void MWindow::openTraffic(QString fileName)
{
QString fileNameTraffic = fileName.split(".").first() + "_traffic.xml";
QFile fileTraffic(fileNameTraffic);
if(fileNameTraffic.isEmpty())
return;
if (!fileTraffic.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(this, tr("Read traffic XML"),
tr("Cannot read file %1:\n%2.")
.arg(QDir::toNativeSeparators(fileName),
fileTraffic.errorString()));
return;
}
if(!dmanager->readTrafficXML(fileTraffic))
{
QMessageBox::critical(this,
"Prase traffic XML",
"Cannot prase XML-file",
QMessageBox::Ok);
} else
{
}
}
......@@ -110,6 +110,7 @@ protected slots:
void openRouting(QString fileName);
void openSource(QString fileName);
void openGoal(QString fileName);
void openTraffic(QString fileName);
void openFileCogMap();
void OpenLineFile();
......
......@@ -848,47 +848,6 @@ QString InifileWidget::WriteHeaderData()
return head_lines;
}
/*
<routing>
<goals>
<goal id="0" final="false" caption="goal 1">
<polygon>
<vertex px="-5.0" py="-5.0" />
<vertex px="-5.0" py="-2.0" />
<vertex px="-3.0" py="-2.0" />
<vertex px="-3.0" py="-5.0" />
<vertex px="-5.0" py="-5.0" />
</polygon>
</goal>
<file>goals.xml</file>
</goals>
</routing>
*/
void InifileWidget::WriteRoutingData(QFile &file)
{
auto *stream = new QXmlStreamWriter(&file);
stream->setAutoFormatting(true);
stream->writeComment("routing");
stream->writeStartElement("routing");
stream->writeStartElement("goals");
QList<JPSGoal *> goallist = dataManager->getGoallist();
dataManager->writeGoals(stream, goallist);
auto goal_FileName = "goals.xml";
stream->writeStartElement("file");
stream->writeCharacters(goal_FileName);
stream->writeEndElement(); //end files
stream->writeEndElement(); //end goals
stream->writeEndDocument();
delete stream;
stream = nullptr;
}
QString InifileWidget::WriteAgentData()
{
//agents information and distribution
......@@ -1638,22 +1597,22 @@ QString InifileWidget::WriteRouteChoiceData()
// Create ini.xml on button push
void InifileWidget::on_pushButton_write_clicked()
{
//check header
//check header (geometry)
if (CheckHeaderData() == 0)
{
return;
}
//goal
if (CheckRoutingData() == false)
if (!CheckRoutingData())
return;
//source
if (CheckSourceData() == false)
if (!CheckSourceData())
return;
//traffic(door)
if (CheckTrafficData() == false)
if (!CheckTrafficData())
return;
//check agents information and distribution
......@@ -1765,6 +1724,7 @@ void InifileWidget::on_pushButton_write_clicked()
file.write(head_lines.toUtf8());
WriteRoutingData(file);
WriteTrafficData(file);
file.write(agen_lines.toUtf8() +
gcfm_lines.toUtf8() +
......@@ -3515,3 +3475,88 @@ void InifileWidget::pushButton_TrafficClicked()
ui->lineEdit_TrafficFile->setText(fileName);
}
/*
<routing>
<goals>
<goal id="0" final="false" caption="goal 1">
<polygon>
<vertex px="-5.0" py="-5.0" />
<vertex px="-5.0" py="-2.0" />
<vertex px="-3.0" py="-2.0" />
<vertex px="-3.0" py="-5.0" />
<vertex px="-5.0" py="-5.0" />
</polygon>
</goal>
<file>goals.xml</file>
</goals>
</routing>
*/
void InifileWidget::WriteRoutingData(QFile &file)
{
auto *stream = new QXmlStreamWriter(&file);
stream->setAutoFormatting(true);
stream->writeComment("routing");
stream->writeStartElement("routing");
stream->writeStartElement("goals");
QList<JPSGoal *> goallist = dataManager->getGoallist();
dataManager->writeGoals(stream, goallist);
auto goal_FileName = "goals.xml";
stream->writeStartElement("file");
stream->writeCharacters(goal_FileName);
stream->writeEndElement(); //end files
stream->writeEndElement(); //end goals
stream->writeEndDocument();
delete stream;
stream = nullptr;
}
/*
since v0.8.8
<traffic_constraints>
<doors>
<door trans_id="1" caption="NaN" state="open" />
</doors>
<file>traffic.xml</file>
</traffic_constraints>
*/
void InifileWidget::WriteTrafficData(QFile &file)
{
auto *stream = new QXmlStreamWriter(&file);
stream->setAutoFormatting(true);
stream->writeComment("traffic information: e.g closed doors or smoked rooms");
stream->writeStartElement("traffic_constraints");
stream->writeStartElement("doors");
QList<jpsCrossing *> crossings = dataManager->get_crossingList();
QList<jpsCrossing *> doorlist;
for(jpsCrossing *crossing:crossings)
{
if(crossing->IsExit())
doorlist.append(crossing);
}
dataManager->writeTraffics(stream, doorlist);
auto traffic_FileName = ui->lineEdit_TrafficFile->text().split("/").first();
stream->writeStartElement("file");
stream->writeCharacters(traffic_FileName);
stream->writeEndElement(); //end files
stream->writeEndElement(); //end traffic
stream->writeEndDocument();
delete stream;
stream = nullptr;
}
......@@ -65,6 +65,7 @@ private:
QString WriteHeaderData();
void WriteRoutingData(QFile &file);
void WriteTrafficData(QFile &file);
QString WriteAgentData();
QString WriteModelGcfmData();
QString WriteModelGompData();
......
......@@ -84,6 +84,7 @@ roomWidget::roomWidget(QWidget *parent, jpsDatamanager *dmanager, jpsGraphicsVie
//connect(ui->highlight,SIGNAL(clicked(bool)),this,SLOT(highlight_room()));
connect(ui->classBox,SIGNAL(activated(int)),this,SLOT(ChangeRoomType()));
connect(ui->classBox,SIGNAL(currentIndexChanged(int)),this,SLOT(ChangeRoomType()));
//tab crossing
connect(ui->pushButton_apply, SIGNAL(clicked(bool)), this, SLOT(applyCrossing()));
connect(ui->addCrossingButton,SIGNAL(clicked(bool)),this,SLOT(new_crossing()));
......@@ -93,6 +94,7 @@ roomWidget::roomWidget(QWidget *parent, jpsDatamanager *dmanager, jpsGraphicsVie
connect(ui->deleteCrossingButton,SIGNAL(clicked(bool)),this,SLOT(delete_crossing()));
connect(ui->crossingList,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(select_crossing()));
//connect(ui->auto_assign_doors,SIGNAL(clicked(bool)),this,SLOT(autoAssignDoors()));
//tab exit
// connect(ui->addExitButton,SIGNAL(clicked(bool)),this,SLOT(new_exit()));
// connect(ui->exitList,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(enable_roomSelectionExits()));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment