Path: blob/main/src/netedit/elements/data/GNEMeanDataHandler.cpp
169684 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file GNEMeanDataHandler.cpp14/// @author Pablo Alvarez Lopez15/// @date Nov 2216///17// Builds meanData objects for netedit18/****************************************************************************/1920#include <netedit/changes/GNEChange_MeanData.h>21#include <netedit/dialogs/basic/GNEOverwriteElement.h>22#include <netedit/elements/data/GNEMeanData.h>23#include <netedit/GNENet.h>24#include <netedit/GNETagProperties.h>25#include <netedit/GNEUndoList.h>26#include <netedit/GNEViewNet.h>27#include <utils/gui/div/GUIDesigns.h>2829#include "GNEMeanDataHandler.h"3031// ===========================================================================32// member method definitions33// ===========================================================================3435GNEMeanDataHandler::GNEMeanDataHandler(GNENet* net, const std::string& filename, const bool allowUndoRedo) :36MeanDataHandler(filename),37myNet(net),38myAllowUndoRedo(allowUndoRedo) {39}404142GNEMeanDataHandler::~GNEMeanDataHandler() {}434445bool46GNEMeanDataHandler::postParserTasks() {47// nothing to do48return true;49}505152bool53GNEMeanDataHandler::buildEdgeMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,54const std::string& file, SUMOTime period, SUMOTime begin, SUMOTime end, const bool trackVehicles,55const std::vector<std::string>& writtenAttributes, const bool aggregate, const std::vector<std::string>& edgeIDs,56const std::string& edgeFile, std::string excludeEmpty, const bool withInternal,57const std::vector<std::string>& detectPersons, const double minSamples, const double maxTravelTime,58const std::vector<std::string>& vTypes, const double speedThreshold) {59// parse attributes60const auto edges = parseEdges(SUMO_TAG_MEANDATA_EDGE, edgeIDs);61// parse edges62const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_EDGE, writtenAttributes);63// check if meanData edge exists64if (!checkValidAdditionalID(SUMO_TAG_MEANDATA_EDGE, id)) {65return false;66} else if (!checkDuplicatedMeanDataElement(SUMO_TAG_MEANDATA_EDGE, id)) {67return false;68} else if ((period != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_PERIOD, period, true)) {69return false;70} else if ((begin != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_BEGIN, begin, true)) {71return false;72} else if ((end != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_END, end, true)) {73return false;74} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MAX_TRAVELTIME, maxTravelTime, true)) {75return false;76} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MIN_SAMPLES, minSamples, true)) {77return false;78} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold, true)) {79return false;80} else if (!checkExcludeEmpty(SUMO_TAG_MEANDATA_EDGE, id, excludeEmpty)) {81return false;82} else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {83GNEMeanData* edgeMeanData = new GNEMeanData(SUMO_TAG_MEANDATA_EDGE, id, myNet, myFilename, file, period, begin, end,84trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,85detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);86if (myAllowUndoRedo) {87myNet->getViewNet()->getUndoList()->begin(edgeMeanData, TL("add meanDataEdge"));88myNet->getViewNet()->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);89myNet->getViewNet()->getUndoList()->end();90} else {91myNet->getAttributeCarriers()->insertMeanData(edgeMeanData);92edgeMeanData->incRef("buildEdgeMeanData");93}94return true;95} else {96return false;97}98}99100101bool102GNEMeanDataHandler::buildLaneMeanData(const CommonXMLStructure::SumoBaseObject* /*sumoBaseObject*/, const std::string& id,103const std::string& file, SUMOTime period, SUMOTime begin, SUMOTime end, const bool trackVehicles,104const std::vector<std::string>& writtenAttributes, const bool aggregate, const std::vector<std::string>& edgeIDs,105const std::string& edgeFile, std::string excludeEmpty, const bool withInternal,106const std::vector<std::string>& detectPersons, const double minSamples, const double maxTravelTime,107const std::vector<std::string>& vTypes, const double speedThreshold) {108// parse attributes109const auto edges = parseEdges(SUMO_TAG_MEANDATA_LANE, edgeIDs);110// parse edges111const auto attributes = parseAttributes(SUMO_TAG_MEANDATA_LANE, writtenAttributes);112// check if meanData edge exists113if (!checkValidAdditionalID(SUMO_TAG_MEANDATA_LANE, id)) {114return false;115} else if (!checkDuplicatedMeanDataElement(SUMO_TAG_MEANDATA_LANE, id)) {116return false;117} else if ((period != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_PERIOD, period, true)) {118return false;119} else if ((begin != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_BEGIN, begin, true)) {120return false;121} else if ((end != TIME2STEPS(-1)) && !checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_END, end, true)) {122return false;123} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MAX_TRAVELTIME, maxTravelTime, true)) {124return false;125} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_MIN_SAMPLES, minSamples, true)) {126return false;127} else if (!checkNegative(SUMO_TAG_MEANDATA_EDGE, id, SUMO_ATTR_HALTING_SPEED_THRESHOLD, speedThreshold, true)) {128return false;129} else if (!checkExcludeEmpty(SUMO_TAG_MEANDATA_EDGE, id, excludeEmpty)) {130return false;131} else if ((edges.size() == edgeIDs.size()) && (attributes.size() == writtenAttributes.size())) {132GNEMeanData* edgeMeanData = new GNEMeanData(SUMO_TAG_MEANDATA_LANE, id, myNet, myFilename, file, period, begin, end,133trackVehicles, attributes, aggregate, edgeIDs, edgeFile, excludeEmpty, withInternal,134detectPersons, minSamples, maxTravelTime, vTypes, speedThreshold);135if (myAllowUndoRedo) {136myNet->getViewNet()->getUndoList()->begin(edgeMeanData, TL("add meanDataLane"));137myNet->getViewNet()->getUndoList()->add(new GNEChange_MeanData(edgeMeanData, true), true);138myNet->getViewNet()->getUndoList()->end();139} else {140myNet->getAttributeCarriers()->insertMeanData(edgeMeanData);141edgeMeanData->incRef("buildEdgeMeanData");142}143return true;144} else {145return false;146}147}148149150std::vector<GNEEdge*>151GNEMeanDataHandler::parseEdges(const SumoXMLTag tag, const std::vector<std::string>& edgeIDs) {152std::vector<GNEEdge*> edges;153for (const auto& edgeID : edgeIDs) {154GNEEdge* edge = myNet->getAttributeCarriers()->retrieveEdge(edgeID, false);155// empty edges aren't allowed. If edge is empty, write error, clear edges and stop156if (edge == nullptr) {157writeError(TLF("Could not build % in netedit", toString(tag)) + std::string("; ") + TL("Edge doesn't exist."));158edges.clear();159return edges;160} else {161edges.push_back(edge);162}163}164return edges;165}166167168std::vector<SumoXMLAttr>169GNEMeanDataHandler::parseAttributes(const SumoXMLTag tag, const std::vector<std::string>& attrStrs) {170std::vector<SumoXMLAttr> attrs;171for (const auto& attrStr : attrStrs) {172if (SUMOXMLDefinitions::Attrs.hasString(attrStr)) {173attrs.push_back(static_cast<SumoXMLAttr>(SUMOXMLDefinitions::Attrs.get(attrStr)));174} else {175writeError(TLF("Could not build % in netedit", toString(tag)) + std::string("; ") + TLF("Attribute '%' doesn't exist.", attrStr));176attrs.clear();177return attrs;178}179}180return attrs;181}182183184bool185GNEMeanDataHandler::checkDuplicatedMeanDataElement(const SumoXMLTag tag, const std::string& id) {186// retrieve meanData element187auto meanDataElement = myNet->getAttributeCarriers()->retrieveMeanData(tag, id, false);188// if meanData exist, check if overwrite (delete)189if (meanDataElement) {190if (myOverwriteElements) {191// delete meanData element (and all of their childrens)192myNet->deleteMeanData(meanDataElement, myNet->getViewNet()->getUndoList());193} else if (myRemainElements) {194// duplicated dataset195return writeWarningDuplicated(tag, meanDataElement->getID(), meanDataElement->getTagProperty()->getTag());196} else {197// open overwrite dialog198GNEOverwriteElement overwriteElementDialog(this, meanDataElement);199// continue depending of result200if (overwriteElementDialog.getResult() == GNEOverwriteElement::Result::ACCEPT) {201// delete meanData element (and all of their childrens)202myNet->deleteMeanData(meanDataElement, myNet->getViewNet()->getUndoList());203} else if (overwriteElementDialog.getResult() == GNEOverwriteElement::Result::CANCEL) {204// duplicated demand205return writeWarningDuplicated(tag, meanDataElement->getID(), meanDataElement->getTagProperty()->getTag());206} else {207return false;208}209}210}211return true;212}213214215bool216GNEMeanDataHandler::checkExcludeEmpty(const SumoXMLTag tag, const std::string& id, const std::string& excludeEmpty) {217if (GNEAttributeCarrier::canParse<bool>(excludeEmpty)) {218return true;219} else if (excludeEmpty == SUMOXMLDefinitions::ExcludeEmptys.getString(ExcludeEmpty::DEFAULTS)) {220return true;221} else {222return writeError(TLF("Could not build % with ID '%' in netedit; Invalid value '%' for %.", toString(tag), id, excludeEmpty, toString(SUMO_ATTR_EXCLUDE_EMPTY)));223}224}225226/****************************************************************************/227228229