// ---------------------------------------------------------------------- void CreateDynamicEdgesTransmissionRangeTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); GroupElement* all_edges = new GroupElement("all.edges"); visualization_w().add_element(all_edges); std::string pref = sc.environment(). optional_string_param("prefix",DrawableEdgeDynamic::PREFIX); std::string node_prefix = sc.environment(). optional_string_param("node_prefix",DrawableNodeDefault::PREFIX); std::string _source_regex = sc.environment().optional_string_param("source_regex", ".*"); std::string _target_regex = sc.environment().optional_string_param("target_regex", ".*"); const shawn::Node &dummy = *(visualization().world().begin_nodes()); std::cout << "Regex: " << _source_regex << std::endl; DrawableEdgeDynamicTransmissionRange* ded = new DrawableEdgeDynamicTransmissionRange(dummy,dummy, pref, node_prefix, _source_regex, _target_regex); ded->init(); visualization_w().add_element(ded); all_edges->add_element(*ded); }
// ---------------------------------------------------------------------- void CreateEstimatedEdgesTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); std::string pref = sc.environment(). optional_string_param("prefix",DrawableEdgeEstimated::PREFIX); std::string node_prefix = sc.environment(). optional_string_param("node_prefix",DrawableNodeDefault::PREFIX); GroupElement* grp = group(sc); for( shawn::World::const_node_iterator it = visualization().world().begin_nodes(), endit = visualization().world().end_nodes(); it != endit; ++it ) { if( it->has_est_position()) { const DrawableNode* dn = drawable_node(*it, node_prefix); DrawableEdgeEstimated* dee = new DrawableEdgeEstimated(*it, *dn, DrawableEdgeEstimated::PREFIX); dee->init(); visualization_w().add_element(dee); if( grp != NULL ) grp->add_element(*dee); } } }
AutoElements:: AutoElements( shawn::SimulationController& sc, Visualization& vis ) throw( std::runtime_error ) : current_ ( NULL ) #ifdef HAVE_BOOST_REGEX , regex_ ( NULL ) #endif { std::string parm; tag_ = sc.environment().optional_string_param("tag", ""); parm=sc.environment().optional_string_param("elem",""); if( !parm.empty() ) { mode_=Single; current_ = vis.element_w(parm); if( current_.is_null() ) throw std::runtime_error(std::string("no such element: ") + parm ); return; } parm=sc.environment().optional_string_param("elem_regex",""); std::string tagregex = sc.environment().optional_string_param("tag_regex",""); if( !parm.empty() || (!tagregex.empty() && !tag_.empty())) { #ifdef HAVE_BOOST_REGEX if(tag_.empty() || tagregex.empty()) mode_=Regex; else mode_=TagRegex; try { if(mode_==Regex) regex_= new boost::regex(parm); else regex_= new boost::regex(tagregex); } catch(...) { throw std::runtime_error(std::string("regular expression is bad")); } vis_cur_=vis.elements().begin(); vis_end_=vis.elements().end(); advance_infeasible(); return; #else throw std::runtime_error(std::string("no regexp support compiled in")); #endif } throw std::runtime_error(std::string("specify elements (either $elem or $elem_regex)")); }
// ---------------------------------------------------------------------- void ConstantElevationTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { double val = sc.environment().required_double_param("height"); std::string n = sc.environment().optional_string_param("name",""); elevation_keeper_w(sc).add( new ConstantElevation(val,n) ); }
// ---------------------------------------------------------------------- void XYZFileElevationTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { std::string fn = sc.environment().required_string_param("file"); std::string outn = sc.environment().required_string_param("name"); std::string xyplacen = sc.environment().optional_string_param("xy_placement",KEY_SCALE); std::string zplacen = sc.environment().optional_string_param("z_placement",KEY_SCALE); bool xyplace_box; bool zplace_box; if( xyplacen == KEY_BOX ) xyplace_box = true; else if( xyplacen==KEY_SCALE ) xyplace_box = false; else throw runtime_error(string("$xy_placement must be ")+KEY_SCALE+string(" or ")+KEY_BOX); if( zplacen == KEY_BOX ) zplace_box = true; else if( zplacen==KEY_SCALE ) zplace_box = false; else throw runtime_error(string("$z_placement must be ")+KEY_SCALE+string(" or ")+KEY_BOX); XYZFile* data = new XYZFile; Vec base_point; Vec scale_vec; XYZFileElevation* elev; try { data->read(fn); if( (data->x_dimension()<2) || (data->y_dimension()<2) ) { ostringstream oss; oss << "XYZ data must have at least 2x2 grid points, but " << fn << " just contains " << data->x_dimension() << "x" << data->y_dimension(); throw runtime_error(oss.str()); } if( xyplace_box ) xy_place_box(sc,*data,base_point,scale_vec); else xy_place_scale(sc,*data,base_point,scale_vec); if( zplace_box ) z_place_box(sc,*data,base_point,scale_vec); else z_place_scale(sc,*data,base_point,scale_vec); elev = new XYZFileElevation(data, base_point, scale_vec.x(),scale_vec.y(),scale_vec.z(), outn); } catch( runtime_error& ) { delete data; throw; } elevation_keeper_w(sc).add( elev ); }
// ---------------------------------------------------------------------- void XYZFileElevationTask:: z_place_scale( shawn::SimulationController& sc, const XYZFile& f, shawn::Vec& bp, shawn::Vec& s ) throw( std::runtime_error ) { bp = Vec( bp.x(), bp.y(), sc.environment().optional_double_param("z_base",0.0) ); s = Vec( s.x(), s.y(), sc.environment().optional_double_param("z_scale",1.0) ); }
// -------------------------------------------------------------------- bool TestbedServiceServer:: start_server( const shawn::SimulationController& sc ) { host_ = sc.environment().required_string_param( "testbedservice_server_host" ); port_ = sc.environment().required_int_param( "testbedservice_server_port" ); TestbedServiceServer::WorkerThread::wsdl_path = sc.environment().required_string_param( "testbedservice_wsdl" ); runner_ = new boost::thread( boost::bind( &WorkerThread::run, &worker_, host_, port_ ) ); return true; }
// ---------------------------------------------------------------------- void CreateEdgesTagTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); #ifndef HAVE_BOOST_REGEX throw std::runtime_error("no boost::regex support compiled in"); #else boost::regex sources(sc.environment().required_string_param("source_regex")); boost::regex targets(sc.environment().required_string_param("target_regex")); std::string taglabel = sc.environment().required_string_param("tag"); std::string pref = sc.environment(). optional_string_param("prefix",DrawableEdgeDefault::PREFIX); std::string node_prefix = sc.environment(). optional_string_param("node_prefix",DrawableNodeDefault::PREFIX); GroupElement* grp = group(sc); for( shawn::World::const_node_iterator it = visualization().world().begin_nodes(), endit = visualization().world().end_nodes(); it != endit; ++it ) { if( boost::regex_search(get_stringtag(&*it,taglabel)->value(),sources)) { for( shawn::Node::const_adjacency_iterator ait = it->begin_adjacent_nodes(), endait = it->end_adjacent_nodes(); ait != endait; ++ait ) if( *it != *ait ) if( boost::regex_search(get_stringtag(&*ait,taglabel)->value(),targets) ) if( (ait->label() > it->label()) || (!boost::regex_search(get_stringtag(&*it,taglabel)->value(),targets)) || (!boost::regex_search(get_stringtag(&*ait,taglabel)->value(),sources)) ) { const DrawableNode* dsrc = drawable_node(*it,node_prefix); const DrawableNode* dtgt = drawable_node(*ait,node_prefix); DrawableEdgeDefault* ded = new DrawableEdgeDefault(*it,*ait,*dsrc,*dtgt,pref); ded->init(); visualization_w().add_element(ded); if( grp != NULL ) grp->add_element(*ded); } } } #endif }
// ---------------------------------------------------------------------- void XYZFileElevationTask:: xy_place_scale( shawn::SimulationController& sc, const XYZFile& f, shawn::Vec& bp, shawn::Vec& s ) throw( std::runtime_error ) { double scale = sc.environment().optional_double_param("xy_scale",1.0); bp = Vec( sc.environment().optional_double_param("x1",0.0), sc.environment().optional_double_param("y1",0.0), bp.z() ); s = Vec( sc.environment().optional_double_param("x_scale",scale), sc.environment().optional_double_param("y_scale",scale), s.z() ); }
// ---------------------------------------------------------------------- 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 SpyglassTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { std::string spyglass_outfile = sc.environment().required_string_param("spyglass_file"); Spyglass::set_spyglass(spyglass_outfile); }
// ---------------------------------------------------------------------- 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 RectangleTopologyTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { double x1 = sc.environment().required_double_param("x1"); double x2 = sc.environment().required_double_param("x2"); double y1 = sc.environment().required_double_param("y1"); double y2 = sc.environment().required_double_param("y2"); std::string n = sc.environment().optional_string_param("name",""); if( x1 > x2 ) throw std::runtime_error("rectangle has negative X size"); if( y1 > y2 ) throw std::runtime_error("rectangle has negative Y size"); topology_keeper_w(sc).add( new RectangleTopology(Box(Vec(x1,y1,0.0), Vec(x2,y2,0.0)), n) ); }
// ---------------------------------------------------------------------- ConstPropertyHandle PropertyConstantVecTask:: create_property( shawn::SimulationController& sc ) throw( std::runtime_error ) { double x = sc.environment().required_double_param("x"); double y = sc.environment().required_double_param("y"); double z = sc.environment().optional_double_param("z",0.0); PropertyConstantVec* pc = new PropertyConstantVec(shawn::Vec(x,y,z)); try { fill_default_params(*pc,sc); } catch( std::runtime_error& ) { delete pc; throw; } return pc; }
// ---------------------------------------------------------------------- void XMLPolygonTopologyTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { std::string f = sc.environment().required_string_param("file"); std::string n = sc.environment().required_string_param("name"); bool create_outer = sc.environment().optional_bool_param("xml_create_outer_hull", false) || sc.environment().optional_bool_param("create_outer_hull", false); bool fix_non_simple_polygons = sc.environment().optional_bool_param("fix_non_simple_polygons", false); XMLPolygonTopology* p = new XMLPolygonTopology; p->set_name(n); p->read(sc, f, create_outer, fix_non_simple_polygons); topology_keeper_w(sc).add(p); }
// ---------------------------------------------------------------------- void CreateGroupTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); GroupElement* ge = new GroupElement( sc.environment().required_string_param("group") ); ge->init(); visualization_w().add_element( ge ); }
// ---------------------------------------------------------------------- void CuboidTopologyTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { double x1 = sc.environment().required_double_param("x1"); double x2 = sc.environment().required_double_param("x2"); double y1 = sc.environment().required_double_param("y1"); double y2 = sc.environment().required_double_param("y2"); double z1 = sc.environment().required_double_param("z1"); double z2 = sc.environment().required_double_param("z2"); std::string n = sc.environment().optional_string_param("name",""); if( x1 > x2 ) throw std::runtime_error("cuboid has negative X size"); if( y1 > y2 ) throw std::runtime_error("cuboid has negative Y size"); if( z1 > z2 ) throw std::runtime_error("cuboid has negative Z size"); topology_keeper_w(sc).add( new CuboidTopology(Box(Vec(x1,y1,z1), Vec(x2,y2,z2)), n) ); }
// ---------------------------------------------------------------------- void SingleSnapshotTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); double t = sc.environment().optional_double_param("time",0.0); int dr = sc.environment().optional_int_param("draft",0); WriterFactoryHandle wfh = sc.keeper_by_name_w<WriterKeeper>("WriterKeeper") ->find_w(sc.environment().optional_string_param("writer", "pdf")); Writer* wr = wfh->create(); wr->set_draft(dr); wr->pre_write( visualization(), sc.environment().optional_string_param("filename", "snapshot"), false ); wr->write_frame( t ); wr->post_write(); delete wr; }
// ---------------------------------------------------------------------- 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 CreateGraphicsTask:: run( shawn::SimulationController& sc ) throw( std::runtime_error ) { VisualizationTask::run(sc); DrawableGraphics* dg = new DrawableGraphics( sc.environment().required_string_param("name"), sc.environment().required_string_param("file") ); dg->init(); visualization_w().add_element( dg ); }
// ---------------------------------------------------------------------- void LatticePointGenerator:: pre_generate( shawn::SimulationController& sc, reading::ConstBoolReadingHandle brh ) throw( std::runtime_error ) { PointGenerator::pre_generate(sc,brh); spacing_ = (float) sc.environment().optional_double_param("spacing", 1.0); bbox_=extract_box(brh); check_fail_count_=true; cur_x_ = cur_y_ = cur_z_ = 0; // mycount = 0; }
// ---------------------------------------------------------------------- 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 }
// ---------------------------------------------------------------------- void DefaultNodeGenerator:: init_processor_factories( shawn::SimulationController& sc, ProcessorFactoryList& pfl ) throw( std::runtime_error ) { shawn::StrTok tok( sc.environment().optional_string_param( "processors", "" ), ", " ); for ( shawn::StrTok::iterator it = tok.begin(), end = tok.end(); it != end; ++it ) { shawn::ProcessorFactoryHandle pfh = sc.processor_keeper_w().find_w( *it ); assert( pfh != NULL ); // not found throws... pfl.push_back( pfh ); } }
// ---------------------------------------------------------------------- ConstPropertyHandle PropertyIndexedTagColorVecTask:: create_property( shawn::SimulationController& sc ) throw( std::runtime_error ) { //if( !sc.environment().optional_string_param("node","").empty() ) // { ABORT_NOT_IMPLEMENTED; } std::string tagname = sc.environment().required_string_param("dynamictag"); PropertyIndexedTagColorVec* rtc = new PropertyIndexedTagColorVec(NULL, tagname); try { fill_default_params(*rtc,sc); } catch( std::runtime_error& ) { delete rtc; throw; } return rtc; }
// ---------------------------------------------------------------------- ConstPropertyHandle PropertySmoothVecTask:: create_property( shawn::SimulationController& sc ) throw( std::runtime_error ) { shawn::Vec val1( sc.environment().required_double_param("start_x"), sc.environment().required_double_param("start_y"), sc.environment().optional_double_param("start_z",0.0) ); shawn::Vec val2( sc.environment().required_double_param("end_x"), sc.environment().required_double_param("end_y"), sc.environment().optional_double_param("end_z", 0.0) ); double tim1 = param_start(sc); double tim3 = param_end(sc); double tim2 = sc.environment().optional_double_param("reach_time",tim3); Transition tr = transition(sc.environment().required_string_param("transition")); PropertySmoothVec* pc = new PropertySmoothVec(val1,val2,tim2,tr); pc->set_start( tim1 ); pc->set_end( tim3 ); pc->set_priority( param_prio(sc) ); return pc; }