1 | #include "global.hh" |
---|
2 | #include "sgraph.hh" |
---|
3 | #include "grammar.hh" |
---|
4 | #include "const.hh" |
---|
5 | #include <cstdio> |
---|
6 | #include <sstream> |
---|
7 | |
---|
8 | extern MGraph mgraph; |
---|
9 | |
---|
10 | //==================================================================================================== |
---|
11 | |
---|
12 | int SGraph::add_base_snode(int mnodeind) |
---|
13 | { |
---|
14 | SNode& newnode = makenewnode(); |
---|
15 | |
---|
16 | newnode.mnode=mnodeind; |
---|
17 | |
---|
18 | for(vector<int>::iterator pm=mgraph[newnode.mnode].pred.begin(); pm!=mgraph[newnode.mnode].pred.end(); ++pm) |
---|
19 | for(vector<int>::iterator ps=mgraph[*pm].snodes.begin(); ps!=mgraph[*pm].snodes.end(); ++ps) |
---|
20 | if(nodes[*ps].in_LH) |
---|
21 | { |
---|
22 | newnode.LV.set(*ps); |
---|
23 | if(nodes[*ps].saturated()) newnode.LV |= nodes[*ps].LH; |
---|
24 | } |
---|
25 | |
---|
26 | mgraph[newnode.mnode].snodes.push_back(lastnodeind()); |
---|
27 | |
---|
28 | newnode.in_LH=true; |
---|
29 | |
---|
30 | newnode.edge.push_back(lastnodeind()); |
---|
31 | |
---|
32 | return lastnodeind(); |
---|
33 | } |
---|
34 | |
---|
35 | //==================================================================================================== |
---|
36 | |
---|
37 | void SGraph::update_left(int headind, int depind) |
---|
38 | { |
---|
39 | SNode &head=nodes[headind], &dep=nodes[depind]; |
---|
40 | |
---|
41 | if(dep.saturated()) head.LV |= dep.LV, head.LD |= dep.LD; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | void SGraph::update_right(int headind, int depind) |
---|
46 | { |
---|
47 | SNode &head=nodes[headind], &dep=nodes[depind]; |
---|
48 | |
---|
49 | dep.LH.set(headind); |
---|
50 | if(head.saturated()) dep.LH |= head.LH; |
---|
51 | } |
---|
52 | |
---|
53 | //==================================================================================================== |
---|
54 | |
---|
55 | int SGraph::clone(int ancind, NodeProp newprop) |
---|
56 | { |
---|
57 | SNode &newnode=makenewnode(); |
---|
58 | SNode &ancnode = nodes[ancind]; |
---|
59 | |
---|
60 | newnode.prop=newprop; |
---|
61 | newnode.mnode=ancnode.mnode; |
---|
62 | mgraph[newnode.mnode].snodes.push_back(lastnodeind()); |
---|
63 | |
---|
64 | return lastnodeind(); |
---|
65 | } |
---|
66 | |
---|
67 | //==================================================================================================== |
---|
68 | |
---|
69 | int SGraph::print_node(FILE* f, int n, unsigned int info) |
---|
70 | { |
---|
71 | char buf[50000]; |
---|
72 | sprint_node(buf,n,-1,info); |
---|
73 | fputs(buf,f); |
---|
74 | } |
---|
75 | |
---|
76 | //---------------------------------------------------------------------------------------------------- |
---|
77 | |
---|
78 | int SGraph::print_node_debug(FILE* f, const char* pref, int n, int anc) |
---|
79 | { |
---|
80 | char buf[50000]; |
---|
81 | sprint_node_debug(buf,pref,n,anc); |
---|
82 | fputs(buf,f); |
---|
83 | } |
---|
84 | |
---|
85 | //---------------------------------------------------------------------------------------------------- |
---|
86 | |
---|
87 | void SGraph::print_arc(FILE* f, int head, int dep, Role role, int dir) // 0 - left, 1 - right |
---|
88 | { |
---|
89 | if(dir==0) |
---|
90 | fprintf(f,"#A %s:%d <-- %d\n", role.str(), dep, head); |
---|
91 | else |
---|
92 | fprintf(f,"#A %s:%d --> %d\n", role.str(), head, dep); |
---|
93 | } |
---|
94 | |
---|
95 | //==================================================================================================== |
---|
96 | |
---|
97 | int SGraph::sprint_node(char* buf, int nodeind, int anc, unsigned int info) |
---|
98 | { |
---|
99 | char* buf0=buf; |
---|
100 | |
---|
101 | SNode &node=nodes[nodeind]; |
---|
102 | |
---|
103 | buf+=sprintf(buf," dgp:%d",nodeind); |
---|
104 | if(anc>=0) buf+=sprintf(buf,"(%d)",anc); |
---|
105 | buf+=sprintf(buf, saturated(nodeind) ? ";s" : ";u"); |
---|
106 | |
---|
107 | if (info&HEADS || info&DEPS) |
---|
108 | buf+=sprintf(buf,";"); |
---|
109 | |
---|
110 | bool cont=false; |
---|
111 | |
---|
112 | if (info&HEADS) |
---|
113 | for(vector<Arc>::iterator h=node.heads.begin(); h!=node.heads.end(); ++h) |
---|
114 | { |
---|
115 | if(cont) buf+=sprintf(buf,","); else cont=true; |
---|
116 | buf+=sprintf(buf,"++%s-%d(%d~%d)",h->role.str(),h->dst,h->headanc,h->depanc); |
---|
117 | } |
---|
118 | |
---|
119 | if (info&DEPS) |
---|
120 | for(vector<Arc>::iterator d=node.deps.begin(); d!=node.deps.end(); ++d) |
---|
121 | { |
---|
122 | // if(! nodes[d->dst].saturated()) continue; // NIE DRUKUJ NIENASYCONYCH PODRZEDNIKOW |
---|
123 | if(cont) buf+=sprintf(buf,","); else cont=true; |
---|
124 | buf+=sprintf(buf,"--%s-%d(%d~%d)",d->role.str(),d->dst,d->headanc,d->depanc); |
---|
125 | } |
---|
126 | |
---|
127 | if (info&SETS) |
---|
128 | { |
---|
129 | int ord=0; |
---|
130 | buf+=sprintf(buf,";{"); |
---|
131 | for(vector<int>::iterator pm=mgraph[node.mnode].pred.begin(); pm!=mgraph[node.mnode].pred.end(); ++pm) |
---|
132 | for(vector<int>::iterator ps=mgraph[*pm].snodes.begin(); ps!=mgraph[*pm].snodes.end(); ++ps) |
---|
133 | buf+=sprintf(buf, ord++ ? ",%d" : "%d", *ps); |
---|
134 | buf+=sprintf(buf,"};{"); |
---|
135 | ord=0;for(int j=0; j<size(); ++j) if(node.LV[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j); |
---|
136 | buf+=sprintf(buf,"};{"); |
---|
137 | ord=0;for(int j=0; j<size(); ++j) if(node.LH[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j); |
---|
138 | buf+=sprintf(buf,"};{"); |
---|
139 | ord=0;for(int j=0; j<size(); ++j) if(node.LD[j]) buf+=sprintf(buf, ord++ ? ",%d" : "%d", j); |
---|
140 | buf+=sprintf(buf,"}"); |
---|
141 | } |
---|
142 | |
---|
143 | if (info&CONSTRAINTS)// buf+=sprint_node_constraints(buf,n); |
---|
144 | { |
---|
145 | buf+=sprintf(buf,";"); |
---|
146 | int cont=0; |
---|
147 | for(Role i=1; i<=Role::count(); ++i) |
---|
148 | if(node.prop.forbidden[i]) buf+=sprintf(buf,"%s!%s",(cont++)?",":"",i.str()); |
---|
149 | for(Role i=1; i<=Role::count(); ++i) |
---|
150 | if(node.prop.required[i]) buf+=sprintf(buf,"%s-%s",(cont++)?",":"",i.str()); |
---|
151 | for(Role i=1; i<=Role::count(); ++i) |
---|
152 | if(node.prop.attached[i]) buf+=sprintf(buf,"%s+%s",(cont++)?",":"",i.str()); |
---|
153 | for(Flag i=1; i<=Flag::count(); ++i) |
---|
154 | if(node.prop.flags [i]) buf+=sprintf(buf,"%s<%s>",(cont++)?",":"",i.str()); |
---|
155 | if(node.prop.init_attached) |
---|
156 | buf+=sprintf(buf,"<init>"); |
---|
157 | if(node.prop.fin_attached) |
---|
158 | buf+=sprintf(buf,"<fin>"); |
---|
159 | |
---|
160 | stringstream oss; |
---|
161 | for(list<Boubble*>::iterator b = node.prop.boubbles.begin(); b != node.prop.boubbles.end(); b++) |
---|
162 | oss << (cont++ ? "," : "") << **b; |
---|
163 | buf+=sprintf(buf,oss.str().c_str()); |
---|
164 | } |
---|
165 | |
---|
166 | // buf+=sprintf(buf,"\n"); |
---|
167 | |
---|
168 | return buf-buf0; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | int SGraph::sprint_node_debug(char* buf, const char* pref, int n, int anc) |
---|
173 | { |
---|
174 | char *buf0 = buf; |
---|
175 | buf+=sprintf(buf,"#%s",pref); |
---|
176 | |
---|
177 | buf+=sprintf(buf,"%-16s",form(n)); |
---|
178 | |
---|
179 | buf+=sprint_node(buf,n,anc,HEADS|DEPS|SETS|CONSTRAINTS); |
---|
180 | |
---|
181 | buf+=sprintf(buf,"/"); |
---|
182 | for(vector<int>::iterator e = nodes[n].edge.begin(); e != nodes[n].edge.end(); e++ ) |
---|
183 | buf += sprintf(buf,"%d ", *e); |
---|
184 | |
---|
185 | buf+=sprintf(buf,"\n"); |
---|
186 | return buf-buf0; |
---|
187 | } |
---|
188 | |
---|