// recursive depth-first handling all paths in Behavior graph // BHorig - original behavior graph // cur_v - current vertex in original graph // lo_vec - vector of lorders // BHnew - new behavior graph containign only paths with correct // lorders from lo_vec // trace - prefix of trace, trace corresponds the path // from "root" of BHorig to current vertex cur_v, including cur_v // return - true if vertex cur_v was added to BHnew, i.e. // iff path has correct lorder bool handle_path(const Behavior& BHorig, const BGVertex& cur_v, const vector<LOrder>& lo_vec, Behavior& BHnew, const Trace& trace, BGVertex& new_v) { //cout<<"prefix trace: ";debugPrint(trace);cout<<"\n"; // get out_edges of cur_v BGOutEdgeIt out_i, out_end; BGEdge e; tie(out_i,out_end) = BHorig.get_out_edges(cur_v); if (out_i==out_end) // if there are no out edges - no recursion { //cout<<"no recursion for: ";debugPrint(trace);cout<<"\n"; // now we have full trace // check if its lorder correct // and if so, add vertex for Step corresponding cur_v if (lorder_in_set(lorder(trace),lo_vec)) { new_v = BHnew.add_step( BHorig.get_step(cur_v) ); return true; } else return false; //BHnew.add_path(trace.begin(),trace.end(), // BHnew.get_root(),fin); } else // recursively handle all child vertices { // flag to check if some path was added for any child vertex bool was_added = false; vector<BGVertex> new_vvec; for (;out_i!=out_end;out_i++) { e = *out_i; // get next child vertex BGVertex child_v = BHorig.get_target(e); // add new Step for given vertex to new trace Trace t = trace; t.push_back(BHorig.get_step(child_v)); //cout<<"form new trace: ";debugPrint(t);cout<<"\n"; // recursive call for this func with new params // if for given edge child_v vertex new_v was added // then store this new_v in vector and set the flag BGVertex new_v; if (handle_path(BHorig,child_v,lo_vec,BHnew,t,new_v)) { was_added = true; new_vvec.push_back(new_v); } } // if at least one path was added, then if (was_added) { // create vertex for current Step BGVertex this_v; this_v = BHnew.add_step(BHorig.get_step(cur_v)); // connect it to all recursively added child vertices for (vector<BGVertex>::const_iterator vvec_it = new_vvec.begin();vvec_it!=new_vvec.end();vvec_it++) BHnew.add_edge(this_v,*vvec_it); // set this vertex as a new_v for our parent new_v = this_v; } return was_added; } }
///////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////// // obtain child vertices for given vertex set vector<BGVertex> obtain_childs(const Behavior& BH, const vector<BGVertex> vvec) { vector<BGVertex> childs; BGOutEdgeIt out_i, out_end; BGEdge e; for (vector<BGVertex>::const_iterator vit = vvec.begin();vit!=vvec.end();vit++) { tie(out_i,out_end) = BH.get_out_edges(*vit); for (;out_i!=out_end;out_i++) { e = *out_i; BGVertex child_v = BH.get_target(e); childs.push_back(child_v); } } return childs; }