LCOV - code coverage report
Current view: top level - src/utils/xml - SAXWeightsHandler.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 86 100 86.0 %
Date: 2024-05-06 15:32:35 Functions: 9 11 81.8 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2007-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    SAXWeightsHandler.cpp
      15             : /// @author  Daniel Krajzewicz
      16             : /// @author  Jakob Erdmann
      17             : /// @author  Michael Behrisch
      18             : /// @date    Fri, 30 Mar 2007
      19             : ///
      20             : // An XML-handler for network weights
      21             : /****************************************************************************/
      22             : #include <config.h>
      23             : 
      24             : #include <utils/common/MsgHandler.h>
      25             : 
      26             : #include "SAXWeightsHandler.h"
      27             : 
      28             : 
      29             : // ===========================================================================
      30             : // method definitions
      31             : // ===========================================================================
      32             : 
      33             : // ---------------------------------------------------------------------------
      34             : // SAXWeightsHandler::ToRetrieveDefinition methods
      35             : // ---------------------------------------------------------------------------
      36             : 
      37         781 : SAXWeightsHandler::ToRetrieveDefinition::ToRetrieveDefinition(const std::string& attributeName,
      38         781 :         bool edgeBased, EdgeFloatTimeLineRetriever& destination) :
      39         781 :     myAttributeName(attributeName),
      40         781 :     myAmEdgeBased(edgeBased),
      41         781 :     myDestination(destination),
      42         781 :     myAggValue(0),
      43         781 :     myNoLanes(0),
      44         781 :     myHadAttribute(0) {
      45         781 : }
      46             : 
      47             : 
      48         781 : SAXWeightsHandler::ToRetrieveDefinition::~ToRetrieveDefinition() {
      49         781 : }
      50             : 
      51             : // ---------------------------------------------------------------------------
      52             : // SAXWeightsHandler methods
      53             : // ---------------------------------------------------------------------------
      54             : 
      55         771 : SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*>& defs, const std::string& file) :
      56             :     SUMOSAXHandler(file),
      57         771 :     myDefinitions(defs),
      58         771 :     myCurrentTimeBeg(-1),
      59        1542 :     myCurrentTimeEnd(-1) {
      60         771 : }
      61             : 
      62             : 
      63           0 : SAXWeightsHandler::SAXWeightsHandler(ToRetrieveDefinition* def, const std::string& file) :
      64             :     SUMOSAXHandler(file),
      65           0 :     myDefinitions({def}),
      66           0 :               myCurrentTimeBeg(-1),
      67           0 : myCurrentTimeEnd(-1) {
      68           0 : }
      69             : 
      70             : 
      71         771 : SAXWeightsHandler::~SAXWeightsHandler() {
      72        1552 :     for (const auto& definition : myDefinitions) {
      73         781 :         delete definition;
      74             :     }
      75         771 : }
      76             : 
      77             : 
      78             : void
      79       11548 : SAXWeightsHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
      80       11548 :     switch (element) {
      81        1203 :         case SUMO_TAG_INTERVAL: {
      82        1203 :             bool ok = true;
      83        2406 :             myCurrentID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
      84        1203 :             myCurrentTimeBeg = STEPS2TIME(attrs.getSUMOTimeReporting(SUMO_ATTR_BEGIN, nullptr, ok));
      85        1203 :             myCurrentTimeEnd = STEPS2TIME(attrs.getSUMOTimeReporting(SUMO_ATTR_END, nullptr, ok));
      86        1203 :             if (myCurrentTimeEnd < myCurrentTimeBeg) {
      87           0 :                 WRITE_ERROR("Interval end time " + toString(myCurrentTimeEnd) + " is lower than interval begin time " + toString(myCurrentTimeBeg));
      88           0 :                 myCurrentTimeEnd = myCurrentTimeBeg;
      89             :             }
      90             :         }
      91        1203 :         break;
      92        9515 :         case SUMO_TAG_EDGE: {
      93        9515 :             bool ok = true;
      94       19030 :             myCurrentEdgeID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, "");
      95        9515 :             tryParse(attrs, true);
      96             :         }
      97        9515 :         break;
      98          10 :         case SUMO_TAG_EDGEREL: {
      99          10 :             tryParseEdgeRel(attrs);
     100             :         }
     101          10 :         break;
     102          17 :         case SUMO_TAG_TAZREL: {
     103          17 :             tryParseTazRel(attrs);
     104             :         }
     105          17 :         break;
     106             :         break;
     107          24 :         case SUMO_TAG_LANE: {
     108          24 :             tryParse(attrs, false);
     109             :         }
     110          24 :         break;
     111             :         default:
     112             :             break;
     113             :     }
     114       11548 : }
     115             : 
     116             : 
     117             : void
     118        9539 : SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
     119             :     // !!!! no error handling!
     120        9539 :     if (isEdge) {
     121             :         // process all that want values directly from the edge
     122       19220 :         for (const auto& definition : myDefinitions) {
     123        9705 :             if (definition->myAmEdgeBased) {
     124        9685 :                 if (attrs.hasAttribute(definition->myAttributeName)) {
     125        9659 :                     definition->myAggValue = attrs.getFloat(definition->myAttributeName);
     126        9659 :                     definition->myNoLanes = 1;
     127        9659 :                     definition->myHadAttribute = true;
     128             :                 } else {
     129          26 :                     definition->myHadAttribute = false;
     130             :                 }
     131             :             } else {
     132          20 :                 definition->myAggValue = 0;
     133          20 :                 definition->myNoLanes = 0;
     134             :             }
     135             :         }
     136             :     } else {
     137             :         // process the current lane values
     138          48 :         for (const auto& definition : myDefinitions) {
     139          24 :             if (!definition->myAmEdgeBased) {
     140             :                 try {
     141          24 :                     definition->myAggValue += attrs.getFloat(definition->myAttributeName);
     142          24 :                     definition->myNoLanes++;
     143          24 :                     definition->myHadAttribute = true;
     144           0 :                 } catch (EmptyData&) {
     145           0 :                     WRITE_ERRORF(TL("Missing value '%' in edge '%'."), definition->myAttributeName, myCurrentEdgeID);
     146           0 :                 } catch (NumberFormatException&) {
     147           0 :                     WRITE_ERROR("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID +
     148             :                                 "' at time step " + toString(myCurrentTimeBeg) + ".");
     149           0 :                 }
     150             :             }
     151             :         }
     152             :     }
     153        9539 : }
     154             : 
     155             : 
     156             : void
     157          10 : SAXWeightsHandler::tryParseEdgeRel(const SUMOSAXAttributes& attrs) {
     158          10 :     if (attrs.hasAttribute(SUMO_ATTR_FROM) && attrs.hasAttribute(SUMO_ATTR_TO)) {
     159          10 :         bool ok = true;
     160          10 :         const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
     161          10 :         const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
     162          20 :         for (ToRetrieveDefinition* ret : myDefinitions) {
     163          10 :             if (attrs.hasAttribute(ret->myAttributeName)) {
     164           0 :                 ret->myDestination.addEdgeRelWeight(from, to,
     165           0 :                                                     attrs.getFloat(ret->myAttributeName),
     166             :                                                     myCurrentTimeBeg, myCurrentTimeEnd);
     167             :             }
     168             :         }
     169             :     }
     170          10 : }
     171             : 
     172             : void
     173          17 : SAXWeightsHandler::tryParseTazRel(const SUMOSAXAttributes& attrs) {
     174          17 :     if (attrs.hasAttribute(SUMO_ATTR_FROM) && attrs.hasAttribute(SUMO_ATTR_TO)) {
     175          17 :         bool ok = true;
     176          17 :         const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
     177          17 :         const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
     178          34 :         for (ToRetrieveDefinition* ret : myDefinitions) {
     179          17 :             if (attrs.hasAttribute(ret->myAttributeName)) {
     180          14 :                 ret->myDestination.addTazRelWeight(myCurrentID, from, to,
     181           7 :                                                    attrs.getFloat(ret->myAttributeName),
     182             :                                                    myCurrentTimeBeg, myCurrentTimeEnd);
     183             :             }
     184             :         }
     185             :     }
     186          17 : }
     187             : 
     188             : 
     189             : void
     190       11548 : SAXWeightsHandler::myEndElement(int element) {
     191       11548 :     if (element == SUMO_TAG_EDGE) {
     192       19220 :         for (const auto& definition : myDefinitions) {
     193        9705 :             if (definition->myHadAttribute) {
     194        9679 :                 definition->myDestination.addEdgeWeight(myCurrentEdgeID,
     195        9679 :                                                         definition->myAggValue / (double)definition->myNoLanes,
     196             :                                                         myCurrentTimeBeg, myCurrentTimeEnd);
     197             :             }
     198             :         }
     199             :     }
     200       11548 : }
     201             : 
     202             : 
     203             : /****************************************************************************/

Generated by: LCOV version 1.14