void LibraryParser::GetLinkDestAttributes(TiXmlNode* pParent) { TiXmlAttribute* Attribute = ( pParent->ToElement())->FirstAttribute() ; string Node; string Interface; while (Attribute ) { if (string(Attribute->Name() ) == "node") { Node = Attribute->ValueStr() ; } else if (string(Attribute->Name() ) == "interface") { Interface = Attribute->ValueStr() ; } Attribute = Attribute->Next() ; } mCurrentLink.SetDestination(Node); mCurrentLink.SetDestinationInterface(Interface) ; }
bool AgentInitializer::parseProperties( TiXmlElement * node, const std::string & sceneFldr) { //first let's decide if this is a velocity modifier if (node->ValueStr() == "VelModifier"){ //this is, we need to find out if we can parse it BFSM::VelModifier *vel = BFSM::parseVelModifier( node, sceneFldr); if ( vel == 0x0 ) { return false; } else { _velModifiers.push_back(vel); } } else if ( isRelevant( node->ValueStr() ) ) { // Extract the attributes of the tag TiXmlAttribute * attr; for ( attr = node->FirstAttribute(); attr; attr = attr->Next() ) { ParseResult result = setFromXMLAttribute( attr->Name(), attr->ValueStr() ); if ( result == FAILURE ) { return false; } else if ( result == IGNORED ) { logger << Logger::WARN_MSG << "Encountered an unexpected per-agent attribute (" << attr->Name() << ") on line " << attr->Row() << "."; } } // Now look for advanced property specifications TiXmlElement* child; for( child = node->FirstChildElement(); child; child = child->NextSiblingElement()) { if ( ! parsePropertySpec( child ) ) { return false; } } } // Irrelevant nodes are, by definition, successful. return true; }
bool Rule::LoadFromXml(TiXmlElement *node) { TiXmlAttribute *attr = node->ToElement()->FirstAttribute(); for (; attr; attr = attr->Next()) { if (string(attr->Name()) != "name" && string(attr->Name()) != "type") params.Add(attr->Name(), attr->ValueStr()); } TiXmlElement *cnode = node->FirstChildElement(); for (; cnode; cnode = cnode->NextSiblingElement()) { if (cnode->ValueStr() == "calaos:condition") { Condition *cond = RulesFactory::CreateCondition(cnode); if (cond) AddCondition(cond); } else if (cnode->ValueStr() == "calaos:action") { Action *action = RulesFactory::CreateAction(cnode); if (action) AddAction(action); } } return true; }
FeuThing::FeuThing(Feu *feu, TiXmlElement *ele, FeuThing *parent) { TiXmlAttribute *attr; TiXmlElement *kid; FeuThing *kidthing; std::string name,value; // Basic element(s) mFeu = feu; mType = ele->ValueStr(); mName = "<unnamed>"; // Walk Attribute list, importing as we go attr = ele->FirstAttribute(); while (attr != NULL) { name = stringof(attr->Name()); value = attr->ValueStr(); info(" \"" + name + "\" ==> \"" + value + "\"\n"); mAttributes[name] = value; // Store in our map if (name == "name") mName = value; attr = attr->Next(); } // Walk child list, descending as we go kid = ele->FirstChildElement(); while (kid != NULL) { kidthing = Feu::convertElement(feu,kid,this); if (kidthing != NULL) adopt(kidthing); kid = kid->NextSiblingElement(); } }
//---------------------------------------------------// void JXmlNode::ClearAllAttributes() { if( m_pXmlElement ){ for( TiXmlAttribute *attr = m_pXmlElement->FirstAttribute(); attr != NULL && attr != m_pXmlElement->LastAttribute(); attr = attr->Next() ){ m_pXmlElement->RemoveAttribute(attr->ValueStr()); } } }
void LibraryParser::GetBiLinkAttributes(TiXmlNode* pParent) { TiXmlAttribute* Attribute = ( pParent->ToElement())->FirstAttribute() ; string Name; string Type ; while (Attribute ) { if (string(Attribute->Name() ) == "name") { Name = Attribute->ValueStr() ; }else if (string(Attribute->Name() ) == "type") { Type = Attribute->ValueStr() ; } Attribute = Attribute->Next() ; } BiLink L ; L.SetName(Name ); L.SetType( Type); mCurrentBiLink = L ; }
void LibraryParser::GetInterfaceAttributes(TiXmlNode* pParent) { TiXmlAttribute* Attribute = ( pParent->ToElement())->FirstAttribute() ; string Name; string Type; int Count = 1; while (Attribute ) { if (string(Attribute->Name() ) == "name") { Name = Attribute->ValueStr() ; } else if (string(Attribute->Name() ) == "type") { Type = Attribute->ValueStr() ; } else if (string(Attribute->Name() ) == "count") { Attribute->QueryIntValue(&Count) ; } Attribute = Attribute->Next() ; } //Add the interface to the current node mCurrentNode.Add(Name, Type, Count) ; }
void LibraryParser::GetLibraryAttributes(TiXmlNode* pParent) { TiXmlAttribute* Attribute = ( pParent->ToElement())->FirstAttribute() ; string Name; while (Attribute ) { if (string(Attribute->Name() ) == "name") { Name = Attribute->ValueStr() ; } Attribute = Attribute->Next() ; } mLib->SetName(Name ) ; }
void CGUIIncludes::ResolveParametersForNode(TiXmlElement *node, const Params& params) { if (!node) return; std::string newValue; // run through this element's attributes, resolving any parameters TiXmlAttribute *attribute = node->FirstAttribute(); while (attribute) { ResolveParamsResult result = ResolveParameters(attribute->ValueStr(), newValue, params); if (result == SINGLE_UNDEFINED_PARAM_RESOLVED && strcmp(node->Value(), "param") == 0 && strcmp(attribute->Name(), "value") == 0 && node->Parent() && strcmp(node->Parent()->Value(), "include") == 0) { // special case: passing <param name="someName" value="$PARAM[undefinedParam]" /> to the nested include // this usually happens when trying to forward a missing parameter from the enclosing include to the nested include // problem: since 'undefinedParam' is not defined, it expands to <param name="someName" value="" /> and overrides any default value set with <param name="someName" default="someValue" /> in the nested include // to prevent this, we'll completely remove this parameter from the nested include call so that the default value can be correctly picked up later node->Parent()->RemoveChild(node); return; } else if (result != NO_PARAMS_FOUND) attribute->SetValue(newValue); attribute = attribute->Next(); } // run through this element's value and children, resolving any parameters TiXmlNode *child = node->FirstChild(); if (child) { if (child->Type() == TiXmlNode::TINYXML_TEXT) { ResolveParamsResult result = ResolveParameters(child->ValueStr(), newValue, params); if (result == SINGLE_UNDEFINED_PARAM_RESOLVED && strcmp(node->Value(), "param") == 0 && node->Parent() && strcmp(node->Parent()->Value(), "include") == 0) { // special case: passing <param name="someName">$PARAM[undefinedParam]</param> to the nested include // we'll remove the offending param tag same as above node->Parent()->RemoveChild(node); } else if (result != NO_PARAMS_FOUND) child->SetValue(newValue); } else if (child->Type() == TiXmlNode::TINYXML_ELEMENT) { do { TiXmlElement *next = child->NextSiblingElement(); // save next as current child might be removed from the tree ResolveParametersForNode(static_cast<TiXmlElement *>(child), params); child = next; } while (child); } } }
UnitDescription* LevelLoader::loadUnitDescription(TiXmlHandle &node) { UnitDescription *unit = new UnitDescription(); unit->imagePath = xmlUtil.getString(node, TAG_IMAGE); unit->locationID = xmlUtil.getInt(node, TAG_LOCATION); TiXmlAttribute *typeAttribute = node.ToElement()->FirstAttribute(); unit->type = typeAttribute->ValueStr(); unit->startAngle = xmlUtil.getFloat(node, TAG_ANGLE); unit->width = xmlUtil.getInt(node, TAG_WIDTH); unit->height = xmlUtil.getInt(node, TAG_HEIGHT); //cout << unit->startAngle << endl; // TODO: If no start angle exists, give a random angle. return unit; }
PickupDescription* LevelLoader::loadPickupDescription(TiXmlHandle &node) { PickupDescription *pickupDesc = new PickupDescription(); // Get the type attribute TiXmlAttribute *typeAttribute = node.ToElement()->FirstAttribute(); pickupDesc->type = typeAttribute->ValueStr(); // Get the remaining attributes pickupDesc->imagePath = xmlUtil.getString(node, TAG_IMAGE); pickupDesc->locationID = xmlUtil.getInt(node, TAG_LOCATION); pickupDesc->startAngle = xmlUtil.getFloat(node, TAG_START_ANGLE); pickupDesc->endAngle = xmlUtil.getFloat(node, TAG_END_ANGLE); pickupDesc->count = xmlUtil.getInt(node, TAG_COUNT); return pickupDesc; }
int MAMELoader::LoadRomGroupEntry(TiXmlElement *romgroupElmt, MAMEGameEntry *gameentry) { MAMERomGroupEntry romgroupentry; //First, get the "type" and "load_method" attributes. If they don't exist, we return with an error. string load_method; if (romgroupElmt->QueryValueAttribute("type", &romgroupentry.type) != TIXML_SUCCESS) return 1; if (romgroupElmt->QueryValueAttribute("load_method", &load_method) != TIXML_SUCCESS) return 1; // Read the encryption type string, if it exists romgroupElmt->QueryValueAttribute("encryption", &romgroupentry.encryption); //Iterate through the attributes of the romgroup element, recording any user-defined values. for (TiXmlAttribute *attr = romgroupElmt->FirstAttribute(); attr; attr = attr->Next()) { // NameTStr() is returning an std::string because we have defined TIXML_USE_STL const string &attrName = attr->NameTStr(); // Ignore the attribute if it is "type" or "load_method"; we already dealt with those, they are mandated. if (attrName.compare("type") && attrName.compare("load_method")) romgroupentry.attributes[attrName] = attr->ValueStr(); } if (load_method == "append") romgroupentry.loadmethod = LM_APPEND; else if (load_method == "append_swap16") romgroupentry.loadmethod = LM_APPEND_SWAP16; else if (load_method == "deinterlace") romgroupentry.loadmethod = LM_DEINTERLACE; else return 1; // load rom entries for (TiXmlElement *romElmt = romgroupElmt->FirstChildElement(); romElmt != 0; romElmt = romElmt->NextSiblingElement()) { const string &elmtName = romElmt->ValueStr(); if (elmtName != "rom") return 1; romgroupentry.roms.push_back(romElmt->GetText()); } gameentry->romgroupentries.push_back(romgroupentry); return 0; }
float XmlUtils::getFloatAttribute(const TiXmlElement* element, const char* name, float def) { TiXmlAttribute* attribute = element->GetAttribute(name); if (attribute == nullptr) return def; const std::string& string = attribute->ValueStr(); try { size_t pos = 0; float value = std::stof(string, &pos); if (pos == string.length()) return value; } catch (const std::exception& e) { B3D_LOGE(locationOf(element) << "Value of attribute \"" << name << "\" is not a valid floating-point number (" << e.what() << ")."); throw ParseError(); } B3D_LOGE(locationOf(element) << "Value of attribute \"" << name << "\" is not a valid floating-point number."); throw ParseError(); }
bool AgentInitializer::parsePropertySpec( TiXmlElement * node ) { if ( node->ValueStr() == "Property" ) { const char * cName = node->Attribute( "name" ); if ( cName == 0x0 ) { logger << Logger::ERR_MSG << "AgentSet Property tag specified on line " << node->Row() << " without a \"name\" attribute."; return false; } ::std::string propName( cName ); return processProperty( propName, node ) != FAILURE; } else if ( VERBOSE ) { logger << Logger::WARN_MSG << "Unexpected tag when looking for a property of an AgentSet parameter set: " << node->ValueStr() << "\n"; TiXmlAttribute * attr; for ( attr = node->FirstAttribute(); attr; attr = attr->Next() ) { if ( setFromXMLAttribute( attr->Name(), attr->ValueStr() ) == FAILURE ) { return false; } } } // Unexpected tags are ignored return true; }
void CGUIIncludes::ResolveExpressions(TiXmlElement *node) { if (!node) return; TiXmlNode *child = node->FirstChild(); if (child && child->Type() == TiXmlNode::TINYXML_TEXT && m_expressionNodes.count(node->ValueStr())) { child->SetValue(ResolveExpressions(child->ValueStr())); } else { TiXmlAttribute *attribute = node->FirstAttribute(); while (attribute) { if (m_expressionAttributes.count(attribute->Name())) attribute->SetValue(ResolveExpressions(attribute->ValueStr())); attribute = attribute->Next(); } } }
int XmlUtils::getIntAttribute(const TiXmlElement* element, const char* name) { TiXmlAttribute* attribute = element->GetAttribute(name); if (attribute == nullptr) { B3D_LOGE(locationOf(element) << "Attribute \"" << name << "\" is missing in tag \"" << element->ValueStr() << "\"."); throw ParseError(); } const std::string& string = attribute->ValueStr(); try { size_t pos = 0; int value = std::stoi(string, &pos); if (pos == string.length()) return value; } catch (const std::exception& e) { B3D_LOGE(locationOf(element) << "Value of attribute \"" << name << "\" is not a valid integer (" << e.what() << ")."); throw ParseError(); } B3D_LOGE(locationOf(element) << "Value of attribute \"" << name << "\" is not a valid integer."); throw ParseError(); }
void CGUIIncludes::ResolveIncludesForNode(TiXmlElement *node) { // we have a node, find any <include file="fileName">tagName</include> tags and replace // recursively with their real includes if (!node) return; // First add the defaults if this is for a control CStdString type; if (node->ValueStr() == "control") { type = node->Attribute("type"); map<CStdString, TiXmlElement>::const_iterator it = m_defaults.find(type); if (it != m_defaults.end()) { const TiXmlElement &element = (*it).second; const TiXmlElement *tag = element.FirstChildElement(); while (tag) { // we insert at the end of block node->InsertEndChild(*tag); tag = tag->NextSiblingElement(); } } } TiXmlElement *include = node->FirstChildElement("include"); while (include && include->FirstChild()) { // have an include tag - grab it's tag name and replace it with the real tag contents const char *file = include->Attribute("file"); if (file) { // we need to load this include from the alternative file LoadIncludes(g_SkinInfo->GetSkinPath(file)); } const char *condition = include->Attribute("condition"); if (condition) { // check this condition if (!g_infoManager.EvaluateBool(condition)) { include = include->NextSiblingElement("include"); continue; } } CStdString tagName = include->FirstChild()->Value(); map<CStdString, TiXmlElement>::const_iterator it = m_includes.find(tagName); if (it != m_includes.end()) { // found the tag(s) to include - let's replace it const TiXmlElement &element = (*it).second; const TiXmlElement *tag = element.FirstChildElement(); while (tag) { // we insert before the <include> element to keep the correct // order (we render in the order given in the xml file) node->InsertBeforeChild(include, *tag); tag = tag->NextSiblingElement(); } // remove the <include>tagName</include> element node->RemoveChild(include); include = node->FirstChildElement("include"); } else { // invalid include CLog::Log(LOGWARNING, "Skin has invalid include: %s", tagName.c_str()); include = include->NextSiblingElement("include"); } } // run through this element's attributes, resolving any constants TiXmlAttribute *attribute = node->FirstAttribute(); while (attribute) { // check the attribute against our set if (m_constantAttributes.count(attribute->NameStr())) attribute->SetValue(ResolveConstant(attribute->ValueStr())); attribute = attribute->Next(); } // also do the value if (node->FirstChild() && node->FirstChild()->Type() == TiXmlNode::TEXT && m_constantNodes.count(node->ValueStr())) node->FirstChild()->SetValue(ResolveConstant(node->FirstChild()->ValueStr())); }
void HoOrionModelParser::ParseRouter(TiXmlNode* pParent) { double TmpDouble; int TmpInt; string Type; stringstream Energy; stringstream Area; int MaxIn = 0; int MaxOut = 0; TiXmlAttribute* Attribute = ( pParent->ToElement())->FirstAttribute() ; while (Attribute ) { if (string(Attribute->Name() ) == "type") { Type = string(Attribute->Value() ) ; } else if (string(Attribute->Name() ) == "maxin") { Attribute->QueryIntValue(&TmpInt) ; MaxIn = TmpInt ; } else if (string(Attribute->Name() ) == "maxout") { Attribute->QueryIntValue(&TmpInt) ; MaxOut = TmpInt ; } else if (string(Attribute->Name() ) == "maxbw") { Attribute->QueryDoubleValue(&TmpDouble) ; //mModel->RouterMaxBw = TmpDouble ; } else if (string(Attribute->Name() ) == "energy") { Energy << Attribute->ValueStr() ; } else if (string(Attribute->Name() ) == "area") { Area << Attribute->ValueStr() ; } else { cout << "Unknown attribute "<< Attribute->Name() << " for wire" << endl ; } Attribute = Attribute->Next() ; } mMaxIn = MaxIn ; mMaxOut = MaxOut ; if (Type == "r1ch32") { mArouter1ch32.resize(MaxIn + 1) ; mErouter1ch32.resize(MaxIn + 1) ; mErouterLeak1ch32.resize(MaxIn + 1) ; mArouter1ch32[0].resize(MaxOut + 1) ; mErouter1ch32[0].resize(MaxOut + 1) ; mErouterLeak1ch32[0].resize(MaxOut + 1) ; for (int i = 1; i <= MaxIn ; i++) { mArouter1ch32[i].resize(MaxOut + 1) ; mErouter1ch32[i].resize(MaxOut + 1) ; mErouterLeak1ch32[i].resize(MaxOut + 1) ; for (int j = 1; j <= MaxOut ; j++) { Area >> mArouter1ch32[i][j]; mArouter1ch32[i][j] = mArouter1ch32[i][j]*1e-12; Energy >> mErouter1ch32[i][j]; Energy >> mErouterLeak1ch32[i][j]; } } //handle first rows nad first column for (int j = 0; j <= MaxOut ; j++) { mArouter1ch32[0][j] = 0; mErouter1ch32[0][j] = 0; mErouterLeak1ch32[0][j] = 0; } for (int i = 0; i <= MaxIn ; i++) { mArouter1ch32[i][0] = 0; mErouter1ch32[i][0] = 0; mErouterLeak1ch32[i][0] = 0; } } else if (Type == "r4ch32") {
void CGUIIncludes::ResolveIncludesForNode(TiXmlElement *node, std::map<INFO::InfoPtr, bool>* xmlIncludeConditions /* = NULL */) { // we have a node, find any <include file="fileName">tagName</include> tags and replace // recursively with their real includes if (!node) return; // First add the defaults if this is for a control std::string type; if (node->ValueStr() == "control") { type = XMLUtils::GetAttribute(node, "type"); std::map<std::string, TiXmlElement>::const_iterator it = m_defaults.find(type); if (it != m_defaults.end()) { // we don't insert <left> et. al. if <posx> or <posy> is specified bool hasPosX(node->FirstChild("posx") != NULL); bool hasPosY(node->FirstChild("posy") != NULL); const TiXmlElement &element = (*it).second; const TiXmlElement *tag = element.FirstChildElement(); while (tag) { std::string value = tag->ValueStr(); bool skip(false); if (hasPosX && (value == "left" || value == "right" || value == "centerleft" || value == "centerright")) skip = true; if (hasPosY && (value == "top" || value == "bottom" || value == "centertop" || value == "centerbottom")) skip = true; // we insert at the end of block if (!skip) node->InsertEndChild(*tag); tag = tag->NextSiblingElement(); } } } TiXmlElement *include = node->FirstChildElement("include"); while (include) { // have an include tag - grab it's tag name and replace it with the real tag contents const char *file = include->Attribute("file"); if (file) { // we need to load this include from the alternative file LoadIncludes(g_SkinInfo->GetSkinPath(file)); } const char *condition = include->Attribute("condition"); if (condition) { // check this condition INFO::InfoPtr conditionID = g_infoManager.Register(condition); bool value = conditionID->Get(); if (xmlIncludeConditions) (*xmlIncludeConditions)[conditionID] = value; if (!value) { include = include->NextSiblingElement("include"); continue; } } Params params; std::string tagName; // determine which form of include call we have const char *name = include->Attribute("content"); if (name) { // 1. <include name="MyControl" /> // 2. <include name="MyControl"> // <param name="posx" value="225" /> // <param name="posy">150</param> // ... // </include> tagName = name; GetParameters(include, "value", params); } else { const TiXmlNode *child = include->FirstChild(); if (child && child->Type() == TiXmlNode::TINYXML_TEXT) { // 3. <include>MyControl</include> // old-style includes for backward compatibility tagName = child->ValueStr(); } } std::map<std::string, std::pair<TiXmlElement, Params>>::const_iterator it = m_includes.find(tagName); if (it != m_includes.end()) { // found the tag(s) to include - let's replace it const TiXmlElement *includeBody = &it->second.first; const Params& defaultParams = it->second.second; const TiXmlElement *tag = includeBody->FirstChildElement(); // combine passed include parameters with their default values into a single list (no overwrites) params.insert(defaultParams.begin(), defaultParams.end()); while (tag) { // we insert before the <include> element to keep the correct // order (we render in the order given in the xml file) TiXmlElement *insertedTag = static_cast<TiXmlElement*>(node->InsertBeforeChild(include, *tag)); // after insertion we resolve parameters even if parameter list is empty (to remove param references) ResolveParametersForNode(insertedTag, params); tag = tag->NextSiblingElement(); } // remove the include element itself node->RemoveChild(include); include = node->FirstChildElement("include"); } else { // invalid include CLog::Log(LOGWARNING, "Skin has invalid include: %s", tagName.c_str()); include = include->NextSiblingElement("include"); } } // run through this element's attributes, resolving any constants TiXmlAttribute *attribute = node->FirstAttribute(); while (attribute) { // check the attribute against our set if (m_constantAttributes.count(attribute->Name())) attribute->SetValue(ResolveConstant(attribute->ValueStr())); if (m_expressionAttributes.count(attribute->Name())) attribute->SetValue(ResolveExpressions(attribute->ValueStr())); attribute = attribute->Next(); } // also do the value if (node->FirstChild() && node->FirstChild()->Type() == TiXmlNode::TINYXML_TEXT && m_constantNodes.count(node->ValueStr())) node->FirstChild()->SetValue(ResolveConstant(node->FirstChild()->ValueStr())); if (node->FirstChild() && node->FirstChild()->Type() == TiXmlNode::TINYXML_TEXT && m_expressionNodes.count(node->ValueStr())) node->FirstChild()->SetValue(ResolveExpressions(node->FirstChild()->ValueStr())); }
/*=====================================================================*/ Property* Property::LoadProperties( string filename ) { // Parameters checking OPENCITY_DEBUG( "Loading file: " << filename ); assert( filename != "" ); // Load the XML metadata file TiXmlDocument xml; if (!xml.LoadFile(filename) || xml.Error()) { OPENCITY_FATAL( xml.ErrorDesc() ); abort(); } // Get the root element TiXmlNode* pRoot = xml.RootElement(); if (pRoot == NULL) { OPENCITY_FATAL( xml.ErrorDesc() ); abort(); } // Initialize few work variables TiXmlNode* pNode = NULL; TiXmlElement* pElement = NULL; TiXmlAttribute* pAttribute = NULL; Property* pProperty = new Property(); // Select the "/object/property/cost" node pNode = XNp_xpath_node(pRoot, OC_METADATA_COST_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "build", (int*)&pProperty->uiBuildCost ); pElement->QueryIntAttribute( "destroy", (int*)&pProperty->uiDestroyCost ); pElement->QueryIntAttribute( "support", (int*)&pProperty->uiSupportCost ); pElement->QueryIntAttribute( "income", (int*)&pProperty->uiIncome ); // RCI ======================================================================== // Select the "/object/property/r/need" node pNode = XNp_xpath_node(pRoot, OC_METADATA_NEED_R_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sResidence.mmNeed.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sResidence.mmNeed.iMax); // Select the "/object/property/r/provide" node pNode = XNp_xpath_node(pRoot, OC_METADATA_PROVIDE_R_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sResidence.mmProvide.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sResidence.mmProvide.iMax); // Select the "/object/property/c/need" node pNode = XNp_xpath_node(pRoot, OC_METADATA_NEED_C_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sCommerce.mmNeed.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sCommerce.mmNeed.iMax); // Select the "/object/property/c/provide" node pNode = XNp_xpath_node(pRoot, OC_METADATA_PROVIDE_C_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sCommerce.mmProvide.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sCommerce.mmProvide.iMax); // Select the "/object/property/i/need" node pNode = XNp_xpath_node(pRoot, OC_METADATA_NEED_I_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sIndustry.mmNeed.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sIndustry.mmNeed.iMax); // Select the "/object/property/i/provide" node pNode = XNp_xpath_node(pRoot, OC_METADATA_PROVIDE_I_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sIndustry.mmProvide.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sIndustry.mmProvide.iMax); // WEG ======================================================================== // Select the "/object/property/w/need" node pNode = XNp_xpath_node(pRoot, OC_METADATA_NEED_W_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sWater.mmNeed.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sWater.mmNeed.iMax); // Select the "/object/property/w/provide" node pNode = XNp_xpath_node(pRoot, OC_METADATA_PROVIDE_W_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sWater.mmProvide.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sWater.mmProvide.iMax); // Select the "/object/property/e/need" node pNode = XNp_xpath_node(pRoot, OC_METADATA_NEED_E_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sElectricity.mmNeed.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sElectricity.mmNeed.iMax); // Select the "/object/property/e/provide" node pNode = XNp_xpath_node(pRoot, OC_METADATA_PROVIDE_E_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sElectricity.mmProvide.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sElectricity.mmProvide.iMax); // Select the "/object/property/g/need" node pNode = XNp_xpath_node(pRoot, OC_METADATA_NEED_G_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sGas.mmNeed.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sGas.mmNeed.iMax); // Select the "/object/property/e/provide" node pNode = XNp_xpath_node(pRoot, OC_METADATA_PROVIDE_G_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sGas.mmProvide.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sGas.mmProvide.iMax); // TN ======================================================================== // Select the "/object/property/t/need" node pNode = XNp_xpath_node(pRoot, OC_METADATA_NEED_T_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sTraffic.mmNeed.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sTraffic.mmNeed.iMax); // Select the "/object/property/t/provide" node pNode = XNp_xpath_node(pRoot, OC_METADATA_PROVIDE_T_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sTraffic.mmProvide.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sTraffic.mmProvide.iMax); // Select the "/object/property/nature/need" node pNode = XNp_xpath_node(pRoot, OC_METADATA_NEED_NATURE_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sNature.mmNeed.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sNature.mmNeed.iMax); // Select the "/object/property/nature/provide" node pNode = XNp_xpath_node(pRoot, OC_METADATA_PROVIDE_NATURE_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "min", (int*)&pProperty->sNature.mmProvide.iMin); pElement->QueryIntAttribute( "max", (int*)&pProperty->sNature.mmProvide.iMax); // ??? ======================================================================== // Select the "/object/property" node pNode = XNp_xpath_node(pRoot, OC_METADATA_PROPERTY_NODE); assert( pNode != NULL ); // Get the <cost> node and fill the structure with the model cost properties pElement = pNode->ToElement(); pElement->QueryIntAttribute( "inhabitant", (int*)&pProperty->uiInhabitant ); pElement->QueryIntAttribute( "worker", (int*)&pProperty->uiWorker ); pElement->QueryIntAttribute( "radius", (int*)&pProperty->uiRadius ); // Select the "/object/property/@type" attribute pAttribute = XAp_xpath_attribute(pRoot, OC_METADATA_STRUCTURE_TYPE_ATTRIBUTE); assert( pAttribute != NULL ); pProperty->eStructureType = _Str2Type(pAttribute->ValueStr()); // Select the "/object/property/direction/@value" attribute pAttribute = XAp_xpath_attribute(pRoot, OC_METADATA_DIRECTION_ATTRIBUTE); assert( pAttribute != NULL ); pProperty->eDirection = _Str2Direction(pAttribute->ValueStr()); // Select the "/object/model" node pNode = XNp_xpath_node(pRoot, OC_METADATA_MODEL_NODE); assert( pNode != NULL ); // Get the <model> node and fill the structure with the model dimension pElement = pNode->ToElement(); pElement->QueryIntAttribute( "width", (int*)&pProperty->uiWidth ); pElement->QueryIntAttribute( "length", (int*)&pProperty->uiLength ); pElement->QueryFloatAttribute( "height", &pProperty->fHeight ); // Debug /* OPENCITY_DEBUG( endl << "W/L/H: " << pProperty->uiWidth << "/" << pProperty->uiLength << "/" << pProperty->fHeight << " | " << "B/D/S/I: " << pProperty->uiBuildCost << "/" << pProperty->uiDestroyCost << "/" << pProperty->uiSupportCost << "/" << pProperty->uiIncome << " | " << "Dir: " << pProperty->eDirection << " | " << "Type: " << pProperty->eStructureType << " | " << "i/w/r: " << pProperty->uiInhabitant << "/" << pProperty->uiWorker << "/" << pProperty->uiRadius << " | " << endl << "r/c/i: " << pProperty->sResidence.mmNeed.iMin << "-" << pProperty->sResidence.mmNeed.iMax << " " << pProperty->sResidence.mmProvide.iMin << "-" << pProperty->sResidence.mmProvide.iMax << " / " << pProperty->sCommerce.mmNeed.iMin << "-" << pProperty->sCommerce.mmNeed.iMax << " " << pProperty->sCommerce.mmProvide.iMin << "-" << pProperty->sCommerce.mmProvide.iMax << " / " << pProperty->sIndustry.mmNeed.iMin << "-" << pProperty->sIndustry.mmNeed.iMax << " " << pProperty->sIndustry.mmProvide.iMin << "-" << pProperty->sIndustry.mmProvide.iMax << " / " << endl << "w/e/g: " << pProperty->sWater.mmNeed.iMin << "-" << pProperty->sWater.mmNeed.iMax << " " << pProperty->sWater.mmProvide.iMin << "-" << pProperty->sWater.mmProvide.iMax << " / " << pProperty->sElectricity.mmNeed.iMin << "-" << pProperty->sElectricity.mmNeed.iMax << " " << pProperty->sElectricity.mmProvide.iMin << "-" << pProperty->sElectricity.mmProvide.iMax << " / " << pProperty->sGas.mmNeed.iMin << "-" << pProperty->sGas.mmNeed.iMax << " " << pProperty->sGas.mmProvide.iMin << "-" << pProperty->sGas.mmProvide.iMax << " / " << endl << "t/n: " << pProperty->sTraffic.mmNeed.iMin << "-" << pProperty->sTraffic.mmNeed.iMax << " " << pProperty->sTraffic.mmProvide.iMin << "-" << pProperty->sTraffic.mmProvide.iMax << " / " << pProperty->sNature.mmNeed.iMin << "-" << pProperty->sNature.mmNeed.iMax << " " << pProperty->sNature.mmProvide.iMin << "-" << pProperty->sNature.mmProvide.iMax ); */ return pProperty; }
std::string XmlUtils::getStringAttribute(const TiXmlElement* element, const char* name, const std::string& def) { assertNotNull(element); TiXmlAttribute* attribute = element->GetAttribute(name); return (attribute ? attribute->ValueStr() : def); }
bool SimXMLLoader::loadFromXML( const std::string & filename, AgentInitializer * agentInit, bool verbose ) { // COnfirms file is // a) available for reading // b) valid xml // c) An "Experiment" if ( verbose ) logger << Logger::INFO_MSG << "Loading from xml: " << filename << "."; TiXmlDocument xml( filename ); bool loadOkay = xml.LoadFile(); if ( !loadOkay ) { // load xml file logger << Logger::ERR_MSG << "Could not load simulation configuration xml (" << filename << ") due to xml syntax errors.\n"; logger << "\t" << xml.ErrorDesc(); return false; } TiXmlElement* experimentNode = xml.RootElement(); if( ! experimentNode ) { logger << Logger::ERR_MSG << "Scene configuration (" << filename << ") does not contain a root element."; return false; } if( experimentNode->ValueStr () != "Experiment" ) { logger << Logger::ERR_MSG << "Scene configuration (" << filename << ")'s root element is not \"Experiment\"."; return false; } std::string absPath; os::path::absPath( filename, absPath ); std::string junk; os::path::split( absPath, _sceneFldr, junk ); logger << Logger::INFO_MSG << "Scene root: " << _sceneFldr << "."; bool commonDone = false; // common experiment parameters parsed bool targetDone = !_sim->hasExpTarget(); // target experiment parameters parsed bool spatialQueryDone = false; // spatial query must be finished before obstacles and agents can be created // Tags I'm not ready to parse - only parse agent sets and obstacles AFTER experiment // parameters std::list< TiXmlElement * > tagQueue; TiXmlElement* child; for( child = experimentNode->FirstChildElement(); child; child = child->NextSiblingElement()) { if ( child->ValueStr() == "Common" ) { // Currently the only "common" experiment parameter is the time step TiXmlAttribute * attr; for ( attr = child->FirstAttribute(); attr; attr = attr->Next() ) { try { if ( !_sim->setExpParam( attr->Name(), attr->ValueStr() ) ) { logger << Logger::WARN_MSG << "Unrecognized parameter in the global \"Common\" parameters (" << attr->Name() << ") on line " << child->Row() << "\n"; } } catch ( XMLParamException e ) { logger << Logger::ERR_MSG << e.what(); return false; } } commonDone = true; } else if ( child->ValueStr() == "AgentProfile" ) { if ( !parseAgentProfile( child, agentInit ) ) { return false; } } else if ( child->ValueStr() == "AgentGroup" ) { if ( ! ( commonDone || targetDone || spatialQueryDone ) ) { tagQueue.push_back( child ); } else { if ( !parseAgentGroup( child, agentInit ) ) { return false; } } } else if ( child->ValueStr() == "ObstacleSet" ) { if ( ! ( commonDone || targetDone || spatialQueryDone) ) { tagQueue.push_back( child ); } else { if ( ! parseObstacleSet( child ) ) { return false; } } } else if ( child->ValueStr() == "Elevation" ) { if ( _sim->hasElevation() ) { logger << Logger::ERR_MSG << "More than one elevation has been specified. Found redundant elevation specification on line " << child->Row() << "."; return false; } Elevation * elevation = ElevationDB::getInstance( child, _sceneFldr ); if ( elevation == 0x0 ) { logger << Logger::ERR_MSG << "Unable to instantiate elevation specifcation on line " << child->Row() << "."; return false; } else { _sim->setElevationInstance( elevation ); } Menge::ELEVATION = elevation; } else if ( child->ValueStr() == "SpatialQuery" ) { if ( _sim->hasSpatialQuery() ) { logger << Logger::ERR_MSG << "More than one spatial query implementation has been specified. Found redundant spatial query specification on line " << child->Row() << "."; return false; } SpatialQuery * spQuery = SpatialQueryDB::getInstance( child, _sceneFldr ); if ( spQuery == 0x0 ) { logger << Logger::ERR_MSG << "Unable to instantiate spatial query specifcation on line " << child->Row() << "."; return false; } else { _sim->setSpatialQuery( spQuery ); spatialQueryDone = true; } } else { // target parameter if ( !targetDone && _sim->isExpTarget( child->ValueStr() ) ) { // Parse the target TiXmlAttribute * attr; for ( attr = child->FirstAttribute(); attr; attr = attr->Next() ) { try { if ( ! _sim->setExpParam( attr->Name(), attr->ValueStr() ) ) { logger << Logger::WARN_MSG << "Unrecognized parameter in the global \"" << child->ValueStr() << "\" parameters (" << attr->Name() << ") on line " << child->Row() << "\n"; } } catch ( XMLParamException e ) { logger << Logger::ERR_MSG << e.what() << " (on line " << child->Row() << ")"; return false; } } targetDone = true; } } } if ( !targetDone || !commonDone || !spatialQueryDone) { logger << Logger::ERR_MSG << "Missing required experiment parameters: \n"; if ( !targetDone ) logger << "\tmodel simulation parameters "; if ( !commonDone ) logger << "\tcommon simulation parameters "; if ( !spatialQueryDone ) logger << "\tSpatial Query "; return false; } // Now parse any of the tags that were skipped while waiting for experiment configuration std::list< TiXmlElement * >::iterator tagItr = tagQueue.begin(); for ( ; tagItr != tagQueue.end(); ++tagItr ) { TiXmlElement * child = *tagItr; if ( child->ValueStr() == "AgentGroup" ) { if ( !parseAgentGroup( child, agentInit ) ) { return false; } } else if ( child->ValueStr() == "ObstacleSet" ) { if ( ! parseObstacleSet( child ) ) { return false; } } else { logger << Logger::ERR_MSG << "XML contains an invalid tag: " << child->ValueStr() << " on line " << child->Row() << "."; return false; } } if ( _agtCount == 0 ) { // TODO: Change this test when agent sources are introduced // in this case, it is possible to start with no agents and then add them // w.r.t. time. logger << Logger::ERR_MSG << "No agents defined in simulation."; return false; } // free up the profiles // TODO: I'll need to save these when I have AgentSources. for ( HASH_MAP< std::string, AgentInitializer * >::iterator itr = _profiles.begin(); itr != _profiles.end(); ++itr ) { delete itr->second; } _profiles.clear(); return _sim->initSpatialQuery(); }
//------------------------------------------------------------------- // Method: // Database::readXMLHelper(TiXmlNode* pParent, const unsigned int & pass) // // Used as a helper to recursively read in taxonomy of items from XML file. // // Arguments: // "pParent" Current node in taxonomy // "pass" the pass number: 1 is the first pass to count the number // of generalized items in the taxonomy; 2 is to read in the // taxonomy into memory (Tree G) //--------------------------------------------------------------------- void Database::readXMLHelper(TiXmlNode* pParent) { if ( !pParent ) return; TiXmlNode* pChild; int t = pParent->Type(); switch (t) { case TiXmlNode::TINYXML_ELEMENT: if (pParent->ValueStr() == "vertex") { TiXmlAttribute * pAttrib = pParent->ToElement()->FirstAttribute(); while (pAttrib) { if (pAttrib->NameTStr() == "value") { TaxVertex * newTaxVertex = new TaxVertex(atoi(pAttrib->Value())); if (pAttrib->ValueStr() == "-1") { G.setRoot(newTaxVertex); headerTable[atoi(pAttrib->Value())] = newTaxVertex; break; } //numGeneralizedItems++; //cout << pAttrib->Value() << endl; headerTable[atoi(pAttrib->Value())] = newTaxVertex; break; } pAttrib=pAttrib->Next(); } } else if (pParent->ValueStr() == "edge") { TiXmlAttribute * pAttrib = pParent->ToElement()->FirstAttribute(); TaxVertex * To; TaxVertex * From; while (pAttrib) { if (pAttrib->NameTStr() == "to" || pAttrib->NameTStr() == "from") { map<int, TaxVertex *>::iterator mapIt; mapIt = headerTable.find(atoi(pAttrib->Value())); //cout << pAttrib->ValueStr() << endl; if (mapIt != headerTable.end()) { if (pAttrib->NameTStr() == "to") { To = (*mapIt).second; } else { From = (*mapIt).second; } } } pAttrib=pAttrib->Next(); } From->addChild(To); } break; default: break; } for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) { readXMLHelper(pChild); } }