//----------------------------------------------------------------------------- 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::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::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 xml_c::read( std::string filename ) { // TODO: robust error checking TiXmlDocument doc; doc = TiXmlDocument( filename ); bool result; result = doc.LoadFile(); if( !result ) { return false; } TiXmlHandle tixml_hdoc( &doc ); TiXmlHandle tixml_root( 0 ); TiXmlElement* tixml_e; TiXmlHandle tixml_h( 0 ); tixml_e = tixml_hdoc.FirstChildElement().Element(); tixml_root = TiXmlHandle( tixml_e ); _root = xml_element_ptr( new xml_element_c() ); process_tixml_element( (void*)tixml_e, _root ); 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( scenario_ptr scenario, analyzer_ptr analyzer, solution_ptr ex_solution, trial_ptr ex_trial, std::string delimiter ) { xml_c xml; xml_element_ptr root; bool result; std::stringstream ss_scenario_file, ss_trial_file, ss_solution_file, ss_analyzer_file; ss_scenario_file << scenario->id << ".scenario"; _scenario_file = ss_scenario_file.str(); ss_trial_file << scenario->id << ".trials"; _trial_file = ss_trial_file.str(); ss_solution_file << scenario->id << ".solutions"; _solution_file = ss_solution_file.str(); ss_analyzer_file << scenario->id << ".analyzer"; _analyzer_file = ss_analyzer_file.str(); // create the xml root for the scenario file root = xml_element_ptr( new xml_element_c() ); root->set_name( "Reveal" ); // create column maps for the trial and solution data _trial_column_map = datamap_ptr( new datamap_c() ); _solution_column_map = datamap_ptr( new datamap_c() ); // write the scenario information to the root result = write_scenario_element( root, scenario, analyzer, ex_trial, ex_solution, delimiter ); if( !result ) return result; // write the scenario xml file xml.root( root ); xml.write( _scenario_file ); // write the analyzer file result = write( _analyzer_file, analyzer ); if( !result ) return false; //_trial_column_map->print(); //_solution_column_map->print(); // open the trial datawriter to append trial data to a flat file _trial_datawriter = Reveal::Core::datawriter_c( _trial_file, delimiter, _trial_column_map ); result = _trial_datawriter.open(); if( !result ) return false; // open the solution datawriter to append solution data to a flat file _solution_datawriter = Reveal::Core::datawriter_c( _solution_file, delimiter, _solution_column_map ); result = _solution_datawriter.open(); if( !result ) return false; return true; }
//----------------------------------------------------------------------------- bool exporter_c::write( std::string analyzer_file, analyzer_ptr analyzer ) { xml_c xml; xml_element_ptr root; root = xml_element_ptr( new xml_element_c() ); root->set_name( "Reveal" ); write_analyzer_element( root, analyzer ); xml.root( root ); xml.write( analyzer_file ); 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 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; }