// ---------------------------------------------------------------------- void TreeCreationTask:: create_tree_from_file( shawn::SimulationController& sc, routing::tree::TreeRouting& routing_instance, const std::string& filename ) throw() { double now = sc.world().current_time(); ifstream file_in; file_in.open(filename.c_str(),ifstream::in); if(!file_in) { ERROR(this->logger(),"Unable to open file " + filename + "!"); abort(); } string buf; while( getline(file_in,buf) ) { StrTok tok(buf,"\t "); double line[6]; int i = 0; for(StrTok::iterator it = tok.begin(); it != tok.end(); ++it) { line[i++] = conv_string_to_double(*it); } shawn::Node* node = sc.world_w().find_node_by_id_w( (int)line[0] ); shawn::Node* sink = sc.world_w().find_node_by_id_w( (int)line[1] ); shawn::Node* neighbor = sc.world_w().find_node_by_id_w( (int)line[2] ); int hops_to_sink = (int)line[3]; double node_x = line[4]; double node_y = line[5]; double node_real_pos_x = conv_string_to_double(conv_double_to_string(node->real_position().x())); double node_real_pos_y = conv_string_to_double(conv_double_to_string(node->real_position().y())); if( !( node && sink && neighbor ) ) { ERROR(this->logger(),"Error while reading file!"); abort(); } if( node_x != node_real_pos_x || node_y != node_real_pos_y ) { ERROR(this->logger(),"Position mismatch! Read x: " + conv_double_to_string(node_x) + ", y: " + conv_double_to_string(node_y) + " Real position x: " + conv_double_to_string(node_real_pos_x) + ", y: " + conv_double_to_string(node_real_pos_y)); abort(); } routing_instance.tree_routing_table_update(*node,*sink,TreeRoutingTableInfo(*neighbor,hops_to_sink,now)); } INFO(this->logger(),"Reading from file: " + filename + " succeeded!"); }
// ---------------------------------------------------------------------- void TreeCreationTask:: create_tree_in_file( shawn::SimulationController& sc, const std::string& filename, const shawn::Node& sink, int max_hops ) throw() { // Avoid appending bool append = sc.environment().optional_bool_param("append",false); if(!append) { remove( filename.c_str() ); } // Init stuff // double now = sink.current_time(); NodesToExamineMap nodes_to_examine; TreeCreationHopsToSinkResult& result = *( new TreeCreationHopsToSinkResult( sc.world_w() ) ); ofstream file_op(filename.c_str(),ios::app); // Treat the sink file_op << sink.id() << "\t" << sink.id() << "\t" << sink.id() << "\t" << 0 << "\t" << sink.real_position().x() << "\t" << sink.real_position().y() << endl; result[sink].hops_ = 0; nodes_to_examine.insert( NodesToExamineMapValueType(0,&sink) ); // Main loop while( ! nodes_to_examine.empty() ) { NodesToExamineMapIterator min_it = nodes_to_examine.begin(); const shawn::Node* min_node = min_it->second; nodes_to_examine.erase( min_it ); for( World::const_adjacency_iterator adj_it = min_node->begin_adjacent_nodes(); adj_it != min_node->end_adjacent_nodes(); ++adj_it ) { int hops = result[*min_node].hops_ + 1; if( hops < result[*adj_it].hops_ ) { assert( result[*adj_it].hops_ == INT_MAX ); result[*adj_it].hops_ = hops; file_op << adj_it->id() << "\t" << sink.id() << "\t" << min_node->id() << "\t" << hops << "\t" << adj_it->real_position().x() << "\t" << adj_it->real_position().y() << endl; if( hops < max_hops ) { nodes_to_examine.insert( NodesToExamineMapValueType( hops, &(*adj_it) ) ); } } } } file_op.clear(); file_op.close(); delete &result; }
// ---------------------------------------------------------------------- void TreeCreationTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { require_world( sc ); bool tree_routing_set = false; bool filename_set = false; string tree_routing_instance = sc.environment().optional_string_param("routing_instance","",&tree_routing_set); string filename = sc.environment().optional_string_param("tree_creation_filename","",&filename_set); TreeRouting* routing_instance = NULL; if( tree_routing_set ) { RoutingBaseHandle rbh = routing::routing_keeper_w(sc).find_w(tree_routing_instance); routing_instance = dynamic_cast<TreeRouting*>( rbh.get() ); if( ! routing_instance ) { ERROR(this->logger(),"The given routing is no TreeRouting!"); abort(); } routing_instance->init( sc.world_w() ); } if( tree_routing_set && filename_set ) { create_tree_from_file(sc,*routing_instance,filename); return; } int sink_id = sc.environment().required_int_param("sink_id"); const Node* sink = sc.world().find_node_by_id( sink_id ); if( tree_routing_set && ( ! filename_set ) ) { create_tree_in_tree_routing( *routing_instance, *sink ); return; } if( ( ! tree_routing_set ) && filename_set ) { int max_hops = sc.environment().optional_int_param("max_hops",INT_MAX); create_tree_in_file( sc, filename, *sink, max_hops ); return; } }
// ---------------------------------------------------------------------- void ExternalAnimationTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); std::string file_name = sc.environment().optional_string_param( "filename", "animation"); double refresh_interval = sc.environment().optional_double_param( "refresh_interval", 1.0); std::string writer_type = sc.environment().optional_string_param("writer", "png"); double event_time = sc.world().scheduler().current_time(); sc.world_w().scheduler_w().new_event(*new WriteAnimationFrameEvent( refresh_interval, visualization_w(), writer_type, file_name, sc ), event_time, NULL); }
// ---------------------------------------------------------------------- void VisualizationTaskCreate:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { Visualization* vis; require_world(sc); std::string name = sc.environment().optional_string_param( "vis", "visualization" ); visualization_keeper_w(sc).add(vis=new Visualization(name)); std::string type = sc.environment().optional_string_param( "node_type", "default" ); DrawableNodeFactoryHandle dnfh = sc.keeper_by_name_w<DrawableNodeKeeper>("DrawableNodeKeeper") ->find_w(sc.environment().optional_string_param("drawable_nodes", type)); sc.world_w().add_node_change_listener(*vis); vis->set_world( sc.world() ); vis->init(); DEBUG( logger(), "created visualization '" << name << "'" ); GroupElement* ge = new GroupElement( "all.nodes" ); ge->init(); vis->add_element(ge); for( shawn::World::const_node_iterator it = sc.world().begin_nodes(); it != sc.world().end_nodes(); ++it ) { DrawableNode *dn = dnfh->create(*it); dn->init(); vis->add_element(dn); ge->add_element(*dn); } }
// ---------------------------------------------------------------------- void CreateLiveviewTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); // Uses the resolution of the vis camera as texture resolution: int texwidth = (int)visualization().camera().width(0.0); int texheight = (int)visualization().camera().height(0.0); // Fit the window size to the configured camera resolution: int width = sc.environment().optional_int_param("winwidth", texwidth + 10); int height = sc.environment().optional_int_param("winheight", texwidth + 10); // Refresh interval in Shaun time (once every simulation round by // default): double refresh_interval = sc.environment().optional_double_param( "refresh_interval", 1.0); // Minimum interval delay in milliseconds (to make a fast simulations // visible; 0 by default): int refresh_delay = sc.environment().optional_int_param("refresh_delay", 0); #ifdef HAVE_BOOST // Creates the external window: createWindow(width, height, texwidth, texheight); // Adds the Liveview refresh event to Shauns event scheduler: double event_time = sc.world().scheduler().current_time(); sc.world_w().scheduler_w().new_event(*new RefreshLiveviewEvent( refresh_interval, refresh_delay, visualization_w()), event_time, NULL); #endif }