source: src/dgp/dgp0.cc @ 5f4d9c3

Last change on this file since 5f4d9c3 was 5f4d9c3, checked in by Maciej Prill <mprill@…>, 12 years ago

Rewritten the build system, added lem UTF-8 version.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1#include "dgp0.hh"
2#include "global.hh"
3
4extern Grammar grammar;
5extern MGraph mgraph;
6extern SGraph sgraph;
7
8SNode* snodes;
9
10extern bool debug;
11
12list<int> nodelist;
13list<int>::iterator processed;
14
15
16void set_initial_constraints(int node)
17{
18  snodes[node].prop.forbidden.reset();
19  snodes[node].prop.required=grammar.obl[snodes[node].mnode->cat];
20}
21
22
23bool changing_constraints(int head, Role role)
24{
25  return grammar.sgl[role] || snodes[head].prop.required[role];
26}
27
28void apply_constraints(int head, Role role)
29{
30  if(grammar.sgl[role]) snodes[head].prop.forbidden.set(role);
31  snodes[head].prop.required.reset(role);
32}
33
34NodeProp compute_prop_left(NodeProp headprop, Role role)
35{
36  NodeProp ret=headprop;
37  if(grammar.sgl[role]) ret.forbidden.set(role);
38  ret.required.reset(role);
39  return ret;
40}
41
42NodeProp compute_prop_right(NodeProp headprop, Role role)
43{
44  NodeProp ret=headprop;
45
46  if(grammar.sgl[role]) ret.forbidden.set(role);
47  ret.required.reset(role);
48  return ret;
49}
50
51int get_node(MNode& mnode, NodeProp p, bitset<MAXNODES>& newheadLH, bitset<MAXNODES>& newheadLV)
52{
53  for(vector<int>::iterator ps=mnode.snodes.begin(); ps!=mnode.snodes.end(); ++ps)
54    if(snodes[*ps].prop==p && snodes[*ps].LH==newheadLH && snodes[*ps].LV==newheadLV)
55      return *ps;
56  return -1;
57}
58
59void connect_left(list<int>::iterator h, list<int>::iterator d, Role r)
60{
61  NodeProp &oldheadprop = snodes[*h].prop;
62  NodeProp newheadprop;
63  bitset<MAXNODES> newheadLV;
64  bitset<MAXNODES> newheadLH;
65  bitset<MAXNODES> newheadLD;
66 
67  newheadprop=compute_prop_left(oldheadprop,r);
68 
69  int newheadind;
70  if(oldheadprop==newheadprop)
71    newheadind = *h;
72  else
73  {
74    newheadLH = snodes[*h].LH;
75    newheadLV = snodes[*d].LV;
76    newheadLD = snodes[*h].LD;
77
78    newheadind = get_node(*(snodes[*h].mnode), newheadprop, newheadLH, newheadLV);
79    if( newheadind < 0 )
80    {
81      newheadind = sgraph.clone(*h,newheadprop);
82      list<int>::iterator nextit=h; ++nextit;
83      nodelist.insert(nextit,newheadind);
84      snodes[newheadind].LH=newheadLH;
85      snodes[newheadind].in_LH=true;
86      snodes[newheadind].LV.reset();
87      snodes[newheadind].LD = newheadLD;
88     
89      if(debug) sgraph.print_node_debug(stderr," C ",newheadind);
90    }
91    else
92      snodes[newheadind].LD |= newheadLD; // TYLKO DLA LD
93  }
94
95  snodes[newheadind].deps.push_back(Arc(*d,r,*h));
96 
97  if(snodes[*d].saturated()) snodes[newheadind].LV |= snodes[*d].LV;
98  snodes[newheadind].LD.set(*d);
99  if(snodes[*d].saturated()) snodes[newheadind].LD |= snodes[*d].LD;
100 
101  if(debug)
102    sgraph.print_arc(stderr,*d,newheadind,r,0), sgraph.print_node_debug(stderr," U ",newheadind);
103}
104
105
106void connect_right(list<int>::iterator h, list<int>::iterator d, Role r)
107{
108  NodeProp &oldheadprop = snodes[*h].prop;
109  NodeProp newheadprop;
110  bitset<MAXNODES> newheadLV;
111  bitset<MAXNODES> newheadLH;
112  bitset<MAXNODES> newheadLD;
113  int newheadind;
114 
115  newheadprop = compute_prop_right(oldheadprop,r);
116  if(oldheadprop==newheadprop)
117    newheadind = *h;
118  else
119  {
120    newheadLH = snodes[*h].LH;
121    newheadLV = snodes[*h].LV;
122    newheadLD = snodes[*h].LD;
123
124    newheadind = get_node(*(snodes[*h].mnode), newheadprop, newheadLH, newheadLV);
125    if( newheadind < 0 )
126    {
127      newheadind = sgraph.clone(*h,newheadprop);
128      snodes[newheadind].LH=newheadLH;
129      snodes[newheadind].in_LH=false;
130      snodes[newheadind].LV=newheadLV;
131      snodes[newheadind].LD=newheadLD;
132      list<int>::iterator nextit=h; ++nextit;
133      nodelist.insert(nextit,newheadind);
134     
135      if(debug) sgraph.print_node_debug(stderr," C ",newheadind);
136    }
137    else
138      snodes[newheadind].LD |= newheadLD; // TYLKO DLA LD
139  }
140 
141  snodes[*d].heads.push_back(Arc(newheadind,r,*h));
142
143  snodes[*d].LH.set(newheadind);
144
145  if(snodes[newheadind].saturated()) snodes[*d].LH |= snodes[newheadind].LH;
146
147  if(debug)
148    sgraph.print_arc(stderr,newheadind,*d,r,1), sgraph.print_node_debug(stderr," U ",*d);
149 
150}
151
152
153void try_connect_dependents(list<int>::iterator j)
154{
155  for(list<int>::iterator i(j); i!=nodelist.begin(); --i)
156    if(sgraph.visible(*i,*j) && sgraph.saturated(*i))
157    {
158      Roles& ji_roles = grammar.connect[snodes[*j].mnode->cat][snodes[*i].mnode->cat];
159      for(RolesIter r=ji_roles.begin(); r!=ji_roles.end();++r)
160        if(grammar.check_constr(snodes[*j].prop,snodes[*i].prop,0,*r))
161          connect_left(j,i,*r);
162    }
163}
164
165
166void try_connect_heads(list<int>::iterator j)
167{
168  for(list<int>::iterator i(j); i!=nodelist.begin(); --i)
169    if(sgraph.visible(*i,*j))
170    {
171      Roles& ij_roles = grammar.connect[snodes[*i].mnode->cat][snodes[*j].mnode->cat];
172      for(RolesIter r=ij_roles.begin(); r!=ij_roles.end();++r)
173        if(grammar.check_constr(snodes[*i].prop,snodes[*j].prop,1,*r))
174          connect_right(i,j,*r);
175    }
176}
177
178
179void reverse_links()
180{
181  list<int>::iterator i = nodelist.begin();
182  for(++i; i!=nodelist.end(); ++i)
183    {
184      for(vector<Arc>::iterator da=sgraph.nodes[*i].deps.begin()--; da!=sgraph.nodes[*i].deps.end(); ++da)
185        sgraph.nodes[da->dst].heads.push_back(Arc(*i,da->role,da->anc));
186      for(vector<Arc>::iterator ha=sgraph.nodes[*i].heads.begin(); ha!=sgraph.nodes[*i].heads.end(); ++ha)
187        sgraph.nodes[ha->dst].deps.push_back(Arc(*i,ha->role,ha->anc));
188    }
189}
190
191
192void dgp0()
193{
194  snodes=sgraph.nodes;
195
196  nodelist.clear();
197  nodelist.push_back(0); // BOS
198  processed=nodelist.begin();
199
200  for(int m=0; m<mgraph.n ; ++m)
201  {
202    int basenode = sgraph.add_base_snode(mgraph.nodes+m); // ma zwracaæ SNode*
203    set_initial_constraints(basenode);
204    nodelist.push_back(basenode);
205
206    if(debug) {sgraph.print_node_debug(stderr,"B  ",basenode);} // STDOUT!!!
207
208    list<int>::iterator cursor=processed;
209    while(++cursor != nodelist.end())
210    {
211      try_connect_dependents(cursor);
212      try_connect_heads(cursor);
213      processed=cursor;
214    }
215  }
216  reverse_links();
217}
Note: See TracBrowser for help on using the repository browser.