Index: src/dgp/dgp1.cc
===================================================================
--- src/dgp/dgp1.cc	(revision b97a55609b527df1f92edbe0c70d03a6385c0132)
+++ src/dgp/dgp1.cc	(revision 519eaf508529829bbfe93c12c3e94f44afccf5ed)
@@ -1,2 +1,5 @@
+#include <iostream>
+using namespace std;
+
 #include "dgp0.hh"
 #include "global.hh"
@@ -79,9 +82,9 @@
 	  {
 	    ret = *ps;
-	    // fprintf(stderr,"FIND EXISTING NODE SUCCEEDED BEACAUSE OF LH/LV equality ()\n");
+	    fprintf(stderr,"#\tsucceeded because of LH/LV equality ()\n");
 	  }
 	else
 	  {
-	    // fprintf(stderr,"FIND EXISTING NODE FAILED BEACAUSE OF LH/LV inequality\n");
+	    fprintf(stderr,"#\tfailed beacause of LH/LV inequality\n");
 	  }
     }
@@ -158,28 +161,24 @@
 //====================================================================================================
 
-int create_new_head_node_left(int h, NodeProp& newheadprop, bitset<MAXNODES>& newheadLH, bitset<MAXNODES>& newheadLD, bitset<MAXNODES>& newheadLV)
-{
-  int newheadind = sgraph.clone(h,newheadprop);
-  // list<int>::iterator nextit=h; ++nextit;
-  // nodelist.insert(nextit,newheadind);
+int create_new_head_node_left(int anc, NodeProp& prop, bitset<MAXNODES>& LH, bitset<MAXNODES>& LD, bitset<MAXNODES>& LV)
+{
+  int newheadind = sgraph.clone(anc,prop);
   nodelist.push_back(newheadind);
-  sgraph[newheadind].LH=newheadLH;
-  sgraph[newheadind].LD = newheadLD;
-  sgraph[newheadind].in_LH=true;
+  sgraph[newheadind].LH = LH;
+  sgraph[newheadind].LD = LD;
+  sgraph[newheadind].in_LH = true;
   sgraph[newheadind].LV.reset();
 
-  copy_links(h,newheadind);
+  copy_links(anc,newheadind);
   create_reverse_links(newheadind);
   
-  if(debug) sgraph.print_node_debug(stderr,"C ",newheadind,h);
+  if(debug) sgraph.print_node_debug(stderr,"add new",newheadind,anc);
   // if(debug) print_sets(newheadind);
   return newheadind;
 }
 
-int create_new_dep_node_left(int d, NodeProp& prop, bitset<MAXNODES>& LH, bitset<MAXNODES>& LD, bitset<MAXNODES>& LV)
-{
-  int newind = sgraph.clone(d,prop);
-  // list<int>::iterator nextit=d; ++nextit;
-  // nodelist.insert(nextit,newind);
+int create_new_dep_node_left(int anc, NodeProp& prop, bitset<MAXNODES>& LH, bitset<MAXNODES>& LD, bitset<MAXNODES>& LV)
+{
+  int newind = sgraph.clone(anc,prop);
   nodelist.push_back(newind);
   sgraph[newind].LH.reset();
@@ -188,8 +187,8 @@
   sgraph[newind].LV.reset();
 
-  copy_links(d,newind);
+  copy_links(anc,newind);
   create_reverse_links(newind);
   
-  if(debug) sgraph.print_node_debug(stderr,"C ",newind,d);
+  if(debug) sgraph.print_node_debug(stderr,"add new",newind,anc);
   // if(debug) print_sets(newind);
   
@@ -197,9 +196,7 @@
 }
 
-int create_new_head_node_right(int h, NodeProp& newheadprop, bitset<MAXNODES>& newheadLH, bitset<MAXNODES>& newheadLD, bitset<MAXNODES>& newheadLV)
-{
-  int newheadind = sgraph.clone(h,newheadprop);
-  // list<int>::iterator nextit=h; ++nextit;
-  // nodelist.insert(nextit,newheadind);
+int create_new_head_node_right(int anc, NodeProp& prop, bitset<MAXNODES>& newheadLH, bitset<MAXNODES>& newheadLD, bitset<MAXNODES>& newheadLV)
+{
+  int newheadind = sgraph.clone(anc,prop);
   nodelist.push_back(newheadind);
   sgraph[newheadind].LH=newheadLH;
@@ -208,8 +205,8 @@
   sgraph[newheadind].LV=newheadLV;
   
-  copy_links(h,newheadind);
+  copy_links(anc,newheadind);
   create_reverse_links(newheadind);
 
-  if(debug) sgraph.print_node_debug(stderr,"C ",newheadind,h);
+  if(debug) sgraph.print_node_debug(stderr,"add new",newheadind,anc);
   // if(debug) print_sets(newheadind);
   
@@ -217,7 +214,7 @@
 }
 
-int create_new_dep_node_right(int d, NodeProp& prop, bitset<MAXNODES>& LH, bitset<MAXNODES>& LD, bitset<MAXNODES>& LV)
-{
-  int newind = sgraph.clone(d,prop);
+int create_new_dep_node_right(int anc, NodeProp& prop, bitset<MAXNODES>& LH, bitset<MAXNODES>& LD, bitset<MAXNODES>& LV)
+{
+  int newind = sgraph.clone(anc,prop);
   nodelist.push_back(newind);
   sgraph[newind].LH=LH;
@@ -226,8 +223,8 @@
   sgraph[newind].LV.reset();
   
-  copy_links(d,newind);
+  copy_links(anc,newind);
   create_reverse_links(newind);
 
-  if(debug) sgraph.print_node_debug(stderr,"C ",newind,d);
+  if(debug) sgraph.print_node_debug(stderr,"ADD NEW",newind,anc);
   // if(debug) print_sets(newind);
   
@@ -253,6 +250,4 @@
     bitset<MAXNODES> newheadLV = sgraph[d].LV;
     bitset<MAXNODES> newheadLD = sgraph[h].LD;
-
-    //    vector<int> newedge;
 
     newheadind = find_existing_node(sgraph[h].mnode, newheadprop, newheadLH, newheadLV);
@@ -302,8 +297,8 @@
   if(sgraph[d].saturated()) sgraph[newheadind].LD |= sgraph[d].LD;
   
-  if(debug) sgraph.print_arc(stderr,newheadind,d,l.role,0);
-  if(debug) sgraph.print_node_debug(stderr,"U ",newheadind,h);
+  if(debug) sgraph.print_arc(stderr,"new link",newheadind,d,l.role,0);
+  if(debug) sgraph.print_node_debug(stderr,"update",newheadind,h);
   // if(debug) print_sets(newheadind);
-  if(debug) sgraph.print_node_debug(stderr,"U ",newdepind,d);
+  if(debug) sgraph.print_node_debug(stderr,"update",newdepind,d);
   // if(debug) print_sets(newdepind);
 }
@@ -380,8 +375,85 @@
   if(sgraph[newheadind].saturated()) sgraph[newdepind].LH |= sgraph[newheadind].LH;
 
-  if(debug) sgraph.print_arc(stderr,newheadind,newdepind,l.role,1);
-  if(debug) sgraph.print_node_debug(stderr,"U ",newheadind,h);
-  if(debug) sgraph.print_node_debug(stderr,"U ",newdepind,d);
-  
+  if(debug) sgraph.print_arc(stderr,"new link",newheadind,newdepind,l.role,1);
+  if(debug) sgraph.print_node_debug(stderr,"update",newheadind,h);
+  if(debug) sgraph.print_node_debug(stderr,"update",newdepind,d);
+  
+}
+
+//====================================================================================================
+
+// bool check_meeting_boubles(list<Boubble*>& hboubbles, list<Boubble*>& dboubbles)
+// {
+//   bool hremove=false;             // czy usun±æ ostatnio sprawdzany b±bel
+//   bool dremove=false;             // czy usun±æ ostatnio sprawdzany b±bel
+
+//   for(list<Boubble*>::iterator hb = hboubbles.begin(); hb != hboubbles.end(); hb = hremove ? hboubbles.erase(hb) : ++hb )
+//     {
+//       hremove=false;
+//       for(list<Boubble*>::iterator db = dboubbles.begin(); db != dboubbles.end(); db = dremove ? dboubbles.erase(db) : ++db )
+// 	{
+// 	  dremove=false;
+// 	  if( (*hb)->rel()==(*db)->rel() && (*hb)->dir()==DOWN && (*db)->dir()==UP && (*hb)->reverse()!=(*db)->reverse() )
+// 	    {
+// 	      int srcnode,dstnode;
+// 	      if( (*hb)->reverse()==false )
+// 		srcnode = (*hb)->src(), dstnode = (*db)->src();
+// 	      else
+// 		srcnode = (*db)->src(), dstnode = (*hb)->src();
+// 	      if( grammar.check_longrel(sgraph.cat(srcnode), sgraph.cat(dstnode), (*hb)->rel()) )
+// 		{
+// 		  hremove=dremove=true;
+// 		  if(debug) fprintf(stderr,"BOUBBLES MET!!!\n");
+// 		}
+// 	      else
+// 		{
+// 		  if(debug) fprintf(stderr,"BOUBBLES' MEETING FAILED!!!\n");
+// 		  return false;
+// 		}
+// 	    }
+// 	}
+//     }
+//   return true;
+// }
+
+//====================================================================================================
+
+bool check_meeting_boubles(list<Boubble*>& boubbles)
+{
+  bool hremove=false;             // czy usun±æ ostatnio sprawdzany b±bel
+  bool dremove=false;             // czy usun±æ ostatnio sprawdzany b±bel
+
+  for(list<Boubble*>::iterator hb = boubbles.begin(); hb != boubbles.end(); hb = hremove ? boubbles.erase(hb) : ++hb )
+    {
+      cout << endl << "hb:" << **hb ;
+      hremove=false;
+      for(list<Boubble*>::iterator db = hb; db != boubbles.end(); db = dremove ? boubbles.erase(db) : ++db )
+	{
+	  cout << " db:" << **db;
+	  dremove=false;
+	  if( (*hb)->rel()==(*db)->rel() && (*hb)->reverse()!=(*db)->reverse() )
+	    {
+	      cout << "Z";
+	      int srcnode,dstnode;
+	      if( (*hb)->reverse()==false )
+		srcnode = (*hb)->src(), dstnode = (*db)->src();
+	      else
+		srcnode = (*db)->src(), dstnode = (*hb)->src();
+	      if( grammar.check_longrel(sgraph.cat(srcnode), sgraph.cat(dstnode), (*hb)->rel()) )
+		{
+		  cout << " REMOVE ";
+		  hremove=dremove=true;
+		  if(debug) fprintf(stderr,"BOUBBLES MET!!!\n");
+		}
+	      else
+		{
+		  cout << " FAIL ";
+		  if(debug) fprintf(stderr,"BOUBBLES' MEETING FAILED!!!\n");
+		  return false;
+		}
+	    }
+	}
+    }
+  return true;
 }
 
@@ -393,5 +465,4 @@
 bool check_boubbles_at_target(list<Boubble*>& boubbles, int node)
 {
-  list<Boubble*>::iterator last; // ostatnio sprawdzany b±bel
   bool remove=false;             // czy usun±æ ostatnio sprawdzany b±bel
 
@@ -399,5 +470,8 @@
     if( (*b)->is_at_target() )
       if( grammar.check_longrel(sgraph.cat((*b)->src()), sgraph.cat(node), (*b)->rel()) )
-	remove=true;
+	{
+	  cout << endl << "REMOVE ChBatT " << **b << endl;
+	  remove=true;
+	}
       else
 	return false;
@@ -412,75 +486,91 @@
 void try_connect_dependents(int j)
 {
-  // for(list<int>::iterator i(j); i!=nodelist.begin(); --i)
-  //   if(sgraph.visible(*i,*j) && sgraph.saturated(*i))
   LViterator lvi(sgraph,j);
   int i;
   while((i=lvi.next()) >= 0)
-    if(sgraph.saturated(i))
-      {
-      if(debug) {fprintf(stderr,"## %d <-- %d ... ",i,j); }
-
-      list<const Link*> ji_links = grammar.connectable2( sgraph.cat(j), sgraph.cat(i), sgraph[j].prop.flags, sgraph[i].prop.flags); // ref do Roles!!!
-      list<const Link*>::iterator ri = ji_links.begin();
-      if(ri == ji_links.end()) { if(debug) fprintf(stderr,"no roles\n"); }
+    {
+      //if(debug) sgraph.print_node_debug(stderr,"D-CUR>",i,-1);
+
+      if(sgraph.saturated(i))
+	{
+	  if(debug) {fprintf(stderr,"%d <--",i); }
+	  
+	  list<const Link*> ji_links = grammar.connectable2( sgraph.cat(j), sgraph.cat(i), sgraph[j].prop.flags, sgraph[i].prop.flags); // ref do Roles!!!
+	  list<const Link*>::iterator ri = ji_links.begin();
+	  if(ri == ji_links.end()) { if(debug) fprintf(stderr,"     no roles\n"); }
+	  else
+	    {
+	      for(; ri != ji_links.end(); ++ri )
+		{
+		  if(debug) fprintf(stderr,"     %s",(*ri)->role.str());
+		  if(!grammar.check_constr2(sgraph[j].prop,sgraph[i].prop,0,**ri ))
+		    { if(debug) fprintf(stderr," ...constraints failed\n"); }
+		  else
+		    {
+		      list<Boubble*> new_head_boubbles = collect_head_boubbles(j,i,(*ri)->role);
+		      list<Boubble*> new_dep_boubbles = collect_dep_boubbles(j,i,(*ri)->role);
+		      if( check_meeting_boubles(new_head_boubbles) &&
+			  check_meeting_boubles(new_dep_boubbles) &&
+			  check_boubbles_at_target(new_head_boubbles,j) &&
+			  check_boubbles_at_target(new_dep_boubbles,i) )
+			{
+			  if(debug) fprintf(stderr," ...SUCCESS!\n");
+			  connect_left( j, i, **ri, new_head_boubbles, new_dep_boubbles);
+			}
+		      else
+			{ if(debug) fprintf(stderr," ...boubbles failed\n"); }
+		    }
+		}
+	    }
+	}
       else
-	{
-	  for(; ri != ji_links.end(); ++ri )
-	    if(!grammar.check_constr2(sgraph[j].prop,sgraph[i].prop,0,**ri ))
-	      { if(debug) fprintf(stderr,"constraints failed\n"); }
-	    else
-	      {
-		list<Boubble*> new_head_boubbles = collect_head_boubbles(j,i,(*ri)->role);
-		list<Boubble*> new_dep_boubbles = collect_dep_boubbles(j,i,(*ri)->role);
-		
-		if( !(check_boubbles_at_target(new_head_boubbles,j) && check_boubbles_at_target(new_dep_boubbles,i)) )
-		  { if(debug) fprintf(stderr,"boubbles failed\n"); }
-		else
-		  {
-		    if(debug) fprintf(stderr,"success\n");
-		    connect_left( j, i, **ri, new_head_boubbles, new_dep_boubbles);
-		  }
-	      }
-	}
-    }
-}
-
-
+	  if(debug) {fprintf(stderr,"%d <--     unsaturated\n",i); }
+    }
+  
+}
 //----------------------------------------------------------------------------------------------------
 
 void try_connect_heads(int j)
 {
-  // for(list<int>::iterator i(j); i!=nodelist.begin(); --i)
-  //   if(sgraph.visible(*i,*j) && sgraph.saturated(*j))
-
   LViterator lvi(sgraph,j);
   int i;
   while((i=lvi.next()) >= 0)
-    if(sgraph.saturated(j))
-    {
-      if(debug) fprintf(stderr, "## %d --> %d ... ",i,j);
-
-      list<const Link*> ij_links = grammar.connectable2( sgraph.cat(i), sgraph.cat(j), sgraph[i].prop.flags, sgraph[j].prop.flags );
-      list<const Link*>::iterator ri = ij_links.begin();
-      if(ri == ij_links.end()) { if(debug) fprintf(stderr,"no roles\n"); }
+    {
+      // if(debug) sgraph.print_node_debug(stderr,"H-CUR> ",i,-1);
+      if(sgraph.saturated(j))
+	{
+	  if(debug) fprintf(stderr, "%d -->",i);
+	  
+	  list<const Link*> ij_links = grammar.connectable2( sgraph.cat(i), sgraph.cat(j), sgraph[i].prop.flags, sgraph[j].prop.flags );
+	  list<const Link*>::iterator ri = ij_links.begin();
+	  if(ri == ij_links.end()) { if(debug) fprintf(stderr,"     no roles\n"); }
+	  else
+	    {
+	      for(; ri != ij_links.end(); ++ri )
+		{
+		  if(debug) fprintf(stderr,"     %s",(*ri)->role.str());
+		  if( !grammar.check_constr2( sgraph[i].prop, sgraph[j].prop, 1, **ri ) )
+		    { if(debug) fprintf(stderr," ...constraints failed\n"); }
+		  else
+		    {
+		      list<Boubble*> new_head_boubbles = collect_head_boubbles(i,j,(*ri)->role);
+		      list<Boubble*> new_dep_boubbles = collect_dep_boubbles(i,j,(*ri)->role);
+		      
+		      if( check_meeting_boubles(new_head_boubbles) &&
+			  check_meeting_boubles(new_dep_boubbles) &&
+			  check_boubbles_at_target(new_head_boubbles,i) &&
+			  check_boubbles_at_target(new_dep_boubbles,j) )
+			{
+			  if(debug) fprintf(stderr," ...SUCCESS!\n");
+			  connect_right( i, j, **ri, new_head_boubbles, new_dep_boubbles );
+			}
+		      else
+			{ if(debug) fprintf(stderr," ...bubbles failed\n",i); }
+		    }
+		}
+	    }
+	}
       else
-	{
-	  for(; ri != ij_links.end(); ++ri )
-	    if( !grammar.check_constr2( sgraph[i].prop, sgraph[j].prop, 1, **ri ) )
-	      { if(debug) fprintf(stderr,"constraints failed\n"); }
-	    else
-	      {
-		list<Boubble*> new_head_boubbles = collect_head_boubbles(i,j,(*ri)->role);
-		list<Boubble*> new_dep_boubbles = collect_dep_boubbles(i,j,(*ri)->role);
-		
-		if( !(check_boubbles_at_target(new_head_boubbles,i) && check_boubbles_at_target(new_dep_boubbles,j)) )
-		  { if(debug) fprintf(stderr,"boubbles failed\n"); }
-		else
-		  {
-		    if(debug) fprintf(stderr,"success\n");
-		    connect_right( i, j, **ri, new_head_boubbles, new_dep_boubbles );
-		  }
-	      }
-	}
+	  if(debug) {fprintf(stderr,"%d <--     unsaturated\n",j); }
     }
 }
@@ -571,11 +661,11 @@
     nodelist.push_back(basenode);
 
-    if(debug) sgraph.print_node_debug(stderr,"B ",basenode,-1); // STDOUT!!!
-    if(debug) print_sets(basenode);
+    if(debug) sgraph.print_node_debug(stderr,"add base",basenode,-1); // STDOUT!!!
+    // if(debug) print_sets(basenode);
 
     list<int>::iterator cursor=processed;
     while(++cursor != nodelist.end())
     {
-      if(debug) sgraph.print_node_debug(stderr,"> ",*cursor,-1);
+      if(debug) sgraph.print_node_debug(stderr,"MAIN-CUR> ",*cursor,-1);
       try_connect_dependents(*cursor);
       try_connect_heads(*cursor);
