Changeset 559


Ignore:
Timestamp:
Dec 14, 2006, 9:07:20 AM (18 years ago)
Author:
dkearney
Message:

adjusted core units code to replace the user's units name with the correctly capitolized units found in the rappture dictionary. this is shown in the gui when someone inputs "c" for celcius, the units code will replace this value with "C". added "*" to the type name if it looks like we are multiplying two or more different types. this feature gets confusing when you are multiplying and dividing types, because order of operations are not very clear.

Location:
trunk/src/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/RpUnits.cc

    r554 r559  
    484484
    485485std::string
    486 RpUnits::getUnitsName() const {
     486RpUnits::getUnitsName(int flags) const {
    487487
    488488    std::stringstream unitText;
     
    490490
    491491    exponent = getExponent();
     492
     493    if ( (RPUNITS_ORIG_EXP & flags) == RPUNITS_POS_EXP)  {
     494        if (exponent < 0) {
     495            exponent = exponent * -1;
     496        }
     497    }
     498    else if ( (RPUNITS_ORIG_EXP & flags) == RPUNITS_NEG_EXP)  {
     499        if (exponent > 0) {
     500            exponent = exponent * -1;
     501        }
     502    }
    492503
    493504    if (exponent == 1) {
     
    765776
    766777int
    767 RpUnits::validate ( const std::string& inUnits,
     778// RpUnits::validate ( const std::string& inUnits,
     779RpUnits::validate ( std::string& inUnits,
    768780                    std::string& type,
    769781                    std::list<std::string>* compatList ) {
     
    782794    // err tells us if we encountered any unrecognized units
    783795    err = RpUnits::units2list(inUnits,inUnitsList,type);
     796    RpUnits::list2units(inUnitsList,inUnits);
    784797    inIter = inUnitsList.begin();
    785798
     
    848861
    849862std::string
    850 RpUnitsListEntry::name() const {
     863RpUnitsListEntry::name(int flags) const {
    851864    std::stringstream name;
    852     name << unit->getUnits() << exponent;
     865    double myExp = exponent;
     866
     867    if ( (RPUNITS_ORIG_EXP & flags) == RPUNITS_POS_EXP)  {
     868        if (myExp < 0) {
     869            myExp = myExp * -1;
     870        }
     871    }
     872    else if ( (RPUNITS_ORIG_EXP & flags) == RPUNITS_NEG_EXP)  {
     873        if (myExp > 0) {
     874            myExp = myExp * -1;
     875        }
     876    }
     877
     878    name << unit->getUnits();
     879
     880    if ((RPUNITS_ORIG_EXP & flags) == RPUNITS_STRICT_NAME) {
     881        // if the user asks for strict naming,
     882        // always place the exponent on the name
     883        name << myExp;
     884    }
     885    else if (myExp != 1.0) {
     886        // if the user does not ask for strict naming,
     887        // check to see if the exponent == 1.
     888        // If not, then add exponent to name
     889        name << myExp;
     890    }
     891
    853892    return std::string(name.str());
    854893}
     
    911950/**********************************************************************/
    912951// METHOD: units2list()
    913 /// Split a string of units into a list of base units with exponents.
     952/// Split a string of units into a list of units with exponents.
    914953/**
    915954 * Splits a string of units like cm2/kVns into a list of units like
    916  * cm2, kV1, ns1 where an exponent is provided for each list entry.
     955 * cm2, kV-1, ns-1 where an exponent is provided for each list entry.
    917956 * List entries are found by comparing units strings to the names
    918957 * in the dictionary.
     
    9651004            // erase the found unit's name from our search string
    9661005            outList.push_front(RpUnitsListEntry(unit,exponent));
    967             type = unit->getType() + type;
     1006            if (type.compare("") == 0) {
     1007                type = unit->getType();
     1008            }
     1009            else if (type[0] == '/') {
     1010                type = unit->getType() + type;
     1011            }
     1012            else {
     1013                type = unit->getType() + "*" + type;
     1014            }
    9681015            myInUnits.erase(idx+offset);
    9691016        }
     
    10061053
    10071054/**********************************************************************/
     1055// METHOD: list2units()
     1056/// Join a list of units into a string with proper exponents.
     1057/**
     1058 * Joings a list of units like cm2, kV-1, ns-1, creating a string
     1059 * like cm2/kVns.
     1060 */
     1061
     1062int
     1063RpUnits::list2units ( RpUnitsList& inList,
     1064                      std::string& outUnitsStr) {
     1065
     1066    RpUnitsListIter inListIter;
     1067    std::string inUnits     = "";
     1068    double exp              = 0;
     1069    int err                 = 0;
     1070    std::string numerator   = "";
     1071    std::string denominator = "";
     1072
     1073    inListIter = inList.begin();
     1074
     1075    while (inListIter != inList.end()) {
     1076        exp = inListIter->getExponent();
     1077        if (exp > 0) {
     1078            numerator += inListIter->name();
     1079        }
     1080        else if (exp < 0) {
     1081            denominator += inListIter->name(RPUNITS_POS_EXP);
     1082        }
     1083        else {
     1084            // we shouldn't get units with exponents of zero
     1085        }
     1086        inListIter++;
     1087    }
     1088
     1089    outUnitsStr = numerator;
     1090    if ( denominator.compare("") != 0 ) {
     1091        outUnitsStr += "/" + denominator;
     1092    }
     1093
     1094    return err;
     1095}
     1096
     1097/**********************************************************************/
    10081098// METHOD: compareListEntryBasis()
    10091099/// Compare two RpUnits objects to see if they are related by a basis
     
    11621252    double copies = 0;
    11631253
     1254    std::list<std::string> compatList;
     1255    std::string listStr;
     1256
    11641257    convertList cList;
    11651258    convertList totalConvList;
     
    12341327    }
    12351328
     1329    convErr = RpUnits::units2list(toUnitsName,toUnitsList,type);
     1330    if (convErr) {
     1331        if (result) {
     1332            *result = convErr;
     1333        }
     1334        retStr = "Unrecognized units: \"" + toUnitsName + "\". Please specify valid Rappture Units";
     1335        return retStr;
     1336    }
     1337
    12361338    convErr = RpUnits::units2list(fromUnitsName,fromUnitsList,type);
    12371339    if (convErr) {
     
    12391341            *result = convErr;
    12401342        }
    1241         retStr = "unrecognized units: \"" + fromUnitsName + "\"";
    1242         return retStr;
    1243     }
    1244 
    1245     convErr = RpUnits::units2list(toUnitsName,toUnitsList,type);
    1246     if (convErr) {
    1247         if (result) {
    1248             *result = convErr;
    1249         }
    1250         retStr = "unrecognized units: \"" + toUnitsName + "\"";
     1343        type = "";
     1344        RpUnits::validate(toUnitsName,type,&compatList);
     1345        list2str(compatList,listStr);
     1346        retStr = "Unrecognized units: \"" + fromUnitsName
     1347                + "\".\nShould be units of type " + type + " (" + listStr + ")";
    12511348        return retStr;
    12521349    }
     
    13161413
    13171414                    convErr++;
    1318                     retStr = "conversion unavailable: (";
     1415                    retStr = "Conversion unavailable: (";
    13191416                    while (fromIter != fromUnitsList.end()) {
    13201417                        /*
     
    13361433                    retStr += ")";
    13371434
     1435                    type = "";
     1436                    RpUnits::validate(toUnitsName,type,&compatList);
     1437                    list2str(compatList,listStr);
     1438                    retStr += "\nPlease enter units of type "
     1439                                + type + " (" + listStr + ")";
     1440
     1441
    13381442                    // exit and report the error
    13391443
     
    13891493            }
    13901494            retStr += ")";
     1495            type = "";
     1496            RpUnits::validate(toUnitsName,type,&compatList);
     1497            list2str(compatList,listStr);
     1498            retStr += "\nPlease enter units of type "
     1499                        + type + " (" + listStr + ")";
     1500
    13911501        }
    13921502        else {
     
    25182628RpUnitsPreset::addPresetMisc () {
    25192629
    2520     RpUnits* volt      = RpUnits::define("V",  NULL, RP_TYPE_ENERGY);
     2630    RpUnits* volt      = RpUnits::define("V",  NULL, RP_TYPE_EPOT);
    25212631    RpUnits* mole      = RpUnits::define("mol",NULL, RP_TYPE_MISC);
    25222632    RpUnits* hertz     = RpUnits::define("Hz", NULL, RP_TYPE_MISC);
     
    25372647// -------------------------------------------------------------------- //
    25382648
     2649/**********************************************************************/
     2650// FUNCTION: list2str()
     2651/// Convert a std::list<std::string> into a comma delimited std::string
     2652/**
     2653 * Iterates through a std::list<std::string> and returns a comma
     2654 * delimited std::string containing the elements of the inputted std::list.
     2655 *
     2656 * Returns 0 on success, anything else is error
     2657 */
     2658
     2659int
     2660list2str (std::list<std::string>& inList, std::string& outString)
     2661{
     2662    int retVal = 1;  // return Value 0 is success, everything else is failure
     2663    unsigned int counter = 0; // check if we hit all elements of inList
     2664    std::list<std::string>::iterator inListIter; // list interator
     2665
     2666    inListIter = inList.begin();
     2667
     2668    while (inListIter != inList.end()) {
     2669        if ( outString.empty() ) {
     2670            outString = *inListIter;
     2671        }
     2672        else {
     2673            outString =  outString + "," + *inListIter;
     2674        }
     2675
     2676        // increment the iterator and loop counter
     2677        inListIter++;
     2678        counter++;
     2679    }
     2680
     2681    if (counter == inList.size()) {
     2682        retVal = 0;
     2683    }
     2684
     2685    return retVal;
     2686}
  • trunk/src/core/RpUnits.h

    r554 r559  
    1919enum RP_UNITS_CONSTS {
    2020    RPUNITS_UNITS_OFF           = 0,
    21     RPUNITS_UNITS_ON            = 1
     21    RPUNITS_UNITS_ON            = 1,
     22
     23    // units naming flags
     24    RPUNITS_POS_EXP             = 1,
     25    RPUNITS_NEG_EXP             = 2,
     26    RPUNITS_STRICT_NAME         = 4,
     27    RPUNITS_ORIG_EXP            = 7
    2228};
    2329
     
    2733//define our different types of units
    2834#define RP_TYPE_ENERGY      "energy"
     35#define RP_TYPE_EPOT        "electric_potential"
    2936#define RP_TYPE_LENGTH      "length"
    3037#define RP_TYPE_TEMP        "temperature"
     
    296303
    297304        // print the name of the object
    298         std::string name() const;
     305        std::string name(int flags=RPUNITS_ORIG_EXP) const;
    299306
    300307        // report the basis of the RpUnits object being stored.
     
    331338        // users member fxns
    332339        std::string getUnits() const;
    333         std::string getUnitsName() const;
     340        std::string getUnitsName(int flags=RPUNITS_ORIG_EXP) const;
    334341        std::string getSearchName() const;
    335342        double getExponent() const;
     
    387394        // returns 0 on success (units are valid)
    388395        // returns !0 on failure (units not valid)
    389         static int validate(const std::string& inUnits,
     396        static int validate(std::string& inUnits,
    390397                            std::string& type,
    391398                            std::list<std::string>* compatList=NULL);
     
    425432        //                   "all"           load all available units
    426433        //  RP_TYPE_ENERGY   "energy"        load units related to energy
     434        //  RP_TYPE_EPOT     "electric_potential" load units related to electric potential
    427435        //  RP_TYPE_LENGTH   "length"        load units related to length
    428436        //  RP_TYPE_TEMP     "temperature"   load units related to temperature
     
    666674                               RpUnitsList& outList,
    667675                               std::string& type);
     676        static int list2units( RpUnitsList& inList,
     677                               std::string& outUnitsStr);
    668678        static int grabExponent(const std::string& inStr, double* exp);
    669679        static int grabUnitString( const std::string& inStr);
     
    696706/*--------------------------------------------------------------------------*/
    697707
     708int list2str (std::list<std::string>& inList, std::string& outString);
     709
    698710#endif // ifdef __cplusplus
    699711
Note: See TracChangeset for help on using the changeset viewer.