//----------------------------------------------------------------------------- bool datareader_c::read_model( component_ptr owner, xml_element_ptr top, std::string key ) { xml_attribute_ptr attribute = top->attribute( "id" ); if( !attribute ) return false; std::string name = attribute->get_value(); xml_element_ptr element; std::string tag; model_ptr model = model_ptr( new model_c() ); if( owner->component_type() == component_c::TRIAL ) { trial_ptr trial = boost::dynamic_pointer_cast<trial_c>( owner ); trial->models.push_back( model ); } else if( owner->component_type() == component_c::SOLUTION ) { solution_ptr solution = boost::dynamic_pointer_cast<solution_c>( owner ); solution->models.push_back( model ); } else { return false; } key = name; model->id = name; for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); tag = element->get_name(); if( tag == "Link" ) { read_link( model, element, key ); } else if( tag == "Joint" ) { read_joint( model, element, key ); } } return true; }
//----------------------------------------------------------------------------- bool datareader_c::read_joint( model_ptr owner, xml_element_ptr top, std::string key ) { xml_attribute_ptr attribute = top->attribute( "id" ); if( !attribute ) return false; std::string name = attribute->get_value(); xml_element_ptr element; std::string tag; joint_ptr joint = joint_ptr( new joint_c() ); component_ptr component = boost::dynamic_pointer_cast<component_c>( joint ); if( key != "" ) key += "::"; key += name; joint->id = name; for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); tag = element->get_name(); if( tag == "Field" ) { read_field( component, element, key ); } } owner->insert( joint ); return true; }
//----------------------------------------------------------------------------- bool datamap_c::map_field_element( xml_element_ptr element, unsigned& column, std::string parent_key ) { xml_attribute_ptr attribute; std::string key = ""; unsigned size = 1; // This method needs more flexibility. Parsing the field here makes it easy // to know size, but the hardcoding of field names limits the capability for( unsigned j = 0; j < element->attributes(); j++ ) { attribute = element->attribute( j ); if( attribute->get_name() == "name" ) { key += attribute->get_value(); if( key == "position" ) { size = 3; } else if( key == "rotation" ) { size = 4; } else if( key == "linear-velocity" ) { size = 3; } else if( key == "angular-velocity" ) { size = 3; } else if( key == "control" ) { size = 6; } if( parent_key != "" ) { key = parent_key + "::" + key; } _map.insert( std::pair<std::string,unsigned>( key, ++column ) ); column += (size - 1); break; } } return true; }
//----------------------------------------------------------------------------- bool xml_c::process_tixml_element( void* tixml_element, xml_element_ptr element ) { // TODO: robust error checking TiXmlElement* tixml_e = (TiXmlElement*) tixml_element; const char* pkey = tixml_e->Value(); const char* ptext = tixml_e->GetText(); element->set_name( pkey ); if( ptext ) element->set_value( ptext ); TiXmlAttribute* a = tixml_e->FirstAttribute(); while( a!= NULL ) { std::string name = a->Name(); std::string value = a->Value(); xml_attribute_ptr attrib = xml_attribute_ptr( new xml_attribute_c() ); attrib->set_name( a->Name() ); attrib->set_value( a->Value() ); element->append( attrib ); a = a->Next(); } TiXmlElement* tixml_child; for( tixml_child = tixml_e->FirstChildElement(); tixml_child != NULL; tixml_child = tixml_child->NextSiblingElement() ) { xml_element_ptr child = xml_element_ptr( new xml_element_c() ); element->append( child ); process_tixml_element( tixml_child, child ); } return true; }
//----------------------------------------------------------------------------- bool exporter_c::write_analyzer_file_element( xml_element_ptr parent ) { xml_element_ptr top, element; xml_attribute_ptr attribute; //unsigned column = 1; // create the file element as the top (root) of this hierarchy top = xml_element_ptr( new xml_element_c() ); top->set_name( "File" ); // specify the type attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "type" ); attribute->set_value( "analyzer" ); top->append( attribute ); // specify the file name attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "name" ); attribute->set_value( _analyzer_file ); top->append( attribute ); // add this hierarchy to the parent node parent->append( top ); return true; }
//----------------------------------------------------------------------------- bool exporter_c::write_trial_file_element( xml_element_ptr parent, trial_ptr ex_trial, std::string delimiter ) { xml_element_ptr top, element; xml_attribute_ptr attribute; unsigned column = 1; // create the file element as the top (root) of this hierarchy top = xml_element_ptr( new xml_element_c() ); top->set_name( "File" ); // specify the type attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "type" ); attribute->set_value( "trial" ); top->append( attribute ); // specify the file name attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "name" ); attribute->set_value( _trial_file ); top->append( attribute ); // specify a delimiter for the flat file attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "delimiter" ); attribute->set_value( delimiter ); top->append( attribute ); // add a time field element as a leaf of top add_field_element( top, "time", column ); // iterate over the models and add each component for( unsigned i = 0; i < ex_trial->models.size(); i++ ) { model_ptr model = ex_trial->models[i]; element = xml_element_ptr( new xml_element_c() ); element->set_name( "Model" ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "id" ); attribute->set_value( model->id ); element->append( attribute ); for( unsigned j = 0; j < model->links.size(); j++ ) { link_ptr link = model->links[j]; write_link_element( element, link, column ); } for( unsigned j = 0; j < model->joints.size(); j++ ) { joint_ptr joint = model->joints[j]; write_joint_element( element, joint, column ); } top->append( element ); } // add this hierarchy to the parent node parent->append( top ); // generate the column map _trial_column_map->map_trial_element( top ); return true; }
//----------------------------------------------------------------------------- bool exporter_c::add_column_attribute( xml_element_ptr element, unsigned& column, unsigned size ) { #ifdef DEFINE_COLUMNS xml_attribute_ptr attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "column" ); attribute->set_value( column ); element->append( attribute ); column += size; #endif return true; }
//----------------------------------------------------------------------------- bool exporter_c::write_scenario_element( xml_element_ptr parent, scenario_ptr scenario, analyzer_ptr analyzer, trial_ptr trial, solution_ptr solution, std::string delimiter ) { xml_c xml; xml_element_ptr top, element; xml_attribute_ptr attribute; assert( analyzer ); // warning supression top = xml_element_ptr( new xml_element_c() ); top->set_name( "Scenario" ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "id" ); attribute->set_value( scenario->id ); top->append( attribute ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "package-id" ); attribute->set_value( scenario->package_id ); top->append( attribute ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "sample-rate" ); attribute->set_value( scenario->sample_rate ); top->append( attribute ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "sample-start-time" ); attribute->set_value( scenario->sample_start_time ); top->append( attribute ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "sample-end-time" ); attribute->set_value( scenario->sample_end_time ); top->append( attribute ); element = xml_element_ptr( new xml_element_c() ); element->set_name( "Description" ); element->set_value( scenario->description ); top->append( element ); for( unsigned i = 0; i < scenario->uris.size(); i++ ) { element = xml_element_ptr( new xml_element_c() ); element->set_name( "URI" ); element->set_value( scenario->uris[i] ); top->append( element ); } write_analyzer_file_element( top ); write_trial_file_element( top, trial, delimiter ); write_solution_file_element( top, solution, delimiter ); parent->append( top ); return true; }
//----------------------------------------------------------------------------- bool xml_c::build_tixml_element( void* tixml_element, xml_element_ptr element ) { // TODO: robust error checking TiXmlElement* tixml_e = (TiXmlElement*) tixml_element; std::string value = element->get_value(); if( value != "" ) tixml_e->LinkEndChild( new TiXmlText( value ) ); for( unsigned i = 0; i < element->attributes(); i++ ) { xml_attribute_ptr attrib = element->attribute( i ); tixml_e->SetAttribute( attrib->get_name(), attrib->get_value() ); } for( unsigned i = 0; i < element->elements(); i++ ) { xml_element_ptr child = element->element( i ); TiXmlElement* tixml_child = new TiXmlElement( child->get_name() ); tixml_e->LinkEndChild( tixml_child ); build_tixml_element( tixml_child, child ); } return true; }
//----------------------------------------------------------------------------- bool datamap_c::map_model_element( xml_element_ptr top, unsigned& column ) { xml_element_ptr element; xml_attribute_ptr attribute; std::string key = ""; for( unsigned i = 0; i < top->attributes(); i++ ) { attribute = top->attribute( i ); if( attribute->get_name() == "id" ) { key = attribute->get_value(); break; } } for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); if( element->get_name() == "Link" ) map_link_element( element, column, key ); else if( element->get_name() == "Joint" ) map_joint_element( element, column, key ); } return true; }
//----------------------------------------------------------------------------- bool datamap_c::map_link_element( xml_element_ptr top, unsigned& column, std::string parent_key ) { xml_element_ptr element; xml_attribute_ptr attribute; std::string key = ""; for( unsigned i = 0; i < top->attributes(); i++ ) { attribute = top->attribute( i ); if( attribute->get_name() == "id" ) { key = attribute->get_value(); break; } } if( parent_key != "" ) { key = parent_key + "::" + key; } for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); if( element->get_name() == "Field" ) map_field_element( element, column, key ); } return true; }
//----------------------------------------------------------------------------- bool datareader_c::read( solution_ptr& solution, std::string scenario_id, xml_element_ptr top ) { if( !buffer_line() ) return false; //print_cells(); solution = solution_ptr( new solution_c( solution_c::MODEL ) ); solution->scenario_id = scenario_id; component_ptr component = boost::dynamic_pointer_cast<solution_c>( solution ); xml_element_ptr element; std::string key, tag; for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); tag = element->get_name(); if( tag == "Field" ) { read_field( component, element ); } else if( tag == "Model" ) { read_model( component, element ); } } return true; }
//----------------------------------------------------------------------------- bool datareader_c::read( trial_ptr& trial, std::string scenario_id, xml_element_ptr top ) { if( !buffer_line() ) return false; //print_cells(); trial = trial_ptr( new trial_c() ); trial->scenario_id = scenario_id; component_ptr component = boost::dynamic_pointer_cast<trial_c>( trial ); xml_element_ptr element; std::string key, name; for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); name = element->get_name(); if( name == "Field" ) { read_field( component, element ); } else if( name == "Model" ) { read_model( component, element ); } } return true; }
//----------------------------------------------------------------------------- bool exporter_c::add_field_element( xml_element_ptr parent, std::string name, unsigned& column, unsigned size ) { xml_element_ptr element; xml_attribute_ptr attribute; element = xml_element_ptr( new xml_element_c() ); element->set_name( "Field" ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "name" ); attribute->set_value( name ); element->append( attribute ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "size" ); attribute->set_value( size ); element->append( attribute ); add_column_attribute( element, column, size ); parent->append( element ); return true; }
//----------------------------------------------------------------------------- bool exporter_c::write_joint_element( xml_element_ptr parent, joint_ptr joint, unsigned& column, bool write_controls ) { xml_element_ptr top, element; xml_attribute_ptr attribute; top = xml_element_ptr( new xml_element_c() ); top->set_name( "Joint" ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "id" ); attribute->set_value( joint->id ); top->append( attribute ); add_field_element( top, "position", column, joint->state.size_q() ); add_field_element( top, "velocity", column, joint->state.size_dq() ); if( write_controls && joint->control.size() ) add_field_element( top, "control", column, joint->control.size() ); parent->append( top ); return true; }
//----------------------------------------------------------------------------- bool exporter_c::write_link_element( xml_element_ptr parent, link_ptr link, unsigned& column ) { xml_element_ptr top, element; xml_attribute_ptr attribute; top = xml_element_ptr( new xml_element_c() ); top->set_name( "Link" ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "id" ); attribute->set_value( link->id ); top->append( attribute ); add_field_element( top, "position", column, 3 ); add_field_element( top, "rotation", column, 4 ); add_field_element( top, "linear-velocity", column, 3 ); add_field_element( top, "angular-velocity", column, 3 ); parent->append( top ); return true; }
//----------------------------------------------------------------------------- bool datareader_c::read_field( component_ptr owner, xml_element_ptr top, std::string key ) { xml_attribute_ptr attribute = top->attribute( "name" ); if( !attribute ) return false; std::string name = attribute->get_value(); // printf( "name: %s\n", name.c_str() ); unsigned size = 1; if( key != "" ) key += "::"; key += name; unsigned column = _column_map->column( key ); //printf( "key[%s], column[%u]\n", key.c_str(), column ); if( owner->component_type() == component_c::TRIAL ) { trial_ptr trial = boost::dynamic_pointer_cast<trial_c>( owner ); if( name == "time" ) { trial->t = _cells[column-1]; return true; } } else if( owner->component_type() == component_c::SOLUTION ) { solution_ptr solution = boost::dynamic_pointer_cast<solution_c>( owner ); if( name == "time" ) { solution->t = _cells[column-1]; return true; } else if( name == "real-time" ) { solution->real_time = _cells[column-1]; return true; } } else if( owner->component_type() == component_c::LINK ) { link_ptr link = boost::dynamic_pointer_cast<link_c>( owner ); if( name == "position" ) { size = 3; link->state.linear_x( _cells[column-1] ); link->state.linear_y( _cells[column] ); link->state.linear_z( _cells[column+1] ); return true; } else if( name == "rotation" ) { size = 4; link->state.angular_x( _cells[column-1] ); link->state.angular_y( _cells[column] ); link->state.angular_z( _cells[column+1] ); link->state.angular_w( _cells[column+2] ); return true; } else if( name == "linear-velocity" ) { size = 3; link->state.linear_dx( _cells[column-1] ); link->state.linear_dy( _cells[column] ); link->state.linear_dz( _cells[column+1] ); return true; } else if( name == "angular-velocity" ) { size = 3; link->state.angular_dx( _cells[column-1] ); link->state.angular_dy( _cells[column] ); link->state.angular_dz( _cells[column+1] ); return true; } } else if( owner->component_type() == component_c::JOINT ) { joint_ptr joint = boost::dynamic_pointer_cast<joint_c>( owner ); if( name == "control" ) { size = 6; for( unsigned i = 0; i < size; i++ ) { unsigned cell = column - 1 + i; joint->control[i] = _cells[cell]; } return true; } } return false; }
//----------------------------------------------------------------------------- bool exporter_c::write_analyzer_element( xml_element_ptr parent, analyzer_ptr analyzer ) { xml_c xml; xml_element_ptr top, element; xml_attribute_ptr attribute; // Note: really only should have to specify the filename for scenario // generation. The rest of the record needs to be exported in a separate // framework as the server needs to validate the pathing and the typing // and it requires a compilation step on the server anyway. top = xml_element_ptr( new xml_element_c() ); top->set_name( "Analyzer" ); // specify the scenario attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "id" ); attribute->set_value( analyzer->scenario_id ); top->append( attribute ); // specify the type attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "type" ); if( analyzer->type == Reveal::Core::analyzer_c::PLUGIN ) attribute->set_value( "plugin" ); else attribute->set_value( "script" ); top->append( attribute ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "source-path" ); attribute->set_value( analyzer->source_path ); top->append( attribute ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "build-path" ); attribute->set_value( analyzer->build_path ); top->append( attribute ); attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "build-target" ); attribute->set_value( analyzer->build_target ); top->append( attribute ); // iterate over any keys and write them as a new element for( unsigned i = 0; i < analyzer->keys.size(); i++ ) { element = xml_element_ptr( new xml_element_c() ); element->set_name( "Datum" ); // write out any keys attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "key" ); attribute->set_value( analyzer->keys[i] ); element->append( attribute ); if( analyzer->keys.size() == analyzer->labels.size() ) { // if there are the same number of keys and labels, write out labels too attribute = xml_attribute_ptr( new xml_attribute_c() ); attribute->set_name( "label" ); attribute->set_value( analyzer->labels[i] ); element->append( attribute ); } top->append( element ); } parent->append( top ); return true; }