Index: applications/doprava/traffic_agent.h
===================================================================
--- applications/doprava/traffic_agent.h (revision 842)
+++ applications/doprava/traffic_agent.h (revision 927)
@@ -13,94 +13,67 @@
 using namespace bdm;
 
+class BaseTrafficAgent;
+
 //! detector of traffic variables
-class Detector{
-	public:
-		string name;             //! detector name
-		int distance;		     //! distance from stop-line
+class Lane{
+	public:
+		Array<string> inputs;
+		Array<string> outputs;
+		vec alpha; //size of outputs
+		string queue;
+		string sg;
 
 		//! function loading info from Setting
 		void from_setting(const Setting &set){
-			UI::get(name,set,"name",UI::compulsory);
-			UI::get(distance,set,"distance",UI::compulsory);
+			UI::get(inputs,set,"inputs",UI::compulsory);
+			UI::get(outputs,set,"outputs",UI::compulsory);
+			UI::get(alpha,set,"alpha",UI::compulsory);
+			UI::get(queue,set,"queue",UI::compulsory);
+			UI::get(sg,set,"sg",UI::compulsory);
 			
 		}
 };
 
-//! detector fo incomming flow
-class DetectorIn : public Detector{
-	public:
-	Array<string> to_sg; // to signal plans
-	void from_setting(const Setting &set){
-		Detector::from_setting(set);
-		UI::get(to_sg, set, "to_sg", UI::compulsory);
-	}
-};
-
-//! structure for output detectors
-class RequestOut {
-	public:
-	string to; // agent
-	Array<string> detectors; //detectors
-	void from_setting(const Setting &set){
-		UI::get(detectors,set,"detectors",UI::compulsory);
-		UI::get(to, set, "to", UI::compulsory);
-	}
-};
-
-
-//! class with physical information about a signal group
-class SignalGroup {
-	public:
-		string name;                 //! names of the group
-		Array<Detector> detectors;   //! (possible) detectors
-		
-		//! function that loads information from Setting
-		void from_setting(const Setting &set);		
-};
-
 //! class that operates on a signal group
-class SignalGroupHandler {
+class LaneHandler {
 	protected:
-		//! pointer to physical signal group
-		SignalGroup *sg;
+		//! pointer to physical lane
+		const Lane &lane;
+		//! agent pointer
+		BaseTrafficAgent *agent;
 		
 	public:
 		//! actual data from the relevant signal group
-		vec det_data;
+		vec inputs;
+		//!
+		double queue;
+		//! 
 		//! description of det_data
-		RV rv_det_data;
+		RV rv_inputs;
+		//! description of det_data
+		RV rv_outputs;
+		//! description of det_data
+		RV rv_queue;
 		//! link from global measured data 
-		datalink ds2data;
-	public:
-		void connect_sg(SignalGroup &sg0, const string &agent_name){
-			sg=&sg0;
-			for (int i=0;i<sg->detectors.length();i++){
-				rv_det_data.add(RV(agent_name +"_"+ sg->detectors(i).name, 2)); //TODO intensity occupancy
-			}
-			
-		}
+		datalink agentin2input;
+		//!
+		datalink output2agentout;
+		//!
+		int queue_index;
+	public:
+		LaneHandler(const Lane &lane0): lane(lane0){
+			for (int i=0;i<lane0.inputs.length();i++){
+				rv_inputs.add(RV(lane.inputs(i), 2));
+			}
+			for (int i=0;i<lane0.outputs.length();i++){
+				rv_outputs.add(RV(lane.outputs(i), 2)); 
+			}
+			rv_queue.add(RV(lane.queue, 1)); 
+		}
+		
+		void connect_data(BaseTrafficAgent &agent0);
 		
 		//! arbitrary function that computes the need of the signal group for green light in common units (number of waiting cars?)
-		double expected_load(){
-			if (det_data.length()>0){
-				return det_data(0); // whatever
-			} else {
-				return 1.0; // mean value
-			}
-		}
-};
-
-class RequestHandler{
-	protected:
-		RequestOut *rq;
-	public:
-		RV rv;
-	public:
-		void connect_request(RequestOut &rq0, const string &agent_name){
-			rq=&rq0;
-			for (int i=0;i<rq->detectors.length();i++){
-				rv.add(RV(rq->detectors(i), 2)); //TODO intensity occupancy
-			}
-		}
+		double expected_output(double green_time);
 };
 
@@ -110,40 +83,82 @@
 */
 class BaseTrafficAgent : public Participant {
-	protected:
+	LOG_LEVEL(BaseTrafficAgent,logdata);
+	public:
 		//! Signal Groups
-		Array<SignalGroup> sg;
-
-		Array<SignalGroupHandler> sgh;
+		Array<Lane> lanes;
+
+		Array<LaneHandler*> lanehs;
 										
 		//!data from messages
-		vec input_data;
+		vec inputs;
 		
 		//! decription of msg_data
-		RV input_rv;
+		RV rv_inputs;
 		
 		//! data to broadcast
-		vec output_data;
+		vec outputs;
 		
 		//! description of broadcast dataind
-		RV output_rv;
+		RV rv_outputs;
+		
+		vec queues;
+		
+		//! description of queues
+		RV rv_queues;
 		
 		//! datalink from DS to output variables
-		datalink ds2output;
-					
-		//! output recepient
-		Array<RequestOut> requests;
-		Array<RequestHandler> request_handler;
+		datalink ds2inputs;
+
+		//! datalink from DS to output variables
+		datalink ds2queues;
 		
 		//! action description
 		RV action_rv;
-		
+				
 		datalink_part action2ds;
 		
+		Array<string> neighbours;
+		
+		Array<RV> rv_neighbours_out;
+		
+		Array<datalink> output2neighbour;
+		
 	public:
 		void validate(){
+			lanehs.set_length(lanes.length());
+			for (int l=0; l<lanes.length(); l++){
+				lanehs(l) = new LaneHandler(lanes(l));
+				
+				rv_inputs.add(lanehs(l)->rv_inputs);
+				rv_outputs.add(lanehs(l)->rv_outputs);
+				rv_queues.add(lanehs(l)->rv_queue);
+			}
+			inputs.set_size(rv_inputs._dsize());
+			outputs.set_size(rv_outputs._dsize());
+			queues.set_size(rv_queues._dsize());
+			
+			for (int l=0; l<lanes.length(); l++){
+				lanehs(l)->connect_data(*this);
+			}
+			
+			//for -- rv_outputs -- 
+			// TODO vybrat rv pro sousedy 
+			rv_neighbours_out.set_length(neighbours.length());
+			output2neighbour.set_length(neighbours.length());
+			
+			for (int i=0; i<neighbours.length(); i++){
+				for (int r=0; r<rv_outputs.length(); r++){
+					int str_pos = rv_outputs.name(r).compare(neighbours(i));
+					if (str_pos>neighbours(i).length()){
+						rv_neighbours_out(i).add(rv_outputs.subselect(vec_1(r)));
+					}
+				}
+				// connect datasource
+				output2neighbour(i).set_connection(rv_neighbours_out(i), rv_outputs);
+			}
+			
+			// lanehs knows RVS
 			// write internal checks if all was loaded OK
 			
-			// set action variable == this is a feature of the agent!
-			action_rv = RV(name+"_Tc", 1); // <======= example
 		}
 		void receive(const Setting &msg){
@@ -151,66 +166,54 @@
 			UI::get(what, msg, "what", UI::compulsory);
 			
-			if (what=="data"){ // 
+			if (what=="new_stable_state"){ // 
 				// field data 
 				// extract decription of teh received datavector
 				shared_ptr<RV> rv=UI::build<RV>(msg,"rv",UI::compulsory);
 				// find if it is needed
-				ivec ind=rv->dataind(input_rv); // position of rv in in_rv;
+				ivec ind=rv->dataind(rv_inputs); // position of rv in in_rv;
 				if (ind.length()>0){ //data are interesting
 					vec dt;
 					UI::get(dt, msg, "value",UI::compulsory); // get data
-					set_subvector(input_data, ind,  dt); //check size?
+					set_subvector(inputs, ind,  dt); //check size?
 				}				
 			} else {
-				bdm_warning("Unknown message of type "+what);
+				Participant::receive(msg);
 			}
 		}
 		void log_register(logger &L, const string &prefix){
 			root::log_register ( L, prefix );
-			logrec->ids.set_size(sg.length()+2);
-			int i;
-			for (i=0;i <sg.length(); i++) {
-				logrec->ids(i)=logrec->L.add_vector(sgh(i).rv_det_data, "");
-			}
-			logrec->ids(i)=logrec->L.add_vector(input_rv,"in_"); i++;
-			logrec->ids(i)=logrec->L.add_vector(output_rv,"out_");
+			if ( log_level[logdata]){
+				L.add_vector ( log_level, logdata, RV ( 1 ), prefix );	
+			}
 		}
 		void log_write() const {
-			int i;
-			for (i=0;i <sg.length(); i++) {
-				logrec->L.log_vector(logrec->ids(i), sgh(i).det_data);
-			}
-			logrec->L.log_vector(logrec->ids(i),input_data); i++;
-			logrec->L.log_vector(logrec->ids(i),output_data); i++;
+			if (log_level[logdata]){
+				log_level.store(logdata, inputs);
+			}
 		}
 		
 		void broadcast(Setting& set){
 			// broadcast data to all neighbours
-			for (int i=0; i<request_handler.length(); i++){
+			for (int i=0; i<neighbours.length(); i++){
 				Setting &msg =set.add(Setting::TypeGroup);
-				RequestHandler &R=request_handler(i);
 				
+				// if...
 				// copy from create message
 				// create msg with fields {to=..., what=data, rv=..., value = ...}
-				UI::save ( requests(i).to, msg, "to");
-				UI::save ( (string)"data", msg, "what");
-				UI::save ( &R.rv, msg, "rv");
-				UI::save(output_data, msg, "value");
-			}
+				UI::save ( neighbours(i), msg, "to");
+				UI::save ( (string)"new_stable_state", msg, "what");
+				UI::save ( &(rv_neighbours_out(i)), msg, "rv");
+				UI::save( output2neighbour(i).pushdown(outputs), msg, "value");
+			}
+			
 		}
 		void adapt(const vec &glob_dt){
 			// copy data from global vector to sSGHandlers
-			for (int i=0; i<sgh.length();i++){
-				sgh(i).ds2data.filldown(glob_dt, sgh(i).det_data);
-			}
+			ds2inputs.filldown(glob_dt, inputs);
 			//copy data fro neighbours
-			ds2output.filldown(glob_dt, output_data);
+			ds2queues.filldown(glob_dt, queues);
 			// copy sg_length ... and others...
 		}
 		void act(vec &glob_ut){
-			vec needs(sgh.length());
-			for (int i=0; i<sgh.length();i++){
-				needs(i) = sgh(i).expected_load();
-			}
 			vec action; // trivial stuff
 			action2ds.filldown(action,glob_ut);
@@ -219,9 +222,7 @@
 		void ds_register(const DS &ds){
 			//register ds2output
-			ds2output.set_connection(output_rv, ds._drv());
-			output_data.set_size(output_rv._dsize());
-			for (int i=0; i<sgh.length(); i++){
-				sgh(i).ds2data.set_connection(sgh(i).rv_det_data, ds._drv());
-			}
+			ds2inputs.set_connection(rv_inputs, ds._drv());
+			ds2queues.set_connection(rv_queues, ds._drv());
+			inputs.set_size(rv_inputs._dsize());
 			action2ds.set_connection( ds._urv(), action_rv);
 		}
