LCOV - code coverage report
Current view: top level - src/od - ODDistrictHandler.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 54 55 98.2 %
Date: 2024-05-03 15:29:52 Functions: 9 10 90.0 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
       4             : // This program and the accompanying materials are made available under the
       5             : // terms of the Eclipse Public License 2.0 which is available at
       6             : // https://www.eclipse.org/legal/epl-2.0/
       7             : // This Source Code may also be made available under the following Secondary
       8             : // Licenses when the conditions for such availability set forth in the Eclipse
       9             : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10             : // or later which is available at
      11             : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12             : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13             : /****************************************************************************/
      14             : /// @file    ODDistrictHandler.cpp
      15             : /// @author  Daniel Krajzewicz
      16             : /// @author  Jakob Erdmann
      17             : /// @author  Michael Behrisch
      18             : /// @date    Sept 2002
      19             : ///
      20             : // An XML-Handler for districts
      21             : /****************************************************************************/
      22             : #include <config.h>
      23             : 
      24             : #include <string>
      25             : #include <utility>
      26             : #include <iostream>
      27             : #include <utils/common/UtilExceptions.h>
      28             : #include <utils/common/MsgHandler.h>
      29             : #include <utils/common/ToString.h>
      30             : #include <utils/xml/SUMOSAXHandler.h>
      31             : #include <utils/xml/SUMOXMLDefinitions.h>
      32             : #include "ODDistrict.h"
      33             : #include "ODDistrictCont.h"
      34             : #include "ODDistrictHandler.h"
      35             : 
      36             : 
      37             : // ===========================================================================
      38             : // method definitions
      39             : // ===========================================================================
      40         142 : ODDistrictHandler::ODDistrictHandler(ODDistrictCont& cont,
      41         142 :                                      const std::string& file)
      42         284 :     : SUMOSAXHandler(file), myContainer(cont), myCurrentDistrict(nullptr) {}
      43             : 
      44             : 
      45         142 : ODDistrictHandler::~ODDistrictHandler() {}
      46             : 
      47             : 
      48             : void
      49        1434 : ODDistrictHandler::myStartElement(int element,
      50             :                                   const SUMOSAXAttributes& attrs) {
      51        1434 :     switch (element) {
      52         341 :         case SUMO_TAG_TAZ:
      53         341 :             openDistrict(attrs);
      54         341 :             break;
      55         331 :         case SUMO_TAG_TAZSOURCE:
      56         331 :             addSource(attrs);
      57         331 :             break;
      58         620 :         case SUMO_TAG_TAZSINK:
      59         620 :             addSink(attrs);
      60         620 :             break;
      61             :         default:
      62             :             break;
      63             :     }
      64        1434 : }
      65             : 
      66             : 
      67             : void
      68        1434 : ODDistrictHandler::myEndElement(int element) {
      69        1434 :     if (element == SUMO_TAG_TAZ) {
      70         341 :         closeDistrict();
      71             :     }
      72        1434 : }
      73             : 
      74             : 
      75             : void
      76         341 : ODDistrictHandler::openDistrict(const SUMOSAXAttributes& attrs) {
      77         341 :     myCurrentDistrict = nullptr;
      78             :     // get the id, report an error if not given or empty...
      79         341 :     bool ok = true;
      80         341 :     std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
      81         341 :     if (!ok) {
      82             :         return;
      83             :     }
      84         341 :     myCurrentDistrict = new ODDistrict(id);
      85         341 :     if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
      86           2 :         const std::vector<std::string>& desc = attrs.get<std::vector<std::string> >(SUMO_ATTR_EDGES, id.c_str(), ok);
      87           5 :         for (const std::string& eID : desc) {
      88           3 :             myCurrentDistrict->addSource(eID, 1.);
      89           3 :             myCurrentDistrict->addSink(eID, 1.);
      90             :         }
      91           2 :     }
      92             : }
      93             : 
      94             : 
      95             : void
      96         331 : ODDistrictHandler::addSource(const SUMOSAXAttributes& attrs) {
      97         331 :     std::pair<std::string, double> vals = parseTAZ(attrs);
      98         331 :     if (vals.second >= 0) {
      99         331 :         myCurrentDistrict->addSource(vals.first, vals.second);
     100             :     }
     101         331 : }
     102             : 
     103             : 
     104             : void
     105         620 : ODDistrictHandler::addSink(const SUMOSAXAttributes& attrs) {
     106         620 :     std::pair<std::string, double> vals = parseTAZ(attrs);
     107         620 :     if (vals.second >= 0) {
     108         620 :         myCurrentDistrict->addSink(vals.first, vals.second);
     109             :     }
     110         620 : }
     111             : 
     112             : 
     113             : 
     114             : std::pair<std::string, double>
     115         951 : ODDistrictHandler::parseTAZ(const SUMOSAXAttributes& attrs) {
     116             :     // check the current district first
     117         951 :     if (myCurrentDistrict == nullptr) {
     118             :         return std::pair<std::string, double>("", -1);
     119             :     }
     120             :     // get the id, report an error if not given or empty...
     121         951 :     bool ok = true;
     122         951 :     std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
     123         951 :     if (!ok) {
     124             :         return std::pair<std::string, double>("", -1);
     125             :     }
     126             :     // get the weight
     127         951 :     double weight = attrs.get<double>(SUMO_ATTR_WEIGHT, id.c_str(), ok);
     128         951 :     if (ok) {
     129         951 :         if (weight < 0) {
     130           0 :             WRITE_ERRORF(TL("'probability' must be positive (in definition of % '%')."), attrs.getObjectType(), id);
     131             :         } else {
     132             :             return std::pair<std::string, double>(id, weight);
     133             :         }
     134             :     }
     135             :     return std::pair<std::string, double>("", -1);
     136             : }
     137             : 
     138             : 
     139             : void
     140         341 : ODDistrictHandler::closeDistrict() {
     141         341 :     if (myCurrentDistrict != nullptr) {
     142         341 :         myContainer.add(myCurrentDistrict->getID(), myCurrentDistrict);
     143             :     }
     144         341 : }
     145             : 
     146             : 
     147             : /****************************************************************************/

Generated by: LCOV version 1.14