source: src/dgp/sgraph.cc @ acbabee

Last change on this file since acbabee was acbabee, checked in by Tomasz Obrebski <obrebski@…>, 9 years ago

many changes, mainly dgp1 algorithm

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